Skip to main content

Crate qusql_mysql

Crate qusql_mysql 

Source
Expand description

This crate allow efficient async communication with Mariadb/Mysql

You can either establish a raw connection using Connection or use a Pool of connections.

§Drop handling

When dropping/cancelling any future or struct returned by the library. The connections will keep working. This is done by having the Connection have an internal state and finish up any partially executed queries when the next query is executed or when Connection::cleanup is called

When dropping a PoolConnection, any partial queries or transactions are finished up in a spawned task. If this task takes too long to execute the connection is closed an a new connection may be established.

This means that if the qusql-mysql is used in a backend to handle web requests, and the web request is cancelled while performing a long running query. Then the long running query will be killed shortly after.

§Efficiency

We spawn very few tasks. Currently the only task spawned is when a PoolConnection is dropped while there is a ongoing query or transaction.

We have few memory allocations. In the cause of a normal query, even one returning a string, no memory will be allocated

The error types returned are all 8 bytes.

The benchmark folder contains a benchmark that compares sqlx to qusql-mysql. When run it shows the we are significantly more efficient than sqlx

TestQusql timeSqlx time
Setup0.921 ms1.189 ms
Insert14218.778 ms15499.612 ms
Select all10968.823 ms15860.648 ms
Select stream9991.353 ms13215.973 ms
Select one19085.157 ms34728.834 ms

§Feature flags

  • stats: Add query count and timing statistics to the Connection
  • chrono: Add bind and decode support for chrono DateTime and Time
  • list_hack: Add support lists as arguments to queries

§Example

use qusql_mysql::{Pool, ConnectionOptions, PoolOptions, ConnectionError, ExecutorExt, Executor};

async fn test() -> Result<(), ConnectionError> {
    let pool = Pool::connect(
        ConnectionOptions::from_url("mysql://user:pw@127.0.0.1:3307/db").unwrap(),
        PoolOptions::new().max_connections(10)
    ).await?;

    let mut conn = pool.acquire().await?;

    let mut tr = conn.begin().await?;
    tr.execute("INSERT INTO `table` (`v`, `t`) VALUES (?)", (42, "test_string")).await?;
    tr.commit().await?;

    let rows: Vec<(i64, &str)> = conn.fetch_all("SELECT `v`, `t` FROM `table`", ()).await?;
    Ok(())
}

§List hack

If the list list_hack feature is enabled. Support for lists as arguments to queries is added this is done done by adding a sufficient number of ?’s to the query.

#[cfg(feature="list_hack")]
async fn test(conn: &mut Connection, vs: &[u32]) -> Result<(), ConnectionError> {
    let rows: Vec<(i64, &str)> = conn.fetch_all(
        "SELECT `v`, `t` FROM `table` WHERE `v` IN (_LIST_)", (list(vs),)).await?;
    Ok(())
}

Here the _LIST_ is replaced with ?,?,..,? where the number of ?’s depend on the length of the list. If the list is empty _LIST_ is replaced by NULL

Re-exports§

pub use bind::List;
pub use bind::list;
pub use connection::ColumnDefinition;
pub use connection::Connection;
pub use connection::ConnectionError;
pub use connection::ConnectionErrorContent;
pub use connection::ConnectionOptions;
pub use connection::ExecuteResult;
pub use connection::Executor;
pub use connection::ExecutorExt;
pub use connection::Query;
pub use connection::QueryIterator;
pub use connection::RowMap;
pub use connection::Transaction;
pub use pool::Pool;
pub use pool::PoolConnection;
pub use pool::PoolOptions;
pub use row::Row;

Modules§

args
Contains Structs to contains and parse rows
auth
Implementation of auth methods
bind
Provide support for binding arguments to queries
connection
Handle a single connection to Mariadb/Mysql
constants
Constants used in the mysql/mariadb protocol
decode
Facilities for decoding values from query result
package_parser
Contains parser used to parse packages
plain_types
Contains simple types that can be used to represent Mysql/Mariadb types that does not have a corresponding rust type.
pool
Implements a pool of connections to Mariadb/Mysql
row
Contains Structs to contains and parse rows