Crate mysql_async[][src]

Expand description

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
    drop(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

Futures used in this crate

Traits used in this crate

Macros

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

Structs

Phantom struct used to specify MySql binary protocol.

Empty flags of a LoadEvent.

Binlog request representation. Please consult MySql documentation.

Binlog event stream.

Represents MySql Column (column packet).

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

MySql server connection.

Use it to parse T: Deserialize from Value.

FromRow conversion error.

FromValue conversion error.

This tracker type indicates that GTIDs are available and contains the GTID string.

Interval. Stored within Sid

Represents MySql’s Ok packet.

Mysql connection options.

Provides a way to build Opts.

Asynchronous pool of MySql connections.

Connection pool constraints.

Connection pool options.

Result of a query or statement execution.

Representaion of a prepared statement query.

Client side representation of a MySql row.

This tracker type indicates that the default schema has been set.

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

This type represents MySql server error.

Represents change in session state (part of MySql’s Ok packet).

SID is a part of the COM_BINLOG_DUMP_GTID command.

Ssl Options.

Prepared statement.

This tracker type indicates that one or more tracked session system variables have been assigned a value.

Phantom struct used to specify MySql text protocol.

This struct represents MySql transaction.

This tracker type indicates that transaction characteristics are available.

This tracker type indicates that transaction state information is available.

Transaction options.

This tracker type is unknown/unsupported.

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

Enums

This type enumerates driver errors.

This type enumerates library errors.

This type enumerates IO errors.

Transaction isolation level.

Representations of parameters of a prepared statement.

Errors that can occur during parsing.

Represents a parsed change in a session state (part of MySql’s Ok packet).

This type enumerates connection URL errors.

Client side representation of a value of MySql column.

Constants

Default inactive_connection_ttl of a pool.

Default pool constraints.

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

Default ttl_check_interval of a pool.

Functions

Will panic if could not convert row to T.

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

Will panic if could not convert v to T

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

Type Definitions

Result type alias for this library.