Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlx-odbc"
version = "0.0.3"
version = "0.0.5"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/sqlpage/sqlx-odbc"
Expand Down Expand Up @@ -34,7 +34,7 @@ odbc-api = "28"
rust_decimal = { version = "1.36.0", default-features = false, features = ["std"], optional = true }
serde = { version = "1.0.219", features = ["derive", "rc"], optional = true }
serde_json = { version = "1.0.142", features = ["raw_value"], optional = true }
sqlx-core = { version = "=0.9.0", default-features = false, features = ["any"] }
sqlx-core = { version = "=0.9.1", default-features = false, features = ["any", "offline"] }
thiserror = "2.0.17"
time = { version = "0.3.47", features = ["formatting", "parsing", "macros"], optional = true }
url = "2.5.7"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This crate is a standalone top-level crate. It depends only on crates published
to crates.io and is not wired into the `sqlx` facade crate, so examples use
`sqlx-core` directly.

You can depend on this crate alongside `sqlx` with its default features.
SQLx's compile-time query macros still cannot target ODBC.

## Minimal Query

```toml
Expand Down Expand Up @@ -43,6 +46,11 @@ SQLx `query()` uses the driver's prepared-statement parameter count to check
binds. Use `sqlx_core::raw_sql::raw_sql()` for SQL that needs direct execution
without bind parameters.

Each open connection uses a worker thread for blocking ODBC calls. Dropping a
query stream stops delivery and lets the worker release its cursor before
running the next operation. A native ODBC call already in progress can still
run to completion.

## Any Driver

Install this crate explicitly before opening ODBC URLs through SQLx
Expand Down
64 changes: 37 additions & 27 deletions src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use sqlx_core::any::{
use sqlx_core::column::Column;
use sqlx_core::connection::{ConnectOptions, Connection};
use sqlx_core::database::Database;
use sqlx_core::executor::Executor;
use sqlx_core::ext::ustr::UStr;
use sqlx_core::row::Row;
use sqlx_core::sql_str::SqlStr;
Expand Down Expand Up @@ -63,6 +64,14 @@ impl AnyConnectionBackend for OdbcConnection {
OdbcTransactionManager::get_transaction_depth(self)
}

fn cached_statements_size(&self) -> usize {
Connection::cached_statements_size(self)
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> {
Connection::clear_cached_statements(self).boxed()
}

fn shrink_buffers(&mut self) {
Connection::shrink_buffers(self);
}
Expand All @@ -85,20 +94,15 @@ impl AnyConnectionBackend for OdbcConnection {
Ok(arguments) => arguments,
Err(error) => return stream::once(future::ready(Err(error))).boxed(),
};
let rx = self.execute_receiver(query, persistent, arguments);

stream::unfold(rx, |rx| async move {
let item = rx.recv_async().await.ok()?;
Some((item, rx))
})
.map(|item| match item? {
Either::Left(result) => Ok(Either::Left(map_result(result))),
Either::Right(row) => {
let column_names = column_names(row.columns());
AnyRow::map_from(&row, column_names).map(Either::Right)
}
})
.boxed()
self.execute_stream(query, persistent, arguments)
.map(|item| match item? {
Either::Left(result) => Ok(Either::Left(map_result(result))),
Either::Right(row) => {
let column_names = column_names(row.columns());
AnyRow::map_from(&row, column_names).map(Either::Right)
}
})
.boxed()
}

fn fetch_optional(
Expand All @@ -110,18 +114,13 @@ impl AnyConnectionBackend for OdbcConnection {
let arguments = arguments.map(map_arguments).transpose();

Box::pin(async move {
let arguments = arguments?;
let rx = self.execute_receiver(query, persistent, arguments);
while let Ok(item) = rx.recv_async().await {
match item? {
Either::Left(_) => {}
Either::Right(row) => {
let column_names = column_names(row.columns());
return AnyRow::map_from(&row, column_names).map(Some);
}
}
}
Ok(None)
self.fetch_first_row(query, persistent, arguments?)
.await?
.map(|row| {
let column_names = column_names(row.columns());
AnyRow::map_from(&row, column_names)
})
.transpose()
})
}

Expand All @@ -131,11 +130,22 @@ impl AnyConnectionBackend for OdbcConnection {
_parameters: &[AnyTypeInfo],
) -> BoxFuture<'c, sqlx_core::Result<AnyStatement>> {
Box::pin(async move {
let statement = self.prepare_blocking(sql)?;
let statement = Executor::prepare_with(self, sql, &[]).await?;
let column_names = column_names(statement.columns());
AnyStatement::try_from_statement(statement, column_names)
})
}

fn describe(
&mut self,
sql: SqlStr,
) -> BoxFuture<'_, sqlx_core::Result<sqlx_core::describe::Describe<sqlx_core::any::Any>>> {
Box::pin(async move {
sqlx_core::executor::Executor::describe(self, sql)
.await?
.try_into_any()
})
}
}

impl<'a> TryFrom<&'a AnyConnectOptions> for OdbcConnectOptions {
Expand Down
Loading