#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Name(pub Vec<String>);
impl Name {
pub fn simple(s: impl Into<String>) -> Self {
Name(vec![s.into()])
}
pub fn last(&self) -> &str {
self.0.last().map(String::as_str).unwrap_or("")
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Query(Box<Query>),
Insert(Insert),
Update(Update),
Delete(Delete),
With(WithStatement),
}
#[derive(Clone, Debug, PartialEq)]
pub struct WithStatement {
pub recursive: bool,
pub ctes: Vec<Cte>,
pub stmt: Box<Statement>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Query {
pub with: Vec<Cte>,
pub recursive: bool,
pub body: SetExpr,
pub order_by: Vec<OrderItem>,
pub limit: Option<Expr>,
pub offset: Option<Expr>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Cte {
pub name: String,
pub columns: Vec<String>,
pub body: CteBody,
}
#[derive(Clone, Debug, PartialEq)]
pub enum CteBody {
Query(Box<Query>),
Dml(Box<Statement>),
}
#[derive(Clone, Debug, PartialEq)]
pub enum SetExpr {
Select(Box<Select>),
SetOp {
op: SetOp,
all: bool,
left: Box<SetExpr>,
right: Box<SetExpr>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SetOp {
Union,
Intersect,
Except,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Distinct {
No,
Yes,
On(Vec<Expr>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Select {
pub distinct: Distinct,
pub projection: Vec<SelectItem>,
pub from: Vec<TableRef>,
pub where_: Option<Expr>,
pub group_by: Vec<Expr>,
pub group_by_rollup: bool,
pub having: Option<Expr>,
pub lock: Option<RowLockClause>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RowLockClause {
pub strength: RowLock,
pub of: Option<Name>,
pub wait: LockWait,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RowLock {
Update,
Share,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LockWait {
Block,
NoWait,
SkipLocked,
}
#[derive(Clone, Debug, PartialEq)]
pub enum SelectItem {
Expr { expr: Expr, alias: Option<String> },
Wildcard,
QualifiedWildcard(Name),
}
#[derive(Clone, Debug, PartialEq)]
pub enum TableRef {
Table {
name: Name,
alias: Option<String>,
},
Subquery {
query: Box<Query>,
alias: String,
},
Function {
name: String,
args: Vec<Expr>,
alias: Option<String>,
},
Join {
left: Box<TableRef>,
right: Box<TableRef>,
kind: JoinKind,
on: Option<Expr>,
using: Vec<String>,
natural: bool,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum JoinKind {
Inner,
Left,
Right,
Full,
Cross,
}
#[derive(Clone, Debug, PartialEq)]
pub struct OrderItem {
pub expr: Expr,
pub desc: bool,
pub nulls_first: Option<bool>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Insert {
pub table: Name,
pub replace: bool,
pub columns: Vec<String>,
pub source: InsertSource,
pub on_conflict: Option<OnConflict>,
pub returning: Vec<SelectItem>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum InsertSource {
Values(Vec<Vec<Expr>>),
Query(Box<Query>),
DefaultValues,
}
#[derive(Clone, Debug, PartialEq)]
pub struct OnConflict {
pub target: Vec<String>,
pub action: ConflictAction,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ConflictAction {
DoNothing,
DoUpdate {
set: Vec<Assignment>,
where_: Option<Expr>,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Assignment {
pub column: String,
pub value: Expr,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Update {
pub table: Name,
pub alias: Option<String>,
pub set: Vec<Assignment>,
pub from: Vec<TableRef>,
pub where_: Option<Expr>,
pub returning: Vec<SelectItem>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Delete {
pub table: Name,
pub alias: Option<String>,
pub using: Vec<TableRef>,
pub where_: Option<Expr>,
pub returning: Vec<SelectItem>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Expr {
Column(Name),
Literal(Literal),
Param(u32),
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
IsNull {
expr: Box<Expr>,
negated: bool,
},
IsBool {
expr: Box<Expr>,
value: bool,
negated: bool,
},
InList {
expr: Box<Expr>,
list: Vec<Expr>,
negated: bool,
},
InSubquery {
expr: Box<Expr>,
query: Box<Query>,
negated: bool,
},
Between {
expr: Box<Expr>,
low: Box<Expr>,
high: Box<Expr>,
negated: bool,
},
Like {
expr: Box<Expr>,
pattern: Box<Expr>,
negated: bool,
case_insensitive: bool,
escape: Option<Box<Expr>>,
},
Regexp {
expr: Box<Expr>,
pattern: Box<Expr>,
negated: bool,
},
Case {
operand: Option<Box<Expr>>,
branches: Vec<(Expr, Expr)>,
else_: Option<Box<Expr>>,
},
Cast {
expr: Box<Expr>,
ty: SqlType,
},
Function(Function),
Subquery(Box<Query>),
Exists {
query: Box<Query>,
negated: bool,
},
Nested(Box<Expr>),
AnyParam {
expr: Box<Expr>,
param: u32,
negated: bool,
},
Extract {
field: String,
expr: Box<Expr>,
},
ArrayLiteral(Vec<Expr>),
AtTimeZone {
expr: Box<Expr>,
zone: Box<Expr>,
},
ArraySubquery(Box<Query>),
Trim {
expr: Box<Expr>,
side: TrimSide,
chars: Option<Box<Expr>>,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
pub name: String,
pub args: Vec<FunctionArg>,
pub distinct: bool,
pub star: bool,
pub filter: Option<Box<Expr>>,
pub over: Option<Window>,
pub order_by: Vec<OrderItem>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum FunctionArg {
Expr(Expr),
Named {
name: String,
value: Expr,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Window {
pub partition_by: Vec<Expr>,
pub order_by: Vec<OrderItem>,
pub frame: Option<Frame>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Frame {
pub units: FrameUnits,
pub start: FrameBound,
pub end: Option<FrameBound>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FrameUnits {
Rows,
Range,
Groups,
}
#[derive(Clone, Debug, PartialEq)]
pub enum FrameBound {
CurrentRow,
Preceding(Option<Box<Expr>>),
Following(Option<Box<Expr>>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TrimSide {
Both,
Leading,
Trailing,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
Null,
Bool(bool),
Number(String),
String(String),
Bytes(Vec<u8>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnaryOp {
Not,
Minus,
Plus,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BinaryOp {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
And,
Or,
Plus,
Minus,
Multiply,
Divide,
Modulo,
Concat,
IntDivide,
NullSafeEq,
JsonGet,
JsonGetText,
BitAnd,
BitOr,
BitXor,
ShiftLeft,
ShiftRight,
Xor,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SqlType {
SmallInt,
Integer,
BigInt,
Real,
Double,
Numeric {
precision: Option<u32>,
scale: Option<u32>,
},
Text,
Varchar(Option<u32>),
Boolean,
Date,
Timestamp {
with_tz: bool,
},
Uuid,
Bytes,
Json,
Jsonb,
}
impl SqlType {
pub fn leaf(&self) -> &'static str {
match self {
SqlType::SmallInt => "smallint",
SqlType::Integer => "integer",
SqlType::BigInt => "bigint",
SqlType::Real => "real",
SqlType::Double => "double",
SqlType::Numeric { .. } => "numeric",
SqlType::Text | SqlType::Varchar(_) => "text",
SqlType::Boolean => "boolean",
SqlType::Date => "date",
SqlType::Timestamp { with_tz: false } => "timestamp",
SqlType::Timestamp { with_tz: true } => "timestamptz",
SqlType::Uuid => "uuid",
SqlType::Bytes => "bytes",
SqlType::Json => "json",
SqlType::Jsonb => "jsonb",
}
}
}