use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::oneshot;
use super::ExtendedPoolConfig;
use crate::error::{QuickDbError, QuickDbResult};
use crate::model::{FieldDefinition, FieldType};
use crate::types::*;
#[derive(Debug, Clone)]
pub struct PooledConnection {
pub id: String,
pub db_type: DatabaseType,
pub alias: String,
}
#[derive(Debug)]
pub enum DatabaseOperation {
Create {
table: String,
data: HashMap<String, DataValue>,
id_strategy: IdStrategy,
alias: String,
response: oneshot::Sender<QuickDbResult<DataValue>>,
},
FindById {
table: String,
id: DataValue,
alias: String,
response: oneshot::Sender<QuickDbResult<Option<DataValue>>>,
},
Find {
table: String,
conditions: Vec<QueryConditionWithConfig>,
options: QueryOptions,
alias: String,
response: oneshot::Sender<QuickDbResult<Vec<DataValue>>>,
},
FindWithBypassCache {
table: String,
conditions: Vec<QueryConditionWithConfig>,
options: QueryOptions,
alias: String,
bypass_cache: bool,
response: oneshot::Sender<QuickDbResult<Vec<DataValue>>>,
},
FindWithGroups {
table: String,
condition_groups: Vec<QueryConditionGroup>,
options: QueryOptions,
alias: String,
response: oneshot::Sender<QuickDbResult<Vec<DataValue>>>,
},
FindWithGroupsWithBypassCache {
table: String,
condition_groups: Vec<QueryConditionGroupWithConfig>,
options: QueryOptions,
alias: String,
bypass_cache: bool,
response: oneshot::Sender<QuickDbResult<Vec<DataValue>>>,
},
Update {
table: String,
conditions: Vec<QueryConditionWithConfig>,
data: HashMap<String, DataValue>,
alias: String,
response: oneshot::Sender<QuickDbResult<u64>>,
},
UpdateWithOperations {
table: String,
conditions: Vec<QueryConditionWithConfig>,
operations: Vec<crate::types::UpdateOperation>,
alias: String,
response: oneshot::Sender<QuickDbResult<u64>>,
},
UpdateById {
table: String,
id: DataValue,
data: HashMap<String, DataValue>,
alias: String,
response: oneshot::Sender<QuickDbResult<bool>>,
},
Delete {
table: String,
conditions: Vec<QueryConditionWithConfig>,
alias: String,
response: oneshot::Sender<QuickDbResult<u64>>,
},
DeleteById {
table: String,
id: DataValue,
alias: String,
response: oneshot::Sender<QuickDbResult<bool>>,
},
Count {
table: String,
conditions: Vec<QueryConditionWithConfig>,
alias: String,
response: oneshot::Sender<QuickDbResult<u64>>,
},
CountWithGroups {
table: String,
condition_groups: Vec<QueryConditionGroupWithConfig>,
alias: String,
response: oneshot::Sender<QuickDbResult<u64>>,
},
CreateTable {
table: String,
fields: HashMap<String, FieldDefinition>,
id_strategy: IdStrategy,
alias: String,
response: oneshot::Sender<QuickDbResult<()>>,
},
CreateIndex {
table: String,
index_name: String,
fields: Vec<String>,
unique: bool,
response: oneshot::Sender<QuickDbResult<()>>,
},
TableExists {
table: String,
response: oneshot::Sender<QuickDbResult<bool>>,
},
DropTable {
table: String,
response: oneshot::Sender<QuickDbResult<()>>,
},
GetServerVersion {
response: oneshot::Sender<QuickDbResult<String>>,
},
CreateStoredProcedure {
config: crate::stored_procedure::StoredProcedureConfig,
response:
oneshot::Sender<QuickDbResult<crate::stored_procedure::StoredProcedureCreateResult>>,
},
ExecuteStoredProcedure {
procedure_name: String,
database: String,
params: Option<std::collections::HashMap<String, crate::types::DataValue>>,
response:
oneshot::Sender<QuickDbResult<crate::stored_procedure::StoredProcedureQueryResult>>,
},
}
#[derive(Debug)]
pub enum DatabaseConnection {
#[cfg(feature = "sqlite-support")]
SQLite(sqlx::SqlitePool),
#[cfg(feature = "postgres-support")]
PostgreSQL(sqlx::PgPool),
#[cfg(feature = "mysql-support")]
MySQL(sqlx::MySqlPool),
#[cfg(feature = "mongodb-support")]
MongoDB(mongodb::Database),
}
pub struct ConnectionWorker {
pub id: String,
pub connection: DatabaseConnection,
pub pool_config: ExtendedPoolConfig,
pub created_at: Instant,
pub last_used: Instant,
pub retry_count: u32,
pub db_type: DatabaseType,
pub adapter: Box<dyn crate::adapter::DatabaseAdapter + Send + Sync>,
}
impl std::fmt::Debug for ConnectionWorker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConnectionWorker")
.field("id", &self.id)
.field("connection", &self.connection)
.field("created_at", &self.created_at)
.field("last_used", &self.last_used)
.field("retry_count", &self.retry_count)
.field("db_type", &self.db_type)
.field("adapter", &"<DatabaseAdapter>")
.finish()
}
}