Expand description
MSSQL driver for SQLx via ODBC.
sqlx-mssql-odbc connects SQLx to Microsoft SQL Server through an ODBC driver
manager. This is the facade crate — it re-exports everything from
[sqlx-mssql-odbc-core].
§Quick start
[dependencies]
sqlx-core = "0.9.0"
sqlx-mssql-odbc = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }use sqlx_core::connection::Connection;
use sqlx_core::row::Row;
use sqlx_mssql_odbc::MssqlConnection;
let mut conn = MssqlConnection::connect(
"mssql://user:password@localhost:1433/database"
).await?;
let row = sqlx_core::query::query("SELECT 42")
.fetch_one(&mut conn)
.await?;
let value: i32 = row.try_get(0)?;
assert_eq!(value, 42);
conn.close().await?;MssqlConnection::connect() accepts a standard mssql:// URL, a raw ODBC
connection string, or a bare DSN name.
§Usage patterns
§Connection pooling
use sqlx_mssql_odbc::MssqlPoolOptions;
let pool = MssqlPoolOptions::new()
.max_connections(10)
.connect("mssql://user:password@localhost:1433/database")
.await?;
let row = sqlx_core::query::query("SELECT 1")
.fetch_one(&pool)
.await?;
pool.close().await;§Parameterised queries
use sqlx_core::row::Row;
use sqlx_mssql_odbc::MssqlConnection;
use sqlx_core::connection::Connection;
let mut conn = MssqlConnection::connect("mssql://…").await?;
let row = sqlx_core::query::query("SELECT @p1 + @p2")
.bind(10i32)
.bind(20i32)
.fetch_one(&mut conn)
.await?;
let total: i32 = row.try_get(0)?;
assert_eq!(total, 30);§Compile-time checked queries (macros feature)
Enable the macros feature to get compile-time validation of SQL against a
live database:
[dependencies]
sqlx-mssql-odbc = { version = "0.1", features = ["macros"] }let mut conn = MssqlConnection::connect("…").await?;
// The column name and type are checked at compile time.
let row = sqlx_mssql_odbc::query!("SELECT 1 AS one")
.fetch_one(&mut conn)
.await?;
assert_eq!(row.one, 1i32);
// Bind parameters with $1, $2, etc.
let row = sqlx_mssql_odbc::query!("SELECT @p1 + @p2 AS total", 10i32, 20i32)
.fetch_one(&mut conn)
.await?;
assert_eq!(row.total, 30);For CI or offline builds, use cargo sqlx prepare to cache the schema so
the macros can check queries without a live database.
§Derive macros (derive feature)
[dependencies]
sqlx-mssql-odbc = { version = "0.1", features = ["derive"] }use sqlx_mssql_odbc::FromRow;
#[derive(Debug, FromRow)]
struct User {
id: i32,
name: String,
email: Option<String>,
}
let users = sqlx_mssql_odbc::query_as!(User, "SELECT id, name, email FROM users")
.fetch_all(&mut conn)
.await?;§Transactions
use sqlx_core::connection::Connection;
use sqlx_core::executor::Executor;
use sqlx_mssql_odbc::MssqlConnection;
let mut conn = MssqlConnection::connect("mssql://…").await?;
conn.begin().await?;
sqlx_core::query::query("INSERT INTO users (name) VALUES (@p1)")
.bind("Alice")
.execute(&mut conn)
.await?;
conn.commit().await?;§URL parameters
| Parameter | Description |
|---|---|
encrypt=true | Enable TLS encryption |
trust_certificate=true | Skip certificate validation |
driver=… | Custom ODBC driver name (default: ODBC Driver 18 for SQL Server) |
§Requirements
On Linux and macOS you need both a driver manager (unixODBC) and the Microsoft ODBC Driver for SQL Server (version 17 or 18). See the repository README for platform-specific installation instructions.
Enable the vendored-unix-odbc feature to statically link unixODBC into
your application on Linux or macOS.
§Features
| Feature | Description |
|---|---|
bigdecimal | [BigDecimal] type support |
chrono | [chrono] datetime types |
rust_decimal / decimal | [rust_decimal::Decimal] support |
json | [serde_json::Value] support |
time | [time] crate datetime types |
uuid | [uuid::Uuid] support |
macros | query!(), query_as!() and other proc macros |
derive | Encode, Decode, Type, FromRow derive macros |
offline | Compile-time query checking with query!() |
migrate | Database migration support |
runtime-tokio | Tokio runtime support |
tls-none | No TLS (default) |
spatial | [geo_types] spatial type support |
vendored-unix-odbc | Statically link unixODBC |
Re-exports§
pub use sqlx_mssql_odbc_core as core;
Modules§
- type_
checking - Type-checking support for compile-time query macros.
Macros§
- expand_
query macros - query
macros - Compile-time checked SQL query for MSSQL via ODBC.
- query_
as macros - Compile-time checked SQL query for MSSQL via ODBC, mapping to a named struct.
- query_
file macros - Compile-time checked SQL query read from a file at compile time.
- query_
file_ as macros - Compile-time checked SQL query read from a file, mapping to a named struct.
- query_
file_ scalar macros - Compile-time checked SQL query read from a file, returning a single scalar.
- query_
scalar macros - Compile-time checked SQL query for MSSQL via ODBC, returning a single scalar value.
Structs§
- Mssql
- MSSQL database marker for SQLx-core traits.
- Mssql
Arguments - Values that can be bound to MSSQL ODBC parameters.
- Mssql
Buffer Settings - Fetch-buffer settings used by the MSSQL ODBC driver.
- Mssql
Column - Column metadata for an MSSQL result set via ODBC.
- Mssql
Connect Options - Connection options for an MSSQL ODBC data source.
- Mssql
Connection - MSSQL connection backed by an actor thread that owns the ODBC connection.
- Mssql
Database Error - Database error details extracted from ODBC diagnostics.
- Mssql
Parameter Collection - Owned MSSQL ODBC parameter storage ready to bind with
odbc-api. - Mssql
Query Result - Result summary for an MSSQL query via ODBC.
- Mssql
Row - Minimal MSSQL row container used by the SQLx-core skeleton.
- Mssql
Statement - Prepared statement metadata for MSSQL via ODBC.
- Mssql
Transaction Manager - Transaction manager for MSSQL via ODBC.
- Mssql
Type Info - Type information for an MSSQL ODBC value.
- Mssql
Value - A small owned MSSQL ODBC value representation.
Enums§
- Mssql
Argument Value - Values that can currently be bound to MSSQL ODBC parameters.
- Mssql
Error - Error type returned by this crate.
- Mssql
Value Kind - Supported owned MSSQL ODBC value kinds.
Traits§
- Data
Type Ext - Helper predicates for
odbc-apidata type groups. - Mssql
Executor - An alias for
Executor<'_, Database = Mssql>.
Type Aliases§
- Mssql
Pool - An alias for
Pool, specialized for MSSQL. - Mssql
Pool Options - An alias for
PoolOptions, specialized for MSSQL. - Mssql
Transaction - An alias for
Transaction, specialized for MSSQL. - Result
- Result alias for this crate.