Skip to content

Commit

Permalink
test columnar insert varbinary
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Jan 31, 2021
1 parent 10f3a4e commit b258c69
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
4 changes: 2 additions & 2 deletions odbc-api/src/buffers/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ impl BufferKind {
DataType::BigInt => BufferKind::I64,
DataType::TinyInt => BufferKind::I8,
DataType::Bit => BufferKind::Bit,
DataType::Varbinary { length } | DataType::Binary { length } => BufferKind::Binary
{ length },
DataType::Varbinary { length }
| DataType::Binary { length } => BufferKind::Binary { length },
DataType::Varchar { length }
| DataType::WVarchar { length }
// Currently no special buffers for fixed lengths text implemented.
Expand Down
4 changes: 2 additions & 2 deletions odbc-api/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl<'c> Connection<'c> {
query: &U16Str,
params: impl ParameterCollection,
) -> Result<Option<CursorImpl<Statement>>, Error> {
let mut stmt = self.connection.allocate_statement()?;

let param_set_size = params.parameter_set_size();

if param_set_size == 0 {
Ok(None)
} else {
let mut stmt = self.connection.allocate_statement()?;

// Reset parameters so we do not dereference stale once by mistake if we call
// `exec_direct`.
stmt.reset_parameters()?;
Expand Down
4 changes: 2 additions & 2 deletions odbc-api/src/handles/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ impl<'s> Statement<'s> {
let dt = match kind {
SqlDataType::UNKNOWN_TYPE => DataType::Unknown,
SqlDataType::EXT_VAR_BINARY => DataType::Varbinary {
length: self.col_octet_length(column_number)?.try_into().unwrap()
length: self.col_octet_length(column_number)?.try_into().unwrap(),
},
SqlDataType::EXT_BINARY => DataType::Binary {
length: self.col_octet_length(column_number)?.try_into().unwrap()
length: self.col_octet_length(column_number)?.try_into().unwrap(),
},
SqlDataType::EXT_W_VARCHAR => DataType::WVarchar {
length: self.col_display_size(column_number)?.try_into().unwrap(),
Expand Down
51 changes: 49 additions & 2 deletions odbc-api/tests/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ fn columnar_fetch_varbinary() {
assert_eq!(DataType::Varbinary { length: 10 }, data_type);
let buffer_kind = BufferKind::from_data_type(data_type).unwrap();
assert_eq!(BufferKind::Binary { length: 10 }, buffer_kind);
let buffer_desc = BufferDescription { kind: buffer_kind, nullable: true };
let buffer_desc = BufferDescription {
kind: buffer_kind,
nullable: true,
};
let row_set_buffer = ColumnarRowSet::new(10, iter::once(buffer_desc));
let mut cursor = cursor.bind_buffer(row_set_buffer).unwrap();
let batch = cursor.fetch().unwrap().unwrap();
Expand Down Expand Up @@ -227,7 +230,10 @@ fn columnar_fetch_binary() {
assert_eq!(DataType::Binary { length: 5 }, data_type);
let buffer_kind = BufferKind::from_data_type(data_type).unwrap();
assert_eq!(BufferKind::Binary { length: 5 }, buffer_kind);
let buffer_desc = BufferDescription { kind: buffer_kind, nullable: true };
let buffer_desc = BufferDescription {
kind: buffer_kind,
nullable: true,
};
let row_set_buffer = ColumnarRowSet::new(10, iter::once(buffer_desc));
let mut cursor = cursor.bind_buffer(row_set_buffer).unwrap();
let batch = cursor.fetch().unwrap().unwrap();
Expand All @@ -243,6 +249,47 @@ fn columnar_fetch_binary() {
assert_eq!(None, col_it.next()); // Expecting iterator end.
}

#[test]
fn columnar_insert_varbinary() {
// Setup
let conn = ENV.connect_with_connection_string(MSSQL).unwrap();
setup_empty_table(&conn, "ColumnarInsertVarbinary", &["VARBINARY(10)"]).unwrap();

// Fill buffer with values
let desc = BufferDescription {
kind: BufferKind::Binary { length: 5 },
nullable: true,
};
let mut buffer = ColumnarRowSet::new(10, iter::once(desc));
buffer.set_num_rows(3);

if let AnyColumnViewMut::Binary(mut writer) = buffer.column_mut(0) {
writer.write(
[Some(&b"Hello"[..]), Some(&b"World"[..]), None]
.iter()
.copied(),
);
} else {
panic!("Expected binary column writer");
};

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

// Query values and compare with expectation
let cursor = conn
.execute("SELECT a FROM ColumnarInsertVarbinary ORDER BY Id", ())
.unwrap()
.unwrap();
let actual = cursor_to_string(cursor);
let expected = "48656C6C6F\n576F726C64\nNULL";
assert_eq!(expected, actual);
}

#[test]
fn all_types() {
let conn = ENV.connect_with_connection_string(MSSQL).unwrap();
Expand Down

0 comments on commit b258c69

Please sign in to comment.