mssqlrust
mssqlrust is a lightweight Rust library for Microsoft SQL Server built on top of Tiberius. It executes queries, stored procedures and views and returns results in a typed hierarchy DataSet → DataTable → DataRow → DataValue
with column metadata, nullable handling and streaming support.
Installation
Add the dependency to your Cargo.toml
:
[]
= "1.0.0"
Adjust the URL or version depending on the source you use.
How it works
The crate wraps the SQL Server connection using Tiberius and exposes an async execute
function that receives a MssqlConfig
and a Command
. The returned data is built into the hierarchy DataSet → DataTable → DataRow → DataValue
for typed access to results.
flowchart LR
A[Create MssqlConfig] --> B[Build Command]
B --> C[execute]
C --> D[SQL Server]
D --> E[DataSet]
E --> F[DataTable]
F --> G[DataRow]
sequenceDiagram
actor Client
participant mssqlrust
participant SQLServer
Client->>mssqlrust: execute(config, cmd)
mssqlrust->>SQLServer: run query
SQLServer-->>mssqlrust: result rows
mssqlrust-->>Client: DataSet
Usage
Basic query
Run a simple text query and read the first row/column. Results are accessed via DataSet → DataTable → DataRow
, and you can compare values directly with native Rust types.
use ;
use MssqlConfig;
use NaiveDate;
use Decimal;
async
Parameterized query
let cmd = query
.with_param
.with_param
.with_param
.with_param;
let ds = execute.await?;
let row = &ds.tables;
println!;
Ok
}
Stored Procedure With Parameters
You can execute stored procedures and pass named parameters (with or without the @
prefix). Parameters accept native Rust types and are converted internally.
use ;
use MssqlConfig;
use NaiveDate;
use Decimal;
async
DataSet structure
classDiagram
class DataSet {
tables: HashMap<String, DataTable>
}
class DataTable {
name: String
columns: Vec<DataColumn>
rows: Vec<DataRow>
}
class DataColumn {
name: String
sql_type: String
size: Option<u32>
nullable: bool
}
class DataRow {
cells: HashMap<String, DataCell>
}
class DataCell {
value: DataValue
}
DataSet --> DataTable
DataTable --> DataColumn
DataTable --> DataRow
DataRow --> DataCell
DataCell --> DataValue
Examples
The tests
directory contains additional examples that show parameterized queries, stored procedures and mapping of various SQL types.
License
This project is distributed under the terms of the MIT license.