Skip to content

do not merge - wip #3966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
]
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async-io = { version = "2.4.1", optional = true }
base64 = { version = "0.22.0", default-features = false, features = ["std"] }
bytes = "1.1.0"
chrono = { version = "0.4.34", default-features = false, features = ["clock"], optional = true }
combine = "4.6"
crc = { version = "3", optional = true }
crossbeam-queue = "0.3.2"
either = "1.6.1"
Expand Down
75 changes: 37 additions & 38 deletions sqlx-core/src/any/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ use crate::any::value::AnyValueKind;
use crate::any::{Any, AnyTypeInfoKind};
use crate::arguments::Arguments;
use crate::encode::{Encode, IsNull};
use crate::encode_owned::IntoEncode;
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<T>(&mut self, value: T) -> Result<(), BoxDynError>
fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError>
where
T: 'q + Encode<'q, Self::Database> + Type<Self::Database>,
T: IntoEncode<Self::Database> + Type<Self::Database>,
{
let _: IsNull = value.encode(&mut self.values)?;
let _: IsNull = value.into_encode().encode(&mut self.values)?;
Ok(())
}

Expand All @@ -30,42 +33,37 @@ impl<'q> Arguments<'q> for AnyArguments<'q> {
}
}

pub struct AnyArgumentBuffer<'q>(#[doc(hidden)] pub Vec<AnyValueKind<'q>>);
#[derive(Default)]
pub struct AnyArgumentBuffer(#[doc(hidden)] pub Vec<AnyValueKind>);

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<A, BoxDynError>
pub fn convert_into<A: Arguments>(self) -> Result<A, BoxDynError>
where
'q: 'a,
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
Option<i64>: Type<A::Database> + Encode<'a, A::Database>,
Option<f32>: Type<A::Database> + Encode<'a, A::Database>,
Option<f64>: Type<A::Database> + Encode<'a, A::Database>,
Option<String>: Type<A::Database> + Encode<'a, A::Database>,
Option<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
bool: Type<A::Database> + Encode<'a, A::Database>,
i16: Type<A::Database> + Encode<'a, A::Database>,
i32: Type<A::Database> + Encode<'a, A::Database>,
i64: Type<A::Database> + Encode<'a, A::Database>,
f32: Type<A::Database> + Encode<'a, A::Database>,
f64: Type<A::Database> + Encode<'a, A::Database>,
&'a str: Type<A::Database> + Encode<'a, A::Database>,
&'a [u8]: Type<A::Database> + Encode<'a, A::Database>,
Option<i32>: IntoEncode<A::Database> + Type<A::Database>,
Option<bool>: IntoEncode<A::Database> + Type<A::Database>,
Option<i16>: IntoEncode<A::Database> + Type<A::Database>,
Option<i32>: IntoEncode<A::Database> + Type<A::Database>,
Option<i64>: IntoEncode<A::Database> + Type<A::Database>,
Option<f32>: IntoEncode<A::Database> + Type<A::Database>,
Option<f64>: IntoEncode<A::Database> + Type<A::Database>,
Option<String>: IntoEncode<A::Database> + Type<A::Database>,
Option<Vec<u8>>: IntoEncode<A::Database> + Type<A::Database>,
bool: IntoEncode<A::Database> + Type<A::Database>,
i16: IntoEncode<A::Database> + Type<A::Database>,
i32: IntoEncode<A::Database> + Type<A::Database>,
i64: IntoEncode<A::Database> + Type<A::Database>,
f32: IntoEncode<A::Database> + Type<A::Database>,
f64: IntoEncode<A::Database> + Type<A::Database>,
String: IntoEncode<A::Database> + Type<A::Database>,
Vec<u8>: IntoEncode<A::Database> + Type<A::Database>,
Arc<String>: IntoEncode<A::Database> + Type<A::Database>,
Arc<str>: IntoEncode<A::Database> + Type<A::Database>,
Arc<Vec<u8>>: IntoEncode<A::Database> + Type<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::<i32>::None),
AnyValueKind::Null(AnyTypeInfoKind::Bool) => out.add(Option::<bool>::None),
Expand All @@ -82,8 +80,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)
Expand Down
29 changes: 21 additions & 8 deletions sqlx-core/src/any/connection/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<AnyArguments<'q>>,
) -> BoxStream<'q, crate::Result<Either<AnyQueryResult, AnyRow>>>;
arguments: Option<AnyArguments>,
) -> BoxStream<'_, crate::Result<Either<AnyQueryResult, AnyRow>>>;

fn fetch_optional<'q>(
&'q mut self,
fn fetch_optional(
&mut self,
query: SqlStr,
persistent: bool,
arguments: Option<AnyArguments<'q>>,
) -> BoxFuture<'q, crate::Result<Option<AnyRow>>>;
arguments: Option<AnyArguments>,
) -> BoxFuture<'_, crate::Result<Option<AnyRow>>> {
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,
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/any/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -71,7 +71,7 @@ where
{
fn encode_by_ref(
&self,
buf: &mut AnyArgumentBuffer<'q>,
buf: &mut AnyArgumentBuffer,
) -> Result<crate::encode::IsNull, crate::error::BoxDynError> {
if let Some(value) = self {
value.encode_by_ref(buf)
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/any/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Statement for AnyStatement {
&self.columns
}

impl_statement_query!(AnyArguments<'_>);
impl_statement_query!(AnyArguments);
}

impl ColumnIndex<AnyStatement> for &'_ str {
Expand Down
21 changes: 8 additions & 13 deletions sqlx-core/src/any/types/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any> for [u8] {
fn type_info() -> AnyTypeInfo {
Expand All @@ -17,22 +17,17 @@ impl Type<Any> for [u8] {
impl<'q> Encode<'q, Any> for &'q [u8] {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Blob((*self).into()));
buf.0.push(AnyValueKind::Blob(Arc::new(self.to_vec())));
Ok(IsNull::No)
}
}

impl<'r> Decode<'r, Any> for &'r [u8] {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
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<u8>` 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(),
}
}
Expand All @@ -44,20 +39,20 @@ impl Type<Any> for Vec<u8> {
}
}

impl<'q> Encode<'q, Any> for Vec<u8> {
impl Encode<'_, Any> for Vec<u8> {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Blob(Cow::Owned(self.clone())));
buf.0.push(AnyValueKind::Blob(Arc::new(self.clone())));
Ok(IsNull::No)
}
}

impl<'r> Decode<'r, Any> for Vec<u8> {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Blob(blob) => Ok(blob.into_owned()),
AnyValueKind::Blob(blob) => Ok(blob.as_ref().clone()),
other => other.unexpected(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/any/types/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ impl Type<Any> for bool {
}
}

impl<'q> Encode<'q, Any> for bool {
impl Encode<'_, Any> for bool {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Bool(*self));
Ok(IsNull::No)
Expand All @@ -26,7 +26,7 @@ impl<'q> Encode<'q, Any> for bool {
impl<'r> Decode<'r, Any> for bool {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Bool(b) => Ok(b),
AnyValueKind::Bool(b) => Ok(*b),
other => other.unexpected(),
}
}
Expand Down
14 changes: 7 additions & 7 deletions sqlx-core/src/any/types/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl Type<Any> for f32 {
}
}

impl<'q> Encode<'q, Any> for f32 {
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
impl Encode<'_, Any> for f32 {
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Real(*self));
Ok(IsNull::No)
}
Expand All @@ -23,7 +23,7 @@ impl<'q> Encode<'q, Any> for f32 {
impl<'r> Decode<'r, Any> for f32 {
fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError> {
match value.kind {
AnyValueKind::Real(r) => Ok(r),
AnyValueKind::Real(r) => Ok(*r),
other => other.unexpected(),
}
}
Expand All @@ -37,10 +37,10 @@ impl Type<Any> for f64 {
}
}

impl<'q> Encode<'q, Any> for f64 {
impl Encode<'_, Any> for f64 {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer<'q>,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
buf.0.push(AnyValueKind::Double(*self));
Ok(IsNull::No)
Expand All @@ -51,8 +51,8 @@ impl<'r> Decode<'r, Any> for f64 {
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
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(),
}
}
Expand Down
Loading
Loading