Skip to content

Commit

Permalink
Merge pull request #417 from JamesChevalier/safe-quoting
Browse files Browse the repository at this point in the history
apply quoting patch
  • Loading branch information
keithdoggett authored Nov 7, 2024
2 parents 1290093 + a9efe07 commit 2d87923
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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

0 comments on commit 2d87923

Please sign in to comment.