Crate mysql_async [] [src]

mysql-async

Tokio based asynchronous MySql client library for rust programming language.

Installation

Library hosted on crates.io.

[dependencies]
mysql = "<desired version>"

Example

extern crate futures;
#[macro_use]
extern crate mysql_async as my;
extern crate tokio_core as tokio;
// ...

use futures::Future;
use my::prelude::*;
use tokio::reactor::Core;

#[derive(Debug, PartialEq, Eq)]
struct Payment {
    customer_id: i32,
    amount: i32,
    account_name: Option<String>,
}

fn main() {
    let mut lp = Core::new().unwrap();

    let payments = vec![
        Payment { customer_id: 1, amount: 2, account_name: None },
        Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) },
        Payment { customer_id: 5, amount: 6, account_name: None },
        Payment { customer_id: 7, amount: 8, account_name: None },
        Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) },
    ];


    let pool = my::Pool::new(database_url, &lp.handle());
    let future = pool.get_conn().and_then(|conn| {
        // Create temporary table
        conn.drop_query(
            r"CREATE TEMPORARY TABLE payment (
                customer_id int not null,
                amount int not null,
                account_name text
            )"
        )
    }).and_then(|conn| {
        // Save payments
        let params = payments.iter().map(|payment| {
            params! {
                "customer_id" => payment.customer_id,
                "amount" => payment.amount,
                "account_name" => payment.account_name.clone(),
            }
        }).collect();

        conn.batch_exec(r"INSERT INTO payment (customer_id, amount, account_name)
                        VALUES (:customer_id, :amount, :account_name)", params)
    }).and_then(|conn| {
        // Load payments from database.
        conn.prep_exec("SELECT customer_id, amount, account_name FROM payment", ())
    }).and_then(|result| {
        // Collect payments
        result.map_and_drop(|row| {
            let (customer_id, amount, account_name) = my::from_row(row);
            Payment {
                customer_id: customer_id,
                amount: amount,
                account_name: account_name,
            }
        })
    }).and_then(|(_ /* conn */, payments)| {
        // The destructor of a connection will return it to the pool,
        // but pool should be disconnected explicitly because it's
        // an asynchronous procedure.
        pool.disconnect().map(|_| payments)
    });

    let loaded_payments = lp.run(future).unwrap();

    assert_eq!(loaded_payments, payments);
}

Reexports

pub extern crate chrono;
pub extern crate time;

Modules

consts

Mysql constants

errors

Errors used in this crate

prelude

Traits used in this crate

Macros

params

This macro allows you to pass named params to a prepared statement.

Structs

Column

Mysql column.

Conn

Mysql connection

Deserialized

Use it to parse T: Deserialize from Value.

ErrPacket

MySql error packet.

Opts

Mysql connection options.

OptsBuilder

Provides a way to build Opts.

Pool

Asynchronous pool of MySql connections.

QueryResult

Result of a query or statement execution.

Row

Row of a result set.

Serialized

Use it to pass T: Serialize as JSON to a prepared statement.

Transaction

This struct represents MySql transaction.

TransactionOptions

Options for transaction

WhiteListFsLocalInfileHandler

Handles local infile requests from filesystem using explicit path white list.

Enums

IsolationLevel

Transaction isolation level.

Params

Parameters of a statement.

Value

Value of a MySql column.

Functions

from_row

Will panic if could not convert row to T.

from_row_opt

Will return Err(row) if could not convert row to T

from_value

Will panic if could not convert v to T

from_value_opt

Will return Err(v) if could not convert v to T

Type Definitions

BoxFuture