Skip to content

Commit 36fffcb

Browse files
committed
feat(any): add unsigned ints + tinyint
1 parent 064d649 commit 36fffcb

File tree

9 files changed

+258
-3
lines changed

9 files changed

+258
-3
lines changed

sqlx-core/src/any/arguments.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,27 @@ impl AnyArguments {
4141
where
4242
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
4343
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
44+
Option<i8>: Type<A::Database> + Encode<'a, A::Database>,
4445
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
4546
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
4647
Option<i64>: Type<A::Database> + Encode<'a, A::Database>,
48+
Option<u8>: Type<A::Database> + Encode<'a, A::Database>,
49+
Option<u16>: Type<A::Database> + Encode<'a, A::Database>,
50+
Option<u32>: Type<A::Database> + Encode<'a, A::Database>,
51+
Option<u64>: Type<A::Database> + Encode<'a, A::Database>,
4752
Option<f32>: Type<A::Database> + Encode<'a, A::Database>,
4853
Option<f64>: Type<A::Database> + Encode<'a, A::Database>,
4954
Option<String>: Type<A::Database> + Encode<'a, A::Database>,
5055
Option<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
5156
bool: Type<A::Database> + Encode<'a, A::Database>,
57+
i8: Type<A::Database> + Encode<'a, A::Database>,
5258
i16: Type<A::Database> + Encode<'a, A::Database>,
5359
i32: Type<A::Database> + Encode<'a, A::Database>,
5460
i64: Type<A::Database> + Encode<'a, A::Database>,
61+
u8: Type<A::Database> + Encode<'a, A::Database>,
62+
u16: Type<A::Database> + Encode<'a, A::Database>,
63+
u32: Type<A::Database> + Encode<'a, A::Database>,
64+
u64: Type<A::Database> + Encode<'a, A::Database>,
5565
f32: Type<A::Database> + Encode<'a, A::Database>,
5666
f64: Type<A::Database> + Encode<'a, A::Database>,
5767
Arc<String>: Type<A::Database> + Encode<'a, A::Database>,
@@ -64,17 +74,31 @@ impl AnyArguments {
6474
match arg {
6575
AnyValueKind::Null(AnyTypeInfoKind::Null) => out.add(Option::<i32>::None),
6676
AnyValueKind::Null(AnyTypeInfoKind::Bool) => out.add(Option::<bool>::None),
77+
AnyValueKind::Null(AnyTypeInfoKind::TinyInt) => out.add(Option::<i8>::None),
6778
AnyValueKind::Null(AnyTypeInfoKind::SmallInt) => out.add(Option::<i16>::None),
6879
AnyValueKind::Null(AnyTypeInfoKind::Integer) => out.add(Option::<i32>::None),
6980
AnyValueKind::Null(AnyTypeInfoKind::BigInt) => out.add(Option::<i64>::None),
81+
AnyValueKind::Null(AnyTypeInfoKind::UnsignedTinyInt) => out.add(Option::<u8>::None),
82+
AnyValueKind::Null(AnyTypeInfoKind::UnsignedSmallInt) => {
83+
out.add(Option::<u16>::None)
84+
}
85+
AnyValueKind::Null(AnyTypeInfoKind::UnsignedInteger) => {
86+
out.add(Option::<u32>::None)
87+
}
88+
AnyValueKind::Null(AnyTypeInfoKind::UnsignedBigInt) => out.add(Option::<u64>::None),
7089
AnyValueKind::Null(AnyTypeInfoKind::Real) => out.add(Option::<f64>::None),
7190
AnyValueKind::Null(AnyTypeInfoKind::Double) => out.add(Option::<f32>::None),
7291
AnyValueKind::Null(AnyTypeInfoKind::Text) => out.add(Option::<String>::None),
7392
AnyValueKind::Null(AnyTypeInfoKind::Blob) => out.add(Option::<Vec<u8>>::None),
7493
AnyValueKind::Bool(b) => out.add(b),
94+
AnyValueKind::TinyInt(i) => out.add(i),
7595
AnyValueKind::SmallInt(i) => out.add(i),
7696
AnyValueKind::Integer(i) => out.add(i),
7797
AnyValueKind::BigInt(i) => out.add(i),
98+
AnyValueKind::UnsignedTinyInt(i) => out.add(i),
99+
AnyValueKind::UnsignedSmallInt(i) => out.add(i),
100+
AnyValueKind::UnsignedInteger(i) => out.add(i),
101+
AnyValueKind::UnsignedBigInt(i) => out.add(i),
78102
AnyValueKind::Real(r) => out.add(r),
79103
AnyValueKind::Double(d) => out.add(d),
80104
AnyValueKind::Text(t) => out.add(t),

sqlx-core/src/any/row.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ impl AnyRow {
8888
AnyTypeInfo: for<'b> TryFrom<&'b <R::Database as Database>::TypeInfo, Error = Error>,
8989
AnyColumn: for<'b> TryFrom<&'b <R::Database as Database>::Column, Error = Error>,
9090
bool: Type<R::Database> + Decode<'a, R::Database>,
91+
i8: Type<R::Database> + Decode<'a, R::Database>,
9192
i16: Type<R::Database> + Decode<'a, R::Database>,
9293
i32: Type<R::Database> + Decode<'a, R::Database>,
9394
i64: Type<R::Database> + Decode<'a, R::Database>,
95+
u8: Type<R::Database> + Decode<'a, R::Database>,
96+
u16: Type<R::Database> + Decode<'a, R::Database>,
97+
u32: Type<R::Database> + Decode<'a, R::Database>,
98+
u64: Type<R::Database> + Decode<'a, R::Database>,
9499
f32: Type<R::Database> + Decode<'a, R::Database>,
95100
f64: Type<R::Database> + Decode<'a, R::Database>,
96101
String: Type<R::Database> + Decode<'a, R::Database>,
@@ -120,9 +125,14 @@ impl AnyRow {
120125
k if value.is_null() => AnyValueKind::Null(k),
121126
AnyTypeInfoKind::Null => AnyValueKind::Null(AnyTypeInfoKind::Null),
122127
AnyTypeInfoKind::Bool => AnyValueKind::Bool(decode(value)?),
128+
AnyTypeInfoKind::TinyInt => AnyValueKind::TinyInt(decode(value)?),
123129
AnyTypeInfoKind::SmallInt => AnyValueKind::SmallInt(decode(value)?),
124130
AnyTypeInfoKind::Integer => AnyValueKind::Integer(decode(value)?),
125131
AnyTypeInfoKind::BigInt => AnyValueKind::BigInt(decode(value)?),
132+
AnyTypeInfoKind::UnsignedTinyInt => AnyValueKind::UnsignedTinyInt(decode(value)?),
133+
AnyTypeInfoKind::UnsignedSmallInt => AnyValueKind::UnsignedSmallInt(decode(value)?),
134+
AnyTypeInfoKind::UnsignedInteger => AnyValueKind::UnsignedInteger(decode(value)?),
135+
AnyTypeInfoKind::UnsignedBigInt => AnyValueKind::UnsignedBigInt(decode(value)?),
126136
AnyTypeInfoKind::Real => AnyValueKind::Real(decode(value)?),
127137
AnyTypeInfoKind::Double => AnyValueKind::Double(decode(value)?),
128138
AnyTypeInfoKind::Blob => AnyValueKind::Blob(decode::<_, Vec<u8>>(value)?.into()),

sqlx-core/src/any/type_info.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ impl AnyTypeInfo {
2020
pub enum AnyTypeInfoKind {
2121
Null,
2222
Bool,
23+
TinyInt,
2324
SmallInt,
2425
Integer,
2526
BigInt,
27+
UnsignedTinyInt,
28+
UnsignedSmallInt,
29+
UnsignedInteger,
30+
UnsignedBigInt,
2631
Real,
2732
Double,
2833
Text,
@@ -39,9 +44,14 @@ impl TypeInfo for AnyTypeInfo {
3944

4045
match self.kind {
4146
Bool => "BOOLEAN",
47+
TinyInt => "TINYINT",
4248
SmallInt => "SMALLINT",
4349
Integer => "INTEGER",
4450
BigInt => "BIGINT",
51+
UnsignedTinyInt => "UNSIGNED TINYINT",
52+
UnsignedSmallInt => "UNSIGNED SMALLINT",
53+
UnsignedInteger => "UNSIGNED INTEGER",
54+
UnsignedBigInt => "UNSIGNED BIGINT",
4555
Real => "REAL",
4656
Double => "DOUBLE",
4757
Text => "TEXT",
@@ -59,6 +69,16 @@ impl Display for AnyTypeInfo {
5969

6070
impl AnyTypeInfoKind {
6171
pub fn is_integer(&self) -> bool {
62-
matches!(self, SmallInt | Integer | BigInt)
72+
matches!(
73+
self,
74+
TinyInt
75+
| SmallInt
76+
| Integer
77+
| BigInt
78+
| UnsignedTinyInt
79+
| UnsignedSmallInt
80+
| UnsignedInteger
81+
| UnsignedBigInt
82+
)
6383
}
6484
}

sqlx-core/src/any/types/int.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ use crate::encode::{Encode, IsNull};
55
use crate::error::BoxDynError;
66
use crate::types::Type;
77

8+
impl Type<Any> for i8 {
9+
fn type_info() -> AnyTypeInfo {
10+
AnyTypeInfo {
11+
kind: AnyTypeInfoKind::TinyInt,
12+
}
13+
}
14+
15+
fn compatible(ty: &AnyTypeInfo) -> bool {
16+
ty.kind().is_integer()
17+
}
18+
}
19+
20+
impl Encode<'_, Any> for i8 {
21+
fn encode_by_ref(
22+
&self,
23+
buf: &mut <Any as Database>::ArgumentBuffer,
24+
) -> Result<IsNull, BoxDynError> {
25+
buf.0.push(AnyValueKind::TinyInt(*self));
26+
Ok(IsNull::No)
27+
}
28+
}
29+
30+
impl<'r> Decode<'r, Any> for i8 {
31+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
32+
value.kind.try_integer()
33+
}
34+
}
35+
836
impl Type<Any> for i16 {
937
fn type_info() -> AnyTypeInfo {
1038
AnyTypeInfo {

sqlx-core/src/any/types/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
//! | Rust type | SQL type(s) |
66
//! |---------------------------------------|------------------------------------------------------|
77
//! | `bool` | BOOLEAN |
8+
//! | `i8` | TINYINT |
89
//! | `i16` | SMALLINT |
910
//! | `i32` | INT |
1011
//! | `i64` | BIGINT |
12+
//! | `u8` | UNSIGNED TINYINT |
13+
//! | `u16` | UNSIGNED SMALLINT |
14+
//! | `u32` | UNSIGNED INT |
15+
//! | `u64` | UNSIGNED BIGINT |
1116
//! | `f32` | FLOAT |
1217
//! | `f64` | DOUBLE |
1318
//! | `&str`, [`String`] | VARCHAR, CHAR, TEXT |
@@ -22,6 +27,7 @@ mod bool;
2227
mod float;
2328
mod int;
2429
mod str;
30+
mod uint;
2531

2632
#[test]
2733
fn test_type_impls() {
@@ -40,10 +46,16 @@ fn test_type_impls() {
4046

4147
has_type::<bool>();
4248

49+
has_type::<i8>();
4350
has_type::<i16>();
4451
has_type::<i32>();
4552
has_type::<i64>();
4653

54+
has_type::<u8>();
55+
has_type::<u16>();
56+
has_type::<u32>();
57+
has_type::<u64>();
58+
4759
has_type::<f32>();
4860
has_type::<f64>();
4961

sqlx-core/src/any/types/uint.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind};
2+
use crate::database::Database;
3+
use crate::decode::Decode;
4+
use crate::encode::{Encode, IsNull};
5+
use crate::error::BoxDynError;
6+
use crate::types::Type;
7+
8+
impl Type<Any> for u8 {
9+
fn type_info() -> AnyTypeInfo {
10+
AnyTypeInfo {
11+
kind: AnyTypeInfoKind::UnsignedTinyInt,
12+
}
13+
}
14+
15+
fn compatible(ty: &AnyTypeInfo) -> bool {
16+
ty.kind().is_integer()
17+
}
18+
}
19+
20+
impl Encode<'_, Any> for u8 {
21+
fn encode_by_ref(
22+
&self,
23+
buf: &mut <Any as Database>::ArgumentBuffer,
24+
) -> Result<IsNull, BoxDynError> {
25+
buf.0.push(AnyValueKind::UnsignedTinyInt(*self));
26+
Ok(IsNull::No)
27+
}
28+
}
29+
30+
impl<'r> Decode<'r, Any> for u8 {
31+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
32+
value.kind.try_integer()
33+
}
34+
}
35+
36+
impl Type<Any> for u16 {
37+
fn type_info() -> AnyTypeInfo {
38+
AnyTypeInfo {
39+
kind: AnyTypeInfoKind::UnsignedTinyInt,
40+
}
41+
}
42+
43+
fn compatible(ty: &AnyTypeInfo) -> bool {
44+
ty.kind().is_integer()
45+
}
46+
}
47+
48+
impl Encode<'_, Any> for u16 {
49+
fn encode_by_ref(
50+
&self,
51+
buf: &mut <Any as Database>::ArgumentBuffer,
52+
) -> Result<IsNull, BoxDynError> {
53+
buf.0.push(AnyValueKind::UnsignedSmallInt(*self));
54+
Ok(IsNull::No)
55+
}
56+
}
57+
58+
impl<'r> Decode<'r, Any> for u16 {
59+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
60+
value.kind.try_integer()
61+
}
62+
}
63+
64+
impl Type<Any> for u32 {
65+
fn type_info() -> AnyTypeInfo {
66+
AnyTypeInfo {
67+
kind: AnyTypeInfoKind::UnsignedInteger,
68+
}
69+
}
70+
71+
fn compatible(ty: &AnyTypeInfo) -> bool {
72+
ty.kind().is_integer()
73+
}
74+
}
75+
76+
impl Encode<'_, Any> for u32 {
77+
fn encode_by_ref(
78+
&self,
79+
buf: &mut <Any as Database>::ArgumentBuffer,
80+
) -> Result<IsNull, BoxDynError> {
81+
buf.0.push(AnyValueKind::UnsignedInteger(*self));
82+
Ok(IsNull::No)
83+
}
84+
}
85+
86+
impl<'r> Decode<'r, Any> for u32 {
87+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
88+
value.kind.try_integer()
89+
}
90+
}
91+
92+
impl Type<Any> for u64 {
93+
fn type_info() -> AnyTypeInfo {
94+
AnyTypeInfo {
95+
kind: AnyTypeInfoKind::UnsignedBigInt,
96+
}
97+
}
98+
99+
fn compatible(ty: &AnyTypeInfo) -> bool {
100+
ty.kind().is_integer()
101+
}
102+
}
103+
104+
impl Encode<'_, Any> for u64 {
105+
fn encode_by_ref(
106+
&self,
107+
buf: &mut <Any as Database>::ArgumentBuffer,
108+
) -> Result<IsNull, BoxDynError> {
109+
buf.0.push(AnyValueKind::UnsignedBigInt(*self));
110+
Ok(IsNull::No)
111+
}
112+
}
113+
114+
impl<'r> Decode<'r, Any> for u64 {
115+
fn decode(value: <Any as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
116+
value.kind.try_integer()
117+
}
118+
}

sqlx-core/src/any/value.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ use std::sync::Arc;
1111
pub enum AnyValueKind {
1212
Null(AnyTypeInfoKind),
1313
Bool(bool),
14+
TinyInt(i8),
1415
SmallInt(i16),
1516
Integer(i32),
1617
BigInt(i64),
18+
UnsignedTinyInt(u8),
19+
UnsignedSmallInt(u16),
20+
UnsignedInteger(u32),
21+
UnsignedBigInt(u64),
1722
Real(f32),
1823
Double(f64),
1924
Text(Arc<String>),
@@ -27,9 +32,14 @@ impl AnyValueKind {
2732
kind: match self {
2833
AnyValueKind::Null(_) => AnyTypeInfoKind::Null,
2934
AnyValueKind::Bool(_) => AnyTypeInfoKind::Bool,
35+
AnyValueKind::TinyInt(_) => AnyTypeInfoKind::TinyInt,
3036
AnyValueKind::SmallInt(_) => AnyTypeInfoKind::SmallInt,
3137
AnyValueKind::Integer(_) => AnyTypeInfoKind::Integer,
3238
AnyValueKind::BigInt(_) => AnyTypeInfoKind::BigInt,
39+
AnyValueKind::UnsignedTinyInt(_) => AnyTypeInfoKind::UnsignedTinyInt,
40+
AnyValueKind::UnsignedSmallInt(_) => AnyTypeInfoKind::UnsignedSmallInt,
41+
AnyValueKind::UnsignedInteger(_) => AnyTypeInfoKind::UnsignedInteger,
42+
AnyValueKind::UnsignedBigInt(_) => AnyTypeInfoKind::UnsignedBigInt,
3343
AnyValueKind::Real(_) => AnyTypeInfoKind::Real,
3444
AnyValueKind::Double(_) => AnyTypeInfoKind::Double,
3545
AnyValueKind::Text(_) => AnyTypeInfoKind::Text,
@@ -45,15 +55,33 @@ impl AnyValueKind {
4555

4656
pub(in crate::any) fn try_integer<T>(&self) -> Result<T, BoxDynError>
4757
where
48-
T: Type<Any> + TryFrom<i16> + TryFrom<i32> + TryFrom<i64>,
58+
T: Type<Any>
59+
+ TryFrom<i8>
60+
+ TryFrom<i16>
61+
+ TryFrom<i32>
62+
+ TryFrom<i64>
63+
+ TryFrom<u8>
64+
+ TryFrom<u16>
65+
+ TryFrom<u32>
66+
+ TryFrom<u64>,
67+
BoxDynError: From<<T as TryFrom<i8>>::Error>,
4968
BoxDynError: From<<T as TryFrom<i16>>::Error>,
5069
BoxDynError: From<<T as TryFrom<i32>>::Error>,
5170
BoxDynError: From<<T as TryFrom<i64>>::Error>,
71+
BoxDynError: From<<T as TryFrom<u8>>::Error>,
72+
BoxDynError: From<<T as TryFrom<u16>>::Error>,
73+
BoxDynError: From<<T as TryFrom<u32>>::Error>,
74+
BoxDynError: From<<T as TryFrom<u64>>::Error>,
5275
{
5376
Ok(match self {
77+
AnyValueKind::TinyInt(i) => (*i).try_into()?,
5478
AnyValueKind::SmallInt(i) => (*i).try_into()?,
5579
AnyValueKind::Integer(i) => (*i).try_into()?,
5680
AnyValueKind::BigInt(i) => (*i).try_into()?,
81+
AnyValueKind::UnsignedTinyInt(i) => (*i).try_into()?,
82+
AnyValueKind::UnsignedSmallInt(i) => (*i).try_into()?,
83+
AnyValueKind::UnsignedInteger(i) => (*i).try_into()?,
84+
AnyValueKind::UnsignedBigInt(i) => (*i).try_into()?,
5785
_ => return self.unexpected(),
5886
})
5987
}

0 commit comments

Comments
 (0)