Skip to content

Commit

Permalink
introduce ColumnDescription::new
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed May 9, 2022
1 parent e12c258 commit e1930c1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
81 changes: 40 additions & 41 deletions odbc-api/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
use std::iter;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use odbc_api::buffers::{try_buffer_from_description, buffer_from_description, BufferDescription};

fn falliable_buffer_allocation(capacity: usize, max_str_len: usize) {
let description = BufferDescription {
kind: odbc_api::buffers::BufferKind::Text { max_str_len },
nullable: true,
};
try_buffer_from_description(capacity, iter::once(description)).unwrap();
}

fn infalliable_buffer_allocation(capacity: usize, max_str_len: usize) {
let description = BufferDescription {
kind: odbc_api::buffers::BufferKind::Text { max_str_len },
nullable: true,
};
buffer_from_description(capacity, iter::once(description));
}


fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fallibale buffer allocation", |b| {
b.iter(|| {
let capacity = 1000;
let max_str_len = 65536;
falliable_buffer_allocation(black_box(capacity), black_box(max_str_len))
})
});
c.bench_function("infallibale buffer allocation", |b| {
b.iter(|| {
let capacity = 1000;
let max_str_len = 65536;
infalliable_buffer_allocation(black_box(capacity), black_box(max_str_len))
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
use std::iter;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use odbc_api::buffers::{buffer_from_description, try_buffer_from_description, BufferDescription};

fn falliable_buffer_allocation(capacity: usize, max_str_len: usize) {
let description = BufferDescription {
kind: odbc_api::buffers::BufferKind::Text { max_str_len },
nullable: true,
};
try_buffer_from_description(capacity, iter::once(description)).unwrap();
}

fn infalliable_buffer_allocation(capacity: usize, max_str_len: usize) {
let description = BufferDescription {
kind: odbc_api::buffers::BufferKind::Text { max_str_len },
nullable: true,
};
buffer_from_description(capacity, iter::once(description));
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fallibale buffer allocation", |b| {
b.iter(|| {
let capacity = 1000;
let max_str_len = 65536;
falliable_buffer_allocation(black_box(capacity), black_box(max_str_len))
})
});
c.bench_function("infallibale buffer allocation", |b| {
b.iter(|| {
let capacity = 1000;
let max_str_len = 65536;
infalliable_buffer_allocation(black_box(capacity), black_box(max_str_len))
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
22 changes: 22 additions & 0 deletions odbc-api/src/handles/column_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ pub struct ColumnDescription {
}

impl ColumnDescription {
/// In production, an 'empty' [`ColumnDescription`] is expected to be constructed via the
/// [`Default`] trait. It is then filled using [`crate::ResultSetMetadata::describe_col`]. When
/// writing test cases however it might be desirable to directly instantiate a
/// [`ColumnDescription`]. This constructor enabels you to do that, without caring which type
/// `SqlChar` resolves to.
pub fn new(name: &str, data_type: DataType, nullability: Nullability) -> Self {
#[cfg(feature = "narrow")]
pub fn utf8_to_vec_char(text: &str) -> Vec<u8> {
text.to_owned().into_bytes()
}
#[cfg(not(feature = "narrow"))]
pub fn utf8_to_vec_char(text: &str) -> Vec<u16> {
use widestring::U16String;
U16String::from_str(text).into_vec()
}
Self {
name: utf8_to_vec_char(name),
data_type,
nullability,
}
}

/// Converts the internal UTF16 representation of the column name into UTF8 and returns the
/// result as a `String`.
pub fn name_to_string(&self) -> Result<String, DecodingError> {
Expand Down
37 changes: 11 additions & 26 deletions odbc-api/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,6 @@ fn connect_to_db(profile: &Profile) {

#[test]
fn describe_columns() {
#[cfg(feature = "narrow")]
pub fn utf8_to_vec_char(text: &str) -> Vec<u8> {
text.to_owned().into_bytes()
}
#[cfg(not(feature = "narrow"))]
pub fn utf8_to_vec_char(text: &str) -> Vec<u16> {
U16String::from_str(text).into_vec()
}

let conn = MSSQL.connection().unwrap();
setup_empty_table(
&conn,
Expand All @@ -168,38 +159,32 @@ fn describe_columns() {
assert_eq!(cursor.num_result_cols().unwrap(), 11);
let mut actual = ColumnDescription::default();

let desc = |name, data_type, nullability| ColumnDescription {
name: utf8_to_vec_char(name),
data_type,
nullability,
};

let kind = DataType::Varchar { length: 255 };
let expected = desc("a", kind, Nullability::NoNulls);
let expected = ColumnDescription::new("a", kind, Nullability::NoNulls);
cursor.describe_col(1, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(1).unwrap());

let kind = DataType::Integer;
let expected = desc("b", kind, Nullability::Nullable);
let expected = ColumnDescription::new("b", kind, Nullability::Nullable);
cursor.describe_col(2, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(2).unwrap());

let kind = DataType::Binary { length: 12 };
let expected = desc("c", kind, Nullability::Nullable);
let expected = ColumnDescription::new("c", kind, Nullability::Nullable);
cursor.describe_col(3, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(3).unwrap());

let kind = DataType::Varbinary { length: 100 };
let expected = desc("d", kind, Nullability::Nullable);
let expected = ColumnDescription::new("d", kind, Nullability::Nullable);
cursor.describe_col(4, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(4).unwrap());

let kind = DataType::WChar { length: 10 };
let expected = desc("e", kind, Nullability::Nullable);
let expected = ColumnDescription::new("e", kind, Nullability::Nullable);
cursor.describe_col(5, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(5).unwrap());
Expand All @@ -208,13 +193,13 @@ fn describe_columns() {
precision: 3,
scale: 2,
};
let expected = desc("f", kind, Nullability::Nullable);
let expected = ColumnDescription::new("f", kind, Nullability::Nullable);
cursor.describe_col(6, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(6).unwrap());

let kind = DataType::Timestamp { precision: 7 };
let expected = desc("g", kind, Nullability::Nullable);
let expected = ColumnDescription::new("g", kind, Nullability::Nullable);
cursor.describe_col(7, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(7).unwrap());
Expand All @@ -224,25 +209,25 @@ fn describe_columns() {
column_size: 16,
decimal_digits: 7,
};
let expected = desc("h", kind, Nullability::Nullable);
let expected = ColumnDescription::new("h", kind, Nullability::Nullable);
cursor.describe_col(8, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(8).unwrap());

let kind = DataType::LongVarchar { length: 2147483647 };
let expected = desc("i", kind, Nullability::Nullable);
let expected = ColumnDescription::new("i", kind, Nullability::Nullable);
cursor.describe_col(9, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(9).unwrap());

let kind = DataType::LongVarbinary { length: 2147483647 };
let expected = desc("j", kind, Nullability::Nullable);
let expected = ColumnDescription::new("j", kind, Nullability::Nullable);
cursor.describe_col(10, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(10).unwrap());

let kind = DataType::Float { precision: 53 };
let expected = desc("k", kind, Nullability::Nullable);
let expected = ColumnDescription::new("k", kind, Nullability::Nullable);
cursor.describe_col(11, &mut actual).unwrap();
assert_eq!(expected, actual);
assert_eq!(kind, cursor.col_data_type(11).unwrap());
Expand Down

0 comments on commit e1930c1

Please sign in to comment.