[−][src]Crate mysql_async
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<(), mysql_async::error::Error> { 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 payments_clone = payments.clone(); let database_url = /* ... */ let pool = mysql_async::Pool::new(database_url); let conn = pool.get_conn().await?; // Create temporary table let conn = conn.drop_query( 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.clone(), } }); let conn = conn.batch_exec(r"INSERT INTO payment (customer_id, amount, account_name) VALUES (:customer_id, :amount, :account_name)", params).await?; // Load payments from database. let result = conn.prep_exec("SELECT customer_id, amount, account_name FROM payment", ()).await?; // Collect payments let (_ /* conn */, loaded_payments) = result.map_and_drop(|row| { let (customer_id, amount, account_name) = mysql_async::from_row(row); Payment { customer_id: customer_id, amount: amount, account_name: account_name, } }).await?; // 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().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 | |
error | Errors used in this crate |
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. |
Column | Represents MySql Column (column packet). |
Compression | When compressing data, the compression level can be specified by a value in this enum. |
Conn | |
Deserialized | Use it to parse |
FromRowError |
|
FromValueError |
|
Opts | Mysql connection options. |
OptsBuilder | Provides a way to build |
Pool | Asynchronous pool of MySql connections. |
PoolConstraints | Connection pool constraints. |
PoolOptions | Connection pool options. |
QueryResult | Result of a query or statement execution. |
Row | Client side representation of a MySql row. |
Serialized | Use it to pass |
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. |
Constants
DEFAULT_INACTIVE_CONNECTION_TTL | Default |
DEFAULT_TTL_CHECK_INTERVAL | Default |
Functions
from_row | Will panic if could not convert |
from_row_opt | Will return |
from_value | Will panic if could not convert |
from_value_opt | Will return |
Type Definitions
BoxFuture | |
InfileHandlerFuture |