-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
support conditional queries in query_as!
#1491
Closed
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
c7bbcd1
WIP: initial implementation of conditional_query_as! (#1488)
NyxCode 7844fc4
restructure code
NyxCode 0d0028c
make conditional_query_as! expand to just sqlx::query! if no branches…
NyxCode 8b7fb08
support inline arguments and remove `?<arg>` syntax
NyxCode 3301a60
Merge branch 'master' into conditional-query
NyxCode 4f2ce57
allow concatenation of segments for backwards compatibility
NyxCode 21060ce
make use of format_ident!
NyxCode dabef59
make calls to expand_query! internally, make (hopefully) completely b…
NyxCode 1f10f64
fix bad call to quote!
NyxCode 860b72d
accidentally swapped { and }
NyxCode 89878ed
add test, clean up
NyxCode 39ae6c2
don't hide query_as! from docs
NyxCode 0601ddb
Merge branch 'master' into conditional-query
NyxCode 6489cdc
add brief summary to conditional/mod.rs
NyxCode b483b98
enable CI for development
NyxCode 1a72621
add documentation, format, derive Debug for QuerySegment and Context
NyxCode 7ff06a5
fix parsing of trailing comma
NyxCode 5767150
re-export futures_core from sqlx-core
NyxCode aade05a
fix return type of conditional map fetch method
NyxCode f280e67
add simple test
NyxCode 64b0ff6
re-export futures_core from sqlx
NyxCode 1fc3697
add an other test
NyxCode fb072bb
remove unused imports
NyxCode ac4e02d
fix tests
NyxCode 024c9be
tests: fix nullability issue
NyxCode d45d86b
tests: add dynamic_filtering test
NyxCode 046c2dd
docs: add user-facing documentation
NyxCode 11be4f3
tests: test inline arguments in query_as!
NyxCode bbe8d07
add debug log
NyxCode 5a29c81
fix off-by-one in parameter generation
NyxCode 85065a1
fix unused import
NyxCode bae5dd6
re-run CI
NyxCode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ name: SQLx | |
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
format: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
|
||
pub fn generate_conditional_map(n: usize) -> TokenStream { | ||
let map_fns = (1..=n).map(|i| format_ident!("F{}", i)).collect::<Vec<_>>(); | ||
let args = (1..=n).map(|i| format_ident!("A{}", i)).collect::<Vec<_>>(); | ||
let variants = (1..=n).map(|i| format_ident!("_{}", i)).collect::<Vec<_>>(); | ||
let variant_declarations = (0..n).map(|i| { | ||
let variant = &variants[i]; | ||
let map_fn = &map_fns[i]; | ||
let args = &args[i]; | ||
quote!(#variant(sqlx::query::Map<'q, DB, #map_fn, #args>)) | ||
}); | ||
|
||
quote! { | ||
#[doc(hidden)] | ||
pub enum ConditionalMap<'q, DB, O, #(#map_fns,)* #(#args,)*> | ||
where | ||
DB: sqlx::Database, | ||
O: Send + Unpin, | ||
#(#map_fns: FnMut(DB::Row) -> sqlx::Result<O> + Send,)* | ||
#(#args: 'q + Send + sqlx::IntoArguments<'q, DB>,)* | ||
{ | ||
#(#variant_declarations),* | ||
} | ||
impl<'q, DB, O, #(#map_fns,)* #(#args,)*> ConditionalMap<'q, DB, O, #(#map_fns,)* #(#args,)*> | ||
where | ||
DB: sqlx::Database, | ||
O: Send + Unpin, | ||
#(#map_fns: FnMut(DB::Row) -> sqlx::Result<O> + Send,)* | ||
#(#args: 'q + Send + sqlx::IntoArguments<'q, DB>,)* | ||
{ | ||
pub fn fetch<'e, 'c: 'e, E>(self, executor: E) -> sqlx::futures_core::stream::BoxStream<'e, sqlx::Result<O>> | ||
where | ||
'q: 'e, | ||
E: 'e + sqlx::Executor<'c, Database = DB>, | ||
DB: 'e, | ||
O: 'e, | ||
#(#map_fns: 'e,)* | ||
{ | ||
match self { #( | ||
Self::#variants(x) => x.fetch(executor) | ||
),* } | ||
} | ||
pub async fn fetch_all<'e, 'c: 'e, E>(self, executor: E) -> sqlx::Result<Vec<O>> | ||
where | ||
'q: 'e, | ||
DB: 'e, | ||
E: 'e + sqlx::Executor<'c, Database = DB>, | ||
O: 'e | ||
{ | ||
match self { #( | ||
Self::#variants(x) => x.fetch_all(executor).await | ||
),* } | ||
} | ||
pub async fn fetch_one<'e, 'c: 'e, E>(self, executor: E) -> sqlx::Result<O> | ||
where | ||
'q: 'e, | ||
E: 'e + sqlx::Executor<'c, Database = DB>, | ||
DB: 'e, | ||
O: 'e, | ||
{ | ||
match self { #( | ||
Self::#variants(x) => x.fetch_one(executor).await | ||
),* } | ||
} | ||
pub async fn fetch_optional<'e, 'c: 'e, E>(self, executor: E) -> sqlx::Result<Option<O>> | ||
where | ||
'q: 'e, | ||
E: 'e + sqlx::Executor<'c, Database = DB>, | ||
DB: 'e, | ||
O: 'e, | ||
{ | ||
match self { #( | ||
Self::#variants(x) => x.fetch_optional(executor).await | ||
),* } | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this?