[−][src]Crate tiberius
An asynchronous, runtime-independent, pure-rust Tabular Data Stream (TDS) implementation for Microsoft SQL Server.
Connecting with async-std
Being not bound to any single runtime, a TcpStream
must be created
separately and injected to the Client
.
use tiberius::{Client, Config, AuthMethod}; use async_std::net::TcpStream; #[async_std::main] async fn main() -> anyhow::Result<()> { // Using the builder method to construct the options. let mut config = Config::new(); config.host("localhost"); config.port(1433); // Using SQL Server authentication. config.authentication(AuthMethod::sql_server("SA", "<password>")); // Taking the address from the configuration, using async-std's // TcpStream to connect to the server. let tcp = TcpStream::connect(config.get_addr()).await?; // We'll disable the Nagle algorithm. Buffering is handled // internally with a `Sink`. tcp.set_nodelay(true)?; // Handling TLS, login and other details related to the SQL Server. let mut client = Client::connect(config, tcp).await?; // A response to a query is a stream of data, that must be // polled to the end before querying again. Using streams allows // fetching data in an asynchronous manner, if needed. let mut stream = client.query("SELECT @P1", &[&-4i32]).await?; // As long as the `next_resultset` returns true, the stream has // more results and can be polled. For each result set, the stream // returns rows until the end of that result. In a case where // `next_resultset` is true, polling again will return rows from // the next query. assert!(stream.next_resultset()); // In this case, we know we have only one query, returning one row // and one column, so calling `into_row` will consume the stream // and return us the first row of the first result. let row = stream.into_row().await?; assert_eq!(Some(-4i32), row.unwrap().get(0)); Ok(()) }
Connecting with Tokio
Tokio is using their own version of AsyncRead
and AsyncWrite
traits,
meaning that when wanting to use Tiberius with Tokio, their TcpStream
needs to be wrapped in Tokio's Compat
module.
use tiberius::{Client, Config, AuthMethod}; use tokio::net::TcpStream; use tokio_util::compat::Tokio02AsyncWriteCompatExt; #[tokio::main] async fn main() -> anyhow::Result<()> { let mut config = Config::new(); config.host("localhost"); config.port(1433); config.authentication(AuthMethod::sql_server("SA", "<password>")); let tcp = TcpStream::connect(config.get_addr()).await?; tcp.set_nodelay(true)?; // To be able to use Tokio's tcp, we're using the `compat_write` from // the `Tokio02AsyncWriteCompatExt` to get a stream compatible with the // traits from the `futures` crate. let mut client = Client::connect(config, tcp.compat_write()).await?; Ok(()) }
Authentication
Tiberius supports different ways of authentication to the SQL Server:
- SQL Server authentication uses the facilities of the database to authenticate the user.
- On Windows, you can authenticate using the currently logged in user or specified Windows credentials.
- If enabling the
integrated-auth-gssapi
feature, it is possible to login with the currently active Kerberos credentials.
TLS
When compiled using the default features, a TLS encryption will be available
and by default, used for all traffic. TLS is handled with the given
TcpStream
. Please see the documentation for EncryptionLevel
for
details.
SQL Browser
On Windows platforms, connecting to the SQL Server might require going through
the SQL Browser service to get the correct port for the named instance. This
feature requires either the sql-browser-async-std
or sql-browser-tokio
feature
flag to be enabled and has a bit different way of connecting:
use tiberius::{Client, Config, AuthMethod}; use async_std::net::TcpStream; // An extra trait that allows connecting to a named instance with the given // `TcpStream`. use tiberius::SqlBrowser; #[async_std::main] async fn main() -> anyhow::Result<()> { let mut config = Config::new(); config.authentication(AuthMethod::sql_server("SA", "<password>")); config.host("localhost"); // The default port of SQL Browser config.port(1434); // The name of the database server instance. config.instance_name("INSTANCE"); // This will create a new `TcpStream` from `async-std`, connected to the // right port of the named instance. let tcp = TcpStream::connect_named(&config).await?; // And from here on continue the connection process in a normal way. let mut client = Client::connect(config, tcp).await?; Ok(()) }
Other features
- If using an ADO.NET connection string, it is possible to create a
Config
from one. Please see the documentation forfrom_ado_string
for details. - If wanting to use Tiberius with SQL Server version 2005, one must
disable the
tds73
feature.
Modules
error | Error module |
numeric | Representations of numeric types. |
time | Date and time handling. |
xml | The XML containers |
Structs
Client |
|
Column | A column of data from a query. |
Config | The |
ExecuteResult | A result from a query execution, listing the number of affected rows. |
QueryResult | A set of |
Row | A row of data from a query. |
Uuid | A Universally Unique Identifier (UUID). |
Enums
AuthMethod | Defines the method of authentication to the server. |
ColumnData | A container of a value that can be represented as a TDS value. |
ColumnType | The type of the column. |
EncryptionLevel | The configured encryption level specifying if encryption is required |
Traits
FromSql | A conversion trait from a TDS type by-reference. |
FromSqlOwned | A conversion trait from a TDS type by-value. |
IntoSql | A by-value conversion trait to a TDS type. |
SqlBrowser | An extension trait to a |
ToSql | A conversion trait to a TDS type. |
Type Definitions
Result | An alias for a result that holds crate's error type as the error. |