use crate::{
concat::Concat,
fmt,
structure::{
AlterTable, CreateTable, Delete, DropTable, Insert, Select, TrCmd::*, Transaction, TransactionCommand, Update,
},
utils::push_unique,
};
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
use crate::structure::{CreateIndex, DropIndex};
impl Transaction {
pub fn as_string(&self) -> String {
let fmts = fmt::one_line();
self.concat(&fmts)
}
pub fn commit(mut self, arg: &str) -> Self {
let cmd = TransactionCommand::new(Commit, arg.trim().to_string());
self._commit = Some(cmd);
self
}
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn alter_table(mut self, alter_table: AlterTable) -> Self {
let cmd = Box::new(alter_table);
self._ordered_commands.push(cmd);
self
}
pub fn create_table(mut self, create_table: CreateTable) -> Self {
let cmd = Box::new(create_table);
self._ordered_commands.push(cmd);
self
}
pub fn delete(mut self, delete: Delete) -> Self {
let cmd = Box::new(delete);
self._ordered_commands.push(cmd);
self
}
pub fn drop_table(mut self, drop_table: DropTable) -> Self {
let cmd = Box::new(drop_table);
self._ordered_commands.push(cmd);
self
}
pub fn insert(mut self, insert: Insert) -> Self {
let cmd = Box::new(insert);
self._ordered_commands.push(cmd);
self
}
pub fn new() -> Self {
Self::default()
}
pub fn print(self) -> Self {
let fmts = fmt::one_line();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}
pub fn raw(mut self, raw_sql: &str) -> Self {
push_unique(&mut self._raw, raw_sql.trim().to_string());
self
}
pub fn release_savepoint(mut self, name: &str) -> Self {
let cmd = Box::new(TransactionCommand::new(ReleaseSavepoint, name.trim().to_string()));
self._ordered_commands.push(cmd);
self
}
pub fn rollback(mut self, arg: &str) -> Self {
let cmd = Box::new(TransactionCommand::new(Rollback, arg.trim().to_string()));
self._ordered_commands.push(cmd);
self
}
pub fn savepoint(mut self, name: &str) -> Self {
let cmd = Box::new(TransactionCommand::new(Savepoint, name.trim().to_string()));
self._ordered_commands.push(cmd);
self
}
pub fn select(mut self, select: Select) -> Self {
let cmd = Box::new(select);
self._ordered_commands.push(cmd);
self
}
#[cfg(not(feature = "sqlite"))]
pub fn set_transaction(mut self, mode: &str) -> Self {
let cmd = TransactionCommand::new(SetTransaction, mode.trim().to_string());
self._set_transaction = Some(cmd);
self
}
#[cfg(not(feature = "sqlite"))]
pub fn start_transaction(mut self, mode: &str) -> Self {
let cmd = TransactionCommand::new(StartTransaction, mode.trim().to_string());
self._start_transaction = Some(cmd);
self
}
pub fn update(mut self, update: Update) -> Self {
let cmd = Box::new(update);
self._ordered_commands.push(cmd);
self
}
}
#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Transaction {
pub fn begin(mut self, mode: &str) -> Self {
let cmd = TransactionCommand::new(Begin, mode.trim().to_string());
self._begin = Some(cmd);
self
}
pub fn create_index(mut self, create_index: CreateIndex) -> Self {
let cmd = Box::new(create_index);
self._ordered_commands.push(cmd);
self
}
pub fn drop_index(mut self, drop_index: DropIndex) -> Self {
let cmd = Box::new(drop_index);
self._ordered_commands.push(cmd);
self
}
}
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
impl Transaction {
pub fn end(mut self, mode: &str) -> Self {
let cmd = TransactionCommand::new(End, mode.trim().to_string());
self._end = Some(cmd);
self
}
}
impl std::fmt::Display for Transaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
impl std::fmt::Debug for Transaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}