Skip to content

Support receiving Expression as a parameter in SpatialBuilder methods #76

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

Merged
merged 5 commits into from
Feb 6, 2023
Merged

Support receiving Expression as a parameter in SpatialBuilder methods #76

merged 5 commits into from
Feb 6, 2023

Conversation

Riley19280
Copy link
Contributor

Currently, the column parameters on the SpatialBuilder class are typehinted as string, so when passing a DB::raw(...) expression into the builder, it gets casted to a string first, then re-wrapped as a column value. This causes issues when attempting a query like
->whereWithin(DB::raw('point(longitude, latitude)'), MultiPolygon::fromWkt(...)),
resulting in point(longitude, latitude) being treated as a column name in sql. By avoiding the string cast, the actual raw value passed is used.

The fix for this was to add the Expression typehint (what DB::raw returns) so that the implicit string cast does not happen. This is then picked up by the $this->getQuery()->getGrammar()->wrap calls (see here, and then here) resulting in a correct raw value in the sql.

@MatanYadaev
Copy link
Owner

Hi @Riley19280 , thanks for the PR.

  1. Can you add tests?
  2. Please update the typehint in the API.md file
  3. Do you think the second parameter should also accept expression?

@MatanYadaev MatanYadaev changed the title Add Expression typehint to support passing DB::raw Support receiving Expression as a parameter in SpatialBuilder methods Jan 25, 2023
@Riley19280
Copy link
Contributor Author

Yeah, I can add tests. It does probably make sense for the second one to do so as well, and also run the first parameter through the toExpression function as well, so that it can also handle geometries.

@Riley19280
Copy link
Contributor Author

@MatanYadaev I made the changes requested, including expanding support for DB::raw and Geometry for both parameters for all functions. The unit tests for the toExpressionString method should suffice for this support, but also added a real world test for my use case.

Copy link
Owner

@MatanYadaev MatanYadaev left a comment

Choose a reason for hiding this comment

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

Thanks!

Can you update the method signatures on API.md?

@Riley19280
Copy link
Contributor Author

Can you update the method signatures on API.md?

Updated, I left off the ExpressionContract as I feel it clutters the docs and is something that anyone could reasonably expect to be supported if they need to use it.

@MatanYadaev MatanYadaev merged commit a335a19 into MatanYadaev:master Feb 6, 2023
@MatanYadaev
Copy link
Owner

Thanks for the contribution @Riley19280 🙏

@Riley19280 Riley19280 deleted the support-db-raw branch February 9, 2023 05:42
@Riley19280 Riley19280 restored the support-db-raw branch February 27, 2023 23:46
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.

2 participants