Module sqlx_mysql::types

source ·
Expand description

Conversions between Rust and MySQL/MariaDB types.

§Types

Rust typeMySQL/MariaDB type(s)
boolTINYINT(1), BOOLEAN, BOOL (see below)
i8TINYINT
i16SMALLINT
i32INT
i64BIGINT
u8TINYINT UNSIGNED
u16SMALLINT UNSIGNED
u32INT UNSIGNED
u64BIGINT UNSIGNED
f32FLOAT
f64DOUBLE
&str, StringVARCHAR, CHAR, TEXT
&[u8], Vec<u8>VARBINARY, BINARY, BLOB
IpAddrVARCHAR, TEXT
Ipv4AddrINET4 (MariaDB-only), VARCHAR, TEXT
Ipv6AddrINET6 (MariaDB-only), VARCHAR, TEXT
MySqlTimeTIME (encode and decode full range)
DurationTIME (for decoding positive values only)
§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.

§NOTE: MySQL’s TIME type is signed

MySQL’s TIME type can be used as either a time-of-day value, or a signed interval. Thus, it may take on negative values.

Decoding a std::time::Duration returns an error if the TIME value is negative.

§chrono

Requires the chrono Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
chrono::DateTime<Utc>TIMESTAMP
chrono::DateTime<Local>TIMESTAMP
chrono::NaiveDateTimeDATETIME
chrono::NaiveDateDATE
chrono::NaiveTimeTIME (time-of-day only)
chrono::TimeDeltaTIME (decodes full range; see note for encoding)

§NOTE: MySQL’s TIME type is dual-purpose

MySQL’s TIME type can be used as either a time-of-day value, or an interval. However, chrono::NaiveTime is designed only to represent a time-of-day.

Decoding a TIME value as chrono::NaiveTime will return an error if the value is out of range.

The MySqlTime type supports the full range and it also implements TryInto<chrono::NaiveTime>.

Decoding a chrono::TimeDelta also supports the full range.

To encode a chrono::TimeDelta, convert it to MySqlTime first using TryFrom/TryInto.

§time

Requires the time Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
time::PrimitiveDateTimeDATETIME
time::OffsetDateTimeTIMESTAMP
time::DateDATE
time::TimeTIME (time-of-day only)
time::DurationTIME (decodes full range; see note for encoding)

§NOTE: MySQL’s TIME type is dual-purpose

MySQL’s TIME type can be used as either a time-of-day value, or an interval. However, time::Time is designed only to represent a time-of-day.

Decoding a TIME value as time::Time will return an error if the value is out of range.

The MySqlTime type supports the full range, and it also implements TryInto<time::Time>.

Decoding a time::Duration also supports the full range.

To encode a time::Duration, convert it to MySqlTime first using TryFrom/TryInto.

§bigdecimal

Requires the bigdecimal Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
bigdecimal::BigDecimalDECIMAL

§decimal

Requires the decimal Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
rust_decimal::DecimalDECIMAL

§uuid

Requires the uuid Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
uuid::UuidBINARY(16), VARCHAR, CHAR, TEXT
uuid::fmt::HyphenatedCHAR(36), UUID (MariaDB-only)
uuid::fmt::SimpleCHAR(32)

§json

Requires the json Cargo feature flag.

Rust typeMySQL/MariaDB type(s)
[Json<T>]JSON
serde_json::JsonValueJSON
&serde_json::value::RawValueJSON

§Nullable

In addition, Option<T> is supported where T implements Type. An Option<T> represents a potentially NULL value from MySQL/MariaDB.

Structs§

  • Container for a MySQL TIME value, which may be an interval or a time-of-day.

Enums§