| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 2 | |
| 3 | use eitherrust-analyzer cargo either 1.18.0 crate/::Eitherrust-analyzer cargo either 1.18.0 Either#; |
| 4 | 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#; |
| 5 | use futures_utilrust-analyzer cargo futures-util 0.3.34 crate/::{StreamExtrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#, TryStreamExtrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#}; |
| 6 | |
| 7 | 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#; |
| 8 | 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#}; |
| 9 | 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#; |
| 10 | 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#}; |
| 11 | 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#}; |
| 12 | 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#; |
| 13 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::queryrust-analyzer cargo sqlx-core 0.8.3 query/::{queryrust-analyzer cargo sqlx-core 0.8.3 query/query()., query_statementrust-analyzer cargo sqlx-core 0.8.3 query/query_statement()., query_statement_withrust-analyzer cargo sqlx-core 0.8.3 query/query_statement_with()., query_with_resultrust-analyzer cargo sqlx-core 0.8.3 query/query_with_result()., Queryrust-analyzer cargo sqlx-core 0.8.3 query/Query#}; |
| 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, mapping results using [`FromRow`]. |
| 17 | /// Returned by [`query_as()`]. |
| 18 | #[must_use = "query must be executed to affect database"] |
| 19 | pub struct QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'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_as/QueryAs#inner.: Queryrust-analyzer cargo sqlx-core 0.8.3 query/Query#<'q, DB, A>, |
| 21 | pub(craterust-analyzer cargo sqlx-core 0.8.3 crate/) outputrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#output.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<O>, |
| 22 | } |
| 23 | |
| 24 | impl<'q, DB, 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 QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 25 | where |
| 26 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 27 | A: 'q + IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>, |
| 28 | { |
| 29 | #[inline] |
| 30 | fn sqlrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]sql().(&self) -> &'q str { |
| 31 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..sqlrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`][`Execute<'q, DB>`]sql().() |
| 32 | } |
| 33 | |
| 34 | #[inline] |
| 35 | fn statementrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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>> { |
| 36 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..statementrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`][`Execute<'q, DB>`]statement().() |
| 37 | } |
| 38 | |
| 39 | #[inline] |
| 40 | fn take_argumentsrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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#> { |
| 41 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..take_argumentsrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`][`Execute<'q, DB>`]take_arguments().() |
| 42 | } |
| 43 | |
| 44 | #[inline] |
| 45 | fn persistentrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`][`Execute<'q, DB>`]persistent().(&self) -> bool { |
| 46 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..persistentrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`][`Execute<'q, DB>`]persistent().() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl<'q, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, O> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'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>> { |
| 51 | /// Bind a value for use with this SQL query. |
| 52 | /// |
| 53 | /// See [`Query::bind`](Query::bind). |
| 54 | pub fn bindrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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_as/impl#[`QueryAs<'q, DB, O, <DB as Database>::Arguments<'q>>`] { |
| 55 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner. = self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(value); |
| 56 | self |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl<'q, DB, O, A> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 61 | where |
| 62 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database# + HasStatementCacherust-analyzer cargo sqlx-core 0.8.3 database/HasStatementCache#, |
| 63 | { |
| 64 | /// If `true`, the statement will get prepared once and cached to the |
| 65 | /// connection's statement cache. |
| 66 | /// |
| 67 | /// If queried once with the flag set to `true`, all subsequent queries |
| 68 | /// matching the one with the flag will use the cached statement until the |
| 69 | /// cache is cleared. |
| 70 | /// |
| 71 | /// If `false`, the prepared statement will be closed after execution. |
| 72 | /// |
| 73 | /// Default: `true`. |
| 74 | pub fn persistentrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]persistent().(mut self, value: bool) -> Selfrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`] { |
| 75 | self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner. = self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner..persistentrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]persistent().(value); |
| 76 | self |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // FIXME: This is very close, nearly 1:1 with `Map` |
| 81 | // noinspection DuplicatedCode |
| 82 | impl<'q, DB, O, A> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 83 | where |
| 84 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 85 | A: 'q + IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>, |
| 86 | 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#>, |
| 87 | { |
| 88 | /// Execute the query and return the generated results as a stream. |
| 89 | pub fn fetchrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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#>> |
| 90 | where |
| 91 | 'q: 'e, |
| 92 | 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>, |
| 93 | DB: 'e, |
| 94 | O: 'e, |
| 95 | A: 'e, |
| 96 | { |
| 97 | // FIXME: this should have used `executor.fetch()` but that's a breaking change |
| 98 | // because this technically allows multiple statements in one query string. |
| 99 | #[allow(deprecated)] |
| 100 | self.fetch_manyrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_many().(executor) |
| 101 | .try_filter_maprust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#try_filter_map().(|step| async move { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(step.rightrust-analyzer cargo either 1.18.0 impl#[`Either<L, R>`]right().()) }) |
| 102 | .boxedrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#boxed().() |
| 103 | } |
| 104 | |
| 105 | /// Execute multiple queries and return the generated results as a stream |
| 106 | /// from each query, in a stream. |
| 107 | #[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."] |
| 108 | pub fn fetch_manyrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_many().<'e, 'c: 'e, E>( |
| 109 | self, |
| 110 | executor: E, |
| 111 | ) -> 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#>> |
| 112 | where |
| 113 | 'q: 'e, |
| 114 | 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>, |
| 115 | DB: 'e, |
| 116 | O: 'e, |
| 117 | A: 'e, |
| 118 | { |
| 119 | executor |
| 120 | .fetch_manyrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#fetch_many().(self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.) |
| 121 | .maprust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#map().(|v| match v { |
| 122 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Eitherrust-analyzer cargo either 1.18.0 Either#::Rightrust-analyzer cargo either 1.18.0 Either#Right#(row)) => O::from_rowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#from_row().(&row).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Eitherrust-analyzer cargo either 1.18.0 Either#::Rightrust-analyzer cargo either 1.18.0 Either#Right#), |
| 123 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Eitherrust-analyzer cargo either 1.18.0 Either#::Leftrust-analyzer cargo either 1.18.0 Either#Left#(v)) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Eitherrust-analyzer cargo either 1.18.0 Either#::Leftrust-analyzer cargo either 1.18.0 Either#Left#(v)), |
| 124 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 125 | }) |
| 126 | .boxedrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#boxed().() |
| 127 | } |
| 128 | |
| 129 | /// Execute the query and return all the resulting rows collected into a [`Vec`]. |
| 130 | /// |
| 131 | /// ### Note: beware result set size. |
| 132 | /// This will attempt to collect the full result set of the query into memory. |
| 133 | /// |
| 134 | /// To avoid exhausting available memory, ensure the result set has a known upper bound, |
| 135 | /// e.g. using `LIMIT`. |
| 136 | #[inline] |
| 137 | pub async fn fetch_allrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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#> |
| 138 | where |
| 139 | 'q: 'e, |
| 140 | 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>, |
| 141 | DB: 'e, |
| 142 | O: 'e, |
| 143 | A: 'e, |
| 144 | { |
| 145 | self.fetchrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch().(executor).try_collectrust-analyzer cargo futures-util 0.3.34 stream/try_stream/TryStreamExt#try_collect().().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(). |
| 146 | } |
| 147 | |
| 148 | /// Execute the query, returning the first row or [`Error::RowNotFound`] otherwise. |
| 149 | /// |
| 150 | /// ### Note: for best performance, ensure the query returns at most one row. |
| 151 | /// Depending on the driver implementation, if your query can return more than one row, |
| 152 | /// it may lead to wasted CPU time and bandwidth on the database server. |
| 153 | /// |
| 154 | /// Even when the driver implementation takes this into account, ensuring the query returns at most one row |
| 155 | /// can result in a more optimal query plan. |
| 156 | /// |
| 157 | /// If your query has a `WHERE` clause filtering a unique column by a single value, you're good. |
| 158 | /// |
| 159 | /// Otherwise, you might want to add `LIMIT 1` to your query. |
| 160 | pub async fn fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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#> |
| 161 | where |
| 162 | 'q: 'e, |
| 163 | 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>, |
| 164 | DB: 'e, |
| 165 | O: 'e, |
| 166 | A: 'e, |
| 167 | { |
| 168 | self.fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_optional().(executor) |
| 169 | .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(). |
| 170 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|row| row.ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#::RowNotFoundrust-analyzer cargo sqlx-core 0.8.3 error/Error#RowNotFound#)) |
| 171 | } |
| 172 | |
| 173 | /// Execute the query, returning the first row or `None` otherwise. |
| 174 | /// |
| 175 | /// ### Note: for best performance, ensure the query returns at most one row. |
| 176 | /// Depending on the driver implementation, if your query can return more than one row, |
| 177 | /// it may lead to wasted CPU time and bandwidth on the database server. |
| 178 | /// |
| 179 | /// Even when the driver implementation takes this into account, ensuring the query returns at most one row |
| 180 | /// can result in a more optimal query plan. |
| 181 | /// |
| 182 | /// If your query has a `WHERE` clause filtering a unique column by a single value, you're good. |
| 183 | /// |
| 184 | /// Otherwise, you might want to add `LIMIT 1` to your query. |
| 185 | pub async fn fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'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#> |
| 186 | where |
| 187 | 'q: 'e, |
| 188 | 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>, |
| 189 | DB: 'e, |
| 190 | O: 'e, |
| 191 | A: 'e, |
| 192 | { |
| 193 | let row = executor.fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#fetch_optional().(self.innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 194 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(row) = row { |
| 195 | O::from_rowrust-analyzer cargo sqlx-core 0.8.3 from_row/FromRow#from_row().(&row).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#) |
| 196 | } else { |
| 197 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Execute a single SQL query as a prepared statement (transparently cached). |
| 203 | /// Maps rows to Rust types using [`FromRow`]. |
| 204 | /// |
| 205 | /// For details about prepared statements and allowed SQL syntax, see [`query()`][crate::query::query]. |
| 206 | /// |
| 207 | /// ### Example: Map Rows using Tuples |
| 208 | /// [`FromRow`] is implemented for tuples of up to 16 elements<sup>1</sup>. |
| 209 | /// Using a tuple of N elements will extract the first N columns from each row using [`Decode`][crate::decode::Decode]. |
| 210 | /// Any extra columns are ignored. |
| 211 | /// |
| 212 | /// See [`sqlx::types`][crate::types] for the types that can be used. |
| 213 | /// |
| 214 | /// The `FromRow` implementation will check [`Type::compatible()`] for each column to ensure a compatible type mapping |
| 215 | /// is used. If an incompatible mapping is detected, an error is returned. |
| 216 | /// To statically assert compatible types at compile time, see the `query!()` family of macros. |
| 217 | /// |
| 218 | /// **NOTE**: `SELECT *` is not recommended with this approach because the ordering of returned columns may be different |
| 219 | /// than expected, especially when using joins. |
| 220 | /// |
| 221 | /// ```rust,no_run |
| 222 | /// # async fn example1() -> sqlx::Result<()> { |
| 223 | /// use sqlx::Connection; |
| 224 | /// use sqlx::PgConnection; |
| 225 | /// |
| 226 | /// // This example can be applied to any database as it only uses standard types and syntax. |
| 227 | /// let mut conn: PgConnection = PgConnection::connect("<Database URL>").await?; |
| 228 | /// |
| 229 | /// sqlx::raw_sql( |
| 230 | /// "CREATE TABLE users(id INTEGER PRIMARY KEY, username TEXT UNIQUE, created_at TIMESTAMPTZ DEFAULT (now()))" |
| 231 | /// ) |
| 232 | /// .execute(&mut conn) |
| 233 | /// .await?; |
| 234 | /// |
| 235 | /// sqlx::query("INSERT INTO users(id, username) VALUES (1, 'alice'), (2, 'bob');") |
| 236 | /// .execute(&mut conn) |
| 237 | /// .await?; |
| 238 | /// |
| 239 | /// // Get the first row of the result (note the `LIMIT 1` for efficiency) |
| 240 | /// // This assumes the `time` feature of SQLx is enabled. |
| 241 | /// let oldest_user: (i32, String, time::OffsetDateTime) = sqlx::query_as( |
| 242 | /// "SELECT id, username, created_at FROM users ORDER BY created_at LIMIT 1" |
| 243 | /// ) |
| 244 | /// .fetch_one(&mut conn) |
| 245 | /// .await?; |
| 246 | /// |
| 247 | /// assert_eq!(oldest_user.0, 1); |
| 248 | /// assert_eq!(oldest_user.1, "alice"); |
| 249 | /// |
| 250 | /// // Get at most one row |
| 251 | /// let maybe_charlie: Option<(i32, String, time::OffsetDateTime)> = sqlx::query_as( |
| 252 | /// "SELECT id, username, created_at FROM users WHERE username = 'charlie'" |
| 253 | /// ) |
| 254 | /// .fetch_optional(&mut conn) |
| 255 | /// .await?; |
| 256 | /// |
| 257 | /// assert_eq!(maybe_charlie, None); |
| 258 | /// |
| 259 | /// // Get all rows in result (Beware of the size of the result set! Consider using `LIMIT`) |
| 260 | /// let users: Vec<(i32, String, time::OffsetDateTime)> = sqlx::query_as( |
| 261 | /// "SELECT id, username, created_at FROM users ORDER BY id" |
| 262 | /// ) |
| 263 | /// .fetch_all(&mut conn) |
| 264 | /// .await?; |
| 265 | /// |
| 266 | /// println!("{users:?}"); |
| 267 | /// # Ok(()) |
| 268 | /// # } |
| 269 | /// ``` |
| 270 | /// |
| 271 | /// <sup>1</sup>: It's impossible in Rust to implement a trait for tuples of arbitrary size. |
| 272 | /// For larger result sets, either use an explicit struct (see below) or use [`query()`][crate::query::query] |
| 273 | /// instead and extract columns dynamically. |
| 274 | /// |
| 275 | /// ### Example: Map Rows using `#[derive(FromRow)]` |
| 276 | /// Using `#[derive(FromRow)]`, we can create a Rust struct to represent our row type |
| 277 | /// so we can look up fields by name instead of tuple index. |
| 278 | /// |
| 279 | /// When querying this way, columns will be matched up to the corresponding fields by name, so `SELECT *` is safe to use. |
| 280 | /// However, you will still want to be aware of duplicate column names in your query when using joins. |
| 281 | /// |
| 282 | /// The derived `FromRow` implementation will check [`Type::compatible()`] for each column to ensure a compatible type |
| 283 | /// mapping is used. If an incompatible mapping is detected, an error is returned. |
| 284 | /// To statically assert compatible types at compile time, see the `query!()` family of macros. |
| 285 | /// |
| 286 | /// An error will also be returned if an expected column is missing from the result set. |
| 287 | /// |
| 288 | /// `#[derive(FromRow)]` supports several control attributes which can be used to change how column names and types |
| 289 | /// are mapped. See [`FromRow`] for details. |
| 290 | /// |
| 291 | /// Using our previous table definition, we can convert our queries like so: |
| 292 | /// ```rust,no_run |
| 293 | /// # async fn example2() -> sqlx::Result<()> { |
| 294 | /// use sqlx::Connection; |
| 295 | /// use sqlx::PgConnection; |
| 296 | /// |
| 297 | /// use time::OffsetDateTime; |
| 298 | /// |
| 299 | /// #[derive(sqlx::FromRow, Debug, PartialEq, Eq)] |
| 300 | /// struct User { |
| 301 | /// id: i64, |
| 302 | /// username: String, |
| 303 | /// // Note: the derive won't compile if the `time` feature of SQLx is not enabled. |
| 304 | /// created_at: OffsetDateTime, |
| 305 | /// } |
| 306 | /// |
| 307 | /// let mut conn: PgConnection = PgConnection::connect("<Database URL>").await?; |
| 308 | /// |
| 309 | /// // Get the first row of the result (note the `LIMIT 1` for efficiency) |
| 310 | /// let oldest_user: User = sqlx::query_as( |
| 311 | /// "SELECT id, username, created_at FROM users ORDER BY created_at LIMIT 1" |
| 312 | /// ) |
| 313 | /// .fetch_one(&mut conn) |
| 314 | /// .await?; |
| 315 | /// |
| 316 | /// assert_eq!(oldest_user.id, 1); |
| 317 | /// assert_eq!(oldest_user.username, "alice"); |
| 318 | /// |
| 319 | /// // Get at most one row |
| 320 | /// let maybe_charlie: Option<User> = sqlx::query_as( |
| 321 | /// "SELECT id, username, created_at FROM users WHERE username = 'charlie'" |
| 322 | /// ) |
| 323 | /// .fetch_optional(&mut conn) |
| 324 | /// .await?; |
| 325 | /// |
| 326 | /// assert_eq!(maybe_charlie, None); |
| 327 | /// |
| 328 | /// // Get all rows in result (Beware of the size of the result set! Consider using `LIMIT`) |
| 329 | /// let users: Vec<User> = sqlx::query_as( |
| 330 | /// "SELECT id, username, created_at FROM users ORDER BY id" |
| 331 | /// ) |
| 332 | /// .fetch_all(&mut conn) |
| 333 | /// .await?; |
| 334 | /// |
| 335 | /// assert_eq!(users[1].id, 2); |
| 336 | /// assert_eq!(users[1].username, "bob"); |
| 337 | /// # Ok(()) |
| 338 | /// # } |
| 339 | /// |
| 340 | /// ``` |
| 341 | #[inline] |
| 342 | pub fn query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as().<'q, DB, O>(sql: &'q str) -> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'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>> |
| 343 | where |
| 344 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 345 | 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#>, |
| 346 | { |
| 347 | QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs# { |
| 348 | innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.: queryrust-analyzer cargo sqlx-core 0.8.3 query/query().(sql), |
| 349 | outputrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#output.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /// Execute a single SQL query, with the given arguments as a prepared statement (transparently cached). |
| 354 | /// Maps rows to Rust types using [`FromRow`]. |
| 355 | /// |
| 356 | /// For details about prepared statements and allowed SQL syntax, see [`query()`][crate::query::query]. |
| 357 | /// |
| 358 | /// For details about type mapping from [`FromRow`], see [`query_as()`]. |
| 359 | #[inline] |
| 360 | pub fn query_as_withrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as_with().<'q, DB, O, A>(sql: &'q str, arguments: A) -> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 361 | where |
| 362 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 363 | A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>, |
| 364 | 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#>, |
| 365 | { |
| 366 | query_as_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as_with_result().(sql, Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(arguments)) |
| 367 | } |
| 368 | |
| 369 | /// Same as [`query_as_with`] but takes arguments as a Result |
| 370 | #[inline] |
| 371 | pub fn query_as_with_resultrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as_with_result().<'q, DB, O, A>( |
| 372 | sql: &'q str, |
| 373 | 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#>, |
| 374 | ) -> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 375 | where |
| 376 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 377 | A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>, |
| 378 | 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#>, |
| 379 | { |
| 380 | QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs# { |
| 381 | innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.: query_with_resultrust-analyzer cargo sqlx-core 0.8.3 query/query_with_result().(sql, arguments), |
| 382 | outputrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#output.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Make a SQL query from a statement, that is mapped to a concrete type. |
| 387 | pub fn query_statement_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as().<'q, DB, O>( |
| 388 | statement: &'q DB::Statementrust-analyzer cargo sqlx-core 0.8.3 database/Database#Statement#<'q>, |
| 389 | ) -> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'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#<'_>> |
| 390 | where |
| 391 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 392 | 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#>, |
| 393 | { |
| 394 | QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs# { |
| 395 | innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.: query_statementrust-analyzer cargo sqlx-core 0.8.3 query/query_statement().(statement), |
| 396 | outputrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#output.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Make a SQL query from a statement, with the given arguments, that is mapped to a concrete type. |
| 401 | pub fn query_statement_as_withrust-analyzer cargo sqlx-core 0.8.3 query_as/query_statement_as_with().<'q, DB, O, A>( |
| 402 | statement: &'q DB::Statementrust-analyzer cargo sqlx-core 0.8.3 database/Database#Statement#<'q>, |
| 403 | arguments: A, |
| 404 | ) -> QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#<'q, DB, O, A> |
| 405 | where |
| 406 | DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#, |
| 407 | A: IntoArgumentsrust-analyzer cargo sqlx-core 0.8.3 arguments/IntoArguments#<'q, DB>, |
| 408 | 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#>, |
| 409 | { |
| 410 | QueryAsrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs# { |
| 411 | innerrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#inner.: query_statement_withrust-analyzer cargo sqlx-core 0.8.3 query/query_statement_with().(statement, arguments), |
| 412 | outputrust-analyzer cargo sqlx-core 0.8.3 query_as/QueryAs#output.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 413 | } |
| 414 | } |