use sqlx::mysql::MySqlRow;
use crate::prelude::*;
impl RowExt for MySqlRow {
fn to_json(&self) -> Value {
row_to_json(self)
}
}
impl DecodeCell for MySqlRow {
fn decode_cell(&self, idx: usize, type_name: &str) -> Cell {
match type_name {
"BOOLEAN" => self.try_get(idx).map_or(Cell::Null, Cell::Bool),
"TINYINT" | "SMALLINT" | "INT" | "MEDIUMINT" | "BIGINT" | "TINYINT UNSIGNED" | "SMALLINT UNSIGNED"
| "INT UNSIGNED" | "MEDIUMINT UNSIGNED" | "YEAR" => self.try_get(idx).map_or(Cell::Null, Cell::Int),
"BIGINT UNSIGNED" => self.try_get(idx).map_or(Cell::Null, Cell::UInt),
"DECIMAL" => self.try_get(idx).map_or(Cell::Null, Cell::Decimal),
"FLOAT" => self.try_get(idx).map_or(Cell::Null, Cell::Float32),
"DOUBLE" => self.try_get(idx).map_or(Cell::Null, Cell::Float64),
"JSON" => self.try_get(idx).map_or(Cell::Null, Cell::Json),
"BLOB" | "TINYBLOB" | "MEDIUMBLOB" | "LONGBLOB" | "BIT" | "GEOMETRY" => self
.try_get(idx)
.map_or_else(|_| self.try_get(idx).map_or(Cell::Null, Cell::Bytes), Cell::Text),
"DATE" => self.try_get(idx).map_or(Cell::Null, Cell::Date),
"TIME" => self.try_get(idx).map_or(Cell::Null, Cell::Time),
"DATETIME" => self.try_get(idx).map_or(Cell::Null, Cell::DateTime),
"TIMESTAMP" => self.try_get(idx).map_or(Cell::Null, Cell::DateTimeUtc),
_ => text_or_bytes(self, idx),
}
}
}
fn text_or_bytes(row: &MySqlRow, idx: usize) -> Cell {
row.try_get::<Vec<u8>, _>(idx).map_or(Cell::Null, |bytes| {
String::from_utf8(bytes).map_or_else(|e| Cell::Bytes(e.into_bytes()), Cell::Text)
})
}