Skip to content

Commit

Permalink
Introduce build_query_scalar for QueryBuilder (#2551)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamquang95 authored Jul 14, 2023
1 parent aee0e18 commit 74370f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions sqlx-core/src/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::encode::Encode;
use crate::from_row::FromRow;
use crate::query::Query;
use crate::query_as::QueryAs;
use crate::query_scalar::QueryScalar;
use crate::types::Type;
use crate::Either;

Expand Down Expand Up @@ -466,6 +467,30 @@ where
}
}

/// Produce an executable query from this builder.
///
/// ### Note: Query is not Checked
/// It is your responsibility to ensure that you produce a syntactically correct query here,
/// this API has no way to check it for you.
///
/// ### Note: Reuse
/// You can reuse this builder afterwards to amortize the allocation overhead of the query
/// string, however you must call [`.reset()`][Self::reset] first, which returns `Self`
/// to the state it was in immediately after [`new()`][Self::new].
///
/// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons.
pub fn build_query_scalar<'q, T>(
&'q mut self,
) -> QueryScalar<'q, DB, T, <DB as HasArguments<'args>>::Arguments>
where
DB: Database,
(T,): for<'r> FromRow<'r, DB::Row>,
{
QueryScalar {
inner: self.build_query_as(),
}
}

/// Reset this `QueryBuilder` back to its initial state.
///
/// The query is truncated to the initial fragment provided to [`new()`][Self::new] and
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/query_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::types::Type;
/// Returned from [`query_scalar`].
#[must_use = "query must be executed to affect database"]
pub struct QueryScalar<'q, DB: Database, O, A> {
inner: QueryAs<'q, DB, (O,), A>,
pub(crate) inner: QueryAs<'q, DB, (O,), A>,
}

impl<'q, DB: Database, O: Send, A: Send> Execute<'q, DB> for QueryScalar<'q, DB, O, A>
Expand Down

0 comments on commit 74370f7

Please sign in to comment.