Crate tiberius

source ·
Expand description

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 futures_state_stream;
extern crate tokio;
extern crate tiberius;
use futures::Future;
use futures_state_stream::StateStream;
use tokio::executor::current_thread;
use tiberius::SqlConnection;

fn main() {
 
   // 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(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(conn_str.as_str())
       .and_then(|conn| {
           conn.simple_query("SELECT 1+2").for_each(|row| {
               let val: i32 = row.get(0);
               assert_eq!(val, 3i32);
               Ok(())
           })
       })
       .and_then(|conn| conn.simple_exec("create table #Temp(gg int);"))
       .and_then(|(_, conn)| conn.simple_exec("UPDATE #Temp SET gg=1 WHERE gg=1"));
 
   current_thread::block_on_all(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 futures_state_stream;
extern crate tokio;
extern crate tiberius;
use futures::Future;
use futures_state_stream::StateStream;
use tokio::executor::current_thread;
use tiberius::SqlConnection;

fn main() {
 
   // 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(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| {
           let val: i32 = row.get(0);
           assert_eq!(val % 2, 0i32);
           Ok(())
       })
   });
   current_thread::block_on_all(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 results and resultsets
Prepared statements
Exported Datatypes (Dates, GUID, …)

Structs

Settings for the connection, everything that isn’t IO/transport specific (e.g. authentication)
A connection to a SQL server with an underlying IO (e.g. socket)
A transaction

Enums

The authentication method that should be used during authentication
The configured encryption level specifying if encryption is required
A unified error enum that contains several errors that might occurr during the lifecycle of this driver

Traits

A variant of Io which can be boxed to allow dynamic dispatch
A type which is constructable from a statement as a statement’s result

Type Definitions