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, Clone)]
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.clone().into_iter().map(|payment| {
            params! {
                "customer_id" => payment.customer_id,
                "amount" => payment.amount,
                "account_name" => payment.account_name.clone(),
            }
        });

        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);
}

Modules

chrono
consts
errors

Errors used in this crate

macros
prelude

Traits used in this crate

time

Simple time handling.

uuid

Generate and parse UUIDs

Macros

params

This macro is a convenient way to pass named parameters to a statement.

Structs

BinaryProtocol

Phantom struct used to specify MySql binary protocol.

Column

Represents MySql Column (column packet).

Conn

Mysql connection

Deserialized

Use it to parse T: Deserialize from Value.

FromRowError

FromRow conversion error.

FromValueError

FromValue conversion error.

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

Client side representation of a MySql row.

Serialized

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

SslOpts

Ssl Options.

Stmt

Prepared statement

TextProtocol

Phantom struct used to specify MySql text protocol.

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

Representations of parameters of a prepared statement.

Value

Client side representation of a value of 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(FromValueError(v)) if could not convert v to T

Type Definitions

BoxFuture