Skip to content

Commit

Permalink
impl narrow for SQLTable
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Feb 4, 2022
1 parent 0230a56 commit be38e4c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 85 deletions.
5 changes: 2 additions & 3 deletions Changelog.md
Expand Up @@ -2,10 +2,9 @@

## (next)

Removed public higher level methods directly dealing with utf16. Similar functions using unicode
remain in the API. This has been done in order better support building against narrow ODBC function
calls on some platforms while keeping complexity in check.
Removed public higher level methods directly dealing with utf16. Similar functions using unicode remain in the API. This has been done in order better support building against narrow ODBC function calls on some platforms while keeping complexity in check.

* Removed `Connection::tables` now takes `&str` instead of `Option<&str>` arguments. Use `""` instead of none
* Removed `Environment::connect_utf16`.
* Removed `Preallocated::execute_utf16`.
* Removed `Connection::execute_utf16`.
Expand Down
25 changes: 10 additions & 15 deletions odbc-api/src/connection.rs
Expand Up @@ -8,7 +8,6 @@ use crate::{
};
use odbc_sys::HDbc;
use std::{borrow::Cow, mem::ManuallyDrop, str, thread::panicking};
use widestring::U16String;

impl<'conn> Drop for Connection<'conn> {
fn drop(&mut self) {
Expand Down Expand Up @@ -392,8 +391,8 @@ impl<'c> Connection<'c> {
/// use odbc_api::{Connection, Cursor, Error, ResultSetMetadata, buffers::TextRowSet};
///
/// fn print_all_tables(conn: &Connection<'_>) -> Result<(), Error> {
/// // Set all filters to None, to really print all tables
/// let cursor = conn.tables(None, None, None, None)?;
/// // Set all filters to an empty string, to really print all tables
/// let cursor = conn.tables("", "", "", "")?;
///
/// // The column are gonna be TABLE_CAT,TABLE_SCHEM,TABLE_NAME,TABLE_TYPE,REMARKS, but may
/// // also contain additional driver specific columns.
Expand Down Expand Up @@ -431,23 +430,19 @@ impl<'c> Connection<'c> {
/// ```
pub fn tables(
&self,
catalog_name: Option<&str>,
schema_name: Option<&str>,
table_name: Option<&str>,
table_type: Option<&str>,
catalog_name: &str,
schema_name: &str,
table_name: &str,
table_type: &str,
) -> Result<CursorImpl<StatementImpl<'_>>, Error> {
let statement = self.allocate_statement()?;

let catalog_name = catalog_name.map(U16String::from_str);
let schema_name = schema_name.map(U16String::from_str);
let table_name = table_name.map(U16String::from_str);
let table_type = table_type.map(U16String::from_str);
execute_tables(
statement,
catalog_name.as_deref(),
schema_name.as_deref(),
table_name.as_deref(),
table_type.as_deref(),
&SqlText::new(catalog_name),
&SqlText::new(schema_name),
&SqlText::new(table_name),
&SqlText::new(table_type),
)
}

Expand Down
10 changes: 4 additions & 6 deletions odbc-api/src/execute.rs
@@ -1,7 +1,5 @@
use std::intrinsics::transmute;

use widestring::U16Str;

use crate::{
borrow_mut_statement::BorrowMutStatement,
handles::{SqlText, Statement},
Expand Down Expand Up @@ -120,10 +118,10 @@ where
/// [`crate::Preallocated`].
pub fn execute_tables<S>(
mut statement: S,
catalog_name: Option<&U16Str>,
schema_name: Option<&U16Str>,
table_name: Option<&U16Str>,
column_name: Option<&U16Str>,
catalog_name: &SqlText,
schema_name: &SqlText,
table_name: &SqlText,
column_name: &SqlText,
) -> Result<CursorImpl<S>, Error>
where
S: BorrowMutStatement,
Expand Down
69 changes: 27 additions & 42 deletions odbc-api/src/handles/statement.rs
@@ -1,7 +1,7 @@
use super::{
as_handle::AsHandle,
bind::{CDataMut, DelayedInput, HasDataType},
buffer::{buf_ptr, clamp_small_int, mut_buf_ptr},
buffer::{clamp_small_int, mut_buf_ptr},
column_description::{ColumnDescription, Nullability},
data_type::DataType,
drop_handle,
Expand All @@ -12,29 +12,23 @@ use super::{
use odbc_sys::{
Desc, FreeStmtOption, HDbc, HStmt, Handle, HandleType, Len, ParamType, Pointer, SQLBindCol,
SQLBindParameter, SQLCloseCursor, SQLDescribeParam, SQLExecute, SQLFetch, SQLFreeStmt,
SQLGetData, SQLNumResultCols, SQLParamData, SQLPutData, SQLTablesW,
SqlDataType, SqlReturn, StatementAttribute, IS_POINTER,
SQLGetData, SQLNumResultCols, SQLParamData, SQLPutData, SqlDataType, SqlReturn,
StatementAttribute, IS_POINTER,
};
use std::{
ffi::c_void,
marker::PhantomData,
mem::ManuallyDrop,
ptr::{null, null_mut},
};
use widestring::U16Str;
use std::{ffi::c_void, marker::PhantomData, mem::ManuallyDrop, ptr::null_mut};

#[cfg(feature = "narrow")]
use odbc_sys::{
SQLColAttribute as sql_col_attribute, SQLColumns as sql_columns,
SQLDescribeCol as sql_describe_col, SQLExecDirect as sql_exec_direc, SQLPrepare as sql_prepare,
SQLSetStmtAttr as sql_set_stmt_attr
SQLSetStmtAttr as sql_set_stmt_attr, SQLTables as sql_tables,
};

#[cfg(not(feature = "narrow"))]
use odbc_sys::{
SQLColAttributeW as sql_col_attribute, SQLColumnsW as sql_columns,
SQLDescribeColW as sql_describe_col, SQLExecDirectW as sql_exec_direc,
SQLPrepareW as sql_prepare, SQLSetStmtAttrW as sql_set_stmt_attr
SQLPrepareW as sql_prepare, SQLSetStmtAttrW as sql_set_stmt_attr, SQLTablesW as sql_tables,
};

/// Wraps a valid (i.e. successfully allocated) ODBC statement handle.
Expand Down Expand Up @@ -167,8 +161,13 @@ pub trait Statement: AsHandle {
let value = num_rows
.map(|r| r as *mut usize as Pointer)
.unwrap_or_else(null_mut);
sql_set_stmt_attr(self.as_sys(), StatementAttribute::RowsFetchedPtr, value, IS_POINTER)
.into_sql_result("SQLSetStmtAttr")
sql_set_stmt_attr(
self.as_sys(),
StatementAttribute::RowsFetchedPtr,
value,
IS_POINTER,
)
.into_sql_result("SQLSetStmtAttr")
}

/// Fetch a column description using the column index.
Expand Down Expand Up @@ -681,38 +680,24 @@ pub trait Statement: AsHandle {
/// otherwise a NulPointer error is emitted.
fn tables(
&mut self,
catalog_name: Option<&U16Str>,
schema_name: Option<&U16Str>,
table_name: Option<&U16Str>,
table_type: Option<&U16Str>,
catalog_name: &SqlText,
schema_name: &SqlText,
table_name: &SqlText,
table_type: &SqlText,
) -> SqlResult<()> {
// Convert each filter into a pair of buffer pointer and buffer length.
let to_buf = |filter: Option<&U16Str>| {
if let Some(text) = filter {
(buf_ptr(text.as_slice()), text.len().try_into().unwrap())
} else {
(null(), 0i16)
}
};

let catalog = to_buf(catalog_name);
let schema = to_buf(schema_name);
let table = to_buf(table_name);
let type_ = to_buf(table_type);

unsafe {
SQLTablesW(
sql_tables(
self.as_sys(),
catalog.0,
catalog.1,
schema.0,
schema.1,
table.0,
table.1,
type_.0,
type_.1,
catalog_name.ptr(),
catalog_name.len_char().try_into().unwrap(),
schema_name.ptr(),
schema_name.len_char().try_into().unwrap(),
table_name.ptr(),
table_name.len_char().try_into().unwrap(),
table_type.ptr(),
table_type.len_char().try_into().unwrap(),
)
.into_sql_result("SQLTablesW")
.into_sql_result("SQLTables")
}
}

Expand Down
18 changes: 8 additions & 10 deletions odbc-api/src/preallocated.rs
@@ -1,5 +1,3 @@
use widestring::U16String;

use crate::{
execute::{execute_columns, execute_tables, execute_with_parameters},
handles::{SqlText, StatementImpl},
Expand Down Expand Up @@ -117,17 +115,17 @@ impl<'o> Preallocated<'o> {
/// all.
pub fn tables(
&mut self,
catalog_name: Option<&str>,
schema_name: Option<&str>,
table_name: Option<&str>,
table_type: Option<&str>,
catalog_name: &str,
schema_name: &str,
table_name: &str,
table_type: &str,
) -> Result<CursorImpl<&mut StatementImpl<'o>>, Error> {
execute_tables(
&mut self.statement,
catalog_name.map(U16String::from_str).as_deref(),
schema_name.map(U16String::from_str).as_deref(),
table_name.map(U16String::from_str).as_deref(),
table_type.map(U16String::from_str).as_deref(),
&SqlText::new(catalog_name),
&SqlText::new(schema_name),
&SqlText::new(table_name),
&SqlText::new(table_type),
)
}

Expand Down
8 changes: 3 additions & 5 deletions odbc-api/tests/integration.rs
Expand Up @@ -1342,7 +1342,7 @@ fn non_ascii_char(profile: &Profile) {
}

// UTF-8 local not present on CI
// #[test_case(MSSQL; "Microsoft SQL Server")]
// #[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
fn wchar(profile: &Profile) {
Expand Down Expand Up @@ -2794,7 +2794,7 @@ fn list_tables(profile: &Profile, expected: &str) {
let table_name = "ListTables";
let conn = profile.setup_empty_table(table_name, &["INTEGER"]).unwrap();

let cursor = conn.tables(None, None, Some(table_name), None).unwrap();
let cursor = conn.tables("", "", table_name, "").unwrap();
let actual = cursor_to_string(cursor).to_lowercase();
assert_eq!(expected.to_lowercase(), actual);
}
Expand All @@ -2809,9 +2809,7 @@ fn list_tables_preallocated(profile: &Profile, expected: &str) {
let conn = profile.setup_empty_table(table_name, &["INTEGER"]).unwrap();
let mut preallocated = conn.preallocate().unwrap();

let cursor = preallocated
.tables(None, None, Some(table_name), None)
.unwrap();
let cursor = preallocated.tables("", "", table_name, "").unwrap();
let actual = cursor_to_string(cursor).to_lowercase();

assert_eq!(expected.to_lowercase(), actual);
Expand Down
8 changes: 4 additions & 4 deletions odbcsv/src/main.rs
Expand Up @@ -479,10 +479,10 @@ fn tables(environment: &Environment, table_opt: &ListTablesOpt) -> Result<(), Er
let conn = open_connection(environment, connect_opts)?;

let cursor = conn.tables(
catalog.as_deref(),
schema.as_deref(),
name.as_deref(),
type_.as_deref(),
catalog.as_deref().unwrap_or_default(),
schema.as_deref().unwrap_or_default(),
name.as_deref().unwrap_or_default(),
type_.as_deref().unwrap_or_default(),
)?;

let hold_stdout = stdout();
Expand Down

0 comments on commit be38e4c

Please sign in to comment.