Skip to content

Commit 74370f7

Browse files
authored
Introduce build_query_scalar for QueryBuilder (#2551)
1 parent aee0e18 commit 74370f7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sqlx-core/src/query_builder.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::encode::Encode;
1010
use crate::from_row::FromRow;
1111
use crate::query::Query;
1212
use crate::query_as::QueryAs;
13+
use crate::query_scalar::QueryScalar;
1314
use crate::types::Type;
1415
use crate::Either;
1516

@@ -466,6 +467,30 @@ where
466467
}
467468
}
468469

470+
/// Produce an executable query from this builder.
471+
///
472+
/// ### Note: Query is not Checked
473+
/// It is your responsibility to ensure that you produce a syntactically correct query here,
474+
/// this API has no way to check it for you.
475+
///
476+
/// ### Note: Reuse
477+
/// You can reuse this builder afterwards to amortize the allocation overhead of the query
478+
/// string, however you must call [`.reset()`][Self::reset] first, which returns `Self`
479+
/// to the state it was in immediately after [`new()`][Self::new].
480+
///
481+
/// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons.
482+
pub fn build_query_scalar<'q, T>(
483+
&'q mut self,
484+
) -> QueryScalar<'q, DB, T, <DB as HasArguments<'args>>::Arguments>
485+
where
486+
DB: Database,
487+
(T,): for<'r> FromRow<'r, DB::Row>,
488+
{
489+
QueryScalar {
490+
inner: self.build_query_as(),
491+
}
492+
}
493+
469494
/// Reset this `QueryBuilder` back to its initial state.
470495
///
471496
/// The query is truncated to the initial fragment provided to [`new()`][Self::new] and

sqlx-core/src/query_scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::types::Type;
1717
/// Returned from [`query_scalar`].
1818
#[must_use = "query must be executed to affect database"]
1919
pub struct QueryScalar<'q, DB: Database, O, A> {
20-
inner: QueryAs<'q, DB, (O,), A>,
20+
pub(crate) inner: QueryAs<'q, DB, (O,), A>,
2121
}
2222

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

0 commit comments

Comments
 (0)