Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply quoting patch #417

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/active_record/connection_adapters/postgis/quoting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module ActiveRecord
module ConnectionAdapters
module PostGIS
module Quoting
def type_cast(value)
case value
when RGeo::Feature::Instance
value.to_s
else
super
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/active_record/connection_adapters/postgis_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "postgis/arel_tosql"
require_relative "postgis/oid/spatial"
require_relative "postgis/oid/date_time"
require_relative "postgis/quoting"
require_relative "postgis/type" # has to be after oid/*
# :startdoc:

Expand All @@ -42,6 +43,7 @@ class PostGISAdapter < PostgreSQLAdapter
# http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
DEFAULT_SRID = 0

include PostGIS::Quoting
include PostGIS::SchemaStatements
include PostGIS::DatabaseStatements

Expand Down
8 changes: 8 additions & 0 deletions test/cases/spatial_queries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ def test_ewkt_parser_query
assert_nil(obj3)
end

def test_geo_safe_where
create_model
SpatialModel.create!(latlon_geo: geographic_factory.point(-72.1, 42.1))
SpatialModel.create!(latlon_geo: geographic_factory.point(10.0, 10.0))
assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count
end

private

def create_model
SpatialModel.lease_connection.create_table(:spatial_models, force: true) do |t|
t.column "latlon", :st_point, srid: 3785
t.column "latlon_geo", :st_point, srid: 4326, geographic: true
t.column "points", :multi_point, srid: 3785
t.column "path", :line_string, srid: 3785
end
Expand Down