Skip to content
sqlpagePublic

About

rust sqlx driver for ODBC databases

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

83 Commits

Folders and files

Repository files navigation

sqlx-odbc

Independent ODBC driver crate for SQLx.

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

[dependencies]
sqlx-odbc = "0.0.3"
sqlx-core = "=0.9.0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
use sqlx_core::connection::Connection;
use sqlx_core::row::Row;
use sqlx_odbc::OdbcConnection;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let mut conn = OdbcConnection::connect("Driver=DuckDB;Database=/tmp/example.duckdb").await?;

    let row = sqlx_core::query::query("SELECT 1")
        .fetch_one(&mut conn)
        .await?;

    let value: i32 = row.try_get(0)?;
    println!("{value}");

    conn.close().await?;
    Ok(())
}

OdbcConnection::connect() accepts a standard ODBC connection string, a bare DSN name, or the legacy odbc: prefix.

Local connection setup failures, such as an unregistered DSN (IM002), return sqlx::Error::Configuration with the original ODBC diagnostic as the cause. Server connection and authentication failures still return sqlx::Error::Database.

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.

Enable the offline feature to use SQLx's offline statement description API. If the application uses other SQLx drivers, enable their offline support too.

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.

ODBC transactions support begin(). Custom begin_with() statements and nested transactions are rejected.

Any Driver

Install this crate explicitly before opening ODBC URLs through SQLx AnyConnection:

use sqlx_core::connection::Connection;

sqlx_core::any::driver::install_drivers(&[sqlx_odbc::any::DRIVER])?;

let mut conn = sqlx_core::any::AnyConnection::connect(
    "odbc:Driver=DuckDB;Database=/tmp/example.duckdb",
)
.await?;

Chrono timestamps

With the chrono feature, DateTime<Local> now binds as RFC 3339 text with its offset, like DateTime<FixedOffset> and DateTime<Utc>. Previously it bound as a plain ODBC timestamp containing the local clock time. The database decides how to convert the text. For example, PostgreSQL and DuckDB use the offset when casting to TIMESTAMP WITH TIME ZONE and discard it when casting to TIMESTAMP.

To keep binding a plain timestamp, use value.naive_local() for the previous wall-clock behavior or value.naive_utc() for a column that stores UTC by convention. If you change what a column stores, review existing values before mixing them with new writes.

ODBC Setup

ODBC uses two native pieces outside this crate:

  1. A driver manager. On Linux and macOS this is usually unixODBC; on Windows, the driver manager is built in.
  2. A database-specific ODBC driver. Install the driver, then either configure a DSN or pass a full connection string using Driver=....

For the fastest local smoke test, install the DuckDB ODBC driver and run:

./scripts/test-driver.sh duckdb

To test the same suite through the PostgreSQL ODBC driver, start PostgreSQL locally and run:

./scripts/test-driver.sh postgres

To test another installed driver, set a connection string and use custom:

ODBC_DATABASE_URL='Driver=ODBC Driver 18 for SQL Server;Server=localhost;TrustServerCertificate=yes' \
    ./scripts/test-driver.sh custom

DuckDB currently works through the default unbuffered fetch path. Some DuckDB ODBC builds reject the column-wise row binding attribute used by buffered fetching, so leave OdbcConnectOptions::max_column_size(None) for DuckDB.

Useful setup guides:

Static unixODBC

If you ship an application on Linux or macOS and do not want users to install the unixODBC driver manager separately, enable this crate's vendored feature:

[dependencies]
sqlx-odbc = { version = "0.0.3", features = ["vendored-unix-odbc"] }
sqlx-core = "=0.9.0"

That feature forwards to odbc-api/vendored-unix-odbc, which builds and statically links the unixODBC driver manager through odbc-sys. It removes the driver-manager install step on Linux and macOS, but your users still need the database-specific ODBC driver itself and any configuration files or connection string values that driver requires. Vendored unixODBC is LGPL-2.1-or-later, so review the licensing implications before distributing statically linked artifacts.

For local development, run fast checks with ./scripts/ci.sh. Run e2e tests against one installed driver with ./scripts/test-driver.sh duckdb or ODBC_DATABASE_URL=... ./scripts/test-driver.sh custom.

About

rust sqlx driver for ODBC databases

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages