Crate tiberius [−] [src]
A pure-rust TDS implementation for Microsoft SQL Server (>=2008)
A simple example
Warning: Do not use simple_query
with user-specified data. Resort to prepared statements for that.
extern crate futures; extern crate tokio_core; extern crate tiberius; use futures::Future; use tokio_core::reactor::Core; use tiberius::SqlConnection; use tiberius::stmt::ResultStreamExt; fn main() { let mut lp = Core::new().unwrap(); // 1: for windows we demonstrate the hardcoded variant // which is equivalent to: // let conn_str = "server=tcp:localhost,1433;integratedSecurity=true;"; // let future = SqlConnection::connect(lp.handle(), conn_str).and_then(|conn| { // and for linux we use the connection string from an environment variable let conn_str = if cfg!(windows) { "server=tcp:localhost,1433;integratedSecurity=true;".to_owned() } else { ::std::env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap() }; let future = SqlConnection::connect(lp.handle(), conn_str.as_str()) .and_then(|conn| { conn.simple_query("SELECT 1+2").for_each_row(|row| { let val: i32 = row.get(0); assert_eq!(val, 3i32); Ok(()) }) }) .and_then(|conn| conn.simple_exec("create table #Temp(gg int);").single()) .and_then(|(_, conn)| conn.simple_exec("UPDATE #Temp SET gg=1 WHERE gg=1").single()); lp.run(future).unwrap(); }
Prepared Statements
Parameters use numeric indexes such as @P1, @P2 for the n-th parameter (starting with 1 for the first)
extern crate futures; extern crate tokio_core; extern crate tiberius; use futures::Future; use tokio_core::reactor::Core; use tiberius::SqlConnection; use tiberius::stmt::ResultStreamExt; fn main() { let mut lp = Core::new().unwrap(); // 1: Same as in the example above let conn_str = if cfg!(windows) { "server=tcp:localhost,1433;integratedSecurity=true;".to_owned() } else { ::std::env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap() }; let future = SqlConnection::connect(lp.handle(), conn_str.as_str()).and_then(|conn| { conn.query("SELECT x FROM (VALUES (1),(2),(3),(4)) numbers(x) WHERE x%@P1=@P2", &[&2i32, &0i32]).for_each_row(|row| { let val: i32 = row.get(0); assert_eq!(val % 2, 0i32); Ok(()) }) }); lp.run(future).unwrap(); }
If you intend to execute the same statement multiple times for the same connection, you should use .prepare
.
For most cases you'll want this though.
Modules
query |
Query results and resultsets |
stmt |
Prepared statements |
ty |
Exported Datatypes (Dates, GUID, ...) |
Structs
ConnectEndpoint |
A target address and connection settings = all that's required to connect to a SQL server |
ConnectParams |
Settings for the connection, everything that isn't IO/transport specific (e.g. authentication) |
SqlConnection |
A connection to a SQL server with an underlying IO (e.g. socket) |
Transaction |
A transaction |
Enums
AuthMethod |
The authentication method that should be used during authentication |
EncryptionLevel |
The configured encryption level specifying if encryption is required |
TdsError |
A unified error enum that contains several errors that might occurr during the lifecycle of this driver |
Traits
BoxableIo |
A variant of Io which can be boxed to allow dynamic dispatch |
StmtResult |
A type which is constructable from a statement as a statement's result |
ToConnectEndpoint |
Something that's able to construct all that's required to connect to an endpoint |
ToIo |
Something that can be converted to an underlying IO |
Type Definitions
TdsResult |