-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add SQL query builder #5
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Craig Bester <[email protected]>
Signed-off-by: Craig Bester <[email protected]>
Signed-off-by: Craig Bester <[email protected]>
Signed-off-by: Craig Bester <[email protected]>
Okay, I think we should scope out pretty much all of your TODO items. The one thing that's unclear to me is how you want to create a procedural macro. The idea of this crate was that we would not rely on procedural macros, but just do straight up code generation. I think that's what the tests are doing, right? |
Sure. I implemented the rest of the basic comparison operators ( I also implemented SELECT "user_id", "role"
FROM "accounts"
JOIN "access" ON "accounts"."user_id" = "access"."user" Accounts::query()
.join::<Access, _>(|(a, ac)| Sql::eq(a.user_id, acl.user))
.select(|(a, acl) (a.user_id, acl.role)|
.to_string(); Joining foreign keys can be made automatic later on.
It was just a passing thought so the generated code would be more readable and so the query builder side could be used more easily, independent of the code generation. It would be nice to have a better separation between the two code-wise too, i.e. feature-gate or separate crates. Either way, not a big deal.
Yes, the code generation/tests just output Rust code as strings. |
Description
Implements a proof-of-concept SQL query builder. Currently only supports basic
SELECT
statements with a few conditionals, and uses the sea-query crate to compose and generate SQL for now (can be replaced later once the top-level API is finalised. since it's mostly opaque).This attempts to match the syntax from #4 (comment).
Example
The filters could also be combined into one using
&
instead of multiplefilter
s.fetch
function is provided behind thepostgres
flag to execute the query using the postgres crate:Just an example, other functions and feature flags could be added for different runtimes.
E.g. trying to compare a string to an integer will throw a compilation error:
TODOFuture Workjoin
.ON
clauses based on foreign keys for joins.join
clauses at compile-time to tables with foreign keys.SELECT *
for all columns.AccountsFields
,AccountsIden
,impl Table for Accounts
, etc.)?eq
on a BOOLEAN column only allows bools etc.)eq
,ne
,like
, etc.) .sea-query
?Problems
The big problem currently is being unable to restrict which tables can join each other at compile time, due to how the
Sources
trait is set up to operate on tuples of tables.E.g.
since trying to implement the same for B causes a "conflicting trait implementation" compilation error.
This restriction can be implemented quite easily at runtime, however.
Type of change
Enhancement (a non-breaking change which adds functionality)
How the change has been tested
Added unit tests. See the README.md for instructions.