14 releases (7 breaking)

Uses new Rust 2021

new 0.8.1 Jun 22, 2022
0.7.0 Jun 16, 2022
0.4.2 Mar 31, 2022

#183 in Database interfaces

Download history 32/week @ 2022-03-04 50/week @ 2022-03-11 65/week @ 2022-03-18 76/week @ 2022-03-25 51/week @ 2022-04-01 15/week @ 2022-04-08 18/week @ 2022-04-15 68/week @ 2022-04-22 124/week @ 2022-04-29 94/week @ 2022-05-06 177/week @ 2022-05-13 39/week @ 2022-05-20 50/week @ 2022-05-27 117/week @ 2022-06-03 37/week @ 2022-06-10 97/week @ 2022-06-17

302 downloads per month
Used in 2 crates (via sqlx-type-macro)

Apache-2.0

140KB
3.5K SLoC

sql-type

crates.io crates.io License actions-badge

Type sql statements

This crate provides a facility to process a sql schema definition, and then use this definition to type the argument and return value of sql statements.

Currently primarily focused on MariaDB/Mysql.

Example code:

use sql_type::{schema::parse_schemas, type_statement, TypeOptions,
    SQLDialect, SQLArguments, StatementType};
let schemas = "
    CREATE TABLE `events` (
      `id` bigint(20) NOT NULL,
      `user` int(11) NOT NULL,
      `message` text NOT NULL
    );";

let mut issues = Vec::new();

// Compute terse representation of the schemas
let schemas = parse_schemas(schemas,
    &mut issues,
    &TypeOptions::new().dialect(SQLDialect::MariaDB));
assert!(issues.is_empty());

let sql = "SELECT `id`, `user`, `message` FROM `events` WHERE `id` = ?";
let stmt = type_statement(&schemas, sql, &mut issues,
    &TypeOptions::new().dialect(SQLDialect::MariaDB).arguments(SQLArguments::QuestionMark));
assert!(issues.is_empty());

let stmt = match stmt {
    StatementType::Select{columns, arguments} => {
        assert_eq!(columns.len(), 3);
        assert_eq!(arguments.len(), 1);
    }
    _ => panic!("Expected select statement")
};

Dependencies

~270KB