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

Diesel integration #658

Merged
merged 19 commits into from
Sep 8, 2023
Merged

Diesel integration #658

merged 19 commits into from
Sep 8, 2023

Conversation

Sytten
Copy link
Contributor

@Sytten Sytten commented Jul 13, 2023

PR Info

See discussion: #168

BLOCKED BY diesel-rs/diesel#3691 and diesel-rs/diesel#3694

New Features

  • Diesel binding

@Sytten
Copy link
Contributor Author

Sytten commented Jul 13, 2023

@weiznich I would greatly appreciate if you could let me know if you think this is a good approach 🙏

@Sytten
Copy link
Contributor Author

Sytten commented Jul 14, 2023

@tyt2y3 @billy1624 This is ready for a first review, we still have to wait for the next diesel release before we can merge though

@Sytten Sytten marked this pull request as ready for review July 14, 2023 22:45
Copy link

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

From the diesel side the implementation looks OK. The one thing I personally would try to improve is enabling prepared statement caching at least for some queries at it really helps with performance.

DB: Backend,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
out.unsafe_to_cache_prepared();

Choose a reason for hiding this comment

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

It might be useful to only call this method depending on whether sea-query believes that a prepared statement is meaningful to cache or not. It will significantly improve performance for queries that are executed more than once.

Diesel generally tries to cache all queries that are somewhat static, which means all queries that do not contain a user input dependent component (beside specific bind parameters).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a good point, I think it will require a hint from sea-query side to implement that but we could offer an API that the user can specify (build_diesel_cached or something). I am unsure how the caching exactly works when using a pool though? Usually the prepared statement is per connection, does diesel try to re-use the same connection? @tyt2y3 @billy1624 if you have any experience with that from the sqlx side that we could translate to diesel that would be super!

Choose a reason for hiding this comment

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

It might be already worth to have a simple heuristics there: There are no bound paramaters -> cache the query, as it does not change for different calls.

For more targeted caching you would need to access the query ast somehow to check if there is for example no IN expression with a variable number of binds in there or no INSERT statement with a variable number of rows.

Choose a reason for hiding this comment

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

Also there is quite a lot of detailed documentation of diesels query caching here: https://docs.diesel.rs/2.1.x/diesel/connection/statement_cache/index.html#a-primer-on-prepared-statement-caching-in-diesel

Copy link
Member

@tyt2y3 tyt2y3 Jul 21, 2023

Choose a reason for hiding this comment

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

Thanks for the info. May be later we can have an API that walks the AST and return whether:

  1. any custom expression used
  2. any in and not_in clause
  3. insert many

@Sytten
Copy link
Contributor Author

Sytten commented Jul 21, 2023

@tyt2y3 builds are failing because the bigdecimal versions dont match without a Cargo.lock, considering what I discussed in #664 are you OK with me adding a lock file to the examples and the crate?

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 21, 2023

considering what I discussed in #664 are you OK with me adding a lock file to the examples and the crate?

Sure, I am okay with it

@Sytten
Copy link
Contributor Author

Sytten commented Aug 31, 2023

@tyt2y3 Ready for review

@Sytten Sytten requested a review from tyt2y3 August 31, 2023 16:04
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo update --manifest-path sea-query-diesel/Cargo.toml --workspace -p bigdecimal:0.4.1 --precise 0.3.1
Copy link

Choose a reason for hiding this comment

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

This shouldn't be required anymore as diesel 2.1.1 added support for bigdecimal 0.4: https://crates.io/crates/diesel/2.1.1/dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha the issue is on sea-query side, they only support 0.3 and the resolver takes 0.4 for diesel so it causes a build failure because the types are incompatible. Its a PITA that we still don't have a way to pin a sub dependency without a lockfile.

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Thank you

@tyt2y3 tyt2y3 changed the base branch from master to diesel September 8, 2023 20:41
@tyt2y3 tyt2y3 merged commit 92b0220 into SeaQL:diesel Sep 8, 2023
28 checks passed
@tyt2y3
Copy link
Member

tyt2y3 commented Sep 23, 2023

Released in https://crates.io/crates/sea-query-diesel/0.1.0

@Sytten
Copy link
Contributor Author

Sytten commented Sep 23, 2023

Thanks much appreciated!

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.

3 participants