Skip to content

Commit 45d656a

Browse files
committed
feat(core): add column types for entity fields
* Add `SqlType` and `ColumnType` with the supported field types * Add `QueryParameter` for `Option<i16>` and `Option<bool>` * Test round-trips of every type on PostgreSQL, SQL Server and MySQL, `Option` fields through `CanyonMapper`
1 parent ae128f9 commit 45d656a

8 files changed

Lines changed: 755 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Year format is defined as: `YYYY-m-d`
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
- `ColumnType` and `SqlType` in `canyon_sql::core`: the column type each Rust field type maps to
15+
- `QueryParameter` for `Option<i16>` and `Option<bool>`
16+
1217
## [0.5.0 - 2023 - 12 - 10]
1318

1419
### Feature

‎canyon_core/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ bb8 = "0.9.1"
3333
postgres = ["tokio-postgres"]
3434
mssql = ["tiberius", "async-std"]
3535
mysql = ["mysql_async", "mysql_common"]
36+
37+
[dev-dependencies]
38+
serde_json = "1"

‎canyon_core/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ pub mod row;
4141
pub mod rows;
4242
#[cfg(any(feature = "postgres", feature = "mysql", feature = "mssql"))]
4343
pub mod transaction;
44+
#[cfg(any(feature = "postgres", feature = "mysql", feature = "mssql"))]
45+
pub mod types;

‎canyon_core/src/query/parameters.rs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ impl QueryParameter for bool {
5252
}
5353
}
5454

55+
impl QueryParameter for Option<bool> {
56+
#[cfg(feature = "postgres")]
57+
fn as_postgres_param(&self) -> &(dyn ToSql + Sync) {
58+
self
59+
}
60+
#[cfg(feature = "mssql")]
61+
fn as_sqlserver_param(&self) -> ColumnData<'_> {
62+
ColumnData::Bit(*self)
63+
}
64+
#[cfg(feature = "mysql")]
65+
fn as_mysql_param(&self) -> Value {
66+
self.to_value()
67+
}
68+
}
69+
5570
impl QueryParameter for i16 {
5671
#[cfg(feature = "postgres")]
5772
fn as_postgres_param(&self) -> &(dyn ToSql + Sync) {
@@ -67,6 +82,21 @@ impl QueryParameter for i16 {
6782
}
6883
}
6984

85+
impl QueryParameter for Option<i16> {
86+
#[cfg(feature = "postgres")]
87+
fn as_postgres_param(&self) -> &(dyn ToSql + Sync) {
88+
self
89+
}
90+
#[cfg(feature = "mssql")]
91+
fn as_sqlserver_param(&self) -> ColumnData<'_> {
92+
ColumnData::I16(*self)
93+
}
94+
#[cfg(feature = "mysql")]
95+
fn as_mysql_param(&self) -> Value {
96+
self.to_value()
97+
}
98+
}
99+
70100
impl QueryParameter for Option<&i16> {
71101
#[cfg(feature = "postgres")]
72102
fn as_postgres_param(&self) -> &(dyn ToSql + Sync) {
@@ -528,6 +558,26 @@ mod mssql_tests {
528558
assert!(matches!(none.as_sqlserver_param(), ColumnData::I16(None)));
529559
}
530560

561+
#[test]
562+
fn owned_optional_i16_and_bool_preserve_some_and_none() {
563+
assert!(matches!(
564+
Some(42_i16).as_sqlserver_param(),
565+
ColumnData::I16(Some(42))
566+
));
567+
assert!(matches!(
568+
None::<i16>.as_sqlserver_param(),
569+
ColumnData::I16(None)
570+
));
571+
assert!(matches!(
572+
Some(true).as_sqlserver_param(),
573+
ColumnData::Bit(Some(true))
574+
));
575+
assert!(matches!(
576+
None::<bool>.as_sqlserver_param(),
577+
ColumnData::Bit(None)
578+
));
579+
}
580+
531581
#[test]
532582
fn u32_parameter_uses_lossless_sql_server_bigint() {
533583
let first_value_beyond_i32 = i32::MAX as u32 + 1;
@@ -590,6 +640,30 @@ mod postgres_tests {
590640
assert!(matches!(none_nullability, IsNull::Yes));
591641
assert!(none_bytes.is_empty());
592642
}
643+
644+
// Writes `param` as a value of `ty` and returns the bytes, `None` for NULL
645+
fn encode(param: &dyn QueryParameter, ty: &Type) -> Option<Vec<u8>> {
646+
let mut bytes = BytesMut::new();
647+
match param
648+
.as_postgres_param()
649+
.to_sql_checked(ty, &mut bytes)
650+
.unwrap()
651+
{
652+
IsNull::No => Some(bytes.to_vec()),
653+
IsNull::Yes => None,
654+
}
655+
}
656+
657+
#[test]
658+
fn owned_optional_i16_and_bool_preserve_some_and_none() {
659+
assert_eq!(
660+
encode(&Some(42_i16), &Type::INT2),
661+
Some(42_i16.to_be_bytes().to_vec())
662+
);
663+
assert_eq!(encode(&None::<i16>, &Type::INT2), None);
664+
assert_eq!(encode(&Some(true), &Type::BOOL), Some(vec![1]));
665+
assert_eq!(encode(&None::<bool>, &Type::BOOL), None);
666+
}
593667
}
594668

595669
#[cfg(all(test, feature = "mysql"))]
@@ -608,6 +682,14 @@ mod mysql_tests {
608682
assert_eq!(none.as_mysql_param(), Value::NULL);
609683
}
610684

685+
#[test]
686+
fn owned_optional_i16_and_bool_preserve_some_and_none() {
687+
assert_eq!(Some(42_i16).as_mysql_param(), Value::Int(42));
688+
assert_eq!(None::<i16>.as_mysql_param(), Value::NULL);
689+
assert_eq!(Some(true).as_mysql_param(), Value::Int(1));
690+
assert_eq!(None::<bool>.as_mysql_param(), Value::NULL);
691+
}
692+
611693
#[test]
612694
fn mysql_datetime_parameters_are_normalized_to_utc() {
613695
let fixed_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap();

0 commit comments

Comments
 (0)