mod data_type;
mod ddl;
mod expr;
mod function;
mod literal;
mod operator;
mod query;
pub use {
data_type::DataType,
ddl::*,
expr::Expr,
function::{Aggregate, AggregateFunction, CountArgExpr, Function},
literal::{DateTimeField, Literal, TrimWhereField},
operator::*,
query::*,
};
use {
serde::{Deserialize, Serialize},
strum_macros::Display,
};
pub trait ToSql {
fn to_sql(&self) -> String;
}
pub trait ToSqlUnquoted {
fn to_sql_unquoted(&self) -> String;
}
#[derive(PartialEq, Debug, Clone, Eq, Hash, Serialize, Deserialize)]
pub struct ForeignKey {
pub name: String,
pub referencing_column_name: String,
pub referenced_table_name: String,
pub referenced_column_name: String,
pub on_delete: ReferentialAction,
pub on_update: ReferentialAction,
}
#[derive(PartialEq, Debug, Clone, Eq, Hash, Serialize, Deserialize, Display)]
pub enum ReferentialAction {
#[strum(to_string = "NO ACTION")]
NoAction,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Statement {
ShowColumns {
table_name: String,
},
Query(Query),
Insert {
table_name: String,
columns: Vec<String>,
source: Query,
},
Update {
table_name: String,
assignments: Vec<Assignment>,
selection: Option<Expr>,
},
Delete {
table_name: String,
selection: Option<Expr>,
},
CreateTable {
if_not_exists: bool,
name: String,
columns: Option<Vec<ColumnDef>>,
source: Option<Box<Query>>,
engine: Option<String>,
foreign_keys: Vec<ForeignKey>,
comment: Option<String>,
},
CreateFunction {
or_replace: bool,
name: String,
args: Vec<OperateFunctionArg>,
return_: Expr,
},
AlterTable {
name: String,
operation: AlterTableOperation,
},
DropTable {
if_exists: bool,
names: Vec<String>,
cascade: bool,
},
DropFunction {
if_exists: bool,
names: Vec<String>,
},
CreateIndex {
name: String,
table_name: String,
column: OrderByExpr,
},
DropIndex {
name: String,
table_name: String,
},
StartTransaction,
Commit,
Rollback,
ShowVariable(Variable),
ShowIndexes(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Assignment {
pub id: String,
pub value: Expr,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Variable {
Tables,
Functions,
Version,
}
impl ToSql for ForeignKey {
fn to_sql(&self) -> String {
let ForeignKey {
referencing_column_name,
referenced_table_name,
referenced_column_name,
name,
on_delete,
on_update,
} = self;
format!(
r#"CONSTRAINT "{name}" FOREIGN KEY ("{referencing_column_name}") REFERENCES "{referenced_table_name}" ("{referenced_column_name}") ON DELETE {on_delete} ON UPDATE {on_update}"#
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Array {
pub elem: Vec<Expr>,
pub named: bool,
}