Skip to content

Commit 718d289

Browse files
committed
odbc: share column metadata extraction
1 parent abe5801 commit 718d289

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

‎sqlx-core/src/odbc/connection/mod.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ fn collect_columns(prepared: &mut PreparedStatement) -> Result<Vec<OdbcColumn>,
2828
let count = prepared.num_result_cols()?;
2929
let mut columns = Vec::with_capacity(count as usize);
3030
for i in 1..=count {
31-
columns.push(create_column(prepared, i as u16)?);
31+
columns.push(describe_column(prepared, i as u16)?);
3232
}
3333
Ok(columns)
3434
}
3535

36-
fn create_column(stmt: &mut PreparedStatement, index: u16) -> Result<OdbcColumn, Error> {
36+
pub(super) fn describe_column<S>(stmt: &mut S, index: u16) -> Result<OdbcColumn, Error>
37+
where
38+
S: ResultSetMetadata,
39+
{
3740
let mut cd = odbc_api::ColumnDescription::default();
3841
stmt.describe_col(index, &mut cd)?;
3942

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::decode_column_name;
1+
use super::describe_column;
22
use crate::error::Error;
33
use crate::odbc::OdbcValueVec;
44
use crate::odbc::{
@@ -29,7 +29,7 @@ fn build_bindings<C: Cursor>(
2929
let column_count = cursor.num_result_cols().unwrap_or(0);
3030
let mut bindings = Vec::with_capacity(column_count as usize);
3131
for index in 1..=column_count {
32-
let column = create_column(cursor, index as u16);
32+
let column = describe_column(cursor, index as u16)?;
3333
let nullable = cursor
3434
.col_nullability(index as u16)
3535
.unwrap_or(Nullability::Unknown)
@@ -249,20 +249,6 @@ fn send_row(tx: &ExecuteSender, row: OdbcRow) -> Result<(), SendError<ExecuteRes
249249
tx.send(Ok(Either::Right(row)))
250250
}
251251

252-
fn create_column<C>(cursor: &mut C, index: u16) -> OdbcColumn
253-
where
254-
C: ResultSetMetadata,
255-
{
256-
let mut cd = odbc_api::ColumnDescription::default();
257-
let _ = cursor.describe_col(index, &mut cd);
258-
259-
OdbcColumn {
260-
name: decode_column_name(cd.name, index),
261-
type_info: OdbcTypeInfo::new(cd.data_type),
262-
ordinal: usize::from(index.checked_sub(1).unwrap()),
263-
}
264-
}
265-
266252
fn map_buffer_desc(
267253
type_info: &OdbcTypeInfo,
268254
nullable: bool,
@@ -320,17 +306,18 @@ fn create_column_data(slice: AnySlice<'_>, column: &OdbcColumn) -> Arc<ColumnDat
320306
})
321307
}
322308

323-
fn build_columns_from_cursor<C>(cursor: &mut C) -> Vec<OdbcColumn>
309+
fn build_columns_from_cursor<C>(cursor: &mut C) -> Result<Vec<OdbcColumn>, Error>
324310
where
325311
C: ResultSetMetadata,
326312
{
327-
let column_count = cursor.num_result_cols().expect("no column count found");
328-
let column_count = u16::try_from(column_count).expect("invalid column count");
313+
let column_count = cursor.num_result_cols()?;
314+
let column_count = u16::try_from(column_count)
315+
.map_err(|_| Error::Protocol(format!("ODBC column count {column_count} exceeds u16")))?;
329316
let mut columns = Vec::with_capacity(usize::from(column_count));
330317
for index in 1..=column_count {
331-
columns.push(create_column(cursor, index));
318+
columns.push(describe_column(cursor, index)?);
332319
}
333-
columns
320+
Ok(columns)
334321
}
335322

336323
fn build_column_data_from_values(
@@ -445,7 +432,7 @@ where
445432

446433
let mut receiver_open = true;
447434

448-
let columns = build_columns_from_cursor(&mut cursor);
435+
let columns = build_columns_from_cursor(&mut cursor)?;
449436
let column_count = columns.len();
450437

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

0 commit comments

Comments
 (0)