Skip to main content

sqlx_json/
mysql.rs

1//! [`RowExt`](crate::RowExt) implementation for `MySQL` rows.
2//!
3//! Maps each column's `type_info().name()` to a [`Cell`]; JSON shapes are
4//! owned by [`encode`](crate::cell::encode). `DECIMAL` decodes to
5//! `Cell::Decimal`, `FLOAT` to `Cell::Float32` (sqlx-mysql strict-checks the
6//! column type), `TIMESTAMP` to `Cell::DateTimeUtc`, and binary-collation `BLOB`,
7//! `BIT`, and `GEOMETRY` to `Cell::Bytes`. Text-like columns, including `MySQL` 9's
8//! `VARBINARY` `information_schema` columns, decode as `Cell::Text` when
9//! valid UTF-8 and `Cell::Bytes` otherwise.
10
11use sqlx::mysql::MySqlRow;
12
13use crate::prelude::*;
14
15impl RowExt for MySqlRow {
16    fn to_json(&self) -> Value {
17        row_to_json(self)
18    }
19}
20
21impl DecodeCell for MySqlRow {
22    fn decode_cell(&self, idx: usize, type_name: &str) -> Cell {
23        match type_name {
24            "BOOLEAN" => self.try_get(idx).map_or(Cell::Null, Cell::Bool),
25
26            "TINYINT" | "SMALLINT" | "INT" | "MEDIUMINT" | "BIGINT" | "TINYINT UNSIGNED" | "SMALLINT UNSIGNED"
27            | "INT UNSIGNED" | "MEDIUMINT UNSIGNED" | "YEAR" => self.try_get(idx).map_or(Cell::Null, Cell::Int),
28
29            "BIGINT UNSIGNED" => self.try_get(idx).map_or(Cell::Null, Cell::UInt),
30
31            "DECIMAL" => self.try_get(idx).map_or(Cell::Null, Cell::Decimal),
32
33            "FLOAT" => self.try_get(idx).map_or(Cell::Null, Cell::Float32),
34
35            "DOUBLE" => self.try_get(idx).map_or(Cell::Null, Cell::Float64),
36
37            "JSON" => self.try_get(idx).map_or(Cell::Null, Cell::Json),
38
39            // sqlx names a column BLOB from the BINARY flag, which MySQL also sets
40            // for `_bin` collations (DESCRIBE output, MariaDB JSON). sqlx's String
41            // decode rejects only the binary collation, so true binary data falls
42            // through to bytes while `_bin` text stays text.
43            "BLOB" | "TINYBLOB" | "MEDIUMBLOB" | "LONGBLOB" | "BIT" | "GEOMETRY" => self
44                .try_get(idx)
45                .map_or_else(|_| self.try_get(idx).map_or(Cell::Null, Cell::Bytes), Cell::Text),
46
47            "DATE" => self.try_get(idx).map_or(Cell::Null, Cell::Date),
48
49            "TIME" => self.try_get(idx).map_or(Cell::Null, Cell::Time),
50
51            "DATETIME" => self.try_get(idx).map_or(Cell::Null, Cell::DateTime),
52
53            // sqlx-mysql's NaiveDateTime decoder only accepts ColumnType::Datetime,
54            // not Timestamp. DateTime<Utc> accepts both; MySQL converts TIMESTAMP
55            // to the session time zone on read, which sqlx sets to `+00:00`.
56            "TIMESTAMP" => self.try_get(idx).map_or(Cell::Null, Cell::DateTimeUtc),
57
58            // VARCHAR, TEXT, ENUM, etc., plus BINARY/VARBINARY: MySQL 9 returns
59            // information_schema columns as VARBINARY even when they hold UTF-8.
60            _ => text_or_bytes(self, idx),
61        }
62    }
63}
64
65/// Decodes a `MySQL` string-family column as UTF-8 text, falling back to raw bytes.
66fn text_or_bytes(row: &MySqlRow, idx: usize) -> Cell {
67    row.try_get::<Vec<u8>, _>(idx).map_or(Cell::Null, |bytes| {
68        String::from_utf8(bytes).map_or_else(|e| Cell::Bytes(e.into_bytes()), Cell::Text)
69    })
70}