Module sqlx_mysql::types
source · Expand description
Conversions between Rust and MySQL types.
Types
Rust type | MySQL type(s) |
---|---|
bool | TINYINT(1), BOOLEAN, BOOL (see below) |
i8 | TINYINT |
i16 | SMALLINT |
i32 | INT |
i64 | BIGINT |
u8 | TINYINT UNSIGNED |
u16 | SMALLINT UNSIGNED |
u32 | INT UNSIGNED |
u64 | BIGINT UNSIGNED |
f32 | FLOAT |
f64 | DOUBLE |
&str , String | VARCHAR, CHAR, TEXT |
&[u8] , Vec<u8> | VARBINARY, BINARY, BLOB |
Note: BOOLEAN
/BOOL
Type
MySQL and MariaDB treat BOOLEAN
as an alias of the TINYINT
type:
For the most part, you can simply use the Rust type bool
when encoding or decoding a value
using the dynamic query interface, or passing a boolean as a parameter to the query macros
(query!()
et al.).
However, because the MySQL wire protocol does not distinguish between TINYINT
and BOOLEAN
,
the query macros cannot know that a TINYINT
column is semantically a boolean.
By default, they will map a TINYINT
column as i8
instead, as that is the safer assumption.
Thus, you must use the type override syntax in the query to tell the macros you are expecting
a bool
column. See the docs for query!()
and query_as!()
for details on this syntax.
chrono
Requires the chrono
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
chrono::DateTime<Utc> | TIMESTAMP |
chrono::DateTime<Local> | TIMESTAMP |
chrono::NaiveDateTime | DATETIME |
chrono::NaiveDate | DATE |
chrono::NaiveTime | TIME |
time
Requires the time
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
time::PrimitiveDateTime | DATETIME |
time::OffsetDateTime | TIMESTAMP |
time::Date | DATE |
time::Time | TIME |
bigdecimal
Requires the bigdecimal
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
bigdecimal::BigDecimal | DECIMAL |
decimal
Requires the decimal
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
rust_decimal::Decimal | DECIMAL |
uuid
Requires the uuid
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
uuid::Uuid | BINARY(16), VARCHAR, CHAR, TEXT |
uuid::fmt::Hyphenated | CHAR(36) |
uuid::fmt::Simple | CHAR(32) |
json
Requires the json
Cargo feature flag.
Rust type | MySQL type(s) |
---|---|
[Json<T> ] | JSON |
serde_json::JsonValue | JSON |
&serde_json::value::RawValue | JSON |
Nullable
In addition, Option<T>
is supported where T
implements Type
. An Option<T>
represents
a potentially NULL
value from MySQL.