Expand description
Crate for typing SQL statements.
use qusql_type::{schema::parse_schemas, type_statement, TypeOptions,
SQLDialect, SQLArguments, StatementType, Issues};
let schemas = "
CREATE TABLE `events` (
`id` bigint(20) NOT NULL,
`user` int(11) NOT NULL,
`message` text NOT NULL
);";
let mut issues = Issues::new(schemas);
let schemas = parse_schemas(schemas,
&mut issues,
&TypeOptions::new().dialect(SQLDialect::MariaDB));
assert!(issues.is_ok());
let sql = "SELECT `id`, `user`, `message` FROM `events` WHERE `id` = ?";
let mut issues = Issues::new(sql);
let stmt = type_statement(&schemas, sql, &mut issues,
&TypeOptions::new().dialect(SQLDialect::MariaDB).arguments(SQLArguments::QuestionMark));
assert!(issues.is_ok());
match stmt {
StatementType::Select{columns, arguments} => {
assert_eq!(columns.len(), 3);
assert_eq!(arguments.len(), 1);
}
_ => panic!("Expected select statement")
};use qusql_type::{
schema::parse_schemas, type_statement, TypeOptions,
SQLDialect, SQLArguments, StatementType, Issues,
};
let schema_sql = "
CREATE TABLE notes (
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
title text NOT NULL,
body text
);";
let opts = TypeOptions::new().dialect(SQLDialect::PostgreSQL);
let mut issues = Issues::new(schema_sql);
let schemas = parse_schemas(schema_sql, &mut issues, &opts);
assert!(issues.is_ok());
let query = "SELECT id, title, body FROM notes WHERE id = $1";
let mut issues = Issues::new(query);
let stmt = type_statement(
&schemas, query, &mut issues,
&TypeOptions::new()
.dialect(SQLDialect::PostgreSQL)
.arguments(SQLArguments::Dollar),
);
assert!(issues.is_ok());
match stmt {
StatementType::Select { columns, arguments } => {
// columns[0] -> id : i32, not-null
// columns[1] -> title: String, not-null
// columns[2] -> body : Option<String>
// arguments[0] -> i32 (the type of `id`)
println!("{} columns, {} arguments", columns.len(), arguments.len());
}
_ => panic!("expected SELECT"),
}Modules§
- schema
- Parse and evaluate SQL schema definitions into a typed representation used for statement type-checking.
Structs§
- Byte
ToChar - Struct to map byte positions in a UTF-8 string to character positions, for error reporting.
- Fragment
- Full
Type - Represent a type with not_null information
- Issue
- An issue encountered during parsing, or later stages
- Issues
- Select
Type Column - A column in select
- Type
Options - Options used when typing sql or parsing a schema
Enums§
- Argument
Key - Key of argument
- Auto
Increment Id - Does the insert yield an auto increment id
- Base
Type - Canonical base type of a type
- Level
- Level of an issues
- SQLArguments
- What kinds or arguments
- SQLDialect
- What sql diarect to parse as
- Statement
Type - Type information of typed statement
- Type
- Represent the type of a value
Functions§
- type_
statement - Type an sql statement with respect to a given schema