From 426323fbb28ee1b501051640aa3799677eb4c922 Mon Sep 17 00:00:00 2001 From: Quang Le Date: Fri, 16 Jun 2023 13:37:49 +0700 Subject: [PATCH] Introduce build_query_scalar for QueryBuilder --- sqlx-core/src/query_builder.rs | 25 +++++++++++++++++++++++++ sqlx-core/src/query_scalar.rs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 10dd4d7e9f..8027b3b9fc 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -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; @@ -448,6 +449,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, >::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 diff --git a/sqlx-core/src/query_scalar.rs b/sqlx-core/src/query_scalar.rs index 197c527e56..0ef8cbde30 100644 --- a/sqlx-core/src/query_scalar.rs +++ b/sqlx-core/src/query_scalar.rs @@ -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>