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

Implement raw subqueries #230

Merged
merged 16 commits into from
Jun 14, 2024
Merged

Implement raw subqueries #230

merged 16 commits into from
Jun 14, 2024

Conversation

treagod
Copy link
Contributor

@treagod treagod commented May 23, 2024

This commit adds support for raw SQL queries to the #filter method and q expressions.

#44

@treagod treagod self-assigned this May 23, 2024
@treagod treagod marked this pull request as draft May 23, 2024 04:56
@treagod treagod force-pushed the raw-filter branch 3 times, most recently from 9892022 to b8e9b14 Compare May 24, 2024 11:03
@treagod treagod marked this pull request as ready for review June 2, 2024 12:00
@treagod treagod requested a review from ellmetha June 2, 2024 12:04
Copy link
Member

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

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

Thanks for starting this! 🙏

This looks good and I think we might need to refactor a few things in order to be able to merge this (especially in order to avoid some duplications). I will definitely need to do another review. 🙂

docs/docs/models-and-databases/queries.md Outdated Show resolved Hide resolved
src/marten/db/query/raw_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/raw_node.cr Show resolved Hide resolved
src/marten/db/query/raw_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/raw_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/raw_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/query.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
@treagod treagod requested a review from ellmetha June 5, 2024 13:05
docs/docs/models-and-databases/queries.md Outdated Show resolved Hide resolved
docs/docs/models-and-databases/raw-sql.md Outdated Show resolved Hide resolved
src/marten/db/query/set.cr Outdated Show resolved Hide resolved
src/marten/db/query/set.cr Outdated Show resolved Hide resolved
src/marten/db/query/set.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/concerns/sanitizer.cr Show resolved Hide resolved
src/marten/db/query/sql/raw_query.cr Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_statement_node.cr Outdated Show resolved Hide resolved
@treagod
Copy link
Contributor Author

treagod commented Jun 12, 2024

@ellmetha I'll left two comments regarding the connecting object (which is related to the formatting issue), which is why I didn't touch any of the connection findings

src/marten/db/query/sql/concerns/sanitizer.cr Show resolved Hide resolved
src/marten/db/query/sql/concerns/sanitizer.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/concerns/sanitizer.cr Outdated Show resolved Hide resolved
src/marten/db/query/set.cr Outdated Show resolved Hide resolved
spec/marten/db/query/expression/filter_spec.cr Outdated Show resolved Hide resolved
spec/marten/db/query/set_spec.cr Outdated Show resolved Hide resolved
it "raises an error when filtering with a misspelled column in a raw SQL condition", tags: "raw" do
Tag.create!(name: "crystal", is_active: true)

expect_raises(Exception) do
Copy link
Member

Choose a reason for hiding this comment

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

Should we target a more specific exception class here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but I'm not sure it's useful here. All database types throw their own exception. I didn't see a common ancestor of these database exceptions, so I chose this generic exception type instead of checking each individual database exception.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

We could define specific blocks for every supported backend and verify that the most precise exception is raised (by wrapping #expect_raises calls within #for_mysql / #for_postgresql / #for_sqlite blocks). That way, other types of exceptions wouldn't be "caught" if there is an issue with the underlying code, but that's very unlikely anyway. Let's keep it that way!

spec/marten/db/query/set_spec.cr Outdated Show resolved Hide resolved
spec/marten/db/query/sql/raw_predicate_node_spec.cr Outdated Show resolved Hide resolved
src/marten/db/query/sql/raw_predicate_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/raw_node.cr Outdated Show resolved Hide resolved
src/marten/db/query/set.cr Outdated Show resolved Hide resolved
src/marten/db/query/node.cr Show resolved Hide resolved
@ellmetha
Copy link
Member

Thanks for the work on this @treagod! We are getting closer! I left additional comments and small fix suggestions; we should be able to merge this very soon. 🎉

@treagod treagod requested a review from ellmetha June 13, 2024 08:20
Copy link
Member

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

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

Thanks for adding this!

@ellmetha ellmetha merged commit 405d843 into main Jun 14, 2024
70 checks passed
@ellmetha ellmetha deleted the raw-filter branch June 14, 2024 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants