sqlx-type

crates.io crates.io License actions-badge

Proc macros to perform type sql queries similarly to sqlx::query, but without the need to run cargo sqlx prepare

A schema definition must be placed in "sqlx-type-schema.sql" in the root of a using crate:

``sql DROP TABLE IF EXISTSt1; CREATE TABLEt1( idint(11) NOT NULL, cbooltinyint(1) NOT NULL, cu8tinyint UNSIGNED NOT NULL, cu16smallint UNSIGNED NOT NULL, cu32int UNSIGNED NOT NULL, cu64bigint UNSIGNED NOT NULL, ci8tinyint, ci16smallint, ci32int, ci64bigint, ctextvarchar(100) NOT NULL, cbytesblob, cf32float, cf64` double ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE t1 MODIFY id int(11) NOT NULL AUTOINCREMENT; ``` See [sqltype::schema] for a detailed description.

This schema can then be used to type queries:

```rust use std::env, sqlx::MySqlPool, sqlx_type::query;

async fn test() -> Result<(), sqlx::Error> { let pool = MySqlPool::connect(&env::var("DATABASE_URL").unwrap()).await?;

let id = query!("INSERT INTO `t1` (`cbool`, `cu8`, `cu16`, `cu32`, `cu64`, `ctext`)
    VALUES (?, ?, ?, ?, ?, ?)", true, 8, 1243, 42, 42, "Hello world")
    .execute(&pool).await?.last_insert_id();

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

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

} ```