36 releases (19 breaking)

0.21.1 Oct 11, 2024
0.20.1 Sep 12, 2024
0.20.0 Jun 13, 2024
0.16.0 Mar 27, 2024
0.4.2 Mar 31, 2022
Download history 15/week @ 2024-07-15 179/week @ 2024-07-29 14/week @ 2024-08-05 10/week @ 2024-08-12 24/week @ 2024-08-26 8/week @ 2024-09-02 134/week @ 2024-09-09 198/week @ 2024-09-16 37/week @ 2024-09-23 45/week @ 2024-09-30 180/week @ 2024-10-07 31/week @ 2024-10-14

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

Apache-2.0

175KB
4K 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

~340KB