use super::Grammar;
use crate::enums::{NullsPosition, OrderDirection};
use crate::error::{Error, Result};
use crate::value::Value;
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Func(Func),
Aliased(Aliased),
Between(Between),
Binary(Binary),
CaseWhen(CaseWhen),
Cast(Cast),
Conditional(Conditional),
Exists(Exists),
In(In),
Subquery(Subquery),
Unary(Unary),
Window(Window),
Literal(Literal),
Placeholder(Placeholder),
Raw(Raw),
Column(Column),
Star(Star),
}
impl Expression {
pub const fn php_class(&self) -> &'static str {
match self {
Expression::Func(_) => "Call\\Func",
Expression::Aliased(_) => "Expression\\Aliased",
Expression::Between(_) => "Expression\\Between",
Expression::Binary(_) => "Expression\\Binary",
Expression::CaseWhen(_) => "Expression\\CaseWhen",
Expression::Cast(_) => "Expression\\Cast",
Expression::Conditional(_) => "Expression\\Conditional",
Expression::Exists(_) => "Expression\\Exists",
Expression::In(_) => "Expression\\In",
Expression::Subquery(_) => "Expression\\Subquery",
Expression::Unary(_) => "Expression\\Unary",
Expression::Window(_) => "Expression\\Window",
Expression::Literal(_) => "Literal",
Expression::Placeholder(_) => "Placeholder",
Expression::Raw(_) => "Raw",
Expression::Column(_) => "Reference\\Column",
Expression::Star(_) => "Star",
}
}
}
macro_rules! into_expression {
($($ty:ident),*) => {$(
impl From<$ty> for Expression {
fn from(node: $ty) -> Self {
Expression::$ty(node)
}
}
)*};
}
into_expression!(
Func,
Aliased,
Between,
Binary,
CaseWhen,
Cast,
Conditional,
Exists,
In,
Subquery,
Unary,
Window,
Literal,
Placeholder,
Raw,
Column,
Star
);
fn boxed(expression: impl Into<Expression>) -> Box<Expression> {
Box::new(expression.into())
}
#[derive(Debug, Clone, PartialEq)]
pub struct Func {
pub name: String,
pub arguments: Vec<Expression>,
pub distinct: bool,
pub filter: Option<Box<Expression>>,
}
impl Func {
pub fn new(
name: impl Into<String>,
arguments: Vec<Expression>,
distinct: bool,
filter: Option<Expression>,
) -> Result<Self> {
let name = name.into();
if !Grammar::is_identifier(&name) {
return Err(Error::Validation(format!("Invalid function name: {name}")));
}
Ok(Func { name, arguments, distinct, filter: filter.map(Box::new) })
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Aliased {
pub expression: Box<Expression>,
pub alias: String,
}
impl Aliased {
pub fn new(expression: impl Into<Expression>, alias: impl Into<String>) -> Self {
Aliased { expression: boxed(expression), alias: alias.into() }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Between {
pub expression: Box<Expression>,
pub low: Box<Expression>,
pub high: Box<Expression>,
pub negated: bool,
}
impl Between {
pub fn new(
expression: impl Into<Expression>,
low: impl Into<Expression>,
high: impl Into<Expression>,
negated: bool,
) -> Self {
Between { expression: boxed(expression), low: boxed(low), high: boxed(high), negated }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Binary {
pub left: Box<Expression>,
pub operator: String,
pub right: Box<Expression>,
pub escape: Option<Box<Expression>>,
}
impl Binary {
pub fn new(left: impl Into<Expression>, operator: impl Into<String>, right: impl Into<Expression>) -> Self {
Binary { left: boxed(left), operator: operator.into(), right: boxed(right), escape: None }
}
#[must_use]
pub fn with_escape(mut self, escape: impl Into<Expression>) -> Self {
self.escape = Some(boxed(escape));
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CaseWhen {
pub condition: Box<Expression>,
pub result: Box<Expression>,
}
impl CaseWhen {
pub fn new(condition: impl Into<Expression>, result: impl Into<Expression>) -> Self {
CaseWhen { condition: boxed(condition), result: boxed(result) }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cast {
pub expression: Box<Expression>,
pub data_type: String,
}
impl Cast {
pub fn new(expression: impl Into<Expression>, data_type: impl Into<String>) -> Result<Self> {
let data_type = data_type.into();
if !Grammar::is_type(&data_type) {
return Err(Error::Validation(format!("Invalid cast type: {data_type}")));
}
Ok(Cast { expression: boxed(expression), data_type })
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Conditional {
pub operand: Option<Box<Expression>>,
pub whens: Vec<CaseWhen>,
pub else_result: Option<Box<Expression>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Exists {
pub subquery: Box<Select>,
pub negated: bool,
}
impl Exists {
pub fn new(subquery: Select, negated: bool) -> Self {
Exists { subquery: Box::new(subquery), negated }
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum InList {
Values(Vec<Expression>),
Subquery(Box<Select>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct In {
pub expression: Box<Expression>,
pub list: InList,
pub negated: bool,
}
impl In {
pub fn new(expression: impl Into<Expression>, list: InList, negated: bool) -> Self {
In { expression: boxed(expression), list, negated }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Subquery {
pub query: Box<Select>,
}
impl Subquery {
pub fn new(query: Select) -> Self {
Subquery { query: Box::new(query) }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Unary {
pub operator: String,
pub operand: Box<Expression>,
pub prefix: bool,
}
impl Unary {
pub fn new(operator: impl Into<String>, operand: impl Into<Expression>, prefix: bool) -> Self {
Unary { operator: operator.into(), operand: boxed(operand), prefix }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Window {
pub function: Box<Expression>,
pub window_name: Option<String>,
pub specification: Option<Box<WindowSpecification>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Literal {
pub value: Value,
}
impl Literal {
pub fn new(value: impl Into<Value>) -> Self {
Literal { value: value.into() }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Placeholder {
pub value: String,
}
impl Placeholder {
pub fn new(value: impl Into<String>) -> Self {
Placeholder { value: value.into() }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Raw {
pub sql: String,
}
impl Raw {
pub fn new(sql: impl Into<String>) -> Self {
Raw { sql: sql.into() }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Column {
pub name: String,
pub table: Option<String>,
pub schema: Option<String>,
}
impl Column {
pub fn new(name: impl Into<String>) -> Self {
Column { name: name.into(), table: None, schema: None }
}
pub fn of(table: impl Into<String>, name: impl Into<String>) -> Self {
Column { name: name.into(), table: Some(table.into()), schema: None }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Star {
pub table: Option<String>,
pub schema: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Table {
pub name: String,
pub alias: Option<String>,
pub schema: Option<String>,
}
impl Table {
pub fn new(name: impl Into<String>) -> Self {
Table { name: name.into(), alias: None, schema: None }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SubquerySource {
pub query: Box<Select>,
pub alias: String,
}
impl SubquerySource {
pub fn new(query: Select, alias: impl Into<String>) -> Self {
SubquerySource { query: Box::new(query), alias: alias.into() }
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum TableSource {
Table(Table),
Subquery(SubquerySource),
}
impl From<Table> for TableSource {
fn from(table: Table) -> Self {
TableSource::Table(table)
}
}
impl From<SubquerySource> for TableSource {
fn from(source: SubquerySource) -> Self {
TableSource::Subquery(source)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct JoinClause {
pub kind: String,
pub table: TableSource,
pub condition: Option<Expression>,
}
impl JoinClause {
pub fn new(kind: impl Into<String>, table: impl Into<TableSource>, condition: Option<Expression>) -> Self {
JoinClause { kind: kind.into(), table: table.into(), condition }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct OrderByItem {
pub expression: Expression,
pub direction: OrderDirection,
pub nulls: Option<NullsPosition>,
}
impl OrderByItem {
pub fn new(expression: impl Into<Expression>, direction: OrderDirection) -> Self {
OrderByItem { expression: expression.into(), direction, nulls: None }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cte {
pub name: String,
pub query: Select,
pub columns: Vec<String>,
pub recursive: bool,
}
impl Cte {
pub fn new(name: impl Into<String>, query: Select) -> Self {
Cte { name: name.into(), query, columns: Vec::new(), recursive: false }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowDefinition {
pub name: String,
pub specification: WindowSpecification,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct WindowSpecification {
pub partition_by: Vec<Expression>,
pub order_by: Vec<OrderByItem>,
pub frame_type: Option<String>,
pub frame_start: Option<String>,
pub frame_end: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Select {
pub columns: Vec<Expression>,
pub from: Option<TableSource>,
pub joins: Vec<JoinClause>,
pub where_clause: Option<Expression>,
pub group_by: Vec<Expression>,
pub having: Option<Expression>,
pub order_by: Vec<OrderByItem>,
pub limit: Option<Expression>,
pub offset: Option<Expression>,
pub distinct: bool,
pub ctes: Vec<Cte>,
pub windows: Vec<WindowDefinition>,
}