-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Fix sql sanitization of multi geoms #81
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
require 'active_support/core_ext/array/wrap' | ||
|
||
module MultiGeomSanitization | ||
private | ||
|
||
# NOTE connection and value order is swapped in Rails 8 | ||
def replace_bind_variable(connection, value) | ||
if value.class.name.start_with?("RGeo::") && value.respond_to?(:map) | ||
super(connection, Array.wrap(value)) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Sanitization::ClassMethods.prepend(MultiGeomSanitization) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,16 @@ def test_arel_visit_RGeo_ActiveRecord_SpatialNamedFunction_with_distinct | |
assert_equal("SPATIAL_FUNC(DISTINCT ST_GeomFromText('POINT (1.0 2.0)'), ST_GeomFromText('LINESTRING (1.0 2.0, 2.0 3.0)'))", sql.value) | ||
end | ||
|
||
def test_multi_geom_sanitization | ||
multi_geom = RGeo::Geos.factory.parse_wkt("MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)),((1 1, 0 0, 0 1, 1 1)))") | ||
sql = "ST_DWithin(geom, :geom, :buffer)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need that |
||
|
||
assert_equal( | ||
"ST_DWithin(geom, 'MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)), ((1 1, 0 0, 0 1, 1 1)))', '10')", | ||
FakeRecord::Base.sanitize_sql([ sql, geom: multi_geom, buffer: 10 ]) | ||
) | ||
end | ||
|
||
private | ||
|
||
def arel_visitor | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,10 @@ def quote(thing, column = nil) | |
"'#{thing.to_s.gsub("'", "\\\\'")}'" | ||
end | ||
end | ||
|
||
def cast_bound_value(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why using this fake record class rather than any record? |
||
value.to_s | ||
end | ||
end | ||
|
||
class ConnectionPool | ||
|
@@ -123,8 +127,14 @@ def quote(thing, column = nil) | |
end | ||
|
||
class Base | ||
include ActiveRecord::Sanitization | ||
|
||
attr_accessor :connection_pool | ||
|
||
def self.with_connection | ||
yield new.connection | ||
end | ||
|
||
def initialize | ||
@connection_pool = ConnectionPool.new | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
require "minitest/autorun" | ||
require "rgeo-activerecord" | ||
require "support/fake_record" | ||
require "active_support/core_ext/date/acts_like" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this? |
||
|
||
Arel::Visitors::ToSql.include RGeo::ActiveRecord::SpatialToSql | ||
Arel::Table.engine = FakeRecord::Base.new | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should we do about this note?