Skip to content

Fix AR type cast to mirror quoting #422

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nikolai-b
Copy link

It seems wrong that

SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).to_sql

uses the EWKB format but when the query is actually executed it uses to_s which uses the WKT without passing any SRID. This was causing me issues for me with preprepared statements but I couldn't create a simple text. I do think this gem should pass the SRID of bindings though.

I'm not very happy with the test I've written here. I looked .values_for_queries or looking at the arel but the binds haven't been type_cast at this point so I've used explain.

Copy link
Member

@keithdoggett keithdoggett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikolai-b thanks for the PR! I think this looks good. Was this only effecting prepared geometries? I thought there was some work done recently that added the SRID to the generated string in the rgeo-activerecord gem.

@inkstak
Copy link
Contributor

inkstak commented Apr 29, 2025

I suggested a similar PR #430 before finding yours.
Yours seems more complete since you handled bounding boxes.

@todtb
Copy link

todtb commented Jun 23, 2025

@keithdoggett

I thought there was some work done recently that added the SRID to the generated string in the rgeo-activerecord gem.

As of rgeo (3.0.1) and rgeo-activerecord (8.0.0) it does not look to be adding SRID. Rather, to_s returns non-extended WKT.

Re test assertions: From the testing I've done, the proposed changes look good and address the issue by using EWKB. (In my case, this was ultimately used with ST_Intersects) Upon inspection, the type_casted_binds in the adapter look good and I think the EXPLAIN approach is sufficient.

@nikolai-b
Copy link
Author

@inkstak thanks, I copied the logic from

def quote(value)
if RGeo::Feature::Geometry.check_type(value)
"'#{RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)}'"
elsif value.is_a?(RGeo::Cartesian::BoundingBox)
"'#{value.min_x},#{value.min_y},#{value.max_x},#{value.max_y}'::box"
else
super
end
end
.

@keithdoggett I don't see any work in rgeo-activerecord around SRID. Given someone else has submitted a similar PR is there something either of us can do to aid in review (e.g. if you prefer the specs in the other PR)? Thanks

@inkstak
Copy link
Contributor

inkstak commented Jul 2, 2025

You'll find a detailled issue at #429
Also #431 seems to be related.

@nikolai-b
Copy link
Author

Thanks you. I've included your spec as it now works, is that ok as it is your code?

Sadly #431 is not fixed by this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants