Skip to content

Commit 1154926

Browse files
committed
track the kind of null arguments in order to provide the appropriate type when converting them
1 parent 16e3f10 commit 1154926

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

sqlx-core/src/any/arguments.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::any::value::AnyValueKind;
2-
use crate::any::Any;
2+
use crate::any::{Any, AnyTypeInfoKind};
33
use crate::arguments::Arguments;
44
use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
@@ -46,6 +46,14 @@ impl<'q> AnyArguments<'q> {
4646
where
4747
'q: 'a,
4848
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
49+
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
50+
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
51+
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
52+
Option<i64>: Type<A::Database> + Encode<'a, A::Database>,
53+
Option<f32>: Type<A::Database> + Encode<'a, A::Database>,
54+
Option<f64>: Type<A::Database> + Encode<'a, A::Database>,
55+
Option<String>: Type<A::Database> + Encode<'a, A::Database>,
56+
Option<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
4957
bool: Type<A::Database> + Encode<'a, A::Database>,
5058
i16: Type<A::Database> + Encode<'a, A::Database>,
5159
i32: Type<A::Database> + Encode<'a, A::Database>,
@@ -59,7 +67,15 @@ impl<'q> AnyArguments<'q> {
5967

6068
for arg in &self.values.0 {
6169
match arg {
62-
AnyValueKind::Null => out.add(Option::<i32>::None),
70+
AnyValueKind::Null(AnyTypeInfoKind::Null) => out.add(Option::<i32>::None),
71+
AnyValueKind::Null(AnyTypeInfoKind::Bool) => out.add(Option::<bool>::None),
72+
AnyValueKind::Null(AnyTypeInfoKind::SmallInt) => out.add(Option::<i16>::None),
73+
AnyValueKind::Null(AnyTypeInfoKind::Integer) => out.add(Option::<i32>::None),
74+
AnyValueKind::Null(AnyTypeInfoKind::BigInt) => out.add(Option::<i64>::None),
75+
AnyValueKind::Null(AnyTypeInfoKind::Real) => out.add(Option::<f64>::None),
76+
AnyValueKind::Null(AnyTypeInfoKind::Double) => out.add(Option::<f32>::None),
77+
AnyValueKind::Null(AnyTypeInfoKind::Text) => out.add(Option::<String>::None),
78+
AnyValueKind::Null(AnyTypeInfoKind::Blob) => out.add(Option::<Vec<u8>>::None),
6379
AnyValueKind::Bool(b) => out.add(b),
6480
AnyValueKind::SmallInt(i) => out.add(i),
6581
AnyValueKind::Integer(i) => out.add(i),
@@ -70,7 +86,6 @@ impl<'q> AnyArguments<'q> {
7086
AnyValueKind::Blob(b) => out.add(&**b),
7187
}?
7288
}
73-
7489
Ok(out)
7590
}
7691
}

sqlx-core/src/any/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub use transaction::AnyTransactionManager;
4242
pub use type_info::{AnyTypeInfo, AnyTypeInfoKind};
4343
pub use value::{AnyValue, AnyValueRef};
4444

45+
use crate::types::Type;
4546
#[doc(hidden)]
4647
pub use value::AnyValueKind;
4748

@@ -65,7 +66,7 @@ impl_column_index_for_statement!(AnyStatement);
6566
// required because some databases have a different handling of NULL
6667
impl<'q, T> Encode<'q, Any> for Option<T>
6768
where
68-
T: Encode<'q, Any> + 'q,
69+
T: Encode<'q, Any> + 'q + Type<Any>,
6970
{
7071
fn encode_by_ref(
7172
&self,
@@ -74,7 +75,7 @@ where
7475
if let Some(value) = self {
7576
value.encode_by_ref(buf)
7677
} else {
77-
buf.0.push(AnyValueKind::Null);
78+
buf.0.push(AnyValueKind::Null(T::type_info().kind));
7879
Ok(crate::encode::IsNull::Yes)
7980
}
8081
}

sqlx-core/src/any/row.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ impl AnyRow {
117117
})?;
118118

119119
let value_kind = match type_info.kind {
120-
_ if value.is_null() => AnyValueKind::Null,
121-
AnyTypeInfoKind::Null => AnyValueKind::Null,
120+
k if value.is_null() => AnyValueKind::Null(k),
121+
AnyTypeInfoKind::Null => AnyValueKind::Null(AnyTypeInfoKind::Null),
122122
AnyTypeInfoKind::Bool => AnyValueKind::Bool(decode(value)?),
123123
AnyTypeInfoKind::SmallInt => AnyValueKind::SmallInt(decode(value)?),
124124
AnyTypeInfoKind::Integer => AnyValueKind::Integer(decode(value)?),

sqlx-core/src/any/value.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::value::{Value, ValueRef};
99
#[derive(Clone, Debug)]
1010
#[non_exhaustive]
1111
pub enum AnyValueKind<'a> {
12-
Null,
12+
Null(AnyTypeInfoKind),
1313
Bool(bool),
1414
SmallInt(i16),
1515
Integer(i32),
@@ -24,7 +24,7 @@ impl AnyValueKind<'_> {
2424
fn type_info(&self) -> AnyTypeInfo {
2525
AnyTypeInfo {
2626
kind: match self {
27-
AnyValueKind::Null => AnyTypeInfoKind::Null,
27+
AnyValueKind::Null(_) => AnyTypeInfoKind::Null,
2828
AnyValueKind::Bool(_) => AnyTypeInfoKind::Bool,
2929
AnyValueKind::SmallInt(_) => AnyTypeInfoKind::SmallInt,
3030
AnyValueKind::Integer(_) => AnyTypeInfoKind::Integer,
@@ -74,7 +74,7 @@ impl Value for AnyValue {
7474
fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_> {
7575
AnyValueRef {
7676
kind: match &self.kind {
77-
AnyValueKind::Null => AnyValueKind::Null,
77+
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
7878
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
7979
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
8080
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
@@ -92,7 +92,7 @@ impl Value for AnyValue {
9292
}
9393

9494
fn is_null(&self) -> bool {
95-
matches!(self.kind, AnyValueKind::Null)
95+
matches!(self.kind, AnyValueKind::Null(_))
9696
}
9797
}
9898

@@ -102,7 +102,7 @@ impl<'a> ValueRef<'a> for AnyValueRef<'a> {
102102
fn to_owned(&self) -> <Self::Database as Database>::Value {
103103
AnyValue {
104104
kind: match &self.kind {
105-
AnyValueKind::Null => AnyValueKind::Null,
105+
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
106106
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
107107
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
108108
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
@@ -120,6 +120,6 @@ impl<'a> ValueRef<'a> for AnyValueRef<'a> {
120120
}
121121

122122
fn is_null(&self) -> bool {
123-
matches!(self.kind, AnyValueKind::Null)
123+
matches!(self.kind, AnyValueKind::Null(_))
124124
}
125125
}

sqlx-sqlite/src/any.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments<'_> {
201201
.0
202202
.into_iter()
203203
.map(|val| match val {
204-
AnyValueKind::Null => SqliteArgumentValue::Null,
204+
AnyValueKind::Null(_) => SqliteArgumentValue::Null,
205205
AnyValueKind::Bool(b) => SqliteArgumentValue::Int(b as i32),
206206
AnyValueKind::SmallInt(i) => SqliteArgumentValue::Int(i as i32),
207207
AnyValueKind::Integer(i) => SqliteArgumentValue::Int(i),

0 commit comments

Comments
 (0)