sql-type

Crates.iosql-type
lib.rssql-type
version0.21.1
sourcesrc
created_at2022-02-25 11:57:04.969679
updated_at2024-10-11 11:34:06.393371
descriptionTyper for sql
homepagehttps://github.com/antialize/sql-type/
repositoryhttps://github.com/antialize/sql-type/
max_upload_size
id539374
size185,302
Jakob Truelsen (antialize)

documentation

README

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")
};
Commit count: 91

cargo fmt