Skip to main content

Crate qusql_mysql_type

Crate qusql_mysql_type 

Source
Expand description

Proc macros to perform type typed mysql queries on top of qusql-mysql.

The queries are typed based on a schema definition, that must be placed in “qusql-mysql-type-schema.sql” in the root of a using crate:

DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
    `id` int(11) NOT NULL,
    `cbool` tinyint(1) NOT NULL DEFAULT false,
    `cu8` tinyint UNSIGNED NOT NULL DEFAULT 0,
    `cu16` smallint UNSIGNED NOT NULL DEFAULT 1,
    `cu32` int UNSIGNED NOT NULL DEFAULT 2,
    `cu64` bigint UNSIGNED NOT NULL DEFAULT 3,
    `ci8` tinyint,
    `ci16` smallint,
    `ci32` int,
    `ci64` bigint,
    `ctext` varchar(100) NOT NULL,
    `cbytes` blob,
    `cf32` float,
    `cf64` double
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `t1`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

See qusql_type::schema for a detailed description.

This schema can then be used to type queries:

use qusql_mysql::connection::{ConnectionOptions, ConnectionError, ExecutorExt};
use qusql_mysql::pool::{Pool, PoolOptions};
use qusql_mysql_type::{execute, fetch_one};

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 id = execute!(&mut conn, "INSERT INTO `t1` (
       `cbool`, `cu8`, `cu16`, `cu32`, `cu64`, `ctext`)
        VALUES (?, ?, ?, ?, ?, ?)",
        true, 8, 1243, 42, 42, "Hello world").await?.last_insert_id();

    let row = fetch_one!(&mut conn,
        "SELECT `cu16`, `ctext`, `ci32` FROM `t1` WHERE `id`=?", id).await?;

    assert_eq!(row.cu16, 1234);
    assert_eq!(row.ctext, "Hello would");
    assert!(row.ci32.is_none());
    Ok(())
}

Macros§

execute
Statically checked execute
fetch
Statically checked streaming fetch with borrowed values
fetch_all
Statically checked fetch_all with borrowed values
fetch_all_as
Statically checked fetch_all with borrowed values into custom type
fetch_all_as_owned
Statically checked fetch_all with owned values into custom type
fetch_all_owned
Statically checked fetch_all with owned values
fetch_as
Statically checked streaming fetch with borrowed values into custom type
fetch_as_owned
Statically checked streaming fetch with owned values into custom type
fetch_one
Statically checked fetch_one with borrowed values
fetch_one_as
Statically checked fetch_one with borrowed values into custom type
fetch_one_as_owned
Statically checked fetch_one with owned values into custom type
fetch_one_owned
Statically checked fetch_one with owned values
fetch_optional
Statically checked fetch_optional with borrowed values
fetch_optional_as
Statically checked fetch_optional with borrowed values into custom type
fetch_optional_as_owned
Statically checked fetch_optional with owned values into custom type
fetch_optional_owned
Statically checked fetch_optional with owned values
fetch_owned
Statically checked streaming fetch with owned values

Traits§

ArgIn
If ArgIn<T> is implemented for J, it means that J can be used as for arguments of type T
ArgOut
If ArgOut<N, T> is implemented for J, it means that J can be construct from column of type T