Crate mysql_async[][src]

mysql-async

Tokio based asynchronous MySql client library for rust programming language.

Installation

Library hosted on crates.io.

[dependencies]
mysql_async = "<desired version>"

Example

use mysql_async::prelude::*;

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

#[tokio::main]
async fn main() -> Result<()> {
    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 database_url = /* ... */

    let pool = mysql_async::Pool::new(database_url);
    let mut conn = pool.get_conn().await?;

    // Create temporary table
    conn.query_drop(
        r"CREATE TEMPORARY TABLE payment (
            customer_id int not null,
            amount int not null,
            account_name text
        )"
    ).await?;

    // Save payments
    let params = payments.clone().into_iter().map(|payment| {
        params! {
            "customer_id" => payment.customer_id,
            "amount" => payment.amount,
            "account_name" => payment.account_name,
        }
    });

    conn.exec_batch(
        r"INSERT INTO payment (customer_id, amount, account_name)
          VALUES (:customer_id, :amount, :account_name)",
        params,
    ).await?;

    // Load payments from database. Type inference will work here.
    let loaded_payments = conn.exec_map(
        "SELECT customer_id, amount, account_name FROM payment",
        (),
        |(customer_id, amount, account_name)| Payment { customer_id, amount, account_name },
    ).await?;

    // Dropped connection will go to the pool
    conn;

    // Pool must be disconnected explicitly because
    // it's an asynchronous operation.
    pool.disconnect().await?;

    assert_eq!(loaded_payments, payments);

    // the async fn returns Result, so
    Ok(())
}

Re-exports

pub use mysql_common::chrono;
pub use mysql_common::time;
pub use mysql_common::uuid;

Modules

consts
futures

Futures used in this crate

params
prelude

Traits used in this crate

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.

BoxFuture
Column

Represents MySql Column (column packet).

Compression

When compressing data, the compression level can be specified by a value in this enum.

Conn

MySql server 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.

PoolConstraints

Connection pool constraints.

PoolOpts

Connection pool options.

QueryResult

Result of a query or statement execution.

QueryWithParams

Representaion of a prepared statement query.

Row

Client side representation of a MySql row.

Serialized

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

ServerError

This type represents MySql server error.

SslOpts

Ssl Options.

Statement

Prepared statement.

TextProtocol

Phantom struct used to specify MySql text protocol.

Transaction

This struct represents MySql transaction.

TxOpts

Transaction options.

WhiteListFsLocalInfileHandler

Handles local infile requests from filesystem using explicit whitelist of paths.

Enums

DriverError

This type enumerates driver errors.

Error

This type enumerates library errors.

IoError

This type enumerates IO errors.

IsolationLevel

Transaction isolation level.

Params

Representations of parameters of a prepared statement.

ParseError

Errors that can occur during parsing.

UrlError

This type enumerates connection URL errors.

Value

Client side representation of a value of MySql column.

Constants

DEFAULT_INACTIVE_CONNECTION_TTL

Default inactive_connection_ttl of a pool.

DEFAULT_POOL_CONSTRAINTS

Default pool constraints.

DEFAULT_STMT_CACHE_SIZE

Each connection will cache up to this number of statements by default.

DEFAULT_TTL_CHECK_INTERVAL

Default ttl_check_interval of a pool.

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

InfileHandlerFuture
Result

Result type alias for this library.