Skip to content

Commit

Permalink
describe binary and varbinary columns
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Feb 4, 2021
1 parent 311ccdb commit 94e7e96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
9 changes: 9 additions & 0 deletions odbc-api/src/handles/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,20 @@ pub enum DataType {
}

impl DataType {


/// This constructor is useful to create an instance of the enumeration using values returned by
/// ODBC Api calls like `SQLDescribeCol`, rather than just initializing a variant directly.
pub fn new(data_type: SqlDataType, column_size: usize, decimal_digits: i16) -> Self {

match data_type {
SqlDataType::UNKNOWN_TYPE => DataType::Unknown,
SqlDataType::EXT_BINARY => DataType::Binary {
length: column_size,
},
SqlDataType::EXT_VAR_BINARY => DataType::Varbinary {
length: column_size,
},
SqlDataType::CHAR => DataType::Char {
length: column_size,
},
Expand Down
59 changes: 32 additions & 27 deletions odbc-api/tests/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,44 @@ fn connect_to_movies_db() {
#[test]
fn describe_columns() {
let conn = ENV.connect_with_connection_string(MSSQL).unwrap();
let sql = "SELECT title, year FROM Movies ORDER BY year;";
setup_empty_table(
&conn,
"DescribeColumns",
&[
"VARCHAR(255) NOT NULL",
"INTEGER",
"BINARY(12)",
"VARBINARY(100)",
],
)
.unwrap();
let sql = "SELECT a,b,c,d FROM DescribeColumns ORDER BY Id;";
let cursor = conn.execute(sql, ()).unwrap().unwrap();

assert_eq!(cursor.num_result_cols().unwrap(), 2);
let mut cd = ColumnDescription::default();
cursor.describe_col(1, &mut cd).unwrap();

cursor.describe_col(1, &mut cd).unwrap();
let name = U16String::from_str("title");
assert_eq!(cursor.num_result_cols().unwrap(), 4);
let mut actual = ColumnDescription::default();

// Expectation title column
let title_desc = ColumnDescription {
name: name.into_vec(),
data_type: DataType::Varchar { length: 255 },
nullability: Nullability::NoNulls,
let desc = |name, data_type, nullability| ColumnDescription {
name: U16String::from_str(name).into_vec(),
data_type,
nullability,
};

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

cursor.describe_col(2, &mut cd).unwrap();
let name = U16String::from_str("year");
let expected = desc("b", DataType::Integer, Nullability::Nullable);
cursor.describe_col(2, &mut actual).unwrap();
assert_eq!(expected, actual);

// Expectation year column
let year_desc = ColumnDescription {
name: name.into_vec(),
data_type: DataType::Integer,
nullability: Nullability::Nullable,
};
let expected = desc("c", DataType::Binary { length: 12 }, Nullability::Nullable);
cursor.describe_col(3, &mut actual).unwrap();
assert_eq!(expected, actual);

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

#[test]
Expand Down Expand Up @@ -333,11 +341,8 @@ fn columnar_insert_varchar() {
};

// Bind buffer and insert values.
conn.execute(
"INSERT INTO ColumnarInsertVarchar (a) VALUES (?)",
&buffer,
)
.unwrap();
conn.execute("INSERT INTO ColumnarInsertVarchar (a) VALUES (?)", &buffer)
.unwrap();

// Query values and compare with expectation
let cursor = conn
Expand Down

0 comments on commit 94e7e96

Please sign in to comment.