#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))]
#![warn(missing_docs)]
#[cfg(not(any(feature = "sqlite", feature = "postgres", feature = "mysql")))]
compile_error!("One of the features sqlite, postgres, mysql must be activated");
pub mod aggregation;
pub mod alter_table;
pub mod conditional;
pub mod create_column;
pub mod create_index;
pub mod create_table;
pub mod create_trigger;
pub mod delete;
pub mod drop_table;
pub mod error;
pub mod insert;
pub mod join_table;
pub mod limit_clause;
pub mod on_conflict;
pub mod ordering;
pub mod select;
pub mod select_column;
pub mod update;
pub mod value;
mod db_specific;
use std::borrow::Cow;
use rorm_declaration::imr::{Annotation, DbType};
use crate::aggregation::SelectAggregator;
use crate::alter_table::{AlterTable, AlterTableData, AlterTableImpl, AlterTableOperation};
use crate::conditional::Condition;
#[cfg(feature = "mysql")]
use crate::create_column::CreateColumnMySQLData;
#[cfg(feature = "postgres")]
use crate::create_column::CreateColumnPostgresData;
#[cfg(feature = "sqlite")]
use crate::create_column::CreateColumnSQLiteData;
use crate::create_column::{CreateColumnImpl, SQLAnnotation};
use crate::create_index::{CreateIndex, CreateIndexData, CreateIndexImpl};
use crate::create_table::{CreateTable, CreateTableData, CreateTableImpl};
use crate::create_trigger::{
SQLCreateTrigger, SQLCreateTriggerOperation, SQLCreateTriggerPointInTime,
};
use crate::delete::{Delete, DeleteData, DeleteImpl};
use crate::drop_table::{DropTable, DropTableData, DropTableImpl};
use crate::insert::{Insert, InsertData, InsertImpl};
use crate::join_table::{JoinTableData, JoinTableImpl, JoinType};
use crate::on_conflict::OnConflict;
use crate::ordering::OrderByEntry;
use crate::select::{Select, SelectData, SelectImpl};
use crate::select_column::{SelectColumnData, SelectColumnImpl};
use crate::update::{Update, UpdateData, UpdateImpl};
use crate::value::Value;
#[derive(Copy, Clone)]
pub enum DBImpl {
#[cfg(feature = "sqlite")]
SQLite,
#[cfg(feature = "postgres")]
Postgres,
#[cfg(feature = "mysql")]
MySQL,
}
impl DBImpl {
pub fn create_table<'until_build, 'post_build>(
&self,
name: &'until_build str,
) -> impl CreateTable<'until_build, 'post_build>
where
'post_build: 'until_build,
{
let d = CreateTableData {
name,
columns: vec![],
if_not_exists: false,
lookup: vec![],
pre_statements: vec![],
statements: vec![],
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => CreateTableImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => CreateTableImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => CreateTableImpl::Postgres(d),
}
}
pub fn create_trigger(
&self,
name: &str,
table_name: &str,
point_in_time: Option<SQLCreateTriggerPointInTime>,
operation: SQLCreateTriggerOperation,
) -> SQLCreateTrigger {
SQLCreateTrigger {
name: name.to_string(),
table_name: table_name.to_string(),
if_not_exists: false,
point_in_time,
operation,
statements: vec![],
for_each_row: false,
}
}
pub fn create_index<'until_build>(
&self,
name: &'until_build str,
table_name: &'until_build str,
) -> impl CreateIndex<'until_build> {
let d = CreateIndexData {
name,
table_name,
unique: false,
if_not_exists: false,
columns: vec![],
condition: None,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => CreateIndexImpl::Sqlite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => CreateIndexImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => CreateIndexImpl::Postgres(d),
}
}
pub fn drop_table<'until_build>(
&self,
name: &'until_build str,
) -> impl DropTable + 'until_build {
let d = DropTableData {
name,
if_exists: false,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => DropTableImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => DropTableImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => DropTableImpl::Postgres(d),
}
}
pub fn alter_table<'until_build, 'post_build>(
&self,
name: &'until_build str,
operation: AlterTableOperation<'until_build, 'post_build>,
) -> impl AlterTable<'post_build> + 'until_build
where
'post_build: 'until_build,
{
let d = AlterTableData {
name,
operation,
lookup: vec![],
statements: vec![],
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => AlterTableImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => AlterTableImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => AlterTableImpl::Postgres(d),
}
}
pub fn create_column<'until_build, 'post_build>(
&self,
table_name: &'until_build str,
name: &'until_build str,
data_type: DbType,
annotations: &'post_build [Annotation],
) -> CreateColumnImpl<'until_build, 'post_build> {
#[cfg(not(any(feature = "postgres", feature = "sqlite")))]
let _ = table_name;
let mut a = vec![];
for x in annotations {
if x.eq_shallow(&Annotation::PrimaryKey) {
a.push(SQLAnnotation { annotation: x });
}
}
for x in annotations {
if !x.eq_shallow(&Annotation::PrimaryKey) {
a.push(SQLAnnotation { annotation: x });
}
}
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => CreateColumnImpl::SQLite(CreateColumnSQLiteData {
name,
table_name,
data_type,
annotations: a,
statements: None,
lookup: None,
}),
#[cfg(feature = "mysql")]
DBImpl::MySQL => CreateColumnImpl::MySQL(CreateColumnMySQLData {
name,
data_type,
annotations: a,
statements: None,
lookup: None,
}),
#[cfg(feature = "postgres")]
DBImpl::Postgres => CreateColumnImpl::Postgres(CreateColumnPostgresData {
name,
table_name,
data_type,
annotations: a,
pre_statements: None,
statements: None,
}),
}
}
pub fn select<'until_build, 'post_build>(
&self,
columns: &'until_build [SelectColumnImpl],
from_clause: &'until_build str,
joins: &'until_build [JoinTableImpl<'until_build, 'post_build>],
order_by_clause: &'until_build [OrderByEntry<'until_build>],
) -> impl Select<'until_build, 'post_build> {
let d = SelectData {
join_tables: joins,
resulting_columns: columns,
limit: None,
offset: None,
from_clause,
where_clause: None,
distinct: false,
lookup: vec![],
order_by_clause,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => SelectImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => SelectImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => SelectImpl::Postgres(d),
}
}
pub fn insert<'until_build, 'post_build>(
&self,
into_clause: &'until_build str,
insert_columns: &'until_build [&'until_build str],
insert_values: &'until_build [&'until_build [Value<'post_build>]],
returning_clause: Option<&'until_build [&'until_build str]>,
) -> impl Insert<'post_build> + 'until_build
where
'post_build: 'until_build,
{
let d = InsertData {
into_clause,
columns: insert_columns,
row_values: insert_values,
lookup: vec![],
on_conflict: OnConflict::ABORT,
returning_clause,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => InsertImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => InsertImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => InsertImpl::Postgres(d),
}
}
pub fn delete<'until_build, 'post_query>(
&self,
table_name: &'until_build str,
) -> impl Delete<'until_build, 'post_query>
where
'post_query: 'until_build,
{
let d = DeleteData {
model: table_name,
lookup: vec![],
where_clause: None,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => DeleteImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => DeleteImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => DeleteImpl::Postgres(d),
}
}
pub fn update<'until_build, 'post_query>(
&self,
table_name: &'until_build str,
) -> impl Update<'until_build, 'post_query>
where
'post_query: 'until_build,
{
let d = UpdateData {
model: table_name,
on_conflict: OnConflict::ABORT,
updates: vec![],
where_clause: None,
lookup: vec![],
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => UpdateImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => UpdateImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => UpdateImpl::Postgres(d),
}
}
pub fn join_table<'until_build, 'post_query>(
&self,
join_type: JoinType,
table_name: &'until_build str,
join_alias: &'until_build str,
join_condition: Cow<'until_build, Condition<'post_query>>,
) -> JoinTableImpl<'until_build, 'post_query> {
let d = JoinTableData {
join_type,
table_name,
join_alias,
join_condition,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => JoinTableImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => JoinTableImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => JoinTableImpl::Postgres(d),
}
}
pub fn select_column<'until_build>(
&self,
table_name: Option<&'until_build str>,
column_name: &'until_build str,
select_alias: Option<&'until_build str>,
aggregation: Option<SelectAggregator>,
) -> SelectColumnImpl<'until_build> {
let d = SelectColumnData {
table_name,
column_name,
select_alias,
aggregation,
};
match self {
#[cfg(feature = "sqlite")]
DBImpl::SQLite => SelectColumnImpl::SQLite(d),
#[cfg(feature = "mysql")]
DBImpl::MySQL => SelectColumnImpl::MySQL(d),
#[cfg(feature = "postgres")]
DBImpl::Postgres => SelectColumnImpl::Postgres(d),
}
}
}