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