Skip to content

Commit 1cf7da5

Browse files
committed
refactor(odbc): simplify OdbcValueVec usage and improve type handling
This commit refactors the OdbcBridge to streamline the usage of OdbcValueVec by removing redundant crate paths and enhancing type handling. The changes improve code clarity and maintainability, particularly in the initialization and data retrieval processes for various data types.
1 parent 94c2628 commit 1cf7da5

File tree

2 files changed

+59
-66
lines changed

2 files changed

+59
-66
lines changed

sqlx-core/src/odbc/connection/odbc_bridge.rs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use super::decode_column_name;
22
use crate::error::Error;
3+
use crate::odbc::OdbcValueVec;
34
use crate::odbc::{
45
connection::MaybePrepared, ColumnData, OdbcArgumentValue, OdbcArguments, OdbcBatch,
56
OdbcBufferSettings, OdbcColumn, OdbcQueryResult, OdbcRow, OdbcTypeInfo,
67
};
78
use either::Either;
89
use flume::{SendError, Sender};
910
use odbc_api::buffers::{AnySlice, BufferDesc, ColumnarAnyBuffer};
10-
use odbc_api::handles::{AsStatementRef, Nullability, Statement};
11+
use odbc_api::handles::{AsStatementRef, CDataMut, Nullability, Statement};
12+
use odbc_api::parameter::CElement;
1113
use odbc_api::{Cursor, IntoParameter, ResultSetMetadata};
1214
use std::sync::Arc;
1315

@@ -292,7 +294,7 @@ where
292294

293295
fn build_column_data_from_values(
294296
columns: &[OdbcColumn],
295-
value_vecs: Vec<crate::odbc::OdbcValueVec>,
297+
value_vecs: Vec<OdbcValueVec>,
296298
nulls_vecs: Vec<Vec<bool>>,
297299
) -> Vec<Arc<ColumnData>> {
298300
value_vecs
@@ -405,34 +407,28 @@ where
405407

406408
let col_arc: Arc<[OdbcColumn]> = Arc::from(columns.clone());
407409

408-
fn init_value_vec(dt: DataType, capacity: usize) -> crate::odbc::OdbcValueVec {
410+
fn init_value_vec(dt: DataType, capacity: usize) -> OdbcValueVec {
409411
match dt {
410-
DataType::TinyInt => crate::odbc::OdbcValueVec::TinyInt(Vec::with_capacity(capacity)),
411-
DataType::SmallInt => crate::odbc::OdbcValueVec::SmallInt(Vec::with_capacity(capacity)),
412-
DataType::Integer => crate::odbc::OdbcValueVec::Integer(Vec::with_capacity(capacity)),
413-
DataType::BigInt => crate::odbc::OdbcValueVec::BigInt(Vec::with_capacity(capacity)),
414-
DataType::Real => crate::odbc::OdbcValueVec::Real(Vec::with_capacity(capacity)),
412+
DataType::TinyInt => OdbcValueVec::TinyInt(Vec::with_capacity(capacity)),
413+
DataType::SmallInt => OdbcValueVec::SmallInt(Vec::with_capacity(capacity)),
414+
DataType::Integer => OdbcValueVec::Integer(Vec::with_capacity(capacity)),
415+
DataType::BigInt => OdbcValueVec::BigInt(Vec::with_capacity(capacity)),
416+
DataType::Real => OdbcValueVec::Real(Vec::with_capacity(capacity)),
415417
DataType::Float { .. } | DataType::Double => {
416-
crate::odbc::OdbcValueVec::Double(Vec::with_capacity(capacity))
417-
}
418-
DataType::Bit => crate::odbc::OdbcValueVec::Bit(Vec::with_capacity(capacity)),
419-
DataType::Date => crate::odbc::OdbcValueVec::Date(Vec::with_capacity(capacity)),
420-
DataType::Time { .. } => crate::odbc::OdbcValueVec::Time(Vec::with_capacity(capacity)),
421-
DataType::Timestamp { .. } => {
422-
crate::odbc::OdbcValueVec::Timestamp(Vec::with_capacity(capacity))
418+
OdbcValueVec::Double(Vec::with_capacity(capacity))
423419
}
420+
DataType::Bit => OdbcValueVec::Bit(Vec::with_capacity(capacity)),
421+
DataType::Date => OdbcValueVec::Date(Vec::with_capacity(capacity)),
422+
DataType::Time { .. } => OdbcValueVec::Time(Vec::with_capacity(capacity)),
423+
DataType::Timestamp { .. } => OdbcValueVec::Timestamp(Vec::with_capacity(capacity)),
424424
DataType::Binary { .. }
425425
| DataType::Varbinary { .. }
426-
| DataType::LongVarbinary { .. } => {
427-
crate::odbc::OdbcValueVec::Binary(Vec::with_capacity(capacity))
428-
}
429-
_ => crate::odbc::OdbcValueVec::Text(Vec::with_capacity(capacity)),
426+
| DataType::LongVarbinary { .. } => OdbcValueVec::Binary(Vec::with_capacity(capacity)),
427+
_ => OdbcValueVec::Text(Vec::with_capacity(capacity)),
430428
}
431429
}
432430

433-
fn push_get_data<
434-
T: Default + Copy + odbc_api::parameter::CElement + odbc_api::handles::CDataMut,
435-
>(
431+
fn push_get_data<T: Default + Copy + CElement + CDataMut>(
436432
cursor_row: &mut odbc_api::CursorRow<'_>,
437433
col_index: u16,
438434
vec: &mut Vec<T>,
@@ -443,9 +439,7 @@ where
443439
vec.push(tmp);
444440
}
445441

446-
fn push_get_data_with_default<
447-
T: Copy + odbc_api::parameter::CElement + odbc_api::handles::CDataMut,
448-
>(
442+
fn push_get_data_with_default<T: Copy + CElement + CDataMut>(
449443
cursor_row: &mut odbc_api::CursorRow<'_>,
450444
col_index: u16,
451445
vec: &mut Vec<T>,
@@ -504,34 +498,30 @@ where
504498
fn push_from_cursor_row(
505499
cursor_row: &mut odbc_api::CursorRow<'_>,
506500
col_index: u16,
507-
values: &mut crate::odbc::OdbcValueVec,
501+
values: &mut OdbcValueVec,
508502
nulls: &mut Vec<bool>,
509503
) {
510504
match values {
511-
crate::odbc::OdbcValueVec::TinyInt(v) => push_get_data(cursor_row, col_index, v, nulls),
512-
crate::odbc::OdbcValueVec::SmallInt(v) => {
513-
push_get_data(cursor_row, col_index, v, nulls)
514-
}
515-
crate::odbc::OdbcValueVec::Integer(v) => push_get_data(cursor_row, col_index, v, nulls),
516-
crate::odbc::OdbcValueVec::BigInt(v) => push_get_data(cursor_row, col_index, v, nulls),
517-
crate::odbc::OdbcValueVec::Real(v) => push_get_data(cursor_row, col_index, v, nulls),
518-
crate::odbc::OdbcValueVec::Double(v) => push_get_data(cursor_row, col_index, v, nulls),
519-
crate::odbc::OdbcValueVec::Bit(v) => {
505+
OdbcValueVec::TinyInt(v) => push_get_data(cursor_row, col_index, v, nulls),
506+
OdbcValueVec::SmallInt(v) => push_get_data(cursor_row, col_index, v, nulls),
507+
OdbcValueVec::Integer(v) => push_get_data(cursor_row, col_index, v, nulls),
508+
OdbcValueVec::BigInt(v) => push_get_data(cursor_row, col_index, v, nulls),
509+
OdbcValueVec::Real(v) => push_get_data(cursor_row, col_index, v, nulls),
510+
OdbcValueVec::Double(v) => push_get_data(cursor_row, col_index, v, nulls),
511+
OdbcValueVec::Bit(v) => {
520512
push_get_data_with_default(cursor_row, col_index, v, nulls, odbc_api::Bit(0))
521513
}
522-
crate::odbc::OdbcValueVec::Date(v) => push_get_data(cursor_row, col_index, v, nulls),
523-
crate::odbc::OdbcValueVec::Time(v) => push_get_data(cursor_row, col_index, v, nulls),
524-
crate::odbc::OdbcValueVec::Timestamp(v) => {
525-
push_get_data(cursor_row, col_index, v, nulls)
526-
}
527-
crate::odbc::OdbcValueVec::Binary(v) => push_binary(cursor_row, col_index, v, nulls),
528-
crate::odbc::OdbcValueVec::Text(v) => push_text(cursor_row, col_index, v, nulls),
514+
OdbcValueVec::Date(v) => push_get_data(cursor_row, col_index, v, nulls),
515+
OdbcValueVec::Time(v) => push_get_data(cursor_row, col_index, v, nulls),
516+
OdbcValueVec::Timestamp(v) => push_get_data(cursor_row, col_index, v, nulls),
517+
OdbcValueVec::Binary(v) => push_binary(cursor_row, col_index, v, nulls),
518+
OdbcValueVec::Text(v) => push_text(cursor_row, col_index, v, nulls),
529519
}
530520
}
531521

532522
loop {
533523
// Initialize per-column containers for this batch
534-
let mut value_vecs: Vec<crate::odbc::OdbcValueVec> = columns
524+
let mut value_vecs: Vec<OdbcValueVec> = columns
535525
.iter()
536526
.map(|c| init_value_vec(c.type_info.data_type(), batch_size))
537527
.collect();

tests/odbc/odbc.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,30 +1043,33 @@ async fn it_works_with_unbuffered_mode() -> anyhow::Result<()> {
10431043
// Create connection with unbuffered settings
10441044
let database_url = std::env::var("DATABASE_URL").unwrap();
10451045
let mut opts = OdbcConnectOptions::from_str(&database_url)?;
1046-
opts.buffer_settings(OdbcBufferSettings {
1047-
batch_size: 128, // batch_size is ignored in unbuffered mode
1048-
max_column_size: None, // Enable unbuffered mode
1049-
});
10501046

1051-
let mut conn = OdbcConnection::connect_with(&opts).await?;
1052-
let count = 450;
1053-
let select = (0..count)
1054-
.map(|i| format!("SELECT {i} AS n"))
1055-
.collect::<Vec<_>>()
1056-
.join(" UNION ALL ");
1057-
1058-
// Test that unbuffered mode works correctly
1059-
let s = conn
1060-
.prepare(&select)
1061-
.await?
1062-
.query()
1063-
.fetch_all(&mut conn)
1064-
.await?;
1065-
assert_eq!(s.len(), count);
1066-
for i in 0..count {
1067-
let row = s.get(i).expect("row expected");
1068-
let as_i64 = row.get::<'_, i64, _>(0);
1069-
assert_eq!(as_i64, i64::try_from(i).unwrap());
1047+
for batch_size in [1, 100, 10000] {
1048+
opts.buffer_settings(OdbcBufferSettings {
1049+
batch_size,
1050+
max_column_size: None,
1051+
});
1052+
1053+
let mut conn = OdbcConnection::connect_with(&opts).await?;
1054+
let count = 450;
1055+
let select = (0..count)
1056+
.map(|i| format!("SELECT {i} AS n"))
1057+
.collect::<Vec<_>>()
1058+
.join(" UNION ALL ");
1059+
1060+
// Test that unbuffered mode works correctly
1061+
let s = conn
1062+
.prepare(&select)
1063+
.await?
1064+
.query()
1065+
.fetch_all(&mut conn)
1066+
.await?;
1067+
assert_eq!(s.len(), count);
1068+
for i in 0..count {
1069+
let row = s.get(i).expect("row expected");
1070+
let as_i64 = row.get::<'_, i64, _>(0);
1071+
assert_eq!(as_i64, i64::try_from(i).unwrap());
1072+
}
10701073
}
10711074
Ok(())
10721075
}

0 commit comments

Comments
 (0)