Skip to main content

Module sql

Module sql 

Source
Expand description

Traits that classify SQL type markers, so query builders can check operations at compile time.

Every expression carries a SQL type marker, a zero-sized type such as postgres::types::Int4 or sqlite::types::Text. The traits here say what a marker can do:

TraitQuestion it answersUsed for
DataTypeIs this a SQL type marker?every typed expression
CompatibleCan these two types be compared?eq, lt, IN, LIKE, …
AssignableCan a value of this type be stored in that column?bind values, INSERT ... SELECT
NumericIs it a number?+ - * /, SUM, AVG, ABS
IntegralIs it an integer?LIMIT/OFFSET, SUBSTR positions
FloatingIs it a floating-point number?dialect type aliases
TextualIs it a string?LIKE, UPPER, LENGTH, ILIKE, regex
BinaryIs it raw bytes?dialect type aliases
TemporalIs it a date, time or timestamp?date/time functions
BooleanLikeCan it be a condition?WHERE, AND/OR/NOT, join ON
ArithmeticOutput / NegOutputWhat type does arithmetic produce?result type of a + b, -a

All traits are implemented by this crate for the dialect markers in sqlite::types, postgres::types and mysql::types. DataType is sealed, so other crates cannot add new markers.

§Examples

Generic code can require a capability with a trait bound:

use drizzle_types::{Compatible, Numeric, postgres::types::{Float8, Int4, Text}};

fn comparable<L: Compatible<R>, R: drizzle_types::DataType>() {}
fn numeric<T: Numeric>() {}

comparable::<Int4, Float8>(); // int4 = float8 is allowed
numeric::<Int4>();
// numeric::<Text>(); // error: `Text` is not a numeric SQL type

Structs§

Array
SQL array type marker: Array<T> is an array whose elements have SQL type T.
Conjunction
The SQL type of a condition list — a tuple of conditions combined with AND.
Placeholder
SQL type of an untyped placeholder, whose value is bound when the query runs.

Traits§

ArithmeticOutput
The SQL type produced by Self <op> Rhs, where both sides are Numeric.
Assignable
SQL types that accept a value of type Rhs: Self is the target (a column or typed placeholder), Rhs the type of the value stored in it.
Binary
Raw byte SQL types.
BooleanLike
SQL types that can be used as a condition: in WHERE, HAVING, join ON, CASE WHEN, and with AND, OR and NOT.
Compatible
SQL types that can be compared with Rhs: Self on the left of =, <, IN, LIKE and similar operators, Rhs on the right.
DataType
A SQL type marker: a zero-sized type that stands for one SQL type.
Floating
Floating-point SQL types.
Integral
Integer SQL types, required where SQL expects a whole number, such as LIMIT/OFFSET and string positions.
NegOutput
The SQL type produced by unary negation, -expr.
Numeric
Numeric SQL types: operands of arithmetic (+, -, *, /, %) and numeric functions such as SUM, AVG, ABS and ROUND.
Temporal
Date and time SQL types: operands of date/time functions.
Textual
String SQL types: operands of LIKE, ILIKE, regex matching and string functions such as UPPER, LOWER, LENGTH and CONCAT.