Skip to content

Commit 4bc1bcd

Browse files
committed
feat: Add new ODBC test for types and improve compatibility checks
This commit introduces a new test for ODBC types, specifically for `odbc-types`, enhancing the test coverage. It also refines compatibility checks in the `Type` trait implementations for various data types, ensuring better handling of character data and improving overall robustness in ODBC interactions.
1 parent aa5d658 commit 4bc1bcd

File tree

7 files changed

+300
-78
lines changed

7 files changed

+300
-78
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ name = "odbc"
336336
path = "tests/odbc/odbc.rs"
337337
required-features = ["odbc"]
338338

339+
[[test]]
340+
name = "odbc-types"
341+
path = "tests/odbc/types.rs"
342+
required-features = ["odbc"]
343+
339344
[[test]]
340345
name = "mssql-types"
341346
path = "tests/mssql/types.rs"

sqlx-core/src/odbc/arguments.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,48 +264,64 @@ mod chrono_encode {
264264

265265
impl<'q> Encode<'q, Odbc> for NaiveDateTime {
266266
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
267-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
267+
buf.push(OdbcArgumentValue::Text(
268+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
269+
));
268270
crate::encode::IsNull::No
269271
}
270272

271273
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
272-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
274+
buf.push(OdbcArgumentValue::Text(
275+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
276+
));
273277
crate::encode::IsNull::No
274278
}
275279
}
276280

277281
impl<'q> Encode<'q, Odbc> for DateTime<Utc> {
278282
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
279-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
283+
buf.push(OdbcArgumentValue::Text(
284+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
285+
));
280286
crate::encode::IsNull::No
281287
}
282288

283289
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
284-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
290+
buf.push(OdbcArgumentValue::Text(
291+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
292+
));
285293
crate::encode::IsNull::No
286294
}
287295
}
288296

289297
impl<'q> Encode<'q, Odbc> for DateTime<FixedOffset> {
290298
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
291-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
299+
buf.push(OdbcArgumentValue::Text(
300+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
301+
));
292302
crate::encode::IsNull::No
293303
}
294304

295305
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
296-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
306+
buf.push(OdbcArgumentValue::Text(
307+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
308+
));
297309
crate::encode::IsNull::No
298310
}
299311
}
300312

301313
impl<'q> Encode<'q, Odbc> for DateTime<Local> {
302314
fn encode(self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
303-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
315+
buf.push(OdbcArgumentValue::Text(
316+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
317+
));
304318
crate::encode::IsNull::No
305319
}
306320

307321
fn encode_by_ref(&self, buf: &mut Vec<OdbcArgumentValue<'q>>) -> crate::encode::IsNull {
308-
buf.push(OdbcArgumentValue::Text(self.format("%Y-%m-%d %H:%M:%S").to_string()));
322+
buf.push(OdbcArgumentValue::Text(
323+
self.format("%Y-%m-%d %H:%M:%S").to_string(),
324+
));
309325
crate::encode::IsNull::No
310326
}
311327
}

sqlx-core/src/odbc/type.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ impl Type<Odbc> for u32 {
164164
OdbcTypeInfo::INTEGER
165165
}
166166
fn compatible(ty: &OdbcTypeInfo) -> bool {
167-
matches!(
168-
ty.data_type(),
169-
DataType::Integer | DataType::BigInt
170-
) || ty.data_type().accepts_character_data() // Allow parsing from strings
167+
matches!(ty.data_type(), DataType::Integer | DataType::BigInt)
168+
|| ty.data_type().accepts_character_data() // Allow parsing from strings
171169
}
172170
}
173171

@@ -178,7 +176,10 @@ impl Type<Odbc> for u64 {
178176
fn compatible(ty: &OdbcTypeInfo) -> bool {
179177
matches!(
180178
ty.data_type(),
181-
DataType::BigInt | DataType::Integer | DataType::Numeric { .. } | DataType::Decimal { .. }
179+
DataType::BigInt
180+
| DataType::Integer
181+
| DataType::Numeric { .. }
182+
| DataType::Decimal { .. }
182183
) || ty.data_type().accepts_character_data() // Allow parsing from strings
183184
}
184185
}
@@ -213,7 +214,8 @@ mod chrono_types {
213214
OdbcTypeInfo::TIME
214215
}
215216
fn compatible(ty: &OdbcTypeInfo) -> bool {
216-
matches!(ty.data_type(), DataType::Time { .. }) || ty.data_type().accepts_character_data()
217+
matches!(ty.data_type(), DataType::Time { .. })
218+
|| ty.data_type().accepts_character_data()
217219
}
218220
}
219221

@@ -222,7 +224,8 @@ mod chrono_types {
222224
OdbcTypeInfo::TIMESTAMP
223225
}
224226
fn compatible(ty: &OdbcTypeInfo) -> bool {
225-
matches!(ty.data_type(), DataType::Timestamp { .. }) || ty.data_type().accepts_character_data()
227+
matches!(ty.data_type(), DataType::Timestamp { .. })
228+
|| ty.data_type().accepts_character_data()
226229
}
227230
}
228231

@@ -231,7 +234,8 @@ mod chrono_types {
231234
OdbcTypeInfo::TIMESTAMP
232235
}
233236
fn compatible(ty: &OdbcTypeInfo) -> bool {
234-
matches!(ty.data_type(), DataType::Timestamp { .. }) || ty.data_type().accepts_character_data()
237+
matches!(ty.data_type(), DataType::Timestamp { .. })
238+
|| ty.data_type().accepts_character_data()
235239
}
236240
}
237241

@@ -240,7 +244,8 @@ mod chrono_types {
240244
OdbcTypeInfo::TIMESTAMP
241245
}
242246
fn compatible(ty: &OdbcTypeInfo) -> bool {
243-
matches!(ty.data_type(), DataType::Timestamp { .. }) || ty.data_type().accepts_character_data()
247+
matches!(ty.data_type(), DataType::Timestamp { .. })
248+
|| ty.data_type().accepts_character_data()
244249
}
245250
}
246251

@@ -249,7 +254,8 @@ mod chrono_types {
249254
OdbcTypeInfo::TIMESTAMP
250255
}
251256
fn compatible(ty: &OdbcTypeInfo) -> bool {
252-
matches!(ty.data_type(), DataType::Timestamp { .. }) || ty.data_type().accepts_character_data()
257+
matches!(ty.data_type(), DataType::Timestamp { .. })
258+
|| ty.data_type().accepts_character_data()
253259
}
254260
}
255261
}
@@ -281,7 +287,10 @@ mod bigdecimal_types {
281287
fn compatible(ty: &OdbcTypeInfo) -> bool {
282288
matches!(
283289
ty.data_type(),
284-
DataType::Numeric { .. } | DataType::Decimal { .. } | DataType::Double | DataType::Float { .. }
290+
DataType::Numeric { .. }
291+
| DataType::Decimal { .. }
292+
| DataType::Double
293+
| DataType::Float { .. }
285294
) || ty.data_type().accepts_character_data()
286295
}
287296
}
@@ -299,7 +308,10 @@ mod decimal_types {
299308
fn compatible(ty: &OdbcTypeInfo) -> bool {
300309
matches!(
301310
ty.data_type(),
302-
DataType::Numeric { .. } | DataType::Decimal { .. } | DataType::Double | DataType::Float { .. }
311+
DataType::Numeric { .. }
312+
| DataType::Decimal { .. }
313+
| DataType::Double
314+
| DataType::Float { .. }
303315
) || ty.data_type().accepts_character_data()
304316
}
305317
}
@@ -312,7 +324,8 @@ mod uuid_types {
312324

313325
impl Type<Odbc> for Uuid {
314326
fn type_info() -> OdbcTypeInfo {
315-
OdbcTypeInfo::varchar(Some(std::num::NonZeroUsize::new(36).unwrap())) // UUID string length
327+
OdbcTypeInfo::varchar(Some(std::num::NonZeroUsize::new(36).unwrap()))
328+
// UUID string length
316329
}
317330
fn compatible(ty: &OdbcTypeInfo) -> bool {
318331
ty.data_type().accepts_character_data() || ty.data_type().accepts_binary_data()

sqlx-core/src/odbc/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ mod decimal_decode {
306306
#[cfg(feature = "uuid")]
307307
mod uuid_decode {
308308
use super::*;
309-
use uuid::Uuid;
310309
use std::str::FromStr;
310+
use uuid::Uuid;
311311

312312
impl<'r> Decode<'r, Odbc> for Uuid {
313313
fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {

sqlx-test/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,10 @@ macro_rules! Postgres_query_for_test_prepared_type {
223223
"SELECT ({0} is not distinct from $1)::int4, {0}, $2"
224224
};
225225
}
226+
227+
#[macro_export]
228+
macro_rules! Odbc_query_for_test_prepared_type {
229+
() => {
230+
"SELECT CASE WHEN {0} = ? THEN 1 ELSE 0 END, {0}, ?"
231+
};
232+
}

0 commit comments

Comments
 (0)