sqlx_mysql/types/mod.rs
1//! Conversions between Rust and **MySQL/MariaDB** types.
2//!
3//! # Types
4//!
5//! | Rust type | MySQL/MariaDB type(s) |
6//! |---------------------------------------|------------------------------------------------------|
7//! | `bool` | TINYINT(1), BOOLEAN, BOOL (see below) |
8//! | `i8` | TINYINT |
9//! | `i16` | SMALLINT |
10//! | `i32` | INT |
11//! | `i64` | BIGINT |
12//! | `u8` | TINYINT UNSIGNED |
13//! | `u16` | SMALLINT UNSIGNED |
14//! | `u32` | INT UNSIGNED |
15//! | `u64` | BIGINT UNSIGNED |
16//! | `f32` | FLOAT |
17//! | `f64` | DOUBLE |
18//! | `&str`, [`String`] | VARCHAR, CHAR, TEXT |
19//! | `&[u8]`, `Vec<u8>` | VARBINARY, BINARY, BLOB |
20//! | `IpAddr` | VARCHAR, TEXT |
21//! | `Ipv4Addr` | INET4 (MariaDB-only), VARCHAR, TEXT |
22//! | `Ipv6Addr` | INET6 (MariaDB-only), VARCHAR, TEXT |
23//! | [`MySqlTime`] | TIME (encode and decode full range) |
24//! | [`Duration`][std::time::Duration] | TIME (for decoding positive values only) |
25//!
26//! ##### Note: `BOOLEAN`/`BOOL` Type
27//! MySQL and MariaDB treat `BOOLEAN` as an alias of the `TINYINT` type:
28//!
29//! * [Using Data Types from Other Database Engines (MySQL)](https://dev.mysql.com/doc/refman/8.0/en/other-vendor-data-types.html)
30//! * [BOOLEAN (MariaDB)](https://mariadb.com/kb/en/boolean/)
31//!
32//! For the most part, you can simply use the Rust type `bool` when encoding or decoding a value
33//! using the dynamic query interface, or passing a boolean as a parameter to the query macros
34//! (`query!()` _et al._).
35//!
36//! However, because the MySQL wire protocol does not distinguish between `TINYINT` and `BOOLEAN`,
37//! the query macros cannot know that a `TINYINT` column is semantically a boolean.
38//! By default, they will map a `TINYINT` column as `i8` instead, as that is the safer assumption.
39//!
40//! Thus, you must use the type override syntax in the query to tell the macros you are expecting
41//! a `bool` column. See the docs for `query!()` and `query_as!()` for details on this syntax.
42//!
43//! ### NOTE: MySQL's `TIME` type is signed
44//! MySQL's `TIME` type can be used as either a time-of-day value, or a signed interval.
45//! Thus, it may take on negative values.
46//!
47//! Decoding a [`std::time::Duration`] returns an error if the `TIME` value is negative.
48//!
49//! ### [`chrono`](https://crates.io/crates/chrono)
50//!
51//! Requires the `chrono` Cargo feature flag.
52//!
53//! | Rust type | MySQL/MariaDB type(s) |
54//! |---------------------------------------|------------------------------------------------------|
55//! | `chrono::DateTime<Utc>` | TIMESTAMP |
56//! | `chrono::DateTime<Local>` | TIMESTAMP |
57//! | `chrono::NaiveDateTime` | DATETIME |
58//! | `chrono::NaiveDate` | DATE |
59//! | `chrono::NaiveTime` | TIME (time-of-day only) |
60//! | `chrono::TimeDelta` | TIME (decodes full range; see note for encoding) |
61//!
62//! ### NOTE: MySQL's `TIME` type is dual-purpose
63//! MySQL's `TIME` type can be used as either a time-of-day value, or an interval.
64//! However, `chrono::NaiveTime` is designed only to represent a time-of-day.
65//!
66//! Decoding a `TIME` value as `chrono::NaiveTime` will return an error if the value is out of range.
67//!
68//! The [`MySqlTime`] type supports the full range and it also implements `TryInto<chrono::NaiveTime>`.
69//!
70//! Decoding a `chrono::TimeDelta` also supports the full range.
71//!
72//! To encode a `chrono::TimeDelta`, convert it to [`MySqlTime`] first using `TryFrom`/`TryInto`.
73//!
74//! ### [`time`](https://crates.io/crates/time)
75//!
76//! Requires the `time` Cargo feature flag.
77//!
78//! | Rust type | MySQL/MariaDB type(s) |
79//! |---------------------------------------|------------------------------------------------------|
80//! | `time::PrimitiveDateTime` | DATETIME |
81//! | `time::OffsetDateTime` | TIMESTAMP |
82//! | `time::Date` | DATE |
83//! | `time::Time` | TIME (time-of-day only) |
84//! | `time::Duration` | TIME (decodes full range; see note for encoding) |
85//!
86//! ### NOTE: MySQL's `TIME` type is dual-purpose
87//! MySQL's `TIME` type can be used as either a time-of-day value, or an interval.
88//! However, `time::Time` is designed only to represent a time-of-day.
89//!
90//! Decoding a `TIME` value as `time::Time` will return an error if the value is out of range.
91//!
92//! The [`MySqlTime`] type supports the full range, and it also implements `TryInto<time::Time>`.
93//!
94//! Decoding a `time::Duration` also supports the full range.
95//!
96//! To encode a `time::Duration`, convert it to [`MySqlTime`] first using `TryFrom`/`TryInto`.
97//!
98//! ### [`bigdecimal`](https://crates.io/crates/bigdecimal)
99//! Requires the `bigdecimal` Cargo feature flag.
100//!
101//! | Rust type | MySQL/MariaDB type(s) |
102//! |---------------------------------------|------------------------------------------------------|
103//! | `bigdecimal::BigDecimal` | DECIMAL |
104//!
105//! ### [`decimal`](https://crates.io/crates/rust_decimal)
106//! Requires the `decimal` Cargo feature flag.
107//!
108//! | Rust type | MySQL/MariaDB type(s) |
109//! |---------------------------------------|------------------------------------------------------|
110//! | `rust_decimal::Decimal` | DECIMAL |
111//!
112//! ### [`uuid`](https://crates.io/crates/uuid)
113//!
114//! Requires the `uuid` Cargo feature flag.
115//!
116//! | Rust type | MySQL/MariaDB type(s) |
117//! |---------------------------------------|------------------------------------------------------|
118//! | `uuid::Uuid` | BINARY(16) (see note) |
119//! | `uuid::fmt::Hyphenated` | CHAR(36), VARCHAR, TEXT, UUID (MariaDB-only) |
120//! | `uuid::fmt::Simple` | CHAR(32), VARCHAR, TEXT |
121//!
122//! #### Note: `Uuid` uses binary format
123//!
124//! MySQL does not have a native datatype for UUIDs.
125//! The `UUID()` function returns a 36-character `TEXT` value,
126//! which encourages storing UUIDs as text.
127//!
128//! MariaDB's `UUID` type stores and retrieves as text, though it has a better representation
129//! for index sorting (see [MariaDB manual: UUID data-type][mariadb-uuid] for details).
130//!
131//! As an opinionated library, SQLx chose to map `uuid::Uuid` to/from binary format by default
132//! (16 bytes, the raw value of a UUID; SQL type `BINARY(16)`).
133//! This saves 20 bytes over the text format for each value.
134//!
135//! The `impl Decode<MySql> for Uuid` does not support the text format, and will return an error.
136//!
137//! If you want to use the text format compatible with the `UUID()` function,
138//! use [`uuid::fmt::Hyphenated`][::uuid::fmt::Hyphenated] in the place of `Uuid`.
139//!
140//! The MySQL official blog has an article showing how to support both binary and text format UUIDs
141//! by storing the binary and adding a generated column for the text format, though this is rather
142//! verbose and fiddly: <https://dev.mysql.com/blog-archive/storing-uuid-values-in-mysql-tables/>
143//!
144//! [mariadb-uuid]: https://mariadb.com/kb/en/uuid-data-type/
145//!
146//! ### [`json`](https://crates.io/crates/serde_json)
147//!
148//! Requires the `json` Cargo feature flag.
149//!
150//! | Rust type | MySQL/MariaDB type(s) |
151//! |---------------------------------------|------------------------------------------------------|
152//! | [`Json<T>`] | JSON |
153//! | `serde_json::JsonValue` | JSON |
154//! | `&serde_json::value::RawValue` | JSON |
155//!
156//! # Nullable
157//!
158//! In addition, `Option<T>` is supported where `T` implements `Type`. An `Option<T>` represents
159//! a potentially `NULL` value from MySQL/MariaDB.
160
161pub(crate) use sqlx_core::types::*;
162
163pub use mysql_time::{MySqlTime, MySqlTimeError, MySqlTimeSign};
164
165mod bool;
166mod bytes;
167mod float;
168mod inet;
169mod int;
170mod mysql_time;
171mod str;
172mod text;
173mod uint;
174
175#[cfg(feature = "json")]
176mod json;
177
178#[cfg(feature = "bigdecimal")]
179mod bigdecimal;
180
181#[cfg(feature = "rust_decimal")]
182mod rust_decimal;
183
184#[cfg(feature = "chrono")]
185mod chrono;
186
187#[cfg(feature = "time")]
188mod time;
189
190#[cfg(feature = "uuid")]
191mod uuid;