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:
| Trait | Question it answers | Used for |
|---|---|---|
DataType | Is this a SQL type marker? | every typed expression |
Compatible | Can these two types be compared? | eq, lt, IN, LIKE, … |
Assignable | Can a value of this type be stored in that column? | bind values, INSERT ... SELECT |
Numeric | Is it a number? | + - * /, SUM, AVG, ABS |
Integral | Is it an integer? | LIMIT/OFFSET, SUBSTR positions |
Floating | Is it a floating-point number? | dialect type aliases |
Textual | Is it a string? | LIKE, UPPER, LENGTH, ILIKE, regex |
Binary | Is it raw bytes? | dialect type aliases |
Temporal | Is it a date, time or timestamp? | date/time functions |
BooleanLike | Can it be a condition? | WHERE, AND/OR/NOT, join ON |
ArithmeticOutput / NegOutput | What 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 typeStructs§
- Array
- SQL array type marker:
Array<T>is an array whose elements have SQL typeT. - 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§
- Arithmetic
Output - The SQL type produced by
Self <op> Rhs, where both sides areNumeric. - Assignable
- SQL types that accept a value of type
Rhs:Selfis the target (a column or typed placeholder),Rhsthe type of the value stored in it. - Binary
- Raw byte SQL types.
- Boolean
Like - SQL types that can be used as a condition: in
WHERE,HAVING, joinON,CASE WHEN, and withAND,ORandNOT. - Compatible
- SQL types that can be compared with
Rhs:Selfon the left of=,<,IN,LIKEand similar operators,Rhson the right. - Data
Type - 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/OFFSETand string positions. - NegOutput
- The SQL type produced by unary negation,
-expr. - Numeric
- Numeric SQL types: operands of arithmetic (
+,-,*,/,%) and numeric functions such asSUM,AVG,ABSandROUND. - 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 asUPPER,LOWER,LENGTHandCONCAT.