Debian Codegraph @

src/query_scalar.rs

raw symbols SCIP
1 use eitherrust-analyzer cargo either 1.18.0 crate/::Eitherrust-analyzer cargo either 1.18.0 Either#;
2 use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::streamrust-analyzer cargo futures-core 0.3.34 stream/::BoxStreamrust-analyzer cargo futures-core 0.3.34 stream/BoxStream#;
3 use futures_utilrust-analyzer cargo futures-util 0.3.34 crate/::{StreamExtrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#, TryFutureExtrust-analyzer cargo futures-util 0.3.34 future/try_future/TryFutureExt#, TryStreamExtrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#};
4
5 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::argumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/::IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#;
6 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::databaserust-analyzer cargo sqlx-core 0.8.3 database/::{Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, HasStatementCacherust-analyzer cargo sqlx-core 0.8.3 database/HasStatementCache#};
7 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::encoderust-analyzer cargo sqlx-core 0.8.3 encode/::Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#;
8 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::errorrust-analyzer cargo sqlx-core 0.8.3 error/::{BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#};
9 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::executorrust-analyzer cargo sqlx-core 0.8.3 executor/::{Executerust-analyzer cargo sqlx-core 0.8.3 executor/Execute#, Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#};
10 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::from_rowrust-analyzer cargo sqlx-core 0.8.3 from_row/::FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#;
11 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/::{
12 query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as()., query_as_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as_with_result()., query_statement_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as()., query_statement_as_withrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as_with()., QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#,
13 };
14 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::typesrust-analyzer cargo sqlx-core 0.8.3 types/::Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#;
15
16 /// A single SQL query as a prepared statement which extracts only the first column of each row.
17 /// Returned by [`query_scalar()`].
18 #[must_use = "query must be executed to affect database"]
19 pub struct QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, O, A> {
20 pub(craterust-analyzer cargo sqlx-core 0.8.3 crate/) innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.: QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, (O,), A>,
21 }
22
23 impl<'q, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, A: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#> Executerust-analyzer cargo sqlx-core 0.8.3 executor/Execute#<'q, DB> for QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
24 where
25 A: 'q + IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>,
26 {
27 #[inline]
28 fn sqlrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`][`Execute<'q, DB>`]sql().(&self) -> &'q str {
29 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..sqlrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]sql().()
30 }
31
32 fn statementrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`][`Execute<'q, DB>`]statement().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&DB::Statementrust-analyzer cargo sqlx-core 0.8.3 database/Database#Statement#<'q>> {
33 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..statementrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]statement().()
34 }
35
36 #[inline]
37 fn take_argumentsrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`][`Execute<'q, DB>`]take_arguments().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<<DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Argumentsrust-analyzer cargo sqlx-core 0.8.3 database/Database#Arguments#<'q>>, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> {
38 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..take_argumentsrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]take_arguments().()
39 }
40
41 #[inline]
42 fn persistentrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`][`Execute<'q, DB>`]persistent().(&self) -> bool {
43 Executerust-analyzer cargo sqlx-core 0.8.3 executor/Execute#::persistentrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]persistent().(&self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.)
44 }
45 }
46
47 impl<'q, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, O> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Argumentsrust-analyzer cargo sqlx-core 0.8.3 database/Database#Arguments#<'q>> {
48 /// Bind a value for use with this SQL query.
49 ///
50 /// See [`Query::bind`](crate::query::Query::bind).
51 pub fn bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().<T: 'q + Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, DB> + Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<DB>>(mut self, value: T) -> Selfrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`] {
52 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner. = self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..bindrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(value);
53 self
54 }
55 }
56
57 impl<'q, DB, O, A> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
58 where
59 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database# + HasStatementCacherust-analyzer cargo sqlx-core 0.8.3 database/HasStatementCache#,
60 {
61 /// If `true`, the statement will get prepared once and cached to the
62 /// connection's statement cache.
63 ///
64 /// If queried once with the flag set to `true`, all subsequent queries
65 /// matching the one with the flag will use the cached statement until the
66 /// cache is cleared.
67 ///
68 /// If `false`, the prepared statement will be closed after execution.
69 ///
70 /// Default: `true`.
71 pub fn persistentrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]persistent().(mut self, value: bool) -> Selfrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`] {
72 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner. = self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..persistentrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]persistent().(value);
73 self
74 }
75 }
76
77 // FIXME: This is very close, nearly 1:1 with `Map`
78 // noinspection DuplicatedCode
79 impl<'q, DB, O, A> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
80 where
81 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
82 O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin#,
83 A: 'q + IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>,
84 (O,): Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin# + for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
85 {
86 /// Execute the query and return the generated results as a stream.
87 #[inline]
88 pub fn fetchrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch().<'e, 'c: 'e, E>(self, executor: E) -> BoxStreamrust-analyzer cargo futures-core 0.3.34 stream/BoxStream#<'e, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<O, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>>
89 where
90 'q: 'e,
91 E: 'e + Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#<'c, Databaserust-analyzer cargo sqlx-core 0.8.3 executor/Executor#Database# = DB>,
92 DB: 'e,
93 A: 'e,
94 O: 'e,
95 {
96 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..fetchrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch().(executor).map_okrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#map_ok().(|it| it.0).boxedrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#boxed().()
97 }
98
99 /// Execute multiple queries and return the generated results as a stream
100 /// from each query, in a stream.
101 #[inline]
102 #[deprecated = "Only the SQLite driver supports multiple statements in one prepared statement and that behavior is deprecated. Use `sqlx::raw_sql()` instead. See https://github.com/launchbadge/sqlx/issues/3108 for discussion."]
103 pub fn fetch_manyrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_many().<'e, 'c: 'e, E>(
104 self,
105 executor: E,
106 ) -> BoxStreamrust-analyzer cargo futures-core 0.3.34 stream/BoxStream#<'e, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Eitherrust-analyzer cargo either 1.18.0 Either#<DB::QueryResultrust-analyzer cargo sqlx-core 0.8.3 database/Database#QueryResult#, O>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>>
107 where
108 'q: 'e,
109 E: 'e + Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#<'c, Databaserust-analyzer cargo sqlx-core 0.8.3 executor/Executor#Database# = DB>,
110 DB: 'e,
111 A: 'e,
112 O: 'e,
113 {
114 #[allow(deprecated)]
115 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.
116 .fetch_manyrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_many().(executor)
117 .map_okrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#map_ok().(|v| v.map_rightrust-analyzer cargo either 1.18.0 impl#[`Either<L, R>`]map_right().(|it| it.0))
118 .boxedrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#boxed().()
119 }
120
121 /// Execute the query and return all the resulting rows collected into a [`Vec`].
122 ///
123 /// ### Note: beware result set size.
124 /// This will attempt to collect the full result set of the query into memory.
125 ///
126 /// To avoid exhausting available memory, ensure the result set has a known upper bound,
127 /// e.g. using `LIMIT`.
128 #[inline]
129 pub async fn fetch_allrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_all().<'e, 'c: 'e, E>(self, executor: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<O>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>
130 where
131 'q: 'e,
132 E: 'e + Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#<'c, Databaserust-analyzer cargo sqlx-core 0.8.3 executor/Executor#Database# = DB>,
133 DB: 'e,
134 (O,): 'e,
135 A: 'e,
136 {
137 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.
138 .fetchrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch().(executor)
139 .map_okrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#map_ok().(|it| it.0)
140 .try_collectrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#try_collect().()
141 .rust-analyzer cargo futures-util 0.3.34 stream/try_stream/try_collect/impl#[`TryCollect<St, C>`][Future]poll().awaitrust-analyzer cargo futures-util 0.3.34 stream/try_stream/try_collect/impl#[`TryCollect<St, C>`][Future]poll().
142 }
143
144 /// Execute the query, returning the first row or [`Error::RowNotFound`] otherwise.
145 ///
146 /// ### Note: for best performance, ensure the query returns at most one row.
147 /// Depending on the driver implementation, if your query can return more than one row,
148 /// it may lead to wasted CPU time and bandwidth on the database server.
149 ///
150 /// Even when the driver implementation takes this into account, ensuring the query returns at most one row
151 /// can result in a more optimal query plan.
152 ///
153 /// If your query has a `WHERE` clause filtering a unique column by a single value, you're good.
154 ///
155 /// Otherwise, you might want to add `LIMIT 1` to your query.
156 #[inline]
157 pub async fn fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().<'e, 'c: 'e, E>(self, executor: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<O, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>
158 where
159 'q: 'e,
160 E: 'e + Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#<'c, Databaserust-analyzer cargo sqlx-core 0.8.3 executor/Executor#Database# = DB>,
161 DB: 'e,
162 O: 'e,
163 A: 'e,
164 {
165 self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_one().(executor).map_okrust-analyzer cargo futures-util 0.3.34 future/try_future/TryFutureExt#map_ok().(|it| it.0).rust-analyzer cargo futures-util 0.3.34 future/try_future/impl#[`MapOk<Fut, F>`][Future]poll().awaitrust-analyzer cargo futures-util 0.3.34 future/try_future/impl#[`MapOk<Fut, F>`][Future]poll().
166 }
167
168 /// Execute the query, returning the first row or `None` otherwise.
169 ///
170 /// ### Note: for best performance, ensure the query returns at most one row.
171 /// Depending on the driver implementation, if your query can return more than one row,
172 /// it may lead to wasted CPU time and bandwidth on the database server.
173 ///
174 /// Even when the driver implementation takes this into account, ensuring the query returns at most one row
175 /// can result in a more optimal query plan.
176 ///
177 /// If your query has a `WHERE` clause filtering a unique column by a single value, you're good.
178 ///
179 /// Otherwise, you might want to add `LIMIT 1` to your query.
180 #[inline]
181 pub async fn fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_optional().<'e, 'c: 'e, E>(self, executor: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<O>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>
182 where
183 'q: 'e,
184 E: 'e + Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#<'c, Databaserust-analyzer cargo sqlx-core 0.8.3 executor/Executor#Database# = DB>,
185 DB: 'e,
186 O: 'e,
187 A: 'e,
188 {
189 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner..fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_optional().(executor).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|it| it.0))
190 }
191 }
192
193 /// Execute a single SQL query as a prepared statement (transparently cached) and extract the first
194 /// column of each row.
195 ///
196 /// Extracts the first column of each row. Additional columns are ignored.
197 /// Any type that implements `Type<DB> + Decode<DB>` may be used.
198 ///
199 /// For details about prepared statements and allowed SQL syntax, see [`query()`][crate::query::query].
200 ///
201 /// ### Example: Simple Lookup
202 /// If you just want to look up a single value with little fanfare, this API is perfect for you:
203 ///
204 /// ```rust,no_run
205 /// # async fn example_lookup() -> Result<(), Box<dyn std::error::Error>> {
206 /// # let mut conn: sqlx::PgConnection = unimplemented!();
207 /// use uuid::Uuid;
208 ///
209 /// // MySQL and MariaDB: use `?`
210 /// let user_id: Option<Uuid> = sqlx::query_scalar("SELECT user_id FROM users WHERE username = $1")
211 /// .bind("alice")
212 /// // Use `&mut` where `conn` is a connection or a transaction, or use `&` for a `Pool`.
213 /// .fetch_optional(&mut conn)
214 /// .await?;
215 ///
216 /// let user_id = user_id.ok_or("unknown user")?;
217 ///
218 /// # Ok(())
219 /// # }
220 /// ```
221 ///
222 /// Note how we're using `.fetch_optional()` because the lookup may return no results,
223 /// in which case we need to be able to handle an empty result set.
224 /// Any rows after the first are ignored.
225 ///
226 /// ### Example: `COUNT`
227 /// This API is the easiest way to invoke an aggregate query like `SELECT COUNT(*)`, because you
228 /// can conveniently extract the result:
229 ///
230 /// ```rust,no_run
231 /// # async fn example_count() -> sqlx::Result<()> {
232 /// # let mut conn: sqlx::PgConnection = unimplemented!();
233 /// // Note that `usize` is not used here because unsigned integers are generally not supported,
234 /// // and `usize` doesn't even make sense as a mapping because the database server may have
235 /// // a completely different architecture.
236 /// //
237 /// // `i64` is generally a safe choice for `COUNT`.
238 /// let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM users WHERE accepted_tos IS TRUE")
239 /// // Use `&mut` where `conn` is a connection or a transaction, or use `&` for a `Pool`.
240 /// .fetch_one(&mut conn)
241 /// .await?;
242 ///
243 /// // The above is functionally equivalent to the following:
244 /// // Note the trailing comma, required for the compiler to recognize a 1-element tuple.
245 /// let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM users WHERE accepted_tos IS TRUE")
246 /// .fetch_one(&mut conn)
247 /// .await?;
248 /// # Ok(())
249 /// # }
250 /// ```
251 ///
252 /// ### Example: `EXISTS`
253 /// To test if a row exists or not, use `SELECT EXISTS(<query>)`:
254 ///
255 /// ```rust,no_run
256 /// # async fn example_exists() -> sqlx::Result<()> {
257 /// # let mut conn: sqlx::PgConnection = unimplemented!();
258 /// // MySQL and MariaDB: use `?`
259 /// let username_taken: bool = sqlx::query_scalar(
260 /// "SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)"
261 /// )
262 /// .bind("alice")
263 /// // Use `&mut` where `conn` is a connection or a transaction, or use `&` for a `Pool`.
264 /// .fetch_one(&mut conn)
265 /// .await?;
266 /// # Ok(())
267 /// # }
268 /// ```
269 ///
270 /// ### Example: Other Aggregates
271 /// Be aware that most other aggregate functions return `NULL` if the query yields an empty set:
272 ///
273 /// ```rust,no_run
274 /// # async fn example_aggregate() -> sqlx::Result<()> {
275 /// # let mut conn: sqlx::PgConnection = unimplemented!();
276 /// let max_upvotes: Option<i64> = sqlx::query_scalar("SELECT MAX(upvotes) FROM posts")
277 /// // Use `&mut` where `conn` is a connection or a transaction, or use `&` for a `Pool`.
278 /// .fetch_one(&mut conn)
279 /// .await?;
280 /// # Ok(())
281 /// # }
282 /// ```
283 ///
284 /// Note how we're using `Option<i64>` with `.fetch_one()`, because we're always expecting one row
285 /// but the column value may be `NULL`. If no rows are returned, this will error.
286 ///
287 /// This is in contrast to using `.fetch_optional()` with `Option<i64>`, which implies that
288 /// we're expecting _either_ a row with a `i64` (`BIGINT`), _or_ no rows at all.
289 ///
290 /// Either way, any rows after the first are ignored.
291 ///
292 /// ### Example: `Vec` of Scalars
293 /// If you want to collect a single column from a query into a vector,
294 /// try `.fetch_all()`:
295 ///
296 /// ```rust,no_run
297 /// # async fn example_vec() -> sqlx::Result<()> {
298 /// # let mut conn: sqlx::PgConnection = unimplemented!();
299 /// let top_users: Vec<String> = sqlx::query_scalar(
300 /// // Note the `LIMIT` to ensure that this doesn't return *all* users:
301 /// "SELECT username
302 /// FROM (
303 /// SELECT SUM(upvotes) total, user_id
304 /// FROM posts
305 /// GROUP BY user_id
306 /// ) top_users
307 /// INNER JOIN users USING (user_id)
308 /// ORDER BY total DESC
309 /// LIMIT 10"
310 /// )
311 /// // Use `&mut` where `conn` is a connection or a transaction, or use `&` for a `Pool`.
312 /// .fetch_all(&mut conn)
313 /// .await?;
314 ///
315 /// // `top_users` could be empty, too.
316 /// assert!(top_users.len() <= 10);
317 /// # Ok(())
318 /// # }
319 /// ```
320 #[inline]
321 pub fn query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().<'q, DB, O>(
322 sql: &'q str,
323 ) -> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Argumentsrust-analyzer cargo sqlx-core 0.8.3 database/Database#Arguments#<'q>>
324 where
325 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
326 (O,): for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
327 {
328 QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar# {
329 innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.: query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as().(sql),
330 }
331 }
332
333 /// Execute a SQL query as a prepared statement (transparently cached), with the given arguments,
334 /// and extract the first column of each row.
335 ///
336 /// See [`query_scalar()`] for details.
337 ///
338 /// For details about prepared statements and allowed SQL syntax, see [`query()`][crate::query::query].
339 #[inline]
340 pub fn query_scalar_withrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar_with().<'q, DB, O, A>(sql: &'q str, arguments: A) -> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
341 where
342 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
343 A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>,
344 (O,): for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
345 {
346 query_scalar_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar_with_result().(sql, Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(arguments))
347 }
348
349 /// Same as [`query_scalar_with`] but takes arguments as Result
350 #[inline]
351 pub fn query_scalar_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar_with_result().<'q, DB, O, A>(
352 sql: &'q str,
353 arguments: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<A, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#>,
354 ) -> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
355 where
356 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
357 A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>,
358 (O,): for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
359 {
360 QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar# {
361 innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.: query_as_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as_with_result().(sql, arguments),
362 }
363 }
364
365 // Make a SQL query from a statement, that is mapped to a concrete value.
366 pub fn query_statement_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_statement_scalar().<'q, DB, O>(
367 statement: &'q DB::Statementrust-analyzer cargo sqlx-core 0.8.3 database/Database#Statement#<'q>,
368 ) -> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Argumentsrust-analyzer cargo sqlx-core 0.8.3 database/Database#Arguments#<'_>>
369 where
370 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
371 (O,): for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
372 {
373 QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar# {
374 innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.: query_statement_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as().(statement),
375 }
376 }
377
378 // Make a SQL query from a statement, with the given arguments, that is mapped to a concrete value.
379 pub fn query_statement_scalar_withrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_statement_scalar_with().<'q, DB, O, A>(
380 statement: &'q DB::Statementrust-analyzer cargo sqlx-core 0.8.3 database/Database#Statement#<'q>,
381 arguments: A,
382 ) -> QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#<'q, DB, O, A>
383 where
384 DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#,
385 A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>,
386 (O,): for<'r> FromRowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#<'r, DB::Rowrust-analyzer cargo sqlx-core 0.8.3 database/Database#Row#>,
387 {
388 QueryScalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar# {
389 innerrust-analyzer cargo sqlx-core 0.8.3 query_scalar/QueryScalar#inner.: query_statement_as_withrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as_with().(statement, arguments),
390 }
391 }