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

Represent wheres and raw wheres in the same way #460

Merged
merged 17 commits into from
Oct 14, 2020

Conversation

matthewmcgarvey
Copy link
Member

@matthewmcgarvey matthewmcgarvey commented Sep 12, 2020

Fixes #459

Avram::Where::Raw and Avram::Where::SqlClause did not have overlapping ancestors so they were stored in separate lists in Avram::QueryBuilder and had different APIs. When the sql string was put together, raw wheres were appended to the where clause. When "OR" support is added, this will no longer be acceptable.

It simplifies the API to have them be used in the same way. This adds a common ancestor of Avram::Where::Condition that has the one method of #prepare. The only problem of doing this is that SqlClauses where being passed a placeholder string that had an incrementing number. Rather than keeping with the push style and checking which types were save to give the placeholder to, I switched the where conditions to take in a supplier of those placeholders so that it could get one if it wanted or just ignore it and not cause the number to increment.

PS: Naming is hard, any ideas on how to name the different abstract classes better?

@matthewmcgarvey matthewmcgarvey changed the title Represent wheres and raw wheres in the same way and order resulting sql correctly Represent wheres and raw wheres in the same way Sep 12, 2020
@jwoertink
Copy link
Member

Ah, I see what you're doing here. That's nice. I'll come back for a review again later, but initial glance over I like the direction. I'll also try and think of a good name for that abstract class. Thanks for doing this!

@matthewmcgarvey matthewmcgarvey force-pushed the wheres-update branch 2 times, most recently from 321e658 to 1150dcf Compare September 18, 2020 17:34
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

Ok, so for the most part I like what this is doing, and I think I'll need it before being able to merge in #442. I think my only hesitation here is if will cause any type-safe issues by these being merged in.... For example, when you query, you never actually call Avram::Where directly... you'll do UserQuery.new.username() or you'll do UserQuery.new.where("").... I may just be overthinking it now that I write it out 😝 But what are your thoughts?

src/avram/where.cr Outdated Show resolved Hide resolved
src/avram/where.cr Outdated Show resolved Hide resolved
end

abstract class ValueHoldingSqlClause < SqlClause
getter value : String | Array(String) | Array(Int32)
Copy link
Member

Choose a reason for hiding this comment

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

I know this type comes from what was defined in Condition before, but any idea why these types specifically? I ask because I'm wondering if with the latest pg and db, will this blow up when someone uses UUID? 🤔 Also, Int64 I guess... I'm assuming this is the value of a DB column which could be many different types... Or maybe I'm wrong and this value is something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not able to hunt down why we convert everything to these types

ultimately, we call out to here https://github.com/crystal-lang/crystal-db/blob/bd456028644853f852f5adfe053142e6ad885e52/src/db/statement.cr#L69-L71
which does not have type signatures so it's not very helpful. Our process ultimately delegates to the #to_db method for each type which in most things converts to a string (Ints, UUIDs, etc).

Copy link
Member

Choose a reason for hiding this comment

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

It'll probably be ok, but lets keep this in mind if we notice something weird popup. I feel like this should have more types on it, but if it works as is then we can just roll with it.

@matthewmcgarvey
Copy link
Member Author

@jwoertink I don't think any type safety was lost/gained. Most queries will never interact with the #where that takes in these base classes, but when they do they won't have to remember whether they should use #where or #raw_where.

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

👍

@matthewmcgarvey matthewmcgarvey merged commit 129b898 into luckyframework:master Oct 14, 2020
@matthewmcgarvey matthewmcgarvey deleted the wheres-update branch October 14, 2020 22:12
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.

SQL where clauses are not kept in the order they are called if using raw wheres
2 participants