diff --git a/Cargo.toml b/Cargo.toml index 6d08df23d3..5e11932b31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -244,6 +244,10 @@ disallowed_methods = 'deny' level = 'warn' check-cfg = [ 'cfg(mariadb, values(any()))', + 'cfg(postgres_12)', + 'cfg(postgres_13)', + 'cfg(postgres_14)', + 'cfg(postgres_15)', 'cfg(sqlite_ipaddr)', 'cfg(sqlite_test_sqlcipher)', ] diff --git a/sqlx-core/src/any/arguments.rs b/sqlx-core/src/any/arguments.rs index 2c05e3fd5b..59d6f4d6e0 100644 --- a/sqlx-core/src/any/arguments.rs +++ b/sqlx-core/src/any/arguments.rs @@ -4,22 +4,24 @@ use crate::arguments::Arguments; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; +use std::sync::Arc; -pub struct AnyArguments<'q> { +#[derive(Default)] +pub struct AnyArguments { #[doc(hidden)] - pub values: AnyArgumentBuffer<'q>, + pub values: AnyArgumentBuffer, } -impl<'q> Arguments<'q> for AnyArguments<'q> { +impl Arguments for AnyArguments { type Database = Any; fn reserve(&mut self, additional: usize, _size: usize) { self.values.0.reserve(additional); } - fn add(&mut self, value: T) -> Result<(), BoxDynError> + fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: 'q + Encode<'q, Self::Database> + Type, + T: Encode<'t, Self::Database> + Type, { let _: IsNull = value.encode(&mut self.values)?; Ok(()) @@ -30,21 +32,13 @@ impl<'q> Arguments<'q> for AnyArguments<'q> { } } -pub struct AnyArgumentBuffer<'q>(#[doc(hidden)] pub Vec>); +#[derive(Default)] +pub struct AnyArgumentBuffer(#[doc(hidden)] pub Vec); -impl Default for AnyArguments<'_> { - fn default() -> Self { - AnyArguments { - values: AnyArgumentBuffer(vec![]), - } - } -} - -impl<'q> AnyArguments<'q> { +impl AnyArguments { #[doc(hidden)] - pub fn convert_to<'a, A: Arguments<'a>>(&'a self) -> Result + pub fn convert_into<'a, A: Arguments>(self) -> Result where - 'q: 'a, Option: Type + Encode<'a, A::Database>, Option: Type + Encode<'a, A::Database>, Option: Type + Encode<'a, A::Database>, @@ -60,12 +54,13 @@ impl<'q> AnyArguments<'q> { i64: Type + Encode<'a, A::Database>, f32: Type + Encode<'a, A::Database>, f64: Type + Encode<'a, A::Database>, - &'a str: Type + Encode<'a, A::Database>, - &'a [u8]: Type + Encode<'a, A::Database>, + Arc: Type + Encode<'a, A::Database>, + Arc: Type + Encode<'a, A::Database>, + Arc>: Type + Encode<'a, A::Database>, { let mut out = A::default(); - for arg in &self.values.0 { + for arg in self.values.0 { match arg { AnyValueKind::Null(AnyTypeInfoKind::Null) => out.add(Option::::None), AnyValueKind::Null(AnyTypeInfoKind::Bool) => out.add(Option::::None), @@ -82,8 +77,9 @@ impl<'q> AnyArguments<'q> { AnyValueKind::BigInt(i) => out.add(i), AnyValueKind::Real(r) => out.add(r), AnyValueKind::Double(d) => out.add(d), - AnyValueKind::Text(t) => out.add(&**t), - AnyValueKind::Blob(b) => out.add(&**b), + AnyValueKind::Text(t) => out.add(t), + AnyValueKind::TextSlice(t) => out.add(t), + AnyValueKind::Blob(b) => out.add(b), }? } Ok(out) diff --git a/sqlx-core/src/any/connection/backend.rs b/sqlx-core/src/any/connection/backend.rs index e59b345ed9..8254a7a6c6 100644 --- a/sqlx-core/src/any/connection/backend.rs +++ b/sqlx-core/src/any/connection/backend.rs @@ -4,6 +4,7 @@ use crate::sql_str::SqlStr; use either::Either; use futures_core::future::BoxFuture; use futures_core::stream::BoxStream; +use futures_util::TryStreamExt; use std::fmt::Debug; pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static { @@ -94,19 +95,31 @@ pub trait AnyConnectionBackend: std::any::Any + Debug + Send + 'static { )) } - fn fetch_many<'q>( - &'q mut self, + fn fetch_many( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxStream<'q, crate::Result>>; + arguments: Option, + ) -> BoxStream<'_, crate::Result>>; - fn fetch_optional<'q>( - &'q mut self, + fn fetch_optional( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxFuture<'q, crate::Result>>; + arguments: Option, + ) -> BoxFuture<'_, crate::Result>> { + let mut stream = self.fetch_many(query, persistent, arguments); + + Box::pin(async move { + while let Some(result) = stream.try_next().await? { + if let Either::Right(row) = result { + return Ok(Some(row)); + } + } + + Ok(None) + }) + } fn prepare_with<'c, 'q: 'c>( &'c mut self, diff --git a/sqlx-core/src/any/database.rs b/sqlx-core/src/any/database.rs index 6e8343e928..6dd47003a2 100644 --- a/sqlx-core/src/any/database.rs +++ b/sqlx-core/src/any/database.rs @@ -25,8 +25,8 @@ impl Database for Any { type Value = AnyValue; type ValueRef<'r> = AnyValueRef<'r>; - type Arguments<'q> = AnyArguments<'q>; - type ArgumentBuffer<'q> = AnyArgumentBuffer<'q>; + type Arguments = AnyArguments; + type ArgumentBuffer = AnyArgumentBuffer; type Statement = AnyStatement; diff --git a/sqlx-core/src/any/mod.rs b/sqlx-core/src/any/mod.rs index 032f4dda03..9d37bbf5ab 100644 --- a/sqlx-core/src/any/mod.rs +++ b/sqlx-core/src/any/mod.rs @@ -56,7 +56,7 @@ pub trait AnyExecutor<'c>: Executor<'c, Database = Any> {} impl<'c, T: Executor<'c, Database = Any>> AnyExecutor<'c> for T {} // NOTE: required due to the lack of lazy normalization -impl_into_arguments_for_arguments!(AnyArguments<'q>); +impl_into_arguments_for_arguments!(AnyArguments); // impl_executor_for_pool_connection!(Any, AnyConnection, AnyRow); // impl_executor_for_transaction!(Any, AnyRow); impl_acquire!(Any, AnyConnection); @@ -71,7 +71,7 @@ where { fn encode_by_ref( &self, - buf: &mut AnyArgumentBuffer<'q>, + buf: &mut AnyArgumentBuffer, ) -> Result { if let Some(value) = self { value.encode_by_ref(buf) diff --git a/sqlx-core/src/any/statement.rs b/sqlx-core/src/any/statement.rs index 98b28c2c32..42c7255736 100644 --- a/sqlx-core/src/any/statement.rs +++ b/sqlx-core/src/any/statement.rs @@ -44,7 +44,7 @@ impl Statement for AnyStatement { &self.columns } - impl_statement_query!(AnyArguments<'_>); + impl_statement_query!(AnyArguments); } impl ColumnIndex for &'_ str { diff --git a/sqlx-core/src/any/types/blob.rs b/sqlx-core/src/any/types/blob.rs index 851c93bf5a..c6c82dde88 100644 --- a/sqlx-core/src/any/types/blob.rs +++ b/sqlx-core/src/any/types/blob.rs @@ -4,7 +4,7 @@ use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; -use std::borrow::Cow; +use std::sync::Arc; impl Type for [u8] { fn type_info() -> AnyTypeInfo { @@ -17,9 +17,9 @@ impl Type for [u8] { impl<'q> Encode<'q, Any> for &'q [u8] { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { - buf.0.push(AnyValueKind::Blob((*self).into())); + buf.0.push(AnyValueKind::Blob(Arc::new(self.to_vec()))); Ok(IsNull::No) } } @@ -27,12 +27,7 @@ impl<'q> Encode<'q, Any> for &'q [u8] { impl<'r> Decode<'r, Any> for &'r [u8] { fn decode(value: ::ValueRef<'r>) -> Result { match value.kind { - AnyValueKind::Blob(Cow::Borrowed(blob)) => Ok(blob), - // This shouldn't happen in practice, it means the user got an `AnyValueRef` - // constructed from an owned `Vec` which shouldn't be allowed by the API. - AnyValueKind::Blob(Cow::Owned(_text)) => { - panic!("attempting to return a borrow that outlives its buffer") - } + AnyValueKind::Blob(blob) => Ok(blob.as_slice()), other => other.unexpected(), } } @@ -44,12 +39,12 @@ impl Type for Vec { } } -impl<'q> Encode<'q, Any> for Vec { +impl Encode<'_, Any> for Vec { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { - buf.0.push(AnyValueKind::Blob(Cow::Owned(self.clone()))); + buf.0.push(AnyValueKind::Blob(Arc::new(self.clone()))); Ok(IsNull::No) } } @@ -57,7 +52,7 @@ impl<'q> Encode<'q, Any> for Vec { impl<'r> Decode<'r, Any> for Vec { fn decode(value: ::ValueRef<'r>) -> Result { match value.kind { - AnyValueKind::Blob(blob) => Ok(blob.into_owned()), + AnyValueKind::Blob(blob) => Ok(blob.as_ref().clone()), other => other.unexpected(), } } diff --git a/sqlx-core/src/any/types/bool.rs b/sqlx-core/src/any/types/bool.rs index fb7ee9d5dd..bc143e9bd5 100644 --- a/sqlx-core/src/any/types/bool.rs +++ b/sqlx-core/src/any/types/bool.rs @@ -13,10 +13,10 @@ impl Type for bool { } } -impl<'q> Encode<'q, Any> for bool { +impl Encode<'_, Any> for bool { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { buf.0.push(AnyValueKind::Bool(*self)); Ok(IsNull::No) @@ -26,7 +26,7 @@ impl<'q> Encode<'q, Any> for bool { impl<'r> Decode<'r, Any> for bool { fn decode(value: ::ValueRef<'r>) -> Result { match value.kind { - AnyValueKind::Bool(b) => Ok(b), + AnyValueKind::Bool(b) => Ok(*b), other => other.unexpected(), } } diff --git a/sqlx-core/src/any/types/float.rs b/sqlx-core/src/any/types/float.rs index 01d6073a2f..119e5ca95f 100644 --- a/sqlx-core/src/any/types/float.rs +++ b/sqlx-core/src/any/types/float.rs @@ -13,8 +13,8 @@ impl Type for f32 { } } -impl<'q> Encode<'q, Any> for f32 { - fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> Result { +impl Encode<'_, Any> for f32 { + fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer) -> Result { buf.0.push(AnyValueKind::Real(*self)); Ok(IsNull::No) } @@ -23,7 +23,7 @@ impl<'q> Encode<'q, Any> for f32 { impl<'r> Decode<'r, Any> for f32 { fn decode(value: AnyValueRef<'r>) -> Result { match value.kind { - AnyValueKind::Real(r) => Ok(r), + AnyValueKind::Real(r) => Ok(*r), other => other.unexpected(), } } @@ -37,10 +37,10 @@ impl Type for f64 { } } -impl<'q> Encode<'q, Any> for f64 { +impl Encode<'_, Any> for f64 { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { buf.0.push(AnyValueKind::Double(*self)); Ok(IsNull::No) @@ -51,8 +51,8 @@ impl<'r> Decode<'r, Any> for f64 { fn decode(value: ::ValueRef<'r>) -> Result { match value.kind { // Widening is safe - AnyValueKind::Real(r) => Ok(r as f64), - AnyValueKind::Double(d) => Ok(d), + AnyValueKind::Real(r) => Ok(*r as f64), + AnyValueKind::Double(d) => Ok(*d), other => other.unexpected(), } } diff --git a/sqlx-core/src/any/types/int.rs b/sqlx-core/src/any/types/int.rs index 56152af146..e2bb43212f 100644 --- a/sqlx-core/src/any/types/int.rs +++ b/sqlx-core/src/any/types/int.rs @@ -17,10 +17,10 @@ impl Type for i16 { } } -impl<'q> Encode<'q, Any> for i16 { +impl Encode<'_, Any> for i16 { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { buf.0.push(AnyValueKind::SmallInt(*self)); Ok(IsNull::No) @@ -45,10 +45,10 @@ impl Type for i32 { } } -impl<'q> Encode<'q, Any> for i32 { +impl Encode<'_, Any> for i32 { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { buf.0.push(AnyValueKind::Integer(*self)); Ok(IsNull::No) @@ -73,10 +73,10 @@ impl Type for i64 { } } -impl<'q> Encode<'q, Any> for i64 { +impl Encode<'_, Any> for i64 { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { buf.0.push(AnyValueKind::BigInt(*self)); Ok(IsNull::No) diff --git a/sqlx-core/src/any/types/str.rs b/sqlx-core/src/any/types/str.rs index 4c00832690..620262491b 100644 --- a/sqlx-core/src/any/types/str.rs +++ b/sqlx-core/src/any/types/str.rs @@ -5,7 +5,7 @@ use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::Type; -use std::borrow::Cow; +use std::sync::Arc; impl Type for str { fn type_info() -> AnyTypeInfo { @@ -16,17 +16,17 @@ impl Type for str { } impl<'a> Encode<'a, Any> for &'a str { - fn encode(self, buf: &mut ::ArgumentBuffer<'a>) -> Result + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result where Self: Sized, { - buf.0.push(AnyValueKind::Text(self.into())); + buf.0.push(AnyValueKind::Text(Arc::new(self.into()))); Ok(IsNull::No) } fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'a>, + buf: &mut ::ArgumentBuffer, ) -> Result { (*self).encode(buf) } @@ -35,12 +35,7 @@ impl<'a> Encode<'a, Any> for &'a str { impl<'a> Decode<'a, Any> for &'a str { fn decode(value: ::ValueRef<'a>) -> Result { match value.kind { - AnyValueKind::Text(Cow::Borrowed(text)) => Ok(text), - // This shouldn't happen in practice, it means the user got an `AnyValueRef` - // constructed from an owned `String` which shouldn't be allowed by the API. - AnyValueKind::Text(Cow::Owned(_text)) => { - panic!("attempting to return a borrow that outlives its buffer") - } + AnyValueKind::Text(text) => Ok(text.as_str()), other => other.unexpected(), } } @@ -52,12 +47,17 @@ impl Type for String { } } -impl<'q> Encode<'q, Any> for String { +impl Encode<'_, Any> for String { + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result { + buf.0.push(AnyValueKind::Text(Arc::new(self))); + Ok(IsNull::No) + } + fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { - buf.0.push(AnyValueKind::Text(Cow::Owned(self.clone()))); + buf.0.push(AnyValueKind::Text(Arc::new(self.clone()))); Ok(IsNull::No) } } @@ -65,7 +65,7 @@ impl<'q> Encode<'q, Any> for String { impl<'r> Decode<'r, Any> for String { fn decode(value: ::ValueRef<'r>) -> Result { match value.kind { - AnyValueKind::Text(text) => Ok(text.into_owned()), + AnyValueKind::Text(text) => Ok(text.to_string()), other => other.unexpected(), } } diff --git a/sqlx-core/src/any/value.rs b/sqlx-core/src/any/value.rs index 9917b39f4f..a85c1dc69c 100644 --- a/sqlx-core/src/any/value.rs +++ b/sqlx-core/src/any/value.rs @@ -1,14 +1,14 @@ -use std::borrow::Cow; - use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind}; use crate::database::Database; use crate::error::BoxDynError; use crate::types::Type; use crate::value::{Value, ValueRef}; +use std::borrow::Cow; +use std::sync::Arc; #[derive(Clone, Debug)] #[non_exhaustive] -pub enum AnyValueKind<'a> { +pub enum AnyValueKind { Null(AnyTypeInfoKind), Bool(bool), SmallInt(i16), @@ -16,11 +16,12 @@ pub enum AnyValueKind<'a> { BigInt(i64), Real(f32), Double(f64), - Text(Cow<'a, str>), - Blob(Cow<'a, [u8]>), + Text(Arc), + TextSlice(Arc), + Blob(Arc>), } -impl AnyValueKind<'_> { +impl AnyValueKind { fn type_info(&self) -> AnyTypeInfo { AnyTypeInfo { kind: match self { @@ -32,6 +33,7 @@ impl AnyValueKind<'_> { AnyValueKind::Real(_) => AnyTypeInfoKind::Real, AnyValueKind::Double(_) => AnyTypeInfoKind::Double, AnyValueKind::Text(_) => AnyTypeInfoKind::Text, + AnyValueKind::TextSlice(_) => AnyTypeInfoKind::Text, AnyValueKind::Blob(_) => AnyTypeInfoKind::Blob, }, } @@ -60,31 +62,19 @@ impl AnyValueKind<'_> { #[derive(Clone, Debug)] pub struct AnyValue { #[doc(hidden)] - pub kind: AnyValueKind<'static>, + pub kind: AnyValueKind, } #[derive(Clone, Debug)] pub struct AnyValueRef<'a> { - pub(crate) kind: AnyValueKind<'a>, + pub(crate) kind: &'a AnyValueKind, } impl Value for AnyValue { type Database = Any; fn as_ref(&self) -> ::ValueRef<'_> { - AnyValueRef { - kind: match &self.kind { - AnyValueKind::Null(k) => AnyValueKind::Null(*k), - AnyValueKind::Bool(b) => AnyValueKind::Bool(*b), - AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i), - AnyValueKind::Integer(i) => AnyValueKind::Integer(*i), - AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i), - AnyValueKind::Real(r) => AnyValueKind::Real(*r), - AnyValueKind::Double(d) => AnyValueKind::Double(*d), - AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Borrowed(t)), - AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Borrowed(b)), - }, - } + AnyValueRef { kind: &self.kind } } fn type_info(&self) -> Cow<'_, ::TypeInfo> { @@ -101,17 +91,7 @@ impl<'a> ValueRef<'a> for AnyValueRef<'a> { fn to_owned(&self) -> ::Value { AnyValue { - kind: match &self.kind { - AnyValueKind::Null(k) => AnyValueKind::Null(*k), - AnyValueKind::Bool(b) => AnyValueKind::Bool(*b), - AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i), - AnyValueKind::Integer(i) => AnyValueKind::Integer(*i), - AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i), - AnyValueKind::Real(r) => AnyValueKind::Real(*r), - AnyValueKind::Double(d) => AnyValueKind::Double(*d), - AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Owned(t.to_string())), - AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Owned(b.to_vec())), - }, + kind: self.kind.clone(), } } diff --git a/sqlx-core/src/arguments.rs b/sqlx-core/src/arguments.rs index 4bf826ed31..7b9da60b9c 100644 --- a/sqlx-core/src/arguments.rs +++ b/sqlx-core/src/arguments.rs @@ -9,7 +9,7 @@ use std::fmt::{self, Write}; /// A tuple of arguments to be sent to the database. // This lint is designed for general collections, but `Arguments` is not meant to be as such. #[allow(clippy::len_without_is_empty)] -pub trait Arguments<'q>: Send + Sized + Default { +pub trait Arguments: Send + Sized + Default { type Database: Database; /// Reserves the capacity for at least `additional` more values (of `size` total bytes) to @@ -17,9 +17,9 @@ pub trait Arguments<'q>: Send + Sized + Default { fn reserve(&mut self, additional: usize, size: usize); /// Add the value to the end of the arguments. - fn add(&mut self, value: T) -> Result<(), BoxDynError> + fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: 'q + Encode<'q, Self::Database> + Type; + T: Encode<'t, Self::Database> + Type; /// The number of arguments that were already added. fn len(&self) -> usize; @@ -29,19 +29,17 @@ pub trait Arguments<'q>: Send + Sized + Default { } } -pub trait IntoArguments<'q, DB: Database>: Sized + Send { - fn into_arguments(self) -> ::Arguments<'q>; +pub trait IntoArguments: Sized + Send { + fn into_arguments(self) -> ::Arguments; } // NOTE: required due to lack of lazy normalization #[macro_export] macro_rules! impl_into_arguments_for_arguments { ($Arguments:path) => { - impl<'q> - $crate::arguments::IntoArguments< - 'q, - <$Arguments as $crate::arguments::Arguments<'q>>::Database, - > for $Arguments + impl + $crate::arguments::IntoArguments<<$Arguments as $crate::arguments::Arguments>::Database> + for $Arguments { fn into_arguments(self) -> $Arguments { self @@ -51,13 +49,10 @@ macro_rules! impl_into_arguments_for_arguments { } /// used by the query macros to prevent supernumerary `.bind()` calls -pub struct ImmutableArguments<'q, DB: Database>(pub ::Arguments<'q>); +pub struct ImmutableArguments(pub ::Arguments); -impl<'q, DB: Database> IntoArguments<'q, DB> for ImmutableArguments<'q, DB> { - fn into_arguments(self) -> ::Arguments<'q> { +impl IntoArguments for ImmutableArguments { + fn into_arguments(self) -> ::Arguments { self.0 } } - -// TODO: Impl `IntoArguments` for &[&dyn Encode] -// TODO: Impl `IntoArguments` for (impl Encode, ...) x16 diff --git a/sqlx-core/src/database.rs b/sqlx-core/src/database.rs index 02d7a1214e..8fc3cc4d12 100644 --- a/sqlx-core/src/database.rs +++ b/sqlx-core/src/database.rs @@ -96,9 +96,9 @@ pub trait Database: 'static + Sized + Send + Debug { type ValueRef<'r>: ValueRef<'r, Database = Self>; /// The concrete `Arguments` implementation for this database. - type Arguments<'q>: Arguments<'q, Database = Self>; + type Arguments: Arguments; /// The concrete type used as a buffer for arguments while encoding. - type ArgumentBuffer<'q>; + type ArgumentBuffer; /// The concrete `Statement` implementation for this database. type Statement: Statement; diff --git a/sqlx-core/src/encode.rs b/sqlx-core/src/encode.rs index ba9a1d40c9..173c283e73 100644 --- a/sqlx-core/src/encode.rs +++ b/sqlx-core/src/encode.rs @@ -29,7 +29,7 @@ impl IsNull { /// Encode a single value to be sent to the database. pub trait Encode<'q, DB: Database> { /// Writes the value of `self` into `buf` in the expected format for the database. - fn encode(self, buf: &mut ::ArgumentBuffer<'q>) -> Result + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result where Self: Sized, { @@ -42,7 +42,7 @@ pub trait Encode<'q, DB: Database> { /// memory. fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result; fn produces(&self) -> Option { @@ -62,14 +62,14 @@ where T: Encode<'q, DB>, { #[inline] - fn encode(self, buf: &mut ::ArgumentBuffer<'q>) -> Result { + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result { >::encode_by_ref(self, buf) } #[inline] fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { <&T as Encode>::encode(self, buf) } @@ -104,7 +104,7 @@ macro_rules! impl_encode_for_option { #[inline] fn encode( self, - buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer<'q>, + buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer, ) -> Result<$crate::encode::IsNull, $crate::error::BoxDynError> { if let Some(v) = self { v.encode(buf) @@ -116,7 +116,7 @@ macro_rules! impl_encode_for_option { #[inline] fn encode_by_ref( &self, - buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer<'q>, + buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer, ) -> Result<$crate::encode::IsNull, $crate::error::BoxDynError> { if let Some(v) = self { v.encode_by_ref(buf) @@ -142,7 +142,7 @@ macro_rules! impl_encode_for_smartpointer { #[inline] fn encode( self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { >::encode_by_ref(self.as_ref(), buf) } @@ -150,7 +150,7 @@ macro_rules! impl_encode_for_smartpointer { #[inline] fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { <&T as Encode>::encode(self, buf) } @@ -178,14 +178,14 @@ where T: ToOwned, { #[inline] - fn encode(self, buf: &mut ::ArgumentBuffer<'q>) -> Result { + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result { <&T as Encode>::encode_by_ref(&self.as_ref(), buf) } #[inline] fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { <&T as Encode>::encode_by_ref(&self.as_ref(), buf) } @@ -207,7 +207,7 @@ macro_rules! forward_encode_impl { impl<'q> Encode<'q, $db> for $for_type { fn encode_by_ref( &self, - buf: &mut <$db as sqlx_core::database::Database>::ArgumentBuffer<'q>, + buf: &mut <$db as sqlx_core::database::Database>::ArgumentBuffer, ) -> Result { <$forward_to as Encode<$db>>::encode(self.as_ref(), buf) } diff --git a/sqlx-core/src/executor.rs b/sqlx-core/src/executor.rs index ab9737c9cd..e1c42fc706 100644 --- a/sqlx-core/src/executor.rs +++ b/sqlx-core/src/executor.rs @@ -204,13 +204,13 @@ pub trait Execute<'q, DB: Database>: Send + Sized { /// will be prepared (and cached) before execution. /// /// Returns `Err` if encoding any of the arguments failed. - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError>; + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError>; /// Returns `true` if the statement should be cached. fn persistent(&self) -> bool; } -impl<'q, DB: Database, T> Execute<'q, DB> for T +impl Execute<'_, DB> for T where T: SqlSafeStr + Send, { @@ -225,7 +225,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { Ok(None) } @@ -235,7 +235,7 @@ where } } -impl<'q, DB: Database, T> Execute<'q, DB> for (T, Option<::Arguments<'q>>) +impl Execute<'_, DB> for (T, Option<::Arguments>) where T: SqlSafeStr + Send, { @@ -250,7 +250,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { Ok(self.1.take()) } diff --git a/sqlx-core/src/query.rs b/sqlx-core/src/query.rs index 97c166116a..268e46a07b 100644 --- a/sqlx-core/src/query.rs +++ b/sqlx-core/src/query.rs @@ -42,7 +42,7 @@ pub struct Map<'q, DB: Database, F, A> { impl<'q, DB, A> Execute<'q, DB> for Query<'q, DB, A> where DB: Database, - A: Send + IntoArguments<'q, DB>, + A: Send + IntoArguments, { #[inline] fn sql(self) -> SqlStr { @@ -60,7 +60,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { self.arguments .take() .transpose() @@ -73,7 +73,7 @@ where } } -impl<'q, DB: Database> Query<'q, DB, ::Arguments<'q>> { +impl<'q, DB: Database> Query<'q, DB, ::Arguments> { /// Bind a value for use with this SQL query. /// /// If the number of times this is called does not match the number of bind parameters that @@ -110,7 +110,7 @@ impl<'q, DB: Database> Query<'q, DB, ::Arguments<'q>> { arguments.add(value) } - fn get_arguments(&mut self) -> Result<&mut DB::Arguments<'q>, BoxDynError> { + fn get_arguments(&mut self) -> Result<&mut DB::Arguments, BoxDynError> { let Some(Ok(arguments)) = self.arguments.as_mut().map(Result::as_mut) else { return Err("A previous call to Query::bind produced an error" .to_owned() @@ -144,7 +144,7 @@ where impl<'q, DB, A: Send> Query<'q, DB, A> where DB: Database, - A: 'q + IntoArguments<'q, DB>, + A: 'q + IntoArguments, { /// Map each row in the result to another type. /// @@ -301,7 +301,7 @@ where impl<'q, DB, F: Send, A: Send> Execute<'q, DB> for Map<'q, DB, F, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, { #[inline] fn sql(self) -> SqlStr { @@ -314,7 +314,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { self.inner.take_arguments() } @@ -329,7 +329,7 @@ where DB: Database, F: FnMut(DB::Row) -> Result + Send, O: Send + Unpin, - A: 'q + Send + IntoArguments<'q, DB>, + A: 'q + Send + IntoArguments, { /// Map each row in the result to another type. /// @@ -500,9 +500,7 @@ where } /// Execute a single SQL query as a prepared statement (explicitly created). -pub fn query_statement( - statement: &DB::Statement, -) -> Query<'_, DB, ::Arguments<'_>> +pub fn query_statement(statement: &DB::Statement) -> Query<'_, DB, ::Arguments> where DB: Database, { @@ -515,13 +513,10 @@ where } /// Execute a single SQL query as a prepared statement (explicitly created), with the given arguments. -pub fn query_statement_with<'q, DB, A>( - statement: &'q DB::Statement, - arguments: A, -) -> Query<'q, DB, A> +pub fn query_statement_with(statement: &DB::Statement, arguments: A) -> Query<'_, DB, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, { Query { database: PhantomData, @@ -655,7 +650,7 @@ where /// /// As an additional benefit, query parameters are usually sent in a compact binary encoding instead of a human-readable /// text encoding, which saves bandwidth. -pub fn query<'a, DB>(sql: impl SqlSafeStr) -> Query<'a, DB, ::Arguments<'a>> +pub fn query<'a, DB>(sql: impl SqlSafeStr) -> Query<'a, DB, ::Arguments> where DB: Database, { @@ -673,7 +668,7 @@ where pub fn query_with<'q, DB, A>(sql: impl SqlSafeStr, arguments: A) -> Query<'q, DB, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, { query_with_result(sql, Ok(arguments)) } @@ -685,7 +680,7 @@ pub fn query_with_result<'q, DB, A>( ) -> Query<'q, DB, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, { Query { database: PhantomData, diff --git a/sqlx-core/src/query_as.rs b/sqlx-core/src/query_as.rs index e58a3f0f8b..00194cba57 100644 --- a/sqlx-core/src/query_as.rs +++ b/sqlx-core/src/query_as.rs @@ -25,7 +25,7 @@ pub struct QueryAs<'q, DB: Database, O, A> { impl<'q, DB, O: Send, A: Send> Execute<'q, DB> for QueryAs<'q, DB, O, A> where DB: Database, - A: 'q + IntoArguments<'q, DB>, + A: 'q + IntoArguments, { #[inline] fn sql(self) -> SqlStr { @@ -38,7 +38,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { self.inner.take_arguments() } @@ -48,7 +48,7 @@ where } } -impl<'q, DB: Database, O> QueryAs<'q, DB, O, ::Arguments<'q>> { +impl<'q, DB: Database, O> QueryAs<'q, DB, O, ::Arguments> { /// Bind a value for use with this SQL query. /// /// See [`Query::bind`](Query::bind). @@ -83,7 +83,7 @@ where impl<'q, DB, O, A> QueryAs<'q, DB, O, A> where DB: Database, - A: 'q + IntoArguments<'q, DB>, + A: 'q + IntoArguments, O: Send + Unpin + for<'r> FromRow<'r, DB::Row>, { /// Execute the query and return the generated results as a stream. @@ -338,9 +338,7 @@ where /// /// ``` #[inline] -pub fn query_as<'q, DB, O>( - sql: impl SqlSafeStr, -) -> QueryAs<'q, DB, O, ::Arguments<'q>> +pub fn query_as<'q, DB, O>(sql: impl SqlSafeStr) -> QueryAs<'q, DB, O, ::Arguments> where DB: Database, O: for<'r> FromRow<'r, DB::Row>, @@ -361,7 +359,7 @@ where pub fn query_as_with<'q, DB, O, A>(sql: impl SqlSafeStr, arguments: A) -> QueryAs<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, O: for<'r> FromRow<'r, DB::Row>, { query_as_with_result(sql, Ok(arguments)) @@ -375,7 +373,7 @@ pub fn query_as_with_result<'q, DB, O, A>( ) -> QueryAs<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, O: for<'r> FromRow<'r, DB::Row>, { QueryAs { @@ -387,7 +385,7 @@ where // Make a SQL query from a statement, that is mapped to a concrete type. pub fn query_statement_as( statement: &DB::Statement, -) -> QueryAs<'_, DB, O, ::Arguments<'_>> +) -> QueryAs<'_, DB, O, ::Arguments> where DB: Database, O: for<'r> FromRow<'r, DB::Row>, @@ -405,7 +403,7 @@ pub fn query_statement_as_with<'q, DB, O, A>( ) -> QueryAs<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, O: for<'r> FromRow<'r, DB::Row>, { QueryAs { diff --git a/sqlx-core/src/query_builder.rs b/sqlx-core/src/query_builder.rs index 7dff67831c..25aa500aea 100644 --- a/sqlx-core/src/query_builder.rs +++ b/sqlx-core/src/query_builder.rs @@ -25,16 +25,16 @@ use crate::Either; /// and `UNNEST()`. [See our FAQ] for details. /// /// [See our FAQ]: https://github.com/launchbadge/sqlx/blob/master/FAQ.md#how-can-i-bind-an-array-to-a-values-clause-how-can-i-do-bulk-inserts -pub struct QueryBuilder<'args, DB> +pub struct QueryBuilder where DB: Database, { query: Arc, init_len: usize, - arguments: Option<::Arguments<'args>>, + arguments: Option<::Arguments>, } -impl Default for QueryBuilder<'_, DB> { +impl Default for QueryBuilder { fn default() -> Self { QueryBuilder { init_len: 0, @@ -46,7 +46,7 @@ impl Default for QueryBuilder<'_, DB> { const ERROR: &str = "BUG: query must not be shared at this point in time"; -impl<'args, DB: Database> QueryBuilder<'args, DB> +impl QueryBuilder where DB: Database, { @@ -55,7 +55,7 @@ where /// Start building a query with an initial SQL fragment, which may be an empty string. pub fn new(init: impl Into) -> Self where - ::Arguments<'args>: Default, + ::Arguments: Default, { let init = init.into(); @@ -73,7 +73,7 @@ where pub fn with_arguments(init: impl Into, arguments: A) -> Self where DB: Database, - A: IntoArguments<'args, DB>, + A: IntoArguments, { let init = init.into(); @@ -152,9 +152,9 @@ where /// /// [`SQLITE_LIMIT_VARIABLE_NUMBER`]: https://www.sqlite.org/limits.html#max_variable_number /// [postgres-limit-issue]: https://github.com/launchbadge/sqlx/issues/671#issuecomment-687043510 - pub fn push_bind(&mut self, value: T) -> &mut Self + pub fn push_bind<'t, T>(&mut self, value: T) -> &mut Self where - T: 'args + Encode<'args, DB> + Type, + T: Encode<'t, DB> + Type, { self.sanity_check(); @@ -199,9 +199,8 @@ where /// assert!(sql.ends_with("in (?, ?) ")); /// # } /// ``` - pub fn separated<'qb, Sep>(&'qb mut self, separator: Sep) -> Separated<'qb, 'args, DB, Sep> + pub fn separated(&mut self, separator: Sep) -> Separated<'_, DB, Sep> where - 'args: 'qb, Sep: Display, { self.sanity_check(); @@ -313,7 +312,7 @@ where pub fn push_values(&mut self, tuples: I, mut push_tuple: F) -> &mut Self where I: IntoIterator, - F: FnMut(Separated<'_, 'args, DB, &'static str>, I::Item), + F: FnMut(Separated<'_, DB, &'static str>, I::Item), { self.sanity_check(); @@ -425,7 +424,7 @@ where pub fn push_tuples(&mut self, tuples: I, mut push_tuple: F) -> &mut Self where I: IntoIterator, - F: FnMut(Separated<'_, 'args, DB, &'static str>, I::Item), + F: FnMut(Separated<'_, DB, &'static str>, I::Item), { self.sanity_check(); @@ -457,7 +456,7 @@ where /// 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(&mut self) -> Query<'_, DB, ::Arguments<'args>> { + pub fn build(&mut self) -> Query<'_, DB, ::Arguments> { self.sanity_check(); Query { @@ -482,7 +481,7 @@ where /// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons. pub fn build_query_as<'q, T: FromRow<'q, DB::Row>>( &'q mut self, - ) -> QueryAs<'q, DB, T, ::Arguments<'args>> { + ) -> QueryAs<'q, DB, T, ::Arguments> { QueryAs { inner: self.build(), output: PhantomData, @@ -503,7 +502,7 @@ where /// 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<'args>> + ) -> QueryScalar<'q, DB, T, ::Arguments> where DB: Database, (T,): for<'r> FromRow<'r, DB::Row>, @@ -547,16 +546,16 @@ where /// /// See [`QueryBuilder::separated()`] for details. #[allow(explicit_outlives_requirements)] -pub struct Separated<'qb, 'args: 'qb, DB, Sep> +pub struct Separated<'qb, DB, Sep> where DB: Database, { - query_builder: &'qb mut QueryBuilder<'args, DB>, + query_builder: &'qb mut QueryBuilder, separator: Sep, push_separator: bool, } -impl<'qb, 'args: 'qb, DB, Sep> Separated<'qb, 'args, DB, Sep> +impl Separated<'_, DB, Sep> where DB: Database, Sep: Display, @@ -587,9 +586,9 @@ where /// Push the separator if applicable, then append a bind argument. /// /// See [`QueryBuilder::push_bind()`] for details. - pub fn push_bind(&mut self, value: T) -> &mut Self + pub fn push_bind<'t, T>(&mut self, value: T) -> &mut Self where - T: 'args + Encode<'args, DB> + Type, + T: Encode<'t, DB> + Type, { if self.push_separator { self.query_builder.push(&self.separator); @@ -605,9 +604,9 @@ where /// without a separator. /// /// Simply calls [`QueryBuilder::push_bind()`] directly. - pub fn push_bind_unseparated(&mut self, value: T) -> &mut Self + pub fn push_bind_unseparated<'t, T>(&mut self, value: T) -> &mut Self where - T: 'args + Encode<'args, DB> + Type, + T: Encode<'t, DB> + Type, { self.query_builder.push_bind(value); self diff --git a/sqlx-core/src/query_scalar.rs b/sqlx-core/src/query_scalar.rs index 1059463874..f760158c4a 100644 --- a/sqlx-core/src/query_scalar.rs +++ b/sqlx-core/src/query_scalar.rs @@ -23,7 +23,7 @@ pub struct QueryScalar<'q, DB: Database, O, A> { impl<'q, DB: Database, O: Send, A: Send> Execute<'q, DB> for QueryScalar<'q, DB, O, A> where - A: 'q + IntoArguments<'q, DB>, + A: 'q + IntoArguments, { #[inline] fn sql(self) -> SqlStr { @@ -35,7 +35,7 @@ where } #[inline] - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { self.inner.take_arguments() } @@ -45,7 +45,7 @@ where } } -impl<'q, DB: Database, O> QueryScalar<'q, DB, O, ::Arguments<'q>> { +impl<'q, DB: Database, O> QueryScalar<'q, DB, O, ::Arguments> { /// Bind a value for use with this SQL query. /// /// See [`Query::bind`](crate::query::Query::bind). @@ -81,7 +81,7 @@ impl<'q, DB, O, A> QueryScalar<'q, DB, O, A> where DB: Database, O: Send + Unpin, - A: 'q + IntoArguments<'q, DB>, + A: 'q + IntoArguments, (O,): Send + Unpin + for<'r> FromRow<'r, DB::Row>, { /// Execute the query and return the generated results as a stream. @@ -321,7 +321,7 @@ where #[inline] pub fn query_scalar<'q, DB, O>( sql: impl SqlSafeStr, -) -> QueryScalar<'q, DB, O, ::Arguments<'q>> +) -> QueryScalar<'q, DB, O, ::Arguments> where DB: Database, (O,): for<'r> FromRow<'r, DB::Row>, @@ -344,7 +344,7 @@ pub fn query_scalar_with<'q, DB, O, A>( ) -> QueryScalar<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, (O,): for<'r> FromRow<'r, DB::Row>, { query_scalar_with_result(sql, Ok(arguments)) @@ -358,7 +358,7 @@ pub fn query_scalar_with_result<'q, DB, O, A>( ) -> QueryScalar<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, (O,): for<'r> FromRow<'r, DB::Row>, { QueryScalar { @@ -369,7 +369,7 @@ where // Make a SQL query from a statement, that is mapped to a concrete value. pub fn query_statement_scalar( statement: &DB::Statement, -) -> QueryScalar<'_, DB, O, ::Arguments<'_>> +) -> QueryScalar<'_, DB, O, ::Arguments> where DB: Database, (O,): for<'r> FromRow<'r, DB::Row>, @@ -386,7 +386,7 @@ pub fn query_statement_scalar_with<'q, DB, O, A>( ) -> QueryScalar<'q, DB, O, A> where DB: Database, - A: IntoArguments<'q, DB>, + A: IntoArguments, (O,): for<'r> FromRow<'r, DB::Row>, { QueryScalar { diff --git a/sqlx-core/src/raw_sql.rs b/sqlx-core/src/raw_sql.rs index d2668ebe40..0292885422 100644 --- a/sqlx-core/src/raw_sql.rs +++ b/sqlx-core/src/raw_sql.rs @@ -120,7 +120,7 @@ pub fn raw_sql(sql: impl SqlSafeStr) -> RawSql { RawSql(sql.into_sql_str()) } -impl<'q, DB: Database> Execute<'q, DB> for RawSql { +impl Execute<'_, DB> for RawSql { fn sql(self) -> SqlStr { self.0 } @@ -129,7 +129,7 @@ impl<'q, DB: Database> Execute<'q, DB> for RawSql { None } - fn take_arguments(&mut self) -> Result::Arguments<'q>>, BoxDynError> { + fn take_arguments(&mut self) -> Result::Arguments>, BoxDynError> { Ok(None) } diff --git a/sqlx-core/src/statement.rs b/sqlx-core/src/statement.rs index 76d0325639..92ae8a629e 100644 --- a/sqlx-core/src/statement.rs +++ b/sqlx-core/src/statement.rs @@ -59,33 +59,33 @@ pub trait Statement: Send + Sync + Clone { Ok(&self.columns()[index.index(self)?]) } - fn query(&self) -> Query<'_, Self::Database, ::Arguments<'_>>; + fn query(&self) -> Query<'_, Self::Database, ::Arguments>; - fn query_with<'s, A>(&'s self, arguments: A) -> Query<'s, Self::Database, A> + fn query_with(&self, arguments: A) -> Query<'_, Self::Database, A> where - A: IntoArguments<'s, Self::Database>; + A: IntoArguments; fn query_as( &self, - ) -> QueryAs<'_, Self::Database, O, ::Arguments<'_>> + ) -> QueryAs<'_, Self::Database, O, ::Arguments> where O: for<'r> FromRow<'r, ::Row>; fn query_as_with<'s, O, A>(&'s self, arguments: A) -> QueryAs<'s, Self::Database, O, A> where O: for<'r> FromRow<'r, ::Row>, - A: IntoArguments<'s, Self::Database>; + A: IntoArguments; fn query_scalar( &self, - ) -> QueryScalar<'_, Self::Database, O, ::Arguments<'_>> + ) -> QueryScalar<'_, Self::Database, O, ::Arguments> where (O,): for<'r> FromRow<'r, ::Row>; fn query_scalar_with<'s, O, A>(&'s self, arguments: A) -> QueryScalar<'s, Self::Database, O, A> where (O,): for<'r> FromRow<'r, ::Row>, - A: IntoArguments<'s, Self::Database>; + A: IntoArguments; } #[macro_export] @@ -97,9 +97,9 @@ macro_rules! impl_statement_query { } #[inline] - fn query_with<'s, A>(&'s self, arguments: A) -> $crate::query::Query<'s, Self::Database, A> + fn query_with(&self, arguments: A) -> $crate::query::Query<'_, Self::Database, A> where - A: $crate::arguments::IntoArguments<'s, Self::Database>, + A: $crate::arguments::IntoArguments, { $crate::query::query_statement_with(self, arguments) } @@ -111,7 +111,7 @@ macro_rules! impl_statement_query { '_, Self::Database, O, - ::Arguments<'_>, + ::Arguments, > where O: for<'r> $crate::from_row::FromRow< @@ -132,7 +132,7 @@ macro_rules! impl_statement_query { 'r, ::Row, >, - A: $crate::arguments::IntoArguments<'s, Self::Database>, + A: $crate::arguments::IntoArguments, { $crate::query_as::query_statement_as_with(self, arguments) } @@ -144,7 +144,7 @@ macro_rules! impl_statement_query { '_, Self::Database, O, - ::Arguments<'_>, + ::Arguments, > where (O,): for<'r> $crate::from_row::FromRow< @@ -165,7 +165,7 @@ macro_rules! impl_statement_query { 'r, ::Row, >, - A: $crate::arguments::IntoArguments<'s, Self::Database>, + A: $crate::arguments::IntoArguments, { $crate::query_scalar::query_statement_scalar_with(self, arguments) } diff --git a/sqlx-core/src/testing/fixtures.rs b/sqlx-core/src/testing/fixtures.rs index 32fdfe2219..17f67e0fda 100644 --- a/sqlx-core/src/testing/fixtures.rs +++ b/sqlx-core/src/testing/fixtures.rs @@ -112,7 +112,7 @@ impl FixtureSnapshot { #[allow(clippy::to_string_trait_impl)] impl ToString for Fixture where - for<'a> ::Arguments<'a>: Default, + for<'a> ::Arguments: Default, { fn to_string(&self) -> String { let mut query = QueryBuilder::::new(""); diff --git a/sqlx-core/src/types/bstr.rs b/sqlx-core/src/types/bstr.rs index 4b6daadfde..5afae9a865 100644 --- a/sqlx-core/src/types/bstr.rs +++ b/sqlx-core/src/types/bstr.rs @@ -39,7 +39,7 @@ where { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { <&[u8] as Encode>::encode(self.as_bytes(), buf) } @@ -52,7 +52,7 @@ where { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { as Encode>::encode(self.as_bytes().to_vec(), buf) } diff --git a/sqlx-core/src/types/json.rs b/sqlx-core/src/types/json.rs index fa187f4aab..67a8d9e0b9 100644 --- a/sqlx-core/src/types/json.rs +++ b/sqlx-core/src/types/json.rs @@ -166,7 +166,7 @@ where { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { as Encode<'q, DB>>::encode(Json(self), buf) } diff --git a/sqlx-core/src/types/non_zero.rs b/sqlx-core/src/types/non_zero.rs index 3d92810ef9..4f04768468 100644 --- a/sqlx-core/src/types/non_zero.rs +++ b/sqlx-core/src/types/non_zero.rs @@ -33,11 +33,11 @@ macro_rules! impl_non_zero { DB: Database, $int: Encode<'q, DB>, { - fn encode_by_ref(&self, buf: &mut ::ArgumentBuffer<'q>) -> Result { + fn encode_by_ref(&self, buf: &mut ::ArgumentBuffer) -> Result { <$int as Encode<'q, DB>>::encode_by_ref(&self.get(), buf) } - fn encode(self, buf: &mut ::ArgumentBuffer<'q>) -> Result + fn encode(self, buf: &mut ::ArgumentBuffer) -> Result where Self: Sized, { diff --git a/sqlx-core/src/types/text.rs b/sqlx-core/src/types/text.rs index f5e323eea8..941e80ce10 100644 --- a/sqlx-core/src/types/text.rs +++ b/sqlx-core/src/types/text.rs @@ -115,7 +115,7 @@ where String: Encode<'q, DB>, DB: Database, { - fn encode_by_ref(&self, buf: &mut ::ArgumentBuffer<'q>) -> Result { + fn encode_by_ref(&self, buf: &mut ::ArgumentBuffer) -> Result { self.0.to_string().encode(buf) } } diff --git a/sqlx-macros-core/src/derives/encode.rs b/sqlx-macros-core/src/derives/encode.rs index 9f26aba0a9..0bbbd0ee87 100644 --- a/sqlx-macros-core/src/derives/encode.rs +++ b/sqlx-macros-core/src/derives/encode.rs @@ -84,7 +84,7 @@ fn expand_derive_encode_transparent( { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<#lifetime>, + buf: &mut ::ArgumentBuffer, ) -> ::std::result::Result<::sqlx::encode::IsNull, ::sqlx::error::BoxDynError> { <#ty as ::sqlx::encode::Encode<#lifetime, DB>>::encode_by_ref(&self.0, buf) } @@ -123,7 +123,7 @@ fn expand_derive_encode_weak_enum( { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> ::std::result::Result<::sqlx::encode::IsNull, ::sqlx::error::BoxDynError> { let value = match self { #(#values)* @@ -173,7 +173,7 @@ fn expand_derive_encode_strong_enum( { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> ::std::result::Result<::sqlx::encode::IsNull, ::sqlx::error::BoxDynError> { let val = match self { #(#value_arms)* diff --git a/sqlx-macros-core/src/query/args.rs b/sqlx-macros-core/src/query/args.rs index 1b338efa3e..701957b42e 100644 --- a/sqlx-macros-core/src/query/args.rs +++ b/sqlx-macros-core/src/query/args.rs @@ -22,7 +22,7 @@ pub fn quote_args( if input.arg_exprs.is_empty() { return Ok(quote! { - let query_args = ::core::result::Result::<_, ::sqlx::error::BoxDynError>::Ok(<#db_path as ::sqlx::database::Database>::Arguments::<'_>::default()); + let query_args = ::core::result::Result::<_, ::sqlx::error::BoxDynError>::Ok(<#db_path as ::sqlx::database::Database>::Arguments::default()); }); } @@ -95,7 +95,7 @@ pub fn quote_args( #args_check - let mut query_args = <#db_path as ::sqlx::database::Database>::Arguments::<'_>::default(); + let mut query_args = <#db_path as ::sqlx::database::Database>::Arguments::default(); query_args.reserve( #args_count, 0 #(+ ::sqlx::encode::Encode::<#db_path>::size_hint(#arg_name))* diff --git a/sqlx-mysql/src/any.rs b/sqlx-mysql/src/any.rs index 028b6c55b8..548bb49a64 100644 --- a/sqlx-mysql/src/any.rs +++ b/sqlx-mysql/src/any.rs @@ -6,7 +6,7 @@ use crate::{ use either::Either; use futures_core::future::BoxFuture; use futures_core::stream::BoxStream; -use futures_util::{stream, FutureExt, StreamExt, TryFutureExt, TryStreamExt}; +use futures_util::{stream, FutureExt, StreamExt, TryFutureExt}; use sqlx_core::any::{ Any, AnyArguments, AnyColumn, AnyConnectOptions, AnyConnectionBackend, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo, AnyTypeInfoKind, @@ -17,7 +17,7 @@ use sqlx_core::describe::Describe; use sqlx_core::executor::Executor; use sqlx_core::sql_str::SqlStr; use sqlx_core::transaction::TransactionManager; -use std::{future, pin::pin}; +use std::future; sqlx_core::declare_driver_with_optional_migrate!(DRIVER = MySql); @@ -77,14 +77,14 @@ impl AnyConnectionBackend for MySqlConnection { Ok(self) } - fn fetch_many<'q>( - &'q mut self, + fn fetch_many( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxStream<'q, sqlx_core::Result>> { + arguments: Option, + ) -> BoxStream<'_, sqlx_core::Result>> { let persistent = persistent && arguments.is_some(); - let arguments = match arguments.as_ref().map(AnyArguments::convert_to).transpose() { + let arguments = match arguments.map(AnyArguments::convert_into).transpose() { Ok(arguments) => arguments, Err(error) => { return stream::once(future::ready(Err(sqlx_core::Error::Encode(error)))).boxed() @@ -103,33 +103,6 @@ impl AnyConnectionBackend for MySqlConnection { ) } - fn fetch_optional<'q>( - &'q mut self, - query: SqlStr, - persistent: bool, - arguments: Option>, - ) -> BoxFuture<'q, sqlx_core::Result>> { - let persistent = persistent && arguments.is_some(); - let arguments = arguments - .as_ref() - .map(AnyArguments::convert_to) - .transpose() - .map_err(sqlx_core::Error::Encode); - - Box::pin(async move { - let arguments = arguments?; - let mut stream = pin!(self.run(query, arguments, persistent).await?); - - while let Some(result) = stream.try_next().await? { - if let Either::Right(row) = result { - return Ok(Some(AnyRow::try_from(&row)?)); - } - } - - Ok(None) - }) - } - fn prepare_with<'c, 'q: 'c>( &'c mut self, sql: SqlStr, diff --git a/sqlx-mysql/src/arguments.rs b/sqlx-mysql/src/arguments.rs index 464529cba2..306431039d 100644 --- a/sqlx-mysql/src/arguments.rs +++ b/sqlx-mysql/src/arguments.rs @@ -37,7 +37,7 @@ impl MySqlArguments { } } -impl<'q> Arguments<'q> for MySqlArguments { +impl Arguments for MySqlArguments { type Database = MySql; fn reserve(&mut self, len: usize, size: usize) { @@ -45,9 +45,9 @@ impl<'q> Arguments<'q> for MySqlArguments { self.values.reserve(size); } - fn add(&mut self, value: T) -> Result<(), BoxDynError> + fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: Encode<'q, Self::Database> + Type, + T: Encode<'t, Self::Database> + Type, { self.add(value) } diff --git a/sqlx-mysql/src/database.rs b/sqlx-mysql/src/database.rs index 0e3f51f532..849ddbde61 100644 --- a/sqlx-mysql/src/database.rs +++ b/sqlx-mysql/src/database.rs @@ -25,8 +25,8 @@ impl Database for MySql { type Value = MySqlValue; type ValueRef<'r> = MySqlValueRef<'r>; - type Arguments<'q> = MySqlArguments; - type ArgumentBuffer<'q> = Vec; + type Arguments = MySqlArguments; + type ArgumentBuffer = Vec; type Statement = MySqlStatement; diff --git a/sqlx-mysql/src/types/mysql_time.rs b/sqlx-mysql/src/types/mysql_time.rs index b549af5765..6af10aa216 100644 --- a/sqlx-mysql/src/types/mysql_time.rs +++ b/sqlx-mysql/src/types/mysql_time.rs @@ -409,10 +409,10 @@ impl<'r> Decode<'r, MySql> for MySqlTime { } } -impl<'q> Encode<'q, MySql> for MySqlTime { +impl Encode<'_, MySql> for MySqlTime { fn encode_by_ref( &self, - buf: &mut ::ArgumentBuffer<'q>, + buf: &mut ::ArgumentBuffer, ) -> Result { if self.is_zero() { buf.put_u8(0); diff --git a/sqlx-postgres/src/any.rs b/sqlx-postgres/src/any.rs index 75ee0d73df..d0542ca47d 100644 --- a/sqlx-postgres/src/any.rs +++ b/sqlx-postgres/src/any.rs @@ -4,9 +4,9 @@ use crate::{ }; use futures_core::future::BoxFuture; use futures_core::stream::BoxStream; -use futures_util::{stream, FutureExt, StreamExt, TryFutureExt, TryStreamExt}; +use futures_util::{stream, FutureExt, StreamExt, TryFutureExt}; use sqlx_core::sql_str::SqlStr; -use std::{future, pin::pin}; +use std::future; use sqlx_core::any::{ Any, AnyArguments, AnyColumn, AnyConnectOptions, AnyConnectionBackend, AnyQueryResult, AnyRow, @@ -79,14 +79,14 @@ impl AnyConnectionBackend for PgConnection { Ok(self) } - fn fetch_many<'q>( - &'q mut self, + fn fetch_many( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxStream<'q, sqlx_core::Result>> { + arguments: Option, + ) -> BoxStream>> { let persistent = persistent && arguments.is_some(); - let arguments = match arguments.as_ref().map(AnyArguments::convert_to).transpose() { + let arguments = match arguments.map(AnyArguments::convert_into).transpose() { Ok(arguments) => arguments, Err(error) => { return stream::once(future::ready(Err(sqlx_core::Error::Encode(error)))).boxed() @@ -105,31 +105,6 @@ impl AnyConnectionBackend for PgConnection { ) } - fn fetch_optional<'q>( - &'q mut self, - query: SqlStr, - persistent: bool, - arguments: Option>, - ) -> BoxFuture<'q, sqlx_core::Result>> { - let persistent = persistent && arguments.is_some(); - let arguments = arguments - .as_ref() - .map(AnyArguments::convert_to) - .transpose() - .map_err(sqlx_core::Error::Encode); - - Box::pin(async move { - let arguments = arguments?; - let mut stream = pin!(self.run(query, arguments, persistent, None).await?); - - if let Some(Either::Right(row)) = stream.try_next().await? { - return Ok(Some(AnyRow::try_from(&row)?)); - } - - Ok(None) - }) - } - fn prepare_with<'c, 'q: 'c>( &'c mut self, sql: SqlStr, diff --git a/sqlx-postgres/src/arguments.rs b/sqlx-postgres/src/arguments.rs index 62a227e52d..c0db982c7d 100644 --- a/sqlx-postgres/src/arguments.rs +++ b/sqlx-postgres/src/arguments.rs @@ -138,7 +138,7 @@ impl PgArguments { } } -impl<'q> Arguments<'q> for PgArguments { +impl Arguments for PgArguments { type Database = Postgres; fn reserve(&mut self, additional: usize, size: usize) { @@ -146,9 +146,9 @@ impl<'q> Arguments<'q> for PgArguments { self.buffer.reserve(size); } - fn add(&mut self, value: T) -> Result<(), BoxDynError> + fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: Encode<'q, Self::Database> + Type, + T: Encode<'t, Self::Database> + Type, { self.add(value) } diff --git a/sqlx-postgres/src/database.rs b/sqlx-postgres/src/database.rs index fbc762615b..77945b8cfe 100644 --- a/sqlx-postgres/src/database.rs +++ b/sqlx-postgres/src/database.rs @@ -27,8 +27,8 @@ impl Database for Postgres { type Value = PgValue; type ValueRef<'r> = PgValueRef<'r>; - type Arguments<'q> = PgArguments; - type ArgumentBuffer<'q> = PgArgumentBuffer; + type Arguments = PgArguments; + type ArgumentBuffer = PgArgumentBuffer; type Statement = PgStatement; diff --git a/sqlx-sqlite/src/any.rs b/sqlx-sqlite/src/any.rs index d33677c64e..1f7063398a 100644 --- a/sqlx-sqlite/src/any.rs +++ b/sqlx-sqlite/src/any.rs @@ -12,13 +12,14 @@ use sqlx_core::any::{ }; use sqlx_core::sql_str::SqlStr; +use crate::arguments::SqliteArgumentsBuffer; use crate::type_info::DataType; use sqlx_core::connection::{ConnectOptions, Connection}; use sqlx_core::database::Database; use sqlx_core::describe::Describe; use sqlx_core::executor::Executor; use sqlx_core::transaction::TransactionManager; -use std::pin::pin; +use std::sync::Arc; sqlx_core::declare_driver_with_optional_migrate!(DRIVER = Sqlite); @@ -78,48 +79,28 @@ impl AnyConnectionBackend for SqliteConnection { Ok(self) } - fn fetch_many<'q>( - &'q mut self, + fn fetch_many( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxStream<'q, sqlx_core::Result>> { - let persistent = persistent && arguments.is_some(); - let args = arguments.map(map_arguments); - - Box::pin( - self.worker - .execute(query, args, self.row_channel_size, persistent, None) - .map_ok(flume::Receiver::into_stream) - .try_flatten_stream() - .map( - move |res: sqlx_core::Result>| match res? { - Either::Left(result) => Ok(Either::Left(map_result(result))), - Either::Right(row) => Ok(Either::Right(AnyRow::try_from(&row)?)), - }, - ), - ) + arguments: Option, + ) -> BoxStream<'_, sqlx_core::Result>> { + self.fetch_with_limit(query, persistent, arguments, None) } - fn fetch_optional<'q>( - &'q mut self, + fn fetch_optional( + &mut self, query: SqlStr, persistent: bool, - arguments: Option>, - ) -> BoxFuture<'q, sqlx_core::Result>> { - let persistent = persistent && arguments.is_some(); - let args = arguments.map(map_arguments); + arguments: Option, + ) -> BoxFuture<'_, sqlx_core::Result>> { + let mut stream = self.fetch_with_limit(query, persistent, arguments, Some(1)); Box::pin(async move { - let mut stream = pin!( - self.worker - .execute(query, args, self.row_channel_size, persistent, Some(1)) - .map_ok(flume::Receiver::into_stream) - .await? - ); - - if let Some(Either::Right(row)) = stream.try_next().await? { - return Ok(Some(AnyRow::try_from(&row)?)); + while let Some(result) = stream.try_next().await? { + if let Either::Right(row) = result { + return Ok(Some(row)); + } } Ok(None) @@ -143,6 +124,32 @@ impl AnyConnectionBackend for SqliteConnection { } } +impl SqliteConnection { + fn fetch_with_limit( + &mut self, + query: SqlStr, + persistent: bool, + arguments: Option, + limit: Option, + ) -> BoxStream<'_, sqlx_core::Result>> { + let persistent = persistent && arguments.is_some(); + let args = arguments.map(map_arguments); + + Box::pin( + self.worker + .execute(query, args, self.row_channel_size, persistent, limit) + .map_ok(flume::Receiver::into_stream) + .try_flatten_stream() + .map( + move |res: sqlx_core::Result>| match res? { + Either::Left(result) => Ok(Either::Left(map_result(result))), + Either::Right(row) => Ok(Either::Right(AnyRow::try_from(&row)?)), + }, + ), + ) + } +} + impl<'a> TryFrom<&'a SqliteTypeInfo> for AnyTypeInfo { type Error = sqlx_core::Error; @@ -203,27 +210,29 @@ impl<'a> TryFrom<&'a AnyConnectOptions> for SqliteConnectOptions { } } -/// Instead of `AnyArguments::convert_into()`, we can do a direct mapping and preserve the lifetime. -fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments<'_> { +// Infallible alternative to AnyArguments::convert_into() +fn map_arguments(args: AnyArguments) -> SqliteArguments { + let values = args + .values + .0 + .into_iter() + .map(|val| match val { + AnyValueKind::Null(_) => SqliteArgumentValue::Null, + AnyValueKind::Bool(b) => SqliteArgumentValue::Int(b as i32), + AnyValueKind::SmallInt(i) => SqliteArgumentValue::Int(i as i32), + AnyValueKind::Integer(i) => SqliteArgumentValue::Int(i), + AnyValueKind::BigInt(i) => SqliteArgumentValue::Int64(i), + AnyValueKind::Real(r) => SqliteArgumentValue::Double(r as f64), + AnyValueKind::Double(d) => SqliteArgumentValue::Double(d), + AnyValueKind::Text(t) => SqliteArgumentValue::Text(Arc::new(t.to_string())), + AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(Arc::new(b.to_vec())), + // AnyValueKind is `#[non_exhaustive]` but we should have covered everything + _ => unreachable!("BUG: missing mapping for {val:?}"), + }) + .collect(); + SqliteArguments { - values: args - .values - .0 - .into_iter() - .map(|val| match val { - AnyValueKind::Null(_) => SqliteArgumentValue::Null, - AnyValueKind::Bool(b) => SqliteArgumentValue::Int(b as i32), - AnyValueKind::SmallInt(i) => SqliteArgumentValue::Int(i as i32), - AnyValueKind::Integer(i) => SqliteArgumentValue::Int(i), - AnyValueKind::BigInt(i) => SqliteArgumentValue::Int64(i), - AnyValueKind::Real(r) => SqliteArgumentValue::Double(r as f64), - AnyValueKind::Double(d) => SqliteArgumentValue::Double(d), - AnyValueKind::Text(t) => SqliteArgumentValue::Text(t), - AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(b), - // AnyValueKind is `#[non_exhaustive]` but we should have covered everything - _ => unreachable!("BUG: missing mapping for {val:?}"), - }) - .collect(), + values: SqliteArgumentsBuffer::new(values), } } diff --git a/sqlx-sqlite/src/arguments.rs b/sqlx-sqlite/src/arguments.rs index 410b4caa99..4fd7da5067 100644 --- a/sqlx-sqlite/src/arguments.rs +++ b/sqlx-sqlite/src/arguments.rs @@ -4,77 +4,71 @@ use crate::statement::StatementHandle; use crate::Sqlite; use atoi::atoi; use libsqlite3_sys::SQLITE_OK; -use std::borrow::Cow; +use std::sync::Arc; pub(crate) use sqlx_core::arguments::*; use sqlx_core::error::BoxDynError; #[derive(Debug, Clone)] -pub enum SqliteArgumentValue<'q> { +pub enum SqliteArgumentValue { Null, - Text(Cow<'q, str>), - Blob(Cow<'q, [u8]>), + Text(Arc), + TextSlice(Arc), + Blob(Arc>), Double(f64), Int(i32), Int64(i64), } #[derive(Default, Debug, Clone)] -pub struct SqliteArguments<'q> { - pub(crate) values: Vec>, +pub struct SqliteArguments { + pub(crate) values: SqliteArgumentsBuffer, } -impl<'q> SqliteArguments<'q> { - pub(crate) fn add(&mut self, value: T) -> Result<(), BoxDynError> +#[derive(Default, Debug, Clone)] +pub struct SqliteArgumentsBuffer(Vec); + +impl SqliteArguments { + pub(crate) fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: Encode<'q, Sqlite>, + T: Encode<'t, Sqlite>, { - let value_length_before_encoding = self.values.len(); + let value_length_before_encoding = self.values.0.len(); match value.encode(&mut self.values) { - Ok(IsNull::Yes) => self.values.push(SqliteArgumentValue::Null), + Ok(IsNull::Yes) => self.values.0.push(SqliteArgumentValue::Null), Ok(IsNull::No) => {} Err(error) => { // reset the value buffer to its previous value if encoding failed so we don't leave a half-encoded value behind - self.values.truncate(value_length_before_encoding); + self.values.0.truncate(value_length_before_encoding); return Err(error); } }; Ok(()) } - - pub(crate) fn into_static(self) -> SqliteArguments<'static> { - SqliteArguments { - values: self - .values - .into_iter() - .map(SqliteArgumentValue::into_static) - .collect(), - } - } } -impl<'q> Arguments<'q> for SqliteArguments<'q> { +impl Arguments for SqliteArguments { type Database = Sqlite; fn reserve(&mut self, len: usize, _size_hint: usize) { - self.values.reserve(len); + self.values.0.reserve(len); } - fn add(&mut self, value: T) -> Result<(), BoxDynError> + fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError> where - T: Encode<'q, Self::Database>, + T: Encode<'t, Self::Database>, { self.add(value) } fn len(&self) -> usize { - self.values.len() + self.values.0.len() } } -impl SqliteArguments<'_> { +impl SqliteArguments { pub(super) fn bind(&self, handle: &mut StatementHandle, offset: usize) -> Result { let mut arg_i = offset; // for handle in &statement.handles { @@ -103,7 +97,7 @@ impl SqliteArguments<'_> { arg_i }; - if n > self.values.len() { + if n > self.values.0.len() { // SQLite treats unbound variables as NULL // we reproduce this here // If you are reading this and think this should be an error, open an issue and we can @@ -113,32 +107,31 @@ impl SqliteArguments<'_> { break; } - self.values[n - 1].bind(handle, param_i)?; + self.values.0[n - 1].bind(handle, param_i)?; } Ok(arg_i - offset) } } -impl SqliteArgumentValue<'_> { - fn into_static(self) -> SqliteArgumentValue<'static> { - use SqliteArgumentValue::*; +impl SqliteArgumentsBuffer { + #[allow(dead_code)] // clippy incorrectly reports this as unused + pub(crate) fn new(values: Vec) -> SqliteArgumentsBuffer { + Self(values) + } - match self { - Null => Null, - Text(text) => Text(text.into_owned().into()), - Blob(blob) => Blob(blob.into_owned().into()), - Int(v) => Int(v), - Int64(v) => Int64(v), - Double(v) => Double(v), - } + pub(crate) fn push(&mut self, value: SqliteArgumentValue) { + self.0.push(value); } +} +impl SqliteArgumentValue { fn bind(&self, handle: &mut StatementHandle, i: usize) -> Result<(), Error> { use SqliteArgumentValue::*; let status = match self { Text(v) => handle.bind_text(i, v), + TextSlice(v) => handle.bind_text(i, v), Blob(v) => handle.bind_blob(i, v), Int(v) => handle.bind_int(i, *v), Int64(v) => handle.bind_int64(i, *v), diff --git a/sqlx-sqlite/src/connection/execute.rs b/sqlx-sqlite/src/connection/execute.rs index 7acbc91ff8..733a1abbe6 100644 --- a/sqlx-sqlite/src/connection/execute.rs +++ b/sqlx-sqlite/src/connection/execute.rs @@ -10,7 +10,7 @@ pub struct ExecuteIter<'a> { handle: &'a mut ConnectionHandle, statement: &'a mut VirtualStatement, logger: QueryLogger, - args: Option>, + args: Option, /// since a `VirtualStatement` can encompass multiple actual statements, /// this keeps track of the number of arguments so far @@ -19,12 +19,12 @@ pub struct ExecuteIter<'a> { goto_next: bool, } -pub(crate) fn iter<'a>( - conn: &'a mut ConnectionState, +pub(crate) fn iter( + conn: &mut ConnectionState, query: impl SqlSafeStr, - args: Option>, + args: Option, persistent: bool, -) -> Result, Error> { +) -> Result, Error> { let query = query.into_sql_str(); // fetch the cached statement or allocate a new one let statement = conn.statements.get(query.as_str(), persistent)?; @@ -43,7 +43,7 @@ pub(crate) fn iter<'a>( fn bind( statement: &mut StatementHandle, - arguments: &Option>, + arguments: &Option, offset: usize, ) -> Result { let mut n = 0; @@ -56,7 +56,7 @@ fn bind( } impl ExecuteIter<'_> { - pub fn finish(&mut self) -> Result<(), Error> { + pub fn finish(self) -> Result<(), Error> { for res in self { let _ = res?; } diff --git a/sqlx-sqlite/src/connection/worker.rs b/sqlx-sqlite/src/connection/worker.rs index ae50f3e896..f1dbea3682 100644 --- a/sqlx-sqlite/src/connection/worker.rs +++ b/sqlx-sqlite/src/connection/worker.rs @@ -63,7 +63,7 @@ enum Command { }, Execute { query: SqlStr, - arguments: Option>, + arguments: Option, persistent: bool, tx: flume::Sender, Error>>, limit: Option, @@ -353,7 +353,7 @@ impl ConnectionWorker { pub(crate) async fn execute( &mut self, query: SqlStr, - args: Option>, + args: Option, chan_size: usize, persistent: bool, limit: Option, @@ -364,7 +364,7 @@ impl ConnectionWorker { .send_async(( Command::Execute { query, - arguments: args.map(SqliteArguments::into_static), + arguments: args, persistent, tx, limit, diff --git a/sqlx-sqlite/src/database.rs b/sqlx-sqlite/src/database.rs index d34d8eb8c3..ca52d1cf6b 100644 --- a/sqlx-sqlite/src/database.rs +++ b/sqlx-sqlite/src/database.rs @@ -1,9 +1,9 @@ pub(crate) use sqlx_core::database::{Database, HasStatementCache}; +use crate::arguments::SqliteArgumentsBuffer; use crate::{ - SqliteArgumentValue, SqliteArguments, SqliteColumn, SqliteConnection, SqliteQueryResult, - SqliteRow, SqliteStatement, SqliteTransactionManager, SqliteTypeInfo, SqliteValue, - SqliteValueRef, + SqliteArguments, SqliteColumn, SqliteConnection, SqliteQueryResult, SqliteRow, SqliteStatement, + SqliteTransactionManager, SqliteTypeInfo, SqliteValue, SqliteValueRef, }; /// Sqlite database driver. @@ -26,8 +26,8 @@ impl Database for Sqlite { type Value = SqliteValue; type ValueRef<'r> = SqliteValueRef<'r>; - type Arguments<'q> = SqliteArguments<'q>; - type ArgumentBuffer<'q> = Vec>; + type Arguments = SqliteArguments; + type ArgumentBuffer = SqliteArgumentsBuffer; type Statement = SqliteStatement; diff --git a/sqlx-sqlite/src/lib.rs b/sqlx-sqlite/src/lib.rs index 36d3ee7e69..17b4bed1ca 100644 --- a/sqlx-sqlite/src/lib.rs +++ b/sqlx-sqlite/src/lib.rs @@ -74,7 +74,7 @@ extern crate sqlx_core; use std::sync::atomic::AtomicBool; -pub use arguments::{SqliteArgumentValue, SqliteArguments}; +pub use arguments::{SqliteArgumentValue, SqliteArguments, SqliteArgumentsBuffer}; pub use column::SqliteColumn; #[cfg(feature = "deserialize")] #[cfg_attr(docsrs, doc(cfg(feature = "deserialize")))] @@ -147,7 +147,7 @@ impl<'c, T: Executor<'c, Database = Sqlite>> SqliteExecutor<'c> for T {} pub type SqliteTransaction<'c> = sqlx_core::transaction::Transaction<'c, Sqlite>; // NOTE: required due to the lack of lazy normalization -impl_into_arguments_for_arguments!(SqliteArguments<'q>); +impl_into_arguments_for_arguments!(SqliteArguments); impl_column_index_for_row!(SqliteRow); impl_column_index_for_statement!(SqliteStatement); impl_acquire!(Sqlite, SqliteConnection); diff --git a/sqlx-sqlite/src/statement/mod.rs b/sqlx-sqlite/src/statement/mod.rs index 407c567045..93485397d3 100644 --- a/sqlx-sqlite/src/statement/mod.rs +++ b/sqlx-sqlite/src/statement/mod.rs @@ -45,7 +45,7 @@ impl Statement for SqliteStatement { &self.columns } - impl_statement_query!(SqliteArguments<'_>); + impl_statement_query!(SqliteArguments); } impl ColumnIndex for &'_ str { @@ -57,20 +57,3 @@ impl ColumnIndex for &'_ str { .copied() } } - -// #[cfg(feature = "any")] -// impl<'q> From> for crate::any::AnyStatement<'q> { -// #[inline] -// fn from(statement: SqliteStatement<'q>) -> Self { -// crate::any::AnyStatement::<'q> { -// columns: statement -// .columns -// .iter() -// .map(|col| col.clone().into()) -// .collect(), -// column_names: statement.column_names, -// parameters: Some(Either::Right(statement.parameters)), -// sql: statement.sql, -// } -// } -// } diff --git a/sqlx-sqlite/src/types/bool.rs b/sqlx-sqlite/src/types/bool.rs index a229298ff9..9228eef236 100644 --- a/sqlx-sqlite/src/types/bool.rs +++ b/sqlx-sqlite/src/types/bool.rs @@ -1,3 +1,4 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -15,11 +16,8 @@ impl Type for bool { } } -impl<'q> Encode<'q, Sqlite> for bool { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for bool { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int((*self).into())); Ok(IsNull::No) diff --git a/sqlx-sqlite/src/types/bytes.rs b/sqlx-sqlite/src/types/bytes.rs index 2d67335a52..6453007bc9 100644 --- a/sqlx-sqlite/src/types/bytes.rs +++ b/sqlx-sqlite/src/types/bytes.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::rc::Rc; use std::sync::Arc; +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -19,12 +20,9 @@ impl Type for [u8] { } } -impl<'q> Encode<'q, Sqlite> for &'q [u8] { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Borrowed(self))); +impl Encode<'_, Sqlite> for &'_ [u8] { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.to_vec()))); Ok(IsNull::No) } @@ -37,19 +35,14 @@ impl<'r> Decode<'r, Sqlite> for &'r [u8] { } impl Encode<'_, Sqlite> for Box<[u8]> { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self.into_vec()))); + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.into_vec()))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Owned( - self.clone().into_vec(), - ))); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.clone().into_vec()))); Ok(IsNull::No) } @@ -65,18 +58,15 @@ impl Type for Vec { } } -impl<'q> Encode<'q, Sqlite> for Vec { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self))); +impl Encode<'_, Sqlite> for Vec { + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self.clone()))); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.clone()))); Ok(IsNull::No) } @@ -88,37 +78,28 @@ impl<'r> Decode<'r, Sqlite> for Vec { } } -impl<'q> Encode<'q, Sqlite> for Cow<'q, [u8]> { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Blob(self)); +impl Encode<'_, Sqlite> for Cow<'_, [u8]> { + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.into()))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Blob(self.clone())); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new(self.to_vec()))); Ok(IsNull::No) } } -impl<'q> Encode<'q, Sqlite> for Arc<[u8]> { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for Arc<[u8]> { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { as Encode<'_, Sqlite>>::encode(self.to_vec(), args) } } -impl<'q> Encode<'q, Sqlite> for Rc<[u8]> { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for Rc<[u8]> { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { as Encode<'_, Sqlite>>::encode(self.to_vec(), args) } } diff --git a/sqlx-sqlite/src/types/chrono.rs b/sqlx-sqlite/src/types/chrono.rs index 5c4a41caff..8d987538d0 100644 --- a/sqlx-sqlite/src/types/chrono.rs +++ b/sqlx-sqlite/src/types/chrono.rs @@ -7,7 +7,7 @@ use crate::{ error::BoxDynError, type_info::DataType, types::Type, - Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef, + Sqlite, SqliteArgumentsBuffer, SqliteTypeInfo, SqliteValueRef, }; use chrono::FixedOffset; use chrono::{ @@ -65,25 +65,25 @@ impl Encode<'_, Sqlite> for DateTime where Tz::Offset: Display, { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.to_rfc3339_opts(SecondsFormat::AutoSi, false), buf) } } impl Encode<'_, Sqlite> for NaiveDateTime { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.format("%F %T%.f").to_string(), buf) } } impl Encode<'_, Sqlite> for NaiveDate { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.format("%F").to_string(), buf) } } impl Encode<'_, Sqlite> for NaiveTime { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.format("%T%.f").to_string(), buf) } } diff --git a/sqlx-sqlite/src/types/float.rs b/sqlx-sqlite/src/types/float.rs index c6e105d783..d23b3f3a08 100644 --- a/sqlx-sqlite/src/types/float.rs +++ b/sqlx-sqlite/src/types/float.rs @@ -1,3 +1,4 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -11,11 +12,8 @@ impl Type for f32 { } } -impl<'q> Encode<'q, Sqlite> for f32 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for f32 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Double((*self).into())); Ok(IsNull::No) @@ -36,11 +34,8 @@ impl Type for f64 { } } -impl<'q> Encode<'q, Sqlite> for f64 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for f64 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Double(*self)); Ok(IsNull::No) diff --git a/sqlx-sqlite/src/types/int.rs b/sqlx-sqlite/src/types/int.rs index e87025e2fb..2cf4209ad6 100644 --- a/sqlx-sqlite/src/types/int.rs +++ b/sqlx-sqlite/src/types/int.rs @@ -1,3 +1,4 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -15,11 +16,8 @@ impl Type for i8 { } } -impl<'q> Encode<'q, Sqlite> for i8 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for i8 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int(*self as i32)); Ok(IsNull::No) @@ -46,11 +44,8 @@ impl Type for i16 { } } -impl<'q> Encode<'q, Sqlite> for i16 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for i16 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int(*self as i32)); Ok(IsNull::No) @@ -73,11 +68,8 @@ impl Type for i32 { } } -impl<'q> Encode<'q, Sqlite> for i32 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for i32 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int(*self)); Ok(IsNull::No) @@ -100,11 +92,8 @@ impl Type for i64 { } } -impl<'q> Encode<'q, Sqlite> for i64 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for i64 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int64(*self)); Ok(IsNull::No) diff --git a/sqlx-sqlite/src/types/json.rs b/sqlx-sqlite/src/types/json.rs index b8b665c4d3..dafd483658 100644 --- a/sqlx-sqlite/src/types/json.rs +++ b/sqlx-sqlite/src/types/json.rs @@ -1,10 +1,11 @@ use serde::{Deserialize, Serialize}; +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::types::{Json, Type}; -use crate::{type_info::DataType, Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef}; +use crate::{type_info::DataType, Sqlite, SqliteTypeInfo, SqliteValueRef}; impl Type for Json { fn type_info() -> SqliteTypeInfo { @@ -20,7 +21,7 @@ impl Encode<'_, Sqlite> for Json where T: Serialize, { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.encode_to_string()?, buf) } } diff --git a/sqlx-sqlite/src/types/str.rs b/sqlx-sqlite/src/types/str.rs index 5392f6401a..c90f9bba55 100644 --- a/sqlx-sqlite/src/types/str.rs +++ b/sqlx-sqlite/src/types/str.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::rc::Rc; use std::sync::Arc; +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -15,12 +16,9 @@ impl Type for str { } } -impl<'q> Encode<'q, Sqlite> for &'q str { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Borrowed(*self))); +impl Encode<'_, Sqlite> for &'_ str { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.to_string()))); Ok(IsNull::No) } @@ -33,19 +31,14 @@ impl<'r> Decode<'r, Sqlite> for &'r str { } impl Encode<'_, Sqlite> for Box { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned(self.into_string()))); + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(String::from(self)))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned( - self.clone().into_string(), - ))); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.to_string()))); Ok(IsNull::No) } @@ -57,18 +50,15 @@ impl Type for String { } } -impl<'q> Encode<'q, Sqlite> for String { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned(self))); +impl Encode<'_, Sqlite> for String { + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned(self.clone()))); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.clone()))); Ok(IsNull::No) } @@ -80,37 +70,28 @@ impl<'r> Decode<'r, Sqlite> for String { } } -impl<'q> Encode<'q, Sqlite> for Cow<'q, str> { - fn encode(self, args: &mut Vec>) -> Result { - args.push(SqliteArgumentValue::Text(self)); +impl Encode<'_, Sqlite> for Cow<'_, str> { + fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.into()))); Ok(IsNull::No) } - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(self.clone())); + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.to_string()))); Ok(IsNull::No) } } -impl<'q> Encode<'q, Sqlite> for Arc { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for Arc { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { >::encode(self.to_string(), args) } } -impl<'q> Encode<'q, Sqlite> for Rc { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for Rc { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { >::encode(self.to_string(), args) } } diff --git a/sqlx-sqlite/src/types/text.rs b/sqlx-sqlite/src/types/text.rs index 80aab0d4ba..ef03caf5a0 100644 --- a/sqlx-sqlite/src/types/text.rs +++ b/sqlx-sqlite/src/types/text.rs @@ -1,4 +1,5 @@ -use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef}; +use crate::arguments::SqliteArgumentsBuffer; +use crate::{Sqlite, SqliteTypeInfo, SqliteValueRef}; use sqlx_core::decode::Decode; use sqlx_core::encode::{Encode, IsNull}; use sqlx_core::error::BoxDynError; @@ -16,11 +17,11 @@ impl Type for Text { } } -impl<'q, T> Encode<'q, Sqlite> for Text +impl Encode<'_, Sqlite> for Text where T: Display, { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.0.to_string(), buf) } } diff --git a/sqlx-sqlite/src/types/time.rs b/sqlx-sqlite/src/types/time.rs index c7ad3b3d05..0e0027882d 100644 --- a/sqlx-sqlite/src/types/time.rs +++ b/sqlx-sqlite/src/types/time.rs @@ -1,3 +1,4 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::value::ValueRef; use crate::{ decode::Decode, @@ -5,7 +6,7 @@ use crate::{ error::BoxDynError, type_info::DataType, types::Type, - Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef, + Sqlite, SqliteTypeInfo, SqliteValueRef, }; use time::format_description::{well_known::Rfc3339, BorrowedFormatItem}; use time::macros::format_description as fd; @@ -55,27 +56,27 @@ impl Type for Time { } impl Encode<'_, Sqlite> for OffsetDateTime { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { Encode::::encode(self.format(&Rfc3339)?, buf) } } impl Encode<'_, Sqlite> for PrimitiveDateTime { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { let format = fd!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"); Encode::::encode(self.format(&format)?, buf) } } impl Encode<'_, Sqlite> for Date { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { let format = fd!("[year]-[month]-[day]"); Encode::::encode(self.format(&format)?, buf) } } impl Encode<'_, Sqlite> for Time { - fn encode_by_ref(&self, buf: &mut Vec>) -> Result { + fn encode_by_ref(&self, buf: &mut SqliteArgumentsBuffer) -> Result { let format = fd!("[hour]:[minute]:[second].[subsecond]"); Encode::::encode(self.format(&format)?, buf) } diff --git a/sqlx-sqlite/src/types/uint.rs b/sqlx-sqlite/src/types/uint.rs index f3d7da8c2f..1e8b1c5392 100644 --- a/sqlx-sqlite/src/types/uint.rs +++ b/sqlx-sqlite/src/types/uint.rs @@ -1,3 +1,4 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -15,11 +16,8 @@ impl Type for u8 { } } -impl<'q> Encode<'q, Sqlite> for u8 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for u8 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int(*self as i32)); Ok(IsNull::No) @@ -46,11 +44,8 @@ impl Type for u16 { } } -impl<'q> Encode<'q, Sqlite> for u16 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for u16 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int(*self as i32)); Ok(IsNull::No) @@ -73,11 +68,8 @@ impl Type for u32 { } } -impl<'q> Encode<'q, Sqlite> for u32 { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { +impl Encode<'_, Sqlite> for u32 { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { args.push(SqliteArgumentValue::Int64(*self as i64)); Ok(IsNull::No) diff --git a/sqlx-sqlite/src/types/uuid.rs b/sqlx-sqlite/src/types/uuid.rs index 99291be86e..718ca5fb5d 100644 --- a/sqlx-sqlite/src/types/uuid.rs +++ b/sqlx-sqlite/src/types/uuid.rs @@ -1,10 +1,11 @@ +use crate::arguments::SqliteArgumentsBuffer; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; use crate::type_info::DataType; use crate::types::Type; use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef}; -use std::borrow::Cow; +use std::sync::Arc; use uuid::{ fmt::{Hyphenated, Simple}, Uuid, @@ -20,12 +21,9 @@ impl Type for Uuid { } } -impl<'q> Encode<'q, Sqlite> for Uuid { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Blob(Cow::Owned( +impl Encode<'_, Sqlite> for Uuid { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Blob(Arc::new( self.as_bytes().to_vec(), ))); @@ -46,12 +44,9 @@ impl Type for Hyphenated { } } -impl<'q> Encode<'q, Sqlite> for Hyphenated { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string()))); +impl Encode<'_, Sqlite> for Hyphenated { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.to_string()))); Ok(IsNull::No) } @@ -72,12 +67,9 @@ impl Type for Simple { } } -impl<'q> Encode<'q, Sqlite> for Simple { - fn encode_by_ref( - &self, - args: &mut Vec>, - ) -> Result { - args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string()))); +impl Encode<'_, Sqlite> for Simple { + fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result { + args.push(SqliteArgumentValue::Text(Arc::new(self.to_string()))); Ok(IsNull::No) } diff --git a/tests/mysql/types.rs b/tests/mysql/types.rs index 9686d41721..e8e37f06ed 100644 --- a/tests/mysql/types.rs +++ b/tests/mysql/types.rs @@ -8,7 +8,7 @@ use std::str::FromStr; use std::sync::Arc; use sqlx::mysql::MySql; -use sqlx::{Executor, FromRow, Row}; +use sqlx::{Executor, Row}; use sqlx::types::Text; diff --git a/tests/postgres/postgres.rs b/tests/postgres/postgres.rs index c580bb4eed..ee7330c621 100644 --- a/tests/postgres/postgres.rs +++ b/tests/postgres/postgres.rs @@ -2142,6 +2142,8 @@ create temporary table person( assert_eq!(people, p_query); Ok(()) } + +#[allow(unused)] async fn test_pg_copy_chunked() -> anyhow::Result<()> { let mut conn = new::().await?; diff --git a/tests/postgres/query_builder.rs b/tests/postgres/query_builder.rs index 5b73bcff35..60be9761d6 100644 --- a/tests/postgres/query_builder.rs +++ b/tests/postgres/query_builder.rs @@ -7,13 +7,13 @@ use sqlx_test::new; #[test] fn test_new() { - let qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users"); + let qb: QueryBuilder = QueryBuilder::new("SELECT * FROM users"); assert_eq!(qb.sql(), "SELECT * FROM users"); } #[test] fn test_push() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users"); + let mut qb: QueryBuilder = QueryBuilder::new("SELECT * FROM users"); let second_line = " WHERE last_name LIKE '[A-N]%';"; qb.push(second_line); @@ -26,7 +26,7 @@ fn test_push() { #[test] #[should_panic] fn test_push_panics_after_build_without_reset() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users;"); + let mut qb: QueryBuilder = QueryBuilder::new("SELECT * FROM users;"); let _query = qb.build(); @@ -35,7 +35,7 @@ fn test_push_panics_after_build_without_reset() { #[test] fn test_push_bind() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users WHERE id = "); + let mut qb: QueryBuilder = QueryBuilder::new("SELECT * FROM users WHERE id = "); qb.push_bind(42i32) .push(" OR membership_level = ") @@ -49,7 +49,7 @@ fn test_push_bind() { #[test] fn test_build() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT * FROM users"); + let mut qb: QueryBuilder = QueryBuilder::new("SELECT * FROM users"); qb.push(" WHERE id = ").push_bind(42i32); let query = qb.build(); @@ -60,7 +60,7 @@ fn test_build() { #[test] fn test_reset() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(""); + let mut qb: QueryBuilder = QueryBuilder::new(""); { let _query = qb @@ -76,7 +76,7 @@ fn test_reset() { #[test] fn test_query_builder_reuse() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(""); + let mut qb: QueryBuilder = QueryBuilder::new(""); let _query = qb .push("SELECT * FROM users WHERE id = ") @@ -92,7 +92,7 @@ fn test_query_builder_reuse() { #[test] fn test_query_builder_with_args() { - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(""); + let mut qb: QueryBuilder = QueryBuilder::new(""); let mut query = qb .push("SELECT * FROM users WHERE id = ") @@ -101,8 +101,7 @@ fn test_query_builder_with_args() { let args = query.take_arguments().unwrap().unwrap(); - let mut qb: QueryBuilder<'_, Postgres> = - QueryBuilder::with_arguments(query.sql().as_str(), args); + let mut qb: QueryBuilder = QueryBuilder::with_arguments(query.sql().as_str(), args); let query = qb.push(" OR membership_level = ").push_bind(3i32).build(); assert_eq!( @@ -118,7 +117,7 @@ async fn test_max_number_of_binds() -> anyhow::Result<()> { // // https://github.com/launchbadge/sqlx/issues/3464 - let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("SELECT ARRAY["); + let mut qb: QueryBuilder = QueryBuilder::new("SELECT ARRAY["); let mut elements = qb.separated(','); diff --git a/tests/sqlite/macros.rs b/tests/sqlite/macros.rs index 74e689260a..0ab4dd56bf 100644 --- a/tests/sqlite/macros.rs +++ b/tests/sqlite/macros.rs @@ -175,6 +175,31 @@ async fn test_query_scalar() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn query_by_string() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let string = "Hello, world!".to_string(); + let ref tuple = ("Hello, world!".to_string(),); + + let result = sqlx::query!( + "SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)", + string, // make sure we don't actually take ownership here + &string[..], + Some(&string), + Some(&string[..]), + Option::::None, + string.clone(), + tuple.0 // make sure we're not trying to move out of a field expression + ) + .fetch_one(&mut conn) + .await?; + + assert_eq!(result.string, string); + + Ok(()) +} + #[sqlx_macros::test] async fn macro_select_from_view() -> anyhow::Result<()> { let mut conn = new::().await?;