use super::common::common_helpers;
use crate::abstract_layer::DbType;
use crate::model::{DbBackendTypeMapper, Model, Row, Value};
use crate::query::builder::{
FourTableSelect, GroupedSelect, InnerJoinedSelect, LeftJoinedSelect, MultiTableSelect,
RelatedSelect, RightJoinedSelect, Select, WhereExpr,
};
use crate::query::filter::FilterExpr;
use chrono::{Datelike, Timelike};
use mysql_async::Pool;
use mysql_async::prelude::*;
use std::collections::HashMap;
use std::marker::PhantomData;
pub struct MySQLTypeMapper;
impl DbBackendTypeMapper for MySQLTypeMapper {
fn sql_type(
rust_type: &str,
is_primary: bool,
is_auto_increment: bool,
is_nullable: bool,
enum_variants: Option<&[&str]>,
) -> String {
if let Some(variants) = enum_variants {
let variants_str = variants
.iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<_>>()
.join(", ");
let mut sql_type = format!("ENUM({})", variants_str);
if !is_nullable {
sql_type.push_str(" NOT NULL");
}
return sql_type;
}
if is_primary {
let int_type = match rust_type {
"i8" | "i16" | "u8" => "TINYINT",
"i32" | "u16" => "INT",
"i64" | "u32" | "u64" => "BIGINT",
_ => "INT", };
if is_auto_increment {
return format!("{int_type} PRIMARY KEY AUTO_INCREMENT");
} else {
return format!("{int_type} PRIMARY KEY");
}
}
let base_type = match rust_type {
"i8" => "TINYINT",
"i16" => "SMALLINT",
"i32" => "INT",
"i64" => "BIGINT",
"u8" => "TINYINT UNSIGNED",
"u16" => "SMALLINT UNSIGNED",
"u32" => "INT UNSIGNED",
"u64" => "BIGINT UNSIGNED",
"f32" => "FLOAT",
"f64" => "DOUBLE",
"String" => "VARCHAR(255)",
"bool" => "TINYINT(1)",
"Vec<u8>" | "&[u8]" => "BLOB",
"DateTime" | "chrono::DateTime" | "NaiveDateTime" | "chrono::NaiveDateTime" => {
"DATETIME"
}
"NaiveDate" | "chrono::NaiveDate" => "DATE",
"NaiveTime" | "chrono::NaiveTime" => "TIME",
"JsonValue" | "serde_json::Value" => "JSON",
_ => "TEXT",
};
let mut sql_type = base_type.to_string();
if !is_nullable {
sql_type.push_str(" NOT NULL");
}
sql_type
}
}
pub struct Database {
pool: Pool,
}
pub struct CreateTableExecutor<'a, T: Model> {
pool: &'a Pool,
table_name: Option<String>,
_marker: std::marker::PhantomData<T>,
}
impl<'a, T: Model> CreateTableExecutor<'a, T> {
pub async fn execute(self) -> anyhow::Result<()> {
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let create_sql = crate::generate_create_table_sql_with_name::<T>(
crate::abstract_layer::DbType::MySQL,
self.table_name.as_deref(),
)?;
conn.query_drop(&create_sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
}
pub struct DropTableExecutor<'a, T: Model> {
pool: &'a Pool,
_marker: std::marker::PhantomData<T>,
}
impl<'a, T: Model> DropTableExecutor<'a, T> {
pub async fn execute(self) -> anyhow::Result<()> {
let sql = format!("DROP TABLE IF EXISTS {}", T::TABLE_NAME);
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
conn.query_drop(&sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
}
pub struct InsertExecutor<'a, I: crate::model::Insertable> {
pool: &'a Pool,
models: I,
_marker: std::marker::PhantomData<I::Model>,
}
impl<'a, I: crate::model::Insertable> InsertExecutor<'a, I> {
pub async fn execute(self) -> anyhow::Result<()> {
let refs = self.models.as_refs();
self.insert_impl::<I::Model>(&refs).await
}
async fn insert_impl<T: Model>(&self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let columns = T::insert_columns();
let (sql, _) = super::common::common_helpers::build_batch_insert_sql_with_columns(
T::TABLE_NAME,
&columns,
models.len(),
);
let all_values =
super::common::common_helpers::collect_batch_insert_values_with_auto_increment::<T>(
models,
);
let params = values_to_params(&all_values)?;
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
}
pub struct InsertOrUpdateExecutor<'a, I: crate::model::Insertable> {
pool: &'a Pool,
models: I,
_marker: std::marker::PhantomData<I::Model>,
}
impl<'a, I: crate::model::Insertable> InsertOrUpdateExecutor<'a, I> {
pub async fn execute(self) -> anyhow::Result<()> {
let refs = self.models.as_refs();
self.insert_or_update_batch::<I::Model>(&refs).await
}
async fn insert_or_update_batch<T: Model>(&self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let columns = T::COLUMNS.join(", ");
let col_count = T::COLUMNS.len();
let mut sql = format!("INSERT INTO {} ({columns}) VALUES ", T::TABLE_NAME);
let mut all_values = Vec::new();
for (idx, model) in models.iter().enumerate() {
if idx > 0 {
sql.push_str(", ");
}
let placeholders: Vec<String> = (1..=col_count).map(|_| "?".to_string()).collect();
sql.push_str(&format!("({})", placeholders.join(", ")));
let values = model.field_values();
all_values.extend(values);
}
sql.push_str(" ON DUPLICATE KEY UPDATE ");
let mut first = true;
for col_name in T::COLUMNS.iter() {
if !first {
sql.push_str(", ");
}
sql.push_str(&format!("{col_name} = VALUES({col_name})"));
first = false;
}
let params = values_to_params(&all_values)?;
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
}
impl Database {
pub async fn connect(_db_type: super::DbType, connection_string: &str) -> anyhow::Result<Self> {
let opts = mysql_async::Opts::from_url(connection_string)
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let pool = Pool::new(opts);
Ok(Self { pool })
}
pub fn create_table<T: Model>(&self) -> CreateTableExecutor<'_, T> {
CreateTableExecutor {
pool: &self.pool,
table_name: None,
_marker: std::marker::PhantomData,
}
}
pub async fn validate_table<T: Model>(&self) -> anyhow::Result<()> {
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let table_exists = self.check_table_exists::<T>(&mut conn).await?;
if !table_exists {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Table does not exist",
T::TABLE_NAME
));
}
self.validate_table_schema::<T>(&mut conn).await
}
async fn check_table_exists<T: Model>(
&self,
conn: &mut mysql_async::Conn,
) -> anyhow::Result<bool> {
let sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?";
let result: Option<u64> = conn
.exec_first(sql, (T::TABLE_NAME,))
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(result.unwrap_or(0) > 0)
}
async fn validate_table_schema<T: Model>(
&self,
conn: &mut mysql_async::Conn,
) -> anyhow::Result<()> {
let sql = r#"
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?
ORDER BY ORDINAL_POSITION
"#;
let rows: Vec<mysql_async::Row> = conn
.exec(sql, (T::TABLE_NAME,))
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut actual_columns: Vec<(String, String, bool)> = Vec::new();
for row in rows {
let name: String = row.get(0).unwrap_or_default();
let col_type: String = row.get(1).unwrap_or_default();
let is_nullable: String = row.get(2).unwrap_or_default();
actual_columns.push((name, col_type, is_nullable == "YES"));
}
if actual_columns.len() != T::COLUMNS.len() {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Column count mismatch: expected {}, but actual is {}",
T::TABLE_NAME,
T::COLUMNS.len(),
actual_columns.len()
));
}
for (i, expected_col) in T::COLUMN_SCHEMA.iter().enumerate() {
if i >= actual_columns.len() {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Missing column: {}",
T::TABLE_NAME,
expected_col.name
));
}
let (actual_name, actual_type, actual_nullable) = &actual_columns[i];
if actual_name != expected_col.name {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Column name mismatch at position {}: expected '{}', but actual is '{}'",
T::TABLE_NAME,
i,
expected_col.name,
actual_name
));
}
let expected_type = crate::abstract_layer::DbType::MySQL.sql_type(
expected_col.rust_type,
expected_col.is_primary,
expected_col.is_auto_increment,
expected_col.is_nullable,
expected_col.enum_variants,
);
let type_to_compare = if expected_col.is_primary {
match expected_col.rust_type {
"i8" | "i16" | "u8" => "TINYINT".to_string(),
"i32" | "u16" => "INT".to_string(),
"i64" | "u32" | "u64" => "BIGINT".to_string(),
_ => "INT".to_string(),
}
} else {
let full_type = crate::abstract_layer::DbType::MySQL.sql_type(
expected_col.rust_type,
false,
expected_col.is_auto_increment,
expected_col.is_nullable,
expected_col.enum_variants,
);
full_type.replace(" NOT NULL", "")
};
if !self.types_compatible(actual_type, &type_to_compare) {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Column type mismatch for '{}': expected '{expected_type}', but actual is '{actual_type}'",
T::TABLE_NAME,
expected_col.name
));
}
if !expected_col.is_primary {
let expected_nullable = expected_col.is_nullable;
if *actual_nullable != expected_nullable {
return Err(anyhow::anyhow!(
"Schema mismatch: table {}, reason: Column nullability mismatch for '{}': expected {}NULL, but actual is {}NULL",
T::TABLE_NAME,
expected_col.name,
if expected_nullable { "" } else { "NOT " },
if *actual_nullable { "" } else { "NOT " }
));
}
}
}
Ok(())
}
fn types_compatible(&self, actual: &str, expected: &str) -> bool {
fn normalize(s: &str) -> String {
let upper = s.to_uppercase();
let base_type = if let Some(pos) = upper.find('(') {
&upper[..pos]
} else {
&upper[..]
};
match base_type {
"TINYINT" => "TINYINT".to_string(),
"SMALLINT" => "SMALLINT".to_string(),
"MEDIUMINT" => "MEDIUMINT".to_string(),
"INT" | "INTEGER" => "INT".to_string(),
"BIGINT" => "BIGINT".to_string(),
t if t.ends_with(" UNSIGNED") => {
let unsigned_type = t.replace(" ", "");
match unsigned_type.as_str() {
"TINYINTUNSIGNED" => "TINYINT UNSIGNED".to_string(),
"SMALLINTUNSIGNED" => "SMALLINT UNSIGNED".to_string(),
"MEDIUMINTUNSIGNED" => "MEDIUMINT UNSIGNED".to_string(),
"INTUNSIGNED" | "INTEGERUNSIGNED" => "INT UNSIGNED".to_string(),
"BIGINTUNSIGNED" => "BIGINT UNSIGNED".to_string(),
_ => t.to_string(),
}
}
"FLOAT" => "FLOAT".to_string(),
"DOUBLE" | "DOUBLEPRECISION" => "DOUBLE".to_string(),
"VARCHAR" | "CHAR" | "TEXT" | "TINYTEXT" | "MEDIUMTEXT" | "LONGTEXT" => {
"VARCHAR".to_string()
}
"BOOL" | "BOOLEAN" => "TINYINT".to_string(),
"BLOB" | "TINYBLOB" | "MEDIUMBLOB" | "LONGBLOB" | "VARBINARY" | "BINARY" => {
"BLOB".to_string()
}
_ => base_type.to_string(),
}
}
normalize(actual) == normalize(expected)
}
pub fn insert<I: crate::model::Insertable>(&self, models: I) -> InsertExecutor<'_, I> {
InsertExecutor {
pool: &self.pool,
models,
_marker: std::marker::PhantomData,
}
}
pub fn insert_or_update<I: crate::model::Insertable>(
&self,
models: I,
) -> InsertOrUpdateExecutor<'_, I> {
InsertOrUpdateExecutor {
pool: &self.pool,
models,
_marker: std::marker::PhantomData,
}
}
pub(crate) async fn insert_impl<T: Model>(&self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let columns = T::insert_columns();
let (sql, _) = super::common::common_helpers::build_batch_insert_sql_with_columns(
T::TABLE_NAME,
&columns,
models.len(),
);
let all_values =
super::common::common_helpers::collect_batch_insert_values_with_auto_increment::<T>(
models,
);
let params = values_to_params(&all_values)?;
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
pub async fn insert_or_update_batch<T: Model>(&self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let columns = T::COLUMNS.join(", ");
let col_count = T::COLUMNS.len();
let mut sql = format!("INSERT INTO {} ({columns}) VALUES ", T::TABLE_NAME);
let mut all_values = Vec::new();
for (idx, model) in models.iter().enumerate() {
if idx > 0 {
sql.push_str(", ");
}
let placeholders: Vec<String> = (1..=col_count).map(|_| "?".to_string()).collect();
sql.push_str(&format!("({})", placeholders.join(", ")));
let values = model.field_values();
all_values.extend(values);
}
sql.push_str(" ON DUPLICATE KEY UPDATE ");
let mut first = true;
for col_name in T::COLUMNS.iter() {
if !first {
sql.push_str(", ");
}
sql.push_str(&format!("{col_name} = VALUES({col_name})"));
first = false;
}
let params = values_to_params(&all_values)?;
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(())
}
pub fn select<T: Model>(&self) -> SelectExecutor<'_, T> {
SelectExecutor {
select: Select::<T>::new(),
pool: &self.pool,
_marker: PhantomData,
}
}
pub fn select_column<T: Model, V>(&self) -> GroupedSelectExecutor<'_, T, V> {
GroupedSelectExecutor {
select: GroupedSelect::<T, V>::new(),
pool: &self.pool,
_marker: PhantomData,
}
}
pub fn delete<T: Model>(&self) -> DeleteExecutor<'_, T> {
DeleteExecutor {
filters: Vec::new(),
pool: &self.pool,
_marker: PhantomData,
}
}
pub fn update<T: Model>(&self) -> UpdateExecutor<'_, T> {
UpdateExecutor {
sets: Vec::new(),
filters: Vec::new(),
pool: &self.pool,
_marker: PhantomData,
}
}
pub fn related<T: Model + 'static, R: Model>(&self) -> RelatedSelectExecutor<'_, T, R> {
RelatedSelectExecutor {
select: Select::<T>::new().from::<T, R>(),
pool: &self.pool,
_marker: PhantomData,
}
}
pub async fn begin(&self) -> anyhow::Result<Transaction<'_>> {
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
conn.query_drop("START TRANSACTION")
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(Transaction {
conn: Some(conn),
pool: &self.pool,
committed: false,
rolled_back: false,
})
}
pub fn drop_table<T: Model>(&self) -> DropTableExecutor<'_, T> {
DropTableExecutor {
pool: &self.pool,
_marker: std::marker::PhantomData,
}
}
pub async fn execute<T: Model>(&self, sql: &str) -> anyhow::Result<Vec<T>> {
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let rows: Vec<mysql_async::Row> = conn
.query(sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut results = Vec::new();
for row in rows {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let ormer_value = convert_mysql_value(&row, i)?;
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = Row::new(data);
let model = T::from_row(&ormer_row)?;
results.push(model);
}
Ok(results)
}
#[deprecated(since = "0.1.0", note = "请使用 execute 方法")]
pub async fn exec_table<T: Model>(&self, sql: &str) -> anyhow::Result<Vec<T>> {
self.execute::<T>(sql).await
}
pub async fn exec_non_query(&self, sql: &str) -> anyhow::Result<u64> {
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
conn.query_drop(sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let affected_rows = conn.affected_rows();
Ok(affected_rows)
}
pub async fn is_valid(&self) -> bool {
if let Ok(mut conn) = self.pool.get_conn().await {
conn.query_drop("SELECT 1").await.is_ok()
} else {
false
}
}
}
pub struct Transaction<'a> {
conn: Option<mysql_async::Conn>,
pool: &'a Pool,
committed: bool,
rolled_back: bool,
}
pub struct TransactionInsertExecutor<'a, I: crate::model::Insertable> {
conn: &'a mut Option<mysql_async::Conn>,
models: I,
_marker: std::marker::PhantomData<&'a ()>,
}
impl<'a, I: crate::model::Insertable> TransactionInsertExecutor<'a, I> {
pub async fn execute(self) -> anyhow::Result<()> {
let refs = self.models.as_refs();
if refs.is_empty() {
return Ok(());
}
let columns = I::Model::insert_columns();
let (sql, _) = super::common::common_helpers::build_batch_insert_sql_with_columns(
I::Model::TABLE_NAME,
&columns,
refs.len(),
);
let all_values =
super::common::common_helpers::collect_batch_insert_values_with_auto_increment::<
I::Model,
>(&refs);
let params = values_to_params(&all_values)?;
if let Some(conn) = self.conn.as_mut() {
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
}
Ok(())
}
}
pub struct TransactionInsertOrUpdateExecutor<'a, I: crate::model::Insertable> {
conn: &'a mut Option<mysql_async::Conn>,
models: I,
_marker: std::marker::PhantomData<&'a ()>,
}
impl<'a, I: crate::model::Insertable> TransactionInsertOrUpdateExecutor<'a, I> {
pub async fn execute(self) -> anyhow::Result<()> {
let refs = self.models.as_refs();
if refs.is_empty() {
return Ok(());
}
let columns = I::Model::COLUMNS.join(", ");
let col_count = I::Model::COLUMNS.len();
let mut sql = format!("INSERT INTO {} ({columns}) VALUES ", I::Model::TABLE_NAME);
let mut all_values = Vec::new();
for (idx, model) in refs.iter().enumerate() {
if idx > 0 {
sql.push_str(", ");
}
let placeholders: Vec<String> = (1..=col_count).map(|_| "?".to_string()).collect();
sql.push_str(&format!("({})", placeholders.join(", ")));
let values = model.field_values();
all_values.extend(values);
}
sql.push_str(" ON DUPLICATE KEY UPDATE ");
let mut first = true;
for col_name in I::Model::COLUMNS.iter() {
if !first {
sql.push_str(", ");
}
sql.push_str(&format!("{col_name} = VALUES({col_name})"));
first = false;
}
let params = values_to_params(&all_values)?;
if let Some(conn) = self.conn.as_mut() {
conn.exec_drop(&sql, params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
}
Ok(())
}
}
impl<'a> Transaction<'a> {
pub async fn commit(mut self) -> anyhow::Result<()> {
if self.committed || self.rolled_back {
return Err(anyhow::anyhow!(
"Transaction already committed or rolled back".to_string(),
));
}
if let Some(mut conn) = self.conn.take() {
conn.query_drop("COMMIT")
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
}
self.committed = true;
Ok(())
}
pub async fn rollback(mut self) -> anyhow::Result<()> {
if self.committed || self.rolled_back {
return Err(anyhow::anyhow!(
"Transaction already committed or rolled back".to_string(),
));
}
if let Some(mut conn) = self.conn.take() {
conn.query_drop("ROLLBACK")
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
}
self.rolled_back = true;
Ok(())
}
pub fn select<T: Model>(&self) -> SelectExecutor<'_, T> {
SelectExecutor {
select: Select::<T>::new(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn select_column<T: Model, V>(&self) -> GroupedSelectExecutor<'_, T, V> {
GroupedSelectExecutor {
select: GroupedSelect::<T, V>::new(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn delete<T: Model>(&self) -> DeleteExecutor<'_, T> {
DeleteExecutor {
filters: Vec::new(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn update<T: Model>(&self) -> UpdateExecutor<'_, T> {
UpdateExecutor {
sets: Vec::new(),
filters: Vec::new(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn insert<I: crate::model::Insertable>(
&mut self,
models: I,
) -> TransactionInsertExecutor<'_, I> {
TransactionInsertExecutor {
conn: &mut self.conn,
models,
_marker: std::marker::PhantomData,
}
}
pub fn insert_or_update<I: crate::model::Insertable>(
&mut self,
models: I,
) -> TransactionInsertOrUpdateExecutor<'_, I> {
TransactionInsertOrUpdateExecutor {
conn: &mut self.conn,
models,
_marker: std::marker::PhantomData,
}
}
#[allow(dead_code)]
async fn insert_impl<T: Model>(&mut self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let columns = T::insert_columns();
let (sql, _) = super::common::common_helpers::build_batch_insert_sql_with_columns(
T::TABLE_NAME,
&columns,
models.len(),
);
let all_values =
super::common::common_helpers::collect_batch_insert_values_with_auto_increment::<T>(
models,
);
let params = values_to_params(&all_values)?;
if let Some(ref mut conn) = self.conn {
conn.exec_drop(&sql, params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
}
Ok(())
}
pub async fn insert_or_update_batch<T: Model>(&mut self, models: &[&T]) -> anyhow::Result<()> {
if models.is_empty() {
return Ok(());
}
let columns = T::COLUMNS.join(", ");
let col_count = T::COLUMNS.len();
let mut sql = format!("INSERT INTO {} ({columns}) VALUES ", T::TABLE_NAME);
let mut all_values = Vec::new();
for (idx, model) in models.iter().enumerate() {
if idx > 0 {
sql.push_str(", ");
}
let placeholders: Vec<String> = (1..=col_count).map(|_| "?".to_string()).collect();
sql.push_str(&format!("({})", placeholders.join(", ")));
let values = model.field_values();
all_values.extend(values);
}
sql.push_str(" ON DUPLICATE KEY UPDATE ");
let mut first = true;
for col_name in T::COLUMNS.iter() {
if !first {
sql.push_str(", ");
}
sql.push_str(&format!("{col_name} = VALUES({col_name})"));
first = false;
}
let params = values_to_params(&all_values)?;
if let Some(ref mut conn) = self.conn {
conn.exec_drop(&sql, params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
}
Ok(())
}
}
pub struct LeftJoinedSelectExecutor<'a, T: Model, J: Model> {
select: LeftJoinedSelect<T, J>,
pool: &'a Pool,
_marker: PhantomData<(T, J)>,
}
pub struct InnerJoinedSelectExecutor<'a, T: Model, J: Model> {
select: InnerJoinedSelect<T, J>,
pool: &'a Pool,
_marker: PhantomData<(T, J)>,
}
pub struct RightJoinedSelectExecutor<'a, T: Model, J: Model> {
select: RightJoinedSelect<T, J>,
pool: &'a Pool,
_marker: PhantomData<(T, J)>,
}
pub struct SelectExecutor<'a, T: Model> {
select: Select<T>,
pool: &'a Pool,
_marker: PhantomData<T>,
}
pub struct MappedSelectExecutor<'a, T: Model, V> {
select: crate::query::builder::MappedSelect<T, V>,
pool: &'a Pool,
_marker: PhantomData<(T, V)>,
}
pub struct GroupedSelectExecutor<'a, T: Model, V> {
select: GroupedSelect<T, V>,
pool: &'a Pool,
_marker: PhantomData<(T, V)>,
}
impl<'a, T: Model, V> MappedSelectExecutor<'a, T, V> {
pub fn to_subquery_sql(&self) -> (String, Vec<crate::model::Value>) {
self.select.to_sql_with_params(DbType::MySQL)
}
pub fn collect<C: FromIterator<V> + 'static>(&self) -> MappedCollectFuture<'a, T, V, C>
where
T: 'static,
V: crate::model::FromRowValues + 'static,
{
MappedCollectFuture {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn clone_with_pool(&self) -> Self {
Self {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
}
pub struct MappedCollectFuture<'a, T: Model, V, C> {
select: crate::query::builder::MappedSelect<T, V>,
pool: &'a Pool,
_marker: PhantomData<(T, V, C)>,
}
impl<'a, T: Model + 'static, V: crate::model::FromRowValues + 'static, C: FromIterator<V> + 'static>
std::future::IntoFuture for MappedCollectFuture<'a, T, V, C>
{
type Output = anyhow::Result<C>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(format!("Failed to get connection: {}", e)))?;
let mysql_params: Vec<mysql_async::Value> = params
.into_iter()
.map(|v| match v {
crate::model::Value::Integer(i) => mysql_async::Value::Int(i),
crate::model::Value::Text(t) => mysql_async::Value::Bytes(t.into_bytes()),
crate::model::Value::Real(r) => mysql_async::Value::Double(r),
crate::model::Value::Boolean(b) => {
mysql_async::Value::Int(if b { 1 } else { 0 })
}
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let rows: Vec<mysql_async::Row> = if mysql_params.is_empty() {
conn.query(&sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?
} else {
conn.exec(&sql, mysql_async::Params::Positional(mysql_params))
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?
};
let mut results = Vec::new();
for row in rows {
let mut values = Vec::new();
for i in 0..row.columns_ref().len() {
let value = convert_mysql_value(&row, i)?;
values.push(value);
}
let v = V::from_row_values(&values)?;
results.push(v);
}
Ok(results.into_iter().collect())
})
}
}
impl<'a, T: Model> SelectExecutor<'a, T> {
pub fn clone_with_pool(&self) -> Self {
Self {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn order_by<F, O>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> O,
O: Into<crate::OrderBy>,
{
Self {
select: self.select.order_by(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn order_by_desc<F, O>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> O,
O: Into<crate::OrderBy>,
{
Self {
select: self.select.order_by_desc(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn left_join<J: Model>(
self,
f: impl FnOnce(T::Where, J::Where) -> WhereExpr,
) -> LeftJoinedSelectExecutor<'a, T, J> {
LeftJoinedSelectExecutor {
select: self.select.left_join::<J>(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn inner_join<J: Model>(
self,
f: impl FnOnce(T::Where, J::Where) -> WhereExpr,
) -> InnerJoinedSelectExecutor<'a, T, J> {
InnerJoinedSelectExecutor {
select: self.select.inner_join::<J>(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn right_join<J: Model>(
self,
f: impl FnOnce(T::Where, J::Where) -> WhereExpr,
) -> RightJoinedSelectExecutor<'a, T, J> {
RightJoinedSelectExecutor {
select: self.select.right_join::<J>(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn map_to<F, M>(self, f: F) -> MappedSelectExecutor<'a, T, M::Output>
where
F: FnOnce(T::Where) -> M,
M: crate::query::builder::MapToResult,
{
let mapped_select = self.select.map_to(f);
MappedSelectExecutor {
select: mapped_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn select_column<F, V>(self, f: F) -> GroupedSelectExecutor<'a, T, V>
where
F: FnOnce(T::Where) -> V,
V: crate::query::builder::SelectColumnResult,
{
let grouped_select = self.select.select_column(f);
GroupedSelectExecutor {
select: grouped_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn collect<C: FromIterator<T> + 'static>(&self) -> CollectFuture<'a, T, C> {
CollectFuture {
executor: self.clone_with_pool(),
_marker: PhantomData,
}
}
pub fn count<F, C>(self, f: F) -> AggregateFuture<'a, T, usize>
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::TypedColumn<C>,
{
let aggregate_select = self.select.count(f);
AggregateFuture {
aggregate_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn sum<F, C>(self, f: F) -> AggregateFuture<'a, T, C::Output>
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::TypedColumn<C>,
C: crate::query::builder::AggregateResultType + 'static,
{
let aggregate_select = self.select.sum(f);
AggregateFuture {
aggregate_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn avg<F, C>(self, f: F) -> AggregateFuture<'a, T, Option<f64>>
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::TypedColumn<C>,
C: crate::query::builder::AggregateResultType + 'static,
{
let aggregate_select = self.select.avg(f);
AggregateFuture {
aggregate_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn max<F, C>(self, f: F) -> AggregateFuture<'a, T, C::Output>
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::TypedColumn<C>,
C: crate::query::builder::AggregateResultType + 'static,
{
let aggregate_select = self.select.max(f);
AggregateFuture {
aggregate_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn min<F, C>(self, f: F) -> AggregateFuture<'a, T, C::Output>
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::TypedColumn<C>,
C: crate::query::builder::AggregateResultType + 'static,
{
let aggregate_select = self.select.min(f);
AggregateFuture {
aggregate_select,
pool: self.pool,
_marker: PhantomData,
}
}
pub fn from<T2, R: Model>(self) -> RelatedSelectExecutor<'a, T, R>
where
T2: Model + 'static,
{
RelatedSelectExecutor {
select: self.select.from::<T2, R>(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn from3<T2, R1: Model, R2: Model>(self) -> MultiTableSelectExecutor<'a, T, R1, R2>
where
T2: Model + 'static,
{
MultiTableSelectExecutor {
select: self.select.from3::<T2, R1, R2>(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn from4<T2, R1: Model, R2: Model, R3: Model>(
self,
) -> FourTableSelectExecutor<'a, T, R1, R2, R3>
where
T2: Model + 'static,
{
FourTableSelectExecutor {
select: self.select.from4::<T2, R1, R2, R3>(),
pool: self.pool,
_marker: PhantomData,
}
}
}
pub struct CollectFuture<'a, T: Model, C: FromIterator<T>> {
executor: SelectExecutor<'a, T>,
_marker: PhantomData<C>,
}
pub struct AggregateFuture<'a, T: Model, R> {
aggregate_select: crate::query::builder::AggregateSelect<T, R>,
pool: &'a Pool,
_marker: PhantomData<(T, R)>,
}
impl<'a, T: Model + 'static, R: crate::model::FromValue + 'static> std::future::IntoFuture
for AggregateFuture<'a, T, R>
{
type Output = anyhow::Result<R>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let (sql, params) = self.aggregate_select.to_sql_with_params(DbType::MySQL);
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let mysql_params: Vec<mysql_async::Value> = params
.into_iter()
.map(|v| match v {
crate::model::Value::Integer(i) => mysql_async::Value::Int(i),
crate::model::Value::Text(t) => mysql_async::Value::Bytes(t.into_bytes()),
crate::model::Value::Real(r) => mysql_async::Value::Double(r),
crate::model::Value::Boolean(b) => {
mysql_async::Value::Int(if b { 1 } else { 0 })
}
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let mut exec_result =
conn.exec_iter(&sql, mysql_params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
if let Some(row) = exec_result.next().await.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})? {
let value: Option<mysql_async::Value> = row.get(0);
let value = value.unwrap_or(mysql_async::Value::NULL);
println!(
"DEBUG MySQL aggregate raw value type: {:?}",
std::mem::discriminant(&value)
);
let ormer_value = match value {
mysql_async::Value::Int(i) => crate::model::Value::Integer(i),
mysql_async::Value::UInt(u) => crate::model::Value::Integer(u as i64),
mysql_async::Value::Float(f) => crate::model::Value::Real(f as f64),
mysql_async::Value::Double(d) => crate::model::Value::Real(d),
mysql_async::Value::Bytes(b) => {
if let Ok(s) = String::from_utf8(b.clone()) {
if let Ok(i) = s.parse::<i64>() {
crate::model::Value::Integer(i)
} else if let Ok(f) = s.parse::<f64>() {
crate::model::Value::Real(f)
} else {
crate::model::Value::Text(s)
}
} else {
crate::model::Value::Null
}
}
mysql_async::Value::Date(_, _, _, _, _, _, _)
| mysql_async::Value::Time(_, _, _, _, _, _) => crate::model::Value::Null,
mysql_async::Value::NULL => crate::model::Value::Null,
};
println!("DEBUG MySQL aggregate ormer_value: {:?}", ormer_value);
R::from_value(&ormer_value)
} else {
R::from_value(&crate::model::Value::Null)
}
})
}
}
impl<'a, T: Model + 'static, C: FromIterator<T> + 'static> std::future::IntoFuture
for CollectFuture<'a, T, C>
{
type Output = anyhow::Result<C>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
impl<'a, T: Model> SelectExecutor<'a, T> {
async fn collect_inner<C: FromIterator<T>>(self) -> anyhow::Result<C> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mysql_params = values_to_params(¶ms)?;
let rows: Vec<mysql_async::Row> = conn
.exec(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut results = Vec::new();
for row in rows {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let ormer_value = convert_mysql_value(&row, i)?;
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = Row::new(data);
let model = T::from_row(&ormer_row)?;
results.push(model);
}
Ok(results.into_iter().collect())
}
}
pub struct DeleteExecutor<'a, T: Model> {
filters: Vec<FilterExpr>,
pool: &'a Pool,
_marker: PhantomData<T>,
}
impl<'a, T: Model> DeleteExecutor<'a, T> {
pub fn filter<F>(mut self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
let where_obj = T::Where::default();
let expr = f(where_obj);
self.filters.push(expr.into());
self
}
pub async fn execute(self) -> anyhow::Result<u64> {
let (sql, params) = self.build_sql_with_params();
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mysql_params = values_to_params(¶ms)?;
conn.exec_drop(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(conn.affected_rows())
}
#[allow(dead_code)]
fn build_sql(&self) -> String {
let (sql, _) = self.build_sql_with_params();
sql
}
fn build_sql_with_params(&self) -> (String, Vec<Value>) {
let mut sql = format!("DELETE FROM {}", T::TABLE_NAME);
let mut params = Vec::new();
if !self.filters.is_empty() {
sql.push_str(" WHERE ");
let mut param_idx: usize = 1;
for (i, filter) in self.filters.iter().enumerate() {
if i > 0 {
sql.push_str(" AND ");
}
let _ = common_helpers::format_filter_with_params(
filter,
&mut sql,
&mut param_idx,
&mut params,
DbType::MySQL,
);
}
}
(sql, params)
}
}
impl<'a, T: Model + 'static> std::future::IntoFuture for DeleteExecutor<'a, T> {
type Output = anyhow::Result<u64>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.execute().await })
}
}
pub struct UpdateExecutor<'a, T: Model> {
sets: Vec<(String, Value)>,
filters: Vec<FilterExpr>,
pool: &'a Pool,
_marker: PhantomData<T>,
}
impl<'a, T: Model> UpdateExecutor<'a, T> {
pub fn filter<F>(mut self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
let where_obj = T::Where::default();
let expr = f(where_obj);
self.filters.push(expr.into());
self
}
pub fn set<F, V, C>(mut self, field_fn: F, value: V) -> Self
where
F: FnOnce(T::Where) -> crate::query::builder::TypedColumn<C>,
V: Into<Value>,
{
let where_obj = T::Where::default();
let column = field_fn(where_obj);
let column_name = column.column_name().to_string();
self.sets.push((column_name, value.into()));
self
}
pub async fn execute(self) -> anyhow::Result<u64> {
let (sql, params) = self.build_sql()?;
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mysql_params = values_to_params(¶ms)?;
let result = conn
.exec_iter(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(result.affected_rows())
}
fn build_sql(&self) -> anyhow::Result<(String, Vec<crate::model::Value>)> {
let mut sql = format!("UPDATE {} SET ", T::TABLE_NAME);
let mut params = Vec::new();
let mut first = true;
for (col_name, value) in &self.sets {
if !first {
sql.push_str(", ");
}
sql.push_str(&format!("{} = ?", col_name));
params.push(value.clone());
first = false;
}
if !self.filters.is_empty() {
sql.push_str(" WHERE ");
let mut param_idx = params.len() + 1;
for (i, filter) in self.filters.iter().enumerate() {
if i > 0 {
sql.push_str(" AND ");
}
let _ = common_helpers::format_filter_with_params(
filter,
&mut sql,
&mut param_idx,
&mut params,
DbType::MySQL,
);
}
}
Ok((sql, params))
}
}
impl<'a, T: Model + 'static> std::future::IntoFuture for UpdateExecutor<'a, T> {
type Output = anyhow::Result<u64>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.execute().await })
}
}
fn values_to_params(values: &[crate::model::Value]) -> anyhow::Result<Vec<mysql_async::Value>> {
let mut params: Vec<mysql_async::Value> = Vec::new();
for value in values {
let param = match value {
crate::model::Value::Integer(v) => mysql_async::Value::Int(*v),
crate::model::Value::Text(v) => mysql_async::Value::Bytes(v.as_bytes().to_vec()),
crate::model::Value::Real(v) => mysql_async::Value::Double(*v),
crate::model::Value::Boolean(v) => mysql_async::Value::Int(if *v { 1 } else { 0 }),
crate::model::Value::Bytes(v) => mysql_async::Value::Bytes(v.clone()),
crate::model::Value::DateTime(v) => mysql_async::Value::Date(
v.year() as u16,
v.month() as u8,
v.day() as u8,
v.hour() as u8,
v.minute() as u8,
v.second() as u8,
v.timestamp_subsec_micros(),
),
crate::model::Value::Json(v) => mysql_async::Value::Bytes(v.to_string().into_bytes()),
crate::model::Value::Uuid(v) => mysql_async::Value::Bytes(v.to_string().into_bytes()),
crate::model::Value::BigInt(v) => mysql_async::Value::Int(*v as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
};
params.push(param);
}
Ok(params)
}
pub struct RelatedSelectExecutor<'a, T: Model, R: Model> {
select: RelatedSelect<T, R>,
pool: &'a Pool,
_marker: PhantomData<(T, R)>,
}
pub struct SelectStream<'a, T: Model> {
select: Select<T>,
pool: &'a mysql_async::Pool,
_marker: std::marker::PhantomData<&'a T>,
}
impl<'a, T: Model> SelectExecutor<'a, T> {
pub fn stream(self) -> SelectStream<'a, T> {
SelectStream {
select: self.select,
pool: self.pool,
_marker: std::marker::PhantomData,
}
}
}
impl<'a, T: Model + 'static> SelectStream<'a, T> {
pub async fn into_iter(self) -> anyhow::Result<SelectStreamIterator<'a, T>> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params: Vec<mysql_async::Value> = params
.iter()
.map(|v| match v {
crate::model::Value::Integer(n) => mysql_async::Value::Int(*n),
crate::model::Value::Text(s) => mysql_async::Value::Bytes(s.as_bytes().to_vec()),
crate::model::Value::Real(f) => mysql_async::Value::Double(*f),
crate::model::Value::Boolean(b) => mysql_async::Value::Int(if *b { 1 } else { 0 }),
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(*b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
use mysql_async::prelude::Query;
let stream = sql
.with(mysql_params)
.stream::<mysql_async::Row, _>(conn)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
Ok(SelectStreamIterator {
stream: Some(stream),
_marker: std::marker::PhantomData,
})
}
}
fn parse_mysql_row<T: Model>(row: &mysql_async::Row) -> anyhow::Result<T> {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let ormer_value = convert_mysql_value(row, i)?;
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = crate::model::Row::new(data);
T::from_row(&ormer_row)
}
pub struct SelectStreamIterator<'a, T: Model> {
stream: Option<
mysql_async::ResultSetStream<
'static,
'static,
'static,
mysql_async::Row,
mysql_async::BinaryProtocol,
>,
>,
_marker: std::marker::PhantomData<&'a T>,
}
impl<'a, T: Model + 'static> SelectStreamIterator<'a, T> {
pub async fn next(&mut self) -> Option<anyhow::Result<T>> {
use futures::StreamExt;
let stream = self.stream.as_mut()?;
match stream.next().await {
Some(Ok(row)) => {
match parse_mysql_row::<T>(&row) {
Ok(model) => Some(Ok(model)),
Err(e) => Some(Err(e)),
}
}
Some(Err(e)) => Some(Err(anyhow::anyhow!(e).context("Database operation failed"))),
None => None,
}
}
}
impl<'a, T: Model, R: Model> RelatedSelectExecutor<'a, T, R> {
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where, R::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn exec(self) -> RelatedCollectFuture<'a, T, R>
where
T: 'static,
R: 'static,
{
RelatedCollectFuture { executor: self }
}
pub async fn collect<C: FromIterator<T>>(self) -> anyhow::Result<C> {
let results = self.collect_inner().await?;
Ok(results.into_iter().collect())
}
async fn collect_inner(self) -> anyhow::Result<Vec<T>> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params: Vec<mysql_async::Value> = params
.iter()
.map(|v| match v {
crate::model::Value::Integer(n) => mysql_async::Value::Int(*n),
crate::model::Value::Text(s) => mysql_async::Value::Bytes(s.as_bytes().to_vec()),
crate::model::Value::Real(f) => mysql_async::Value::Double(*f),
crate::model::Value::Boolean(b) => mysql_async::Value::Int(if *b { 1 } else { 0 }),
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(*b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let rows: Vec<mysql_async::Row> =
conn.exec(&sql, mysql_params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let mut results = Vec::new();
for row in rows {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let column_info = &T::COLUMN_SCHEMA[i];
let rust_type = column_info.rust_type;
let is_nullable = column_info.is_nullable;
let ormer_value = if is_nullable {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Integer(val),
None => crate::model::Value::Null,
}
}
"String" => {
let v: Option<String> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Text(val),
None => crate::model::Value::Null,
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Real(val),
None => crate::model::Value::Null,
}
}
"bool" => {
let v: Option<i8> = row.get(i).unwrap_or(None);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => crate::model::Value::Null,
_ => crate::model::Value::Null,
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported nullable column type: {rust_type}"
)));
}
}
} else {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i);
match v {
Some(val) => crate::model::Value::Integer(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected integer type)",
col_name
)));
}
}
}
"String" => {
let v: Option<String> = row.get(i);
match v {
Some(val) => crate::model::Value::Text(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected String type)",
col_name
)));
}
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i);
match v {
Some(val) => crate::model::Value::Real(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected float type)",
col_name
)));
}
}
}
"bool" => {
let v: Option<i8> = row.get(i);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected bool type)",
col_name
)));
}
_ => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (invalid bool value)",
col_name
)));
}
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
}
};
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = Row::new(data);
let model = T::from_row(&ormer_row)?;
results.push(model);
}
Ok(results)
}
}
pub struct RelatedCollectFuture<'a, T: Model, R: Model> {
executor: RelatedSelectExecutor<'a, T, R>,
}
impl<'a, T: Model + 'static, R: Model + 'static> std::future::IntoFuture
for RelatedCollectFuture<'a, T, R>
{
type Output = anyhow::Result<Vec<T>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
pub struct MultiTableSelectExecutor<'a, T: Model, R1: Model, R2: Model> {
select: MultiTableSelect<T, R1, R2>,
pool: &'a Pool,
_marker: PhantomData<(T, R1, R2)>,
}
impl<'a, T: Model, R1: Model, R2: Model> MultiTableSelectExecutor<'a, T, R1, R2> {
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where, R1::Where, R2::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn exec(self) -> MultiTableCollectFuture<'a, T, R1, R2>
where
T: 'static,
R1: 'static,
R2: 'static,
{
MultiTableCollectFuture { executor: self }
}
async fn collect_inner(self) -> anyhow::Result<Vec<T>> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params: Vec<mysql_async::Value> = params
.iter()
.map(|v| match v {
crate::model::Value::Integer(n) => mysql_async::Value::Int(*n),
crate::model::Value::Text(s) => mysql_async::Value::Bytes(s.as_bytes().to_vec()),
crate::model::Value::Real(f) => mysql_async::Value::Double(*f),
crate::model::Value::Boolean(b) => mysql_async::Value::Int(if *b { 1 } else { 0 }),
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(*b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let rows: Vec<mysql_async::Row> =
conn.exec(&sql, mysql_params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let mut results = Vec::new();
for row in rows {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let column_info = &T::COLUMN_SCHEMA[i];
let rust_type = column_info.rust_type;
let is_nullable = column_info.is_nullable;
let ormer_value = if is_nullable {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Integer(val),
None => crate::model::Value::Null,
}
}
"String" => {
let v: Option<String> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Text(val),
None => crate::model::Value::Null,
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Real(val),
None => crate::model::Value::Null,
}
}
"bool" => {
let v: Option<i8> = row.get(i).unwrap_or(None);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => crate::model::Value::Null,
_ => crate::model::Value::Null,
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported nullable column type: {rust_type}"
)));
}
}
} else {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i);
match v {
Some(val) => crate::model::Value::Integer(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected integer type)",
col_name
)));
}
}
}
"String" => {
let v: Option<String> = row.get(i);
match v {
Some(val) => crate::model::Value::Text(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected String type)",
col_name
)));
}
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i);
match v {
Some(val) => crate::model::Value::Real(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected float type)",
col_name
)));
}
}
}
"bool" => {
let v: Option<i8> = row.get(i);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected bool type)",
col_name
)));
}
_ => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (invalid bool value)",
col_name
)));
}
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
}
};
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = Row::new(data);
let model = T::from_row(&ormer_row)?;
results.push(model);
}
Ok(results)
}
}
pub struct MultiTableCollectFuture<'a, T: Model, R1: Model, R2: Model> {
executor: MultiTableSelectExecutor<'a, T, R1, R2>,
}
impl<'a, T: Model + 'static, R1: Model + 'static, R2: Model + 'static> std::future::IntoFuture
for MultiTableCollectFuture<'a, T, R1, R2>
{
type Output = anyhow::Result<Vec<T>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
pub struct FourTableSelectExecutor<'a, T: Model, R1: Model, R2: Model, R3: Model> {
select: FourTableSelect<T, R1, R2, R3>,
pool: &'a Pool,
_marker: PhantomData<(T, R1, R2, R3)>,
}
impl<'a, T: Model, R1: Model, R2: Model, R3: Model> FourTableSelectExecutor<'a, T, R1, R2, R3> {
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where, R1::Where, R2::Where, R3::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn exec(self) -> FourTableCollectFuture<'a, T, R1, R2, R3>
where
T: 'static,
R1: 'static,
R2: 'static,
R3: 'static,
{
FourTableCollectFuture { executor: self }
}
async fn collect_inner(self) -> anyhow::Result<Vec<T>> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mysql_params: Vec<mysql_async::Value> = params
.iter()
.map(|v| match v {
crate::model::Value::Integer(n) => mysql_async::Value::Int(*n),
crate::model::Value::Text(s) => mysql_async::Value::Bytes(s.as_bytes().to_vec()),
crate::model::Value::Real(f) => mysql_async::Value::Double(*f),
crate::model::Value::Boolean(b) => mysql_async::Value::Int(if *b { 1 } else { 0 }),
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(*b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let rows: Vec<mysql_async::Row> =
conn.exec(&sql, mysql_params)
.await
.map_err(|e: mysql_async::Error| {
anyhow::anyhow!(e).context("Database operation failed")
})?;
let mut results = Vec::new();
for row in rows {
let mut data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let column_info = &T::COLUMN_SCHEMA[i];
let rust_type = column_info.rust_type;
let is_nullable = column_info.is_nullable;
let ormer_value = if is_nullable {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Integer(val),
None => crate::model::Value::Null,
}
}
"String" => {
let v: Option<String> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Text(val),
None => crate::model::Value::Null,
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i).unwrap_or(None);
match v {
Some(val) => crate::model::Value::Real(val),
None => crate::model::Value::Null,
}
}
"bool" => {
let v: Option<i8> = row.get(i).unwrap_or(None);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => crate::model::Value::Null,
_ => crate::model::Value::Null,
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported nullable column type: {rust_type}"
)));
}
}
} else {
match rust_type {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
let v: Option<i64> = row.get(i);
match v {
Some(val) => crate::model::Value::Integer(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected integer type)",
col_name
)));
}
}
}
"String" => {
let v: Option<String> = row.get(i);
match v {
Some(val) => crate::model::Value::Text(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected String type)",
col_name
)));
}
}
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i);
match v {
Some(val) => crate::model::Value::Real(val),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected float type)",
col_name
)));
}
}
}
"bool" => {
let v: Option<i8> = row.get(i);
match v {
Some(1) => crate::model::Value::Integer(1),
Some(0) => crate::model::Value::Integer(0),
None => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (expected bool type)",
col_name
)));
}
_ => {
return Err(anyhow::anyhow!(format!(
"Failed to parse non-nullable column '{}' (invalid bool value)",
col_name
)));
}
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
}
};
data.insert(col_name.to_string(), ormer_value);
}
let ormer_row = Row::new(data);
let model = T::from_row(&ormer_row)?;
results.push(model);
}
Ok(results)
}
}
pub struct FourTableCollectFuture<'a, T: Model, R1: Model, R2: Model, R3: Model> {
executor: FourTableSelectExecutor<'a, T, R1, R2, R3>,
}
impl<'a, T: Model + 'static, R1: Model + 'static, R2: Model + 'static, R3: Model + 'static>
std::future::IntoFuture for FourTableCollectFuture<'a, T, R1, R2, R3>
{
type Output = anyhow::Result<Vec<T>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
impl<'a, T: Model, J: Model> LeftJoinedSelectExecutor<'a, T, J> {
pub fn clone_with_pool(&self) -> Self {
Self {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn collect<C: FromIterator<(T, Option<J>)> + 'static>(
&self,
) -> LeftJoinCollectFuture<'a, T, J> {
LeftJoinCollectFuture {
executor: self.clone_with_pool(),
_marker: PhantomData,
}
}
pub fn execute(self) -> LeftJoinCollectFuture<'a, T, J>
where
T: 'static,
J: 'static,
{
self.collect::<Vec<(T, Option<J>)>>()
}
}
pub struct LeftJoinCollectFuture<'a, T: Model, J: Model> {
executor: LeftJoinedSelectExecutor<'a, T, J>,
_marker: PhantomData<(T, J)>,
}
impl<'a, T: Model + 'static, J: Model + 'static> std::future::IntoFuture
for LeftJoinCollectFuture<'a, T, J>
{
type Output = anyhow::Result<Vec<(T, Option<J>)>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
impl<'a, T: Model, J: Model> LeftJoinedSelectExecutor<'a, T, J> {
async fn collect_inner<C: FromIterator<(T, Option<J>)>>(self) -> anyhow::Result<C> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params = values_to_params(¶ms)?;
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let rows: Vec<mysql_async::Row> = conn
.exec(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut results = Vec::new();
let t_col_count = T::COLUMNS.len();
for row in rows {
let mut t_data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let rust_type = T::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
let v: i64 = row.get(i).unwrap_or(0);
crate::model::Value::Integer(v)
}
"String" => {
let v: String = row.get(i).unwrap_or(String::new());
crate::model::Value::Text(v)
}
"f32" | "f64" => {
let v: f64 = row.get(i).unwrap_or(0.0);
crate::model::Value::Real(v)
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
t_data.insert(col_name.to_string(), ormer_value);
}
let t_model = T::from_row(&Row::new(t_data))?;
let mut j_data = HashMap::new();
let mut j_is_null = true;
for (i, col_name) in J::COLUMNS.iter().enumerate() {
let idx = t_col_count + i;
let rust_type = J::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
match row.get_opt::<i64, usize>(idx) {
None | Some(Err(_)) => crate::model::Value::Integer(0),
Some(Ok(v)) => {
if v != 0 {
j_is_null = false;
}
crate::model::Value::Integer(v)
}
}
}
"String" => {
match row.get_opt::<String, usize>(idx) {
None | Some(Err(_)) => crate::model::Value::Text(String::new()),
Some(Ok(v)) => {
if !v.is_empty() {
j_is_null = false;
}
crate::model::Value::Text(v)
}
}
}
"f32" | "f64" => {
match row.get_opt::<f64, usize>(idx) {
None | Some(Err(_)) => crate::model::Value::Real(0.0),
Some(Ok(v)) => {
if v != 0.0 {
j_is_null = false;
}
crate::model::Value::Real(v)
}
}
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
j_data.insert(col_name.to_string(), ormer_value);
}
if j_is_null {
results.push((t_model, None));
} else {
let j_model = J::from_row(&Row::new(j_data))?;
results.push((t_model, Some(j_model)));
}
}
Ok(results.into_iter().collect())
}
}
impl<'a, T: Model, J: Model> InnerJoinedSelectExecutor<'a, T, J> {
pub fn clone_with_pool(&self) -> Self {
Self {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn collect<C: FromIterator<(T, J)> + 'static>(&self) -> InnerJoinCollectFuture<'a, T, J> {
InnerJoinCollectFuture {
executor: self.clone_with_pool(),
_marker: PhantomData,
}
}
pub fn execute(self) -> InnerJoinCollectFuture<'a, T, J>
where
T: 'static,
J: 'static,
{
self.collect::<Vec<(T, J)>>()
}
}
pub struct InnerJoinCollectFuture<'a, T: Model, J: Model> {
executor: InnerJoinedSelectExecutor<'a, T, J>,
_marker: PhantomData<(T, J)>,
}
impl<'a, T: Model + 'static, J: Model + 'static> std::future::IntoFuture
for InnerJoinCollectFuture<'a, T, J>
{
type Output = anyhow::Result<Vec<(T, J)>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
impl<'a, T: Model, J: Model> InnerJoinedSelectExecutor<'a, T, J> {
async fn collect_inner<C: FromIterator<(T, J)>>(self) -> anyhow::Result<C> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params = values_to_params(¶ms)?;
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let rows: Vec<mysql_async::Row> = conn
.exec(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut results = Vec::new();
let t_col_count = T::COLUMNS.len();
for row in rows {
let mut t_data = HashMap::new();
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let rust_type = T::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
let v: i64 = row.get(i).unwrap_or(0);
crate::model::Value::Integer(v)
}
"String" => {
let v: String = row.get(i).unwrap_or(String::new());
crate::model::Value::Text(v)
}
"f32" | "f64" => {
let v: f64 = row.get(i).unwrap_or(0.0);
crate::model::Value::Real(v)
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
t_data.insert(col_name.to_string(), ormer_value);
}
let t_model = T::from_row(&Row::new(t_data))?;
let mut j_data = HashMap::new();
for (i, col_name) in J::COLUMNS.iter().enumerate() {
let idx = t_col_count + i;
let rust_type = J::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
let v: i64 = row.get(idx).unwrap_or(0);
crate::model::Value::Integer(v)
}
"String" => {
let v: String = row.get(idx).unwrap_or(String::new());
crate::model::Value::Text(v)
}
"f32" | "f64" => {
let v: f64 = row.get(idx).unwrap_or(0.0);
crate::model::Value::Real(v)
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
j_data.insert(col_name.to_string(), ormer_value);
}
let j_model = J::from_row(&Row::new(j_data))?;
results.push((t_model, j_model));
}
Ok(results.into_iter().collect())
}
}
impl<'a, T: Model, J: Model> RightJoinedSelectExecutor<'a, T, J> {
pub fn clone_with_pool(&self) -> Self {
Self {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn range<RR: Into<crate::query::builder::RangeBounds>>(self, range: RR) -> Self {
Self {
select: self.select.range(range),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn collect<C: FromIterator<(Option<T>, J)> + 'static>(
&self,
) -> RightJoinCollectFuture<'a, T, J> {
RightJoinCollectFuture {
executor: self.clone_with_pool(),
_marker: PhantomData,
}
}
pub fn execute(self) -> RightJoinCollectFuture<'a, T, J>
where
T: 'static,
J: 'static,
{
self.collect::<Vec<(Option<T>, J)>>()
}
}
pub struct RightJoinCollectFuture<'a, T: Model, J: Model> {
executor: RightJoinedSelectExecutor<'a, T, J>,
_marker: PhantomData<(T, J)>,
}
pub struct GroupedCollectFuture<'a, T: Model, V, C> {
executor: GroupedSelectExecutor<'a, T, V>,
_marker: PhantomData<(T, V, C)>,
}
impl<'a, T: Model + 'static, J: Model + 'static> std::future::IntoFuture
for RightJoinCollectFuture<'a, T, J>
{
type Output = anyhow::Result<Vec<(Option<T>, J)>>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.executor.collect_inner().await })
}
}
impl<'a, T: Model, J: Model> RightJoinedSelectExecutor<'a, T, J> {
async fn collect_inner<C: FromIterator<(Option<T>, J)>>(self) -> anyhow::Result<C> {
let (sql, params) = self.select.to_sql_with_params(DbType::MySQL);
let mysql_params = values_to_params(¶ms)?;
let mut conn = self
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let rows: Vec<mysql_async::Row> = conn
.exec(&sql, mysql_params)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?;
let mut results = Vec::new();
let t_col_count = T::COLUMNS.len();
for row in rows {
let mut t_data = HashMap::new();
let mut t_is_null = true;
for (i, col_name) in T::COLUMNS.iter().enumerate() {
let rust_type = T::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
let v: Option<i64> = row.get(i);
if v.is_some() {
t_is_null = false;
}
crate::model::Value::Integer(v.unwrap_or(0))
}
"String" => {
let v: Option<String> = row.get(i);
if v.is_some() {
t_is_null = false;
}
crate::model::Value::Text(v.unwrap_or_default())
}
"f32" | "f64" => {
let v: Option<f64> = row.get(i);
if v.is_some() {
t_is_null = false;
}
crate::model::Value::Real(v.unwrap_or(0.0))
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
t_data.insert(col_name.to_string(), ormer_value);
}
let t_model = if t_is_null {
None
} else {
Some(T::from_row(&Row::new(t_data))?)
};
let mut j_data = HashMap::new();
for (i, col_name) in J::COLUMNS.iter().enumerate() {
let idx = t_col_count + i;
let rust_type = J::COLUMN_SCHEMA[i].rust_type;
let ormer_value = match rust_type {
"i32" | "i64" | "u32" | "u64" => {
let v: i64 = row.get(idx).unwrap_or(0);
crate::model::Value::Integer(v)
}
"String" => {
let v: String = row.get(idx).unwrap_or(String::new());
crate::model::Value::Text(v)
}
"f32" | "f64" => {
let v: f64 = row.get(idx).unwrap_or(0.0);
crate::model::Value::Real(v)
}
_ => {
return Err(anyhow::anyhow!(format!(
"Unsupported column type: {rust_type}"
)));
}
};
j_data.insert(col_name.to_string(), ormer_value);
}
let j_model = J::from_row(&Row::new(j_data))?;
results.push((t_model, j_model));
}
Ok(results.into_iter().collect())
}
}
fn convert_mysql_value(
row: &mysql_async::Row,
index: usize,
) -> anyhow::Result<crate::model::Value> {
use mysql_async::Value;
let value = row.get::<Option<Value>, _>(index).unwrap_or(None);
match value {
Some(Value::NULL) | None => Ok(crate::model::Value::Null),
Some(Value::Int(i)) => Ok(crate::model::Value::Integer(i)),
Some(Value::UInt(u)) => Ok(crate::model::Value::Integer(u as i64)),
Some(Value::Float(f)) => Ok(crate::model::Value::Real(f as f64)),
Some(Value::Double(d)) => Ok(crate::model::Value::Real(d)),
Some(Value::Bytes(b)) => {
if let Ok(s) = String::from_utf8(b.clone()) {
if let Ok(i) = s.parse::<i64>() {
Ok(crate::model::Value::Integer(i))
} else if let Ok(f) = s.parse::<f64>() {
Ok(crate::model::Value::Real(f))
} else {
Ok(crate::model::Value::Text(s))
}
} else {
Ok(crate::model::Value::Text(String::new()))
}
}
_ => Err(anyhow::anyhow!(format!(
"Unsupported MySQL value type at index {}",
index
))),
}
}
impl<'a, T: Model, V> GroupedSelectExecutor<'a, T, V> {
pub fn collect<C: FromIterator<V> + 'static>(&self) -> GroupedCollectFuture<'a, T, V, C>
where
T: 'static,
V: crate::model::FromRowValues + 'static,
{
GroupedCollectFuture {
executor: GroupedSelectExecutor {
select: self.select.clone(),
pool: self.pool,
_marker: PhantomData,
},
_marker: PhantomData,
}
}
pub fn group_by<F, G>(self, f: F) -> Self
where
F: FnOnce(<T as Model>::Where) -> G,
G: crate::query::builder::GroupByColumns,
{
Self {
select: self.select.group_by(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn having<F>(self, f: F) -> Self
where
F: FnOnce(<T as Model>::Where) -> crate::query::builder::WhereExpr,
{
Self {
select: self.select.having(f),
pool: self.pool,
_marker: PhantomData,
}
}
pub fn filter<F>(self, f: F) -> Self
where
F: FnOnce(T::Where) -> crate::query::builder::WhereExpr,
{
Self {
select: self.select.filter(f),
pool: self.pool,
_marker: PhantomData,
}
}
}
impl<'a, T: Model + 'static, V: crate::model::FromRowValues + 'static, C: FromIterator<V> + 'static>
std::future::IntoFuture for GroupedCollectFuture<'a, T, V, C>
{
type Output = anyhow::Result<C>;
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let (sql, params) = self.executor.select.build_sql(DbType::MySQL);
let mut conn = self
.executor
.pool
.get_conn()
.await
.map_err(|e| anyhow::anyhow!(format!("Failed to get connection: {}", e)))?;
let mysql_params: Vec<mysql_async::Value> = params
.into_iter()
.map(|v| match v {
crate::model::Value::Integer(i) => mysql_async::Value::Int(i),
crate::model::Value::Text(t) => mysql_async::Value::Bytes(t.into_bytes()),
crate::model::Value::Real(r) => mysql_async::Value::Double(r),
crate::model::Value::Boolean(b) => {
mysql_async::Value::Int(if b { 1 } else { 0 })
}
crate::model::Value::Bytes(b) => mysql_async::Value::Bytes(b.clone()),
crate::model::Value::DateTime(dt) => mysql_async::Value::Date(
dt.year() as u16,
dt.month() as u8,
dt.day() as u8,
dt.hour() as u8,
dt.minute() as u8,
dt.second() as u8,
dt.timestamp_subsec_micros(),
),
crate::model::Value::Json(j) => {
mysql_async::Value::Bytes(j.to_string().into_bytes())
}
crate::model::Value::Uuid(u) => {
mysql_async::Value::Bytes(u.to_string().into_bytes())
}
crate::model::Value::BigInt(b) => mysql_async::Value::Int(b as i64),
crate::model::Value::Null => mysql_async::Value::NULL,
})
.collect();
let rows: Vec<mysql_async::Row> = if mysql_params.is_empty() {
conn.query(&sql)
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?
} else {
conn.exec(&sql, mysql_async::Params::Positional(mysql_params))
.await
.map_err(|e| anyhow::anyhow!(e).context("Database operation failed"))?
};
let mut results = Vec::new();
let column_count = self.executor.select.column_count();
for row in rows {
let mut values = Vec::with_capacity(column_count);
for i in 0..column_count {
let value = convert_mysql_value(&row, i)?;
values.push(value);
}
let v = V::from_row_values(&values)?;
results.push(v);
}
Ok(results.into_iter().collect())
})
}
}