mssql-client
High-level async SQL Server client with type-state connection management.
Overview
This is the primary public API surface for the rust-mssql-driver project. It provides a type-safe, ergonomic interface for working with SQL Server databases using modern async Rust patterns.
Features
- Type-state pattern: Compile-time enforcement of connection states
- Async/await: Built on Tokio for efficient async I/O
- Prepared statements: Automatic caching with LRU eviction
- Transactions: Full transaction support with savepoints
- Azure support: Automatic routing and failover handling
- Streaming results: Memory-efficient processing of large result sets
- Bulk insert: High-performance bulk data loading
- Table-valued parameters: Pass collections to stored procedures
Type-State Connection Management
The client uses a compile-time type-state pattern that ensures invalid operations are caught at compile time:
Disconnected -> Ready (via connect())
Ready -> InTransaction (via begin_transaction())
Ready -> Streaming (via query that returns a stream)
InTransaction -> Ready (via commit() or rollback())
Usage
use ;
async
Transactions with Savepoints
let mut tx = client.begin_transaction.await?;
tx.execute.await?;
// Create a savepoint for partial rollback
let sp = tx.savepoint.await?;
tx.execute.await?;
// Rollback to savepoint if needed
// tx.rollback_to(&sp).await?;
tx.commit.await?;
Streaming Large Results
use StreamExt;
let mut stream = client
.query_stream
.await?;
while let Some = stream.next.await
Bulk Insert
use ;
let bulk = builder
.column
.column
.build;
let result = client.bulk_insert.await?;
println!;
Feature Flags
| Flag | Default | Description |
|---|---|---|
chrono |
Yes | Date/time type support via chrono |
uuid |
Yes | UUID type support |
decimal |
Yes | Decimal type support via rust_decimal |
json |
No | JSON type support via serde_json |
otel |
No | OpenTelemetry instrumentation |
Modules
| Module | Description |
|---|---|
client |
Main Client type and connection management |
config |
Connection configuration and parsing |
query |
Query building and execution |
row |
Row and column access |
stream |
Result streaming types |
transaction |
Transaction and savepoint handling |
bulk |
Bulk insert operations |
tvp |
Table-valued parameters |
from_row |
Row-to-struct mapping trait |
to_params |
Struct-to-parameters trait |
statement_cache |
Prepared statement caching |
instrumentation |
OpenTelemetry integration |
Examples
See the examples/ directory for complete examples:
basic.rs- Simple queries and parameter bindingtransactions.rs- Transaction handling with savepointsbulk_insert.rs- High-performance bulk loadingderive_macros.rs- Using#[derive(FromRow)]and#[derive(ToParams)]streaming.rs- Processing large result sets
License
MIT OR Apache-2.0