tiberius 0.3.0

A TDS (MSSQL) driver
Documentation

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.