pub mod check;
pub mod fmt;
use std::num::ParseIntError;
use std::ops::Deref;
use std::str::{self, Bytes, FromStr};
use strum_macros::{EnumIter, EnumString};
use fmt::{ToTokens, TokenStream};
use indexmap::{IndexMap, IndexSet};
use crate::custom_err;
use crate::dialect::TokenType::{self, *};
use crate::dialect::{from_token, is_identifier, Token};
use crate::parser::{parse::YYCODETYPE, ParserError};
#[derive(Default)]
pub struct ParameterInfo {
pub count: u32,
pub names: IndexSet<String>,
}
impl TokenStream for ParameterInfo {
type Error = ParseIntError;
fn append(&mut self, ty: TokenType, value: Option<&str>) -> Result<(), Self::Error> {
if ty == TK_VARIABLE {
if let Some(variable) = value {
if variable == "?" {
self.count = self.count.saturating_add(1);
} else if variable.as_bytes()[0] == b'?' {
let n = u32::from_str(&variable[1..])?;
if n > self.count {
self.count = n;
}
} else if self.names.insert(variable.to_owned()) {
self.count = self.count.saturating_add(1);
}
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Cmd {
Explain(Stmt),
ExplainQueryPlan(Stmt),
Stmt(Stmt),
}
pub(crate) enum ExplainKind {
Explain,
QueryPlan,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Stmt {
AlterTable(Box<(QualifiedName, AlterTableBody)>),
Analyze(Option<QualifiedName>),
Attach {
expr: Box<Expr>,
db_name: Box<Expr>,
key: Option<Box<Expr>>,
},
Begin(Option<TransactionType>, Option<Name>),
Commit(Option<Name>), CreateIndex {
unique: bool,
if_not_exists: bool,
idx_name: Box<QualifiedName>,
tbl_name: Name,
columns: Vec<SortedColumn>,
where_clause: Option<Box<Expr>>,
},
CreateTable {
temporary: bool, if_not_exists: bool,
tbl_name: QualifiedName,
body: Box<CreateTableBody>,
},
CreateTrigger(Box<CreateTrigger>),
CreateView {
temporary: bool,
if_not_exists: bool,
view_name: QualifiedName,
columns: Option<Vec<IndexedColumn>>,
select: Box<Select>,
},
CreateVirtualTable(Box<CreateVirtualTable>),
Delete(Box<Delete>),
Detach(Box<Expr>), DropIndex {
if_exists: bool,
idx_name: QualifiedName,
},
DropTable {
if_exists: bool,
tbl_name: QualifiedName,
},
DropTrigger {
if_exists: bool,
trigger_name: QualifiedName,
},
DropView {
if_exists: bool,
view_name: QualifiedName,
},
Insert(Box<Insert>),
Pragma(Box<QualifiedName>, Option<Box<PragmaBody>>),
Reindex {
obj_name: Option<QualifiedName>,
},
Release(Name), Rollback {
tx_name: Option<Name>,
savepoint_name: Option<Name>, },
Savepoint(Name),
Select(Box<Select>),
Update(Box<Update>),
Vacuum(Option<Name>, Option<Box<Expr>>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateVirtualTable {
pub if_not_exists: bool,
pub tbl_name: QualifiedName,
pub module_name: Name,
pub args: Option<Vec<String>>, }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateTrigger {
pub temporary: bool,
pub if_not_exists: bool,
pub trigger_name: QualifiedName,
pub time: Option<TriggerTime>,
pub event: TriggerEvent,
pub tbl_name: QualifiedName,
pub for_each_row: bool,
pub when_clause: Option<Expr>,
pub commands: Vec<TriggerCmd>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Insert {
pub with: Option<With>,
pub or_conflict: Option<ResolveType>, pub tbl_name: QualifiedName,
pub columns: Option<DistinctNames>,
pub body: InsertBody,
pub returning: Option<Vec<ResultColumn>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Update {
pub with: Option<With>,
pub or_conflict: Option<ResolveType>,
pub tbl_name: QualifiedName,
pub indexed: Option<Indexed>,
pub sets: Vec<Set>,
pub from: Option<FromClause>,
pub where_clause: Option<Box<Expr>>,
pub returning: Option<Vec<ResultColumn>>,
pub order_by: Option<Vec<SortedColumn>>,
pub limit: Option<Box<Limit>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Delete {
pub with: Option<With>,
pub tbl_name: QualifiedName,
pub indexed: Option<Indexed>,
pub where_clause: Option<Box<Expr>>,
pub returning: Option<Vec<ResultColumn>>,
pub order_by: Option<Vec<SortedColumn>>,
pub limit: Option<Box<Limit>>,
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TableInternalId(usize);
impl Default for TableInternalId {
fn default() -> Self {
Self(1)
}
}
impl From<usize> for TableInternalId {
fn from(value: usize) -> Self {
Self(value)
}
}
impl std::ops::AddAssign<usize> for TableInternalId {
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs;
}
}
impl From<TableInternalId> for usize {
fn from(value: TableInternalId) -> Self {
value.0
}
}
impl std::fmt::Display for TableInternalId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "t{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Expr {
Between {
lhs: Box<Expr>,
not: bool,
start: Box<Expr>,
end: Box<Expr>,
},
Binary(Box<Expr>, Operator, Box<Expr>),
Case {
base: Option<Box<Expr>>,
when_then_pairs: Vec<(Expr, Expr)>,
else_expr: Option<Box<Expr>>,
},
Cast {
expr: Box<Expr>,
type_name: Option<Type>,
},
Collate(Box<Expr>, String),
DoublyQualified(Name, Name, Name),
Exists(Box<Select>),
FunctionCall {
name: Id,
distinctness: Option<Distinctness>,
args: Option<Vec<Expr>>,
order_by: Option<Vec<SortedColumn>>,
filter_over: Option<FunctionTail>,
},
FunctionCallStar {
name: Id,
filter_over: Option<FunctionTail>,
},
Id(Id),
Column {
database: Option<usize>,
table: TableInternalId,
column: usize,
is_rowid_alias: bool,
},
RowId {
database: Option<usize>,
table: TableInternalId,
},
InList {
lhs: Box<Expr>,
not: bool,
rhs: Option<Vec<Expr>>,
},
InSelect {
lhs: Box<Expr>,
not: bool,
rhs: Box<Select>,
},
InTable {
lhs: Box<Expr>,
not: bool,
rhs: QualifiedName,
args: Option<Vec<Expr>>,
},
IsNull(Box<Expr>),
Like {
lhs: Box<Expr>,
not: bool,
op: LikeOperator,
rhs: Box<Expr>,
escape: Option<Box<Expr>>,
},
Literal(Literal),
Name(Name),
NotNull(Box<Expr>),
Parenthesized(Vec<Expr>),
Qualified(Name, Name),
Raise(ResolveType, Option<Box<Expr>>),
Subquery(Box<Select>),
Unary(UnaryOperator, Box<Expr>),
Variable(String),
}
impl Expr {
pub fn parenthesized(x: Self) -> Self {
Self::Parenthesized(vec![x])
}
pub fn id(xt: YYCODETYPE, x: Token) -> Self {
Self::Id(Id::from_token(xt, x))
}
pub fn collate(x: Self, ct: YYCODETYPE, c: Token) -> Self {
Self::Collate(Box::new(x), from_token(ct, c))
}
pub fn cast(x: Self, type_name: Option<Type>) -> Self {
Self::Cast {
expr: Box::new(x),
type_name,
}
}
pub fn binary(left: Self, op: YYCODETYPE, right: Self) -> Self {
Self::Binary(Box::new(left), Operator::from(op), Box::new(right))
}
pub fn ptr(left: Self, op: Token, right: Self) -> Self {
let mut ptr = Operator::ArrowRight;
if op.1 == b"->>" {
ptr = Operator::ArrowRightShift;
}
Self::Binary(Box::new(left), ptr, Box::new(right))
}
pub fn like(lhs: Self, not: bool, op: LikeOperator, rhs: Self, escape: Option<Self>) -> Self {
Self::Like {
lhs: Box::new(lhs),
not,
op,
rhs: Box::new(rhs),
escape: escape.map(Box::new),
}
}
pub fn not_null(x: Self, op: YYCODETYPE) -> Self {
if op == TK_ISNULL as YYCODETYPE {
Self::IsNull(Box::new(x))
} else if op == TK_NOTNULL as YYCODETYPE {
Self::NotNull(Box::new(x))
} else {
unreachable!()
}
}
pub fn unary(op: UnaryOperator, x: Self) -> Self {
Self::Unary(op, Box::new(x))
}
pub fn between(lhs: Self, not: bool, start: Self, end: Self) -> Self {
Self::Between {
lhs: Box::new(lhs),
not,
start: Box::new(start),
end: Box::new(end),
}
}
pub fn in_list(lhs: Self, not: bool, rhs: Option<Vec<Self>>) -> Self {
Self::InList {
lhs: Box::new(lhs),
not,
rhs,
}
}
pub fn in_select(lhs: Self, not: bool, rhs: Select) -> Self {
Self::InSelect {
lhs: Box::new(lhs),
not,
rhs: Box::new(rhs),
}
}
pub fn in_table(lhs: Self, not: bool, rhs: QualifiedName, args: Option<Vec<Self>>) -> Self {
Self::InTable {
lhs: Box::new(lhs),
not,
rhs,
args,
}
}
pub fn sub_query(query: Select) -> Self {
Self::Subquery(Box::new(query))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Literal {
Numeric(String),
String(String),
Blob(String),
Keyword(String),
Null,
CurrentDate,
CurrentTime,
CurrentTimestamp,
}
impl Literal {
pub fn from_ctime_kw(token: Token) -> Self {
if b"CURRENT_DATE".eq_ignore_ascii_case(token.1) {
Self::CurrentDate
} else if b"CURRENT_TIME".eq_ignore_ascii_case(token.1) {
Self::CurrentTime
} else if b"CURRENT_TIMESTAMP".eq_ignore_ascii_case(token.1) {
Self::CurrentTimestamp
} else {
unreachable!()
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LikeOperator {
Glob,
Like,
Match,
Regexp,
}
impl LikeOperator {
pub fn from_token(token_type: YYCODETYPE, token: Token) -> Self {
if token_type == TK_MATCH as YYCODETYPE {
return Self::Match;
} else if token_type == TK_LIKE_KW as YYCODETYPE {
let token = token.1;
if b"LIKE".eq_ignore_ascii_case(token) {
return Self::Like;
} else if b"GLOB".eq_ignore_ascii_case(token) {
return Self::Glob;
} else if b"REGEXP".eq_ignore_ascii_case(token) {
return Self::Regexp;
}
}
unreachable!()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Operator {
Add,
And,
ArrowRight,
ArrowRightShift,
BitwiseAnd,
BitwiseOr,
BitwiseNot,
Concat,
Equals,
Divide,
Greater,
GreaterEquals,
Is,
IsNot,
LeftShift,
Less,
LessEquals,
Modulus,
Multiply,
NotEquals,
Or,
RightShift,
Subtract,
}
impl From<YYCODETYPE> for Operator {
fn from(token_type: YYCODETYPE) -> Self {
match token_type {
x if x == TK_AND as YYCODETYPE => Self::And,
x if x == TK_OR as YYCODETYPE => Self::Or,
x if x == TK_LT as YYCODETYPE => Self::Less,
x if x == TK_GT as YYCODETYPE => Self::Greater,
x if x == TK_GE as YYCODETYPE => Self::GreaterEquals,
x if x == TK_LE as YYCODETYPE => Self::LessEquals,
x if x == TK_EQ as YYCODETYPE => Self::Equals,
x if x == TK_NE as YYCODETYPE => Self::NotEquals,
x if x == TK_BITAND as YYCODETYPE => Self::BitwiseAnd,
x if x == TK_BITOR as YYCODETYPE => Self::BitwiseOr,
x if x == TK_BITNOT as YYCODETYPE => Self::BitwiseNot,
x if x == TK_LSHIFT as YYCODETYPE => Self::LeftShift,
x if x == TK_RSHIFT as YYCODETYPE => Self::RightShift,
x if x == TK_PLUS as YYCODETYPE => Self::Add,
x if x == TK_MINUS as YYCODETYPE => Self::Subtract,
x if x == TK_STAR as YYCODETYPE => Self::Multiply,
x if x == TK_SLASH as YYCODETYPE => Self::Divide,
x if x == TK_REM as YYCODETYPE => Self::Modulus,
x if x == TK_CONCAT as YYCODETYPE => Self::Concat,
x if x == TK_IS as YYCODETYPE => Self::Is,
x if x == TK_NOT as YYCODETYPE => Self::IsNot,
_ => unreachable!(),
}
}
}
impl Operator {
pub fn is_commutative(&self) -> bool {
matches!(
self,
Operator::Add
| Operator::Multiply
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::Equals
| Operator::NotEquals
)
}
pub fn is_comparison(&self) -> bool {
matches!(
self,
Self::Equals
| Self::NotEquals
| Self::Less
| Self::LessEquals
| Self::Greater
| Self::GreaterEquals
| Self::Is
| Self::IsNot
)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UnaryOperator {
BitwiseNot,
Negative,
Not,
Positive,
}
impl From<YYCODETYPE> for UnaryOperator {
fn from(token_type: YYCODETYPE) -> Self {
match token_type {
x if x == TK_BITNOT as YYCODETYPE => Self::BitwiseNot,
x if x == TK_MINUS as YYCODETYPE => Self::Negative,
x if x == TK_NOT as YYCODETYPE => Self::Not,
x if x == TK_PLUS as YYCODETYPE => Self::Positive,
_ => unreachable!(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Select {
pub with: Option<With>,
pub body: SelectBody,
pub order_by: Option<Vec<SortedColumn>>, pub limit: Option<Box<Limit>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelectBody {
pub select: Box<OneSelect>,
pub compounds: Option<Vec<CompoundSelect>>,
}
impl SelectBody {
pub(crate) fn push(&mut self, cs: CompoundSelect) -> Result<(), ParserError> {
use crate::ast::check::ColumnCount;
if let ColumnCount::Fixed(n) = self.select.column_count() {
if let ColumnCount::Fixed(m) = cs.select.column_count() {
if n != m {
return Err(custom_err!(
"SELECTs to the left and right of {} do not have the same number of result columns",
cs.operator
));
}
}
}
if let Some(ref mut v) = self.compounds {
v.push(cs);
} else {
self.compounds = Some(vec![cs]);
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CompoundSelect {
pub operator: CompoundOperator,
pub select: Box<OneSelect>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CompoundOperator {
Union,
UnionAll,
Except,
Intersect,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OneSelect {
Select(Box<SelectInner>),
Values(Vec<Vec<Expr>>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelectInner {
pub distinctness: Option<Distinctness>,
pub columns: Vec<ResultColumn>,
pub from: Option<FromClause>,
pub where_clause: Option<Expr>,
pub group_by: Option<GroupBy>,
pub window_clause: Option<Vec<WindowDef>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FromClause {
pub select: Option<Box<SelectTable>>, pub joins: Option<Vec<JoinedSelectTable>>,
op: Option<JoinOperator>, }
impl FromClause {
pub(crate) fn empty() -> Self {
Self {
select: None,
joins: None,
op: None,
}
}
pub(crate) fn push(
&mut self,
table: SelectTable,
jc: Option<JoinConstraint>,
) -> Result<(), ParserError> {
let op = self.op.take();
if let Some(op) = op {
let jst = JoinedSelectTable {
operator: op,
table,
constraint: jc,
};
if jst.operator.is_natural() && jst.constraint.is_some() {
return Err(custom_err!(
"a NATURAL join may not have an ON or USING clause"
));
}
if let Some(ref mut joins) = self.joins {
joins.push(jst);
} else {
self.joins = Some(vec![jst]);
}
} else {
if jc.is_some() {
return Err(custom_err!("a JOIN clause is required before ON"));
}
debug_assert!(self.select.is_none());
debug_assert!(self.joins.is_none());
self.select = Some(Box::new(table));
}
Ok(())
}
pub(crate) fn push_op(&mut self, op: JoinOperator) {
self.op = Some(op);
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Distinctness {
Distinct,
All,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResultColumn {
Expr(Expr, Option<As>),
Star,
TableStar(Name),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum As {
As(Name),
Elided(Name), }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JoinedSelectTable {
pub operator: JoinOperator,
pub table: SelectTable,
pub constraint: Option<JoinConstraint>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SelectTable {
Table(QualifiedName, Option<As>, Option<Indexed>),
TableCall(QualifiedName, Option<Vec<Expr>>, Option<As>),
Select(Box<Select>, Option<As>),
Sub(FromClause, Option<As>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum JoinOperator {
Comma,
TypedJoin(Option<JoinType>),
}
impl JoinOperator {
pub(crate) fn from(
token: Token,
n1: Option<Name>,
n2: Option<Name>,
) -> Result<Self, ParserError> {
Ok({
let mut jt = JoinType::try_from(token.1)?;
for n in [&n1, &n2].into_iter().flatten() {
jt |= JoinType::try_from(n.0.as_ref())?;
}
if (jt & (JoinType::INNER | JoinType::OUTER)) == (JoinType::INNER | JoinType::OUTER)
|| (jt & (JoinType::OUTER | JoinType::LEFT | JoinType::RIGHT)) == JoinType::OUTER
{
return Err(custom_err!(
"unsupported JOIN type: {:?} {:?} {:?}",
str::from_utf8(token.1),
n1,
n2
));
}
Self::TypedJoin(Some(jt))
})
}
fn is_natural(&self) -> bool {
match self {
Self::TypedJoin(Some(jt)) => jt.contains(JoinType::NATURAL),
_ => false,
}
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JoinType: u8 {
const INNER = 0x01;
const CROSS = 0x02;
const NATURAL = 0x04;
const LEFT = 0x08;
const RIGHT = 0x10;
const OUTER = 0x20;
}
}
impl TryFrom<&[u8]> for JoinType {
type Error = ParserError;
fn try_from(s: &[u8]) -> Result<Self, ParserError> {
if b"CROSS".eq_ignore_ascii_case(s) {
Ok(Self::INNER | Self::CROSS)
} else if b"FULL".eq_ignore_ascii_case(s) {
Ok(Self::LEFT | Self::RIGHT | Self::OUTER)
} else if b"INNER".eq_ignore_ascii_case(s) {
Ok(Self::INNER)
} else if b"LEFT".eq_ignore_ascii_case(s) {
Ok(Self::LEFT | Self::OUTER)
} else if b"NATURAL".eq_ignore_ascii_case(s) {
Ok(Self::NATURAL)
} else if b"RIGHT".eq_ignore_ascii_case(s) {
Ok(Self::RIGHT | Self::OUTER)
} else if b"OUTER".eq_ignore_ascii_case(s) {
Ok(Self::OUTER)
} else {
Err(custom_err!(
"unsupported JOIN type: {:?}",
str::from_utf8(s)
))
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum JoinConstraint {
On(Expr),
Using(DistinctNames),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GroupBy {
pub exprs: Vec<Expr>,
pub having: Option<Box<Expr>>, }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Id(pub String);
impl Id {
pub fn from_token(ty: YYCODETYPE, token: Token) -> Self {
Self(from_token(ty, token))
}
}
#[derive(Clone, Debug, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Name(pub String);
impl Name {
pub fn from_token(ty: YYCODETYPE, token: Token) -> Self {
Self(from_token(ty, token))
}
fn as_bytes(&self) -> QuotedIterator<'_> {
if self.0.is_empty() {
return QuotedIterator(self.0.bytes(), 0);
}
let bytes = self.0.as_bytes();
let mut quote = bytes[0];
if quote != b'"' && quote != b'`' && quote != b'\'' && quote != b'[' {
return QuotedIterator(self.0.bytes(), 0);
} else if quote == b'[' {
quote = b']';
}
debug_assert!(bytes.len() > 1);
debug_assert_eq!(quote, bytes[bytes.len() - 1]);
let sub = &self.0.as_str()[1..bytes.len() - 1];
if quote == b']' {
return QuotedIterator(sub.bytes(), 0); }
QuotedIterator(sub.bytes(), quote)
}
}
struct QuotedIterator<'s>(Bytes<'s>, u8);
impl Iterator for QuotedIterator<'_> {
type Item = u8;
fn next(&mut self) -> Option<u8> {
match self.0.next() {
x @ Some(b) => {
if b == self.1 && self.0.next() != Some(self.1) {
panic!("Malformed string literal: {:?}", self.0);
}
x
}
x => x,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.1 == 0 {
return self.0.size_hint();
}
(0, None)
}
}
fn eq_ignore_case_and_quote(mut it: QuotedIterator<'_>, mut other: QuotedIterator<'_>) -> bool {
loop {
match (it.next(), other.next()) {
(Some(b1), Some(b2)) => {
if !b1.eq_ignore_ascii_case(&b2) {
return false;
}
}
(None, None) => break,
_ => return false,
}
}
true
}
impl std::hash::Hash for Name {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
self.as_bytes()
.for_each(|b| hasher.write_u8(b.to_ascii_lowercase()));
}
}
impl PartialEq for Name {
fn eq(&self, other: &Self) -> bool {
eq_ignore_case_and_quote(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<str> for Name {
fn eq(&self, other: &str) -> bool {
eq_ignore_case_and_quote(self.as_bytes(), QuotedIterator(other.bytes(), 0u8))
}
}
impl PartialEq<&str> for Name {
fn eq(&self, other: &&str) -> bool {
eq_ignore_case_and_quote(self.as_bytes(), QuotedIterator(other.bytes(), 0u8))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct QualifiedName {
pub db_name: Option<Name>,
pub name: Name,
pub alias: Option<Name>, }
impl QualifiedName {
pub fn single(name: Name) -> Self {
Self {
db_name: None,
name,
alias: None,
}
}
pub fn fullname(db_name: Name, name: Name) -> Self {
Self {
db_name: Some(db_name),
name,
alias: None,
}
}
pub fn xfullname(db_name: Name, name: Name, alias: Name) -> Self {
Self {
db_name: Some(db_name),
name,
alias: Some(alias),
}
}
pub fn alias(name: Name, alias: Name) -> Self {
Self {
db_name: None,
name,
alias: Some(alias),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DistinctNames(IndexSet<Name>);
impl DistinctNames {
pub fn new(name: Name) -> Self {
let mut dn = Self(IndexSet::new());
dn.0.insert(name);
dn
}
pub fn single(name: Name) -> Self {
let mut dn = Self(IndexSet::with_capacity(1));
dn.0.insert(name);
dn
}
pub fn insert(&mut self, name: Name) -> Result<(), ParserError> {
if self.0.contains(&name) {
return Err(custom_err!("column \"{}\" specified more than once", name));
}
self.0.insert(name);
Ok(())
}
}
impl Deref for DistinctNames {
type Target = IndexSet<Name>;
fn deref(&self) -> &IndexSet<Name> {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AlterTableBody {
RenameTo(Name),
AddColumn(ColumnDefinition), RenameColumn {
old: Name,
new: Name,
},
DropColumn(Name), }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CreateTableBody {
ColumnsAndConstraints {
columns: IndexMap<Name, ColumnDefinition>,
constraints: Option<Vec<NamedTableConstraint>>,
options: TableOptions,
},
AsSelect(Box<Select>),
}
impl CreateTableBody {
pub fn columns_and_constraints(
columns: IndexMap<Name, ColumnDefinition>,
constraints: Option<Vec<NamedTableConstraint>>,
options: TableOptions,
) -> Result<Self, ParserError> {
Ok(Self::ColumnsAndConstraints {
columns,
constraints,
options,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColumnDefinition {
pub col_name: Name,
pub col_type: Option<Type>,
pub constraints: Vec<NamedColumnConstraint>,
}
impl ColumnDefinition {
pub fn add_column(columns: &mut IndexMap<Name, Self>, mut cd: Self) -> Result<(), ParserError> {
let col_name = &cd.col_name;
if columns.contains_key(col_name) {
return Err(custom_err!("duplicate column name: {}", col_name));
}
if let Some(ref mut col_type) = cd.col_type {
let mut split = col_type.name.split_ascii_whitespace();
let truncate = if split
.next_back()
.is_some_and(|s| s.eq_ignore_ascii_case("ALWAYS"))
&& split
.next_back()
.is_some_and(|s| s.eq_ignore_ascii_case("GENERATED"))
{
let mut generated = false;
for constraint in &cd.constraints {
if let ColumnConstraint::Generated { .. } = constraint.constraint {
generated = true;
break;
}
}
generated
} else {
false
};
if truncate {
let new_type: Vec<&str> = split.collect();
col_type.name = new_type.join(" ");
}
}
for constraint in &cd.constraints {
if let ColumnConstraint::ForeignKey {
clause:
ForeignKeyClause {
tbl_name, columns, ..
},
..
} = &constraint.constraint
{
if columns.as_ref().map_or(0, Vec::len) > 1 {
return Err(custom_err!(
"foreign key on {} should reference only one column of table {}",
col_name,
tbl_name
));
}
}
}
columns.insert(col_name.clone(), cd);
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NamedColumnConstraint {
pub name: Option<Name>,
pub constraint: ColumnConstraint,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ColumnConstraint {
PrimaryKey {
order: Option<SortOrder>,
conflict_clause: Option<ResolveType>,
auto_increment: bool,
},
NotNull {
nullable: bool,
conflict_clause: Option<ResolveType>,
},
Unique(Option<ResolveType>),
Check(Expr),
Default(Expr),
Defer(DeferSubclause), Collate {
collation_name: Name, },
ForeignKey {
clause: ForeignKeyClause,
deref_clause: Option<DeferSubclause>,
},
Generated {
expr: Expr,
typ: Option<Id>,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NamedTableConstraint {
pub name: Option<Name>,
pub constraint: TableConstraint,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TableConstraint {
PrimaryKey {
columns: Vec<SortedColumn>,
auto_increment: bool,
conflict_clause: Option<ResolveType>,
},
Unique {
columns: Vec<SortedColumn>,
conflict_clause: Option<ResolveType>,
},
Check(Expr),
ForeignKey {
columns: Vec<IndexedColumn>,
clause: ForeignKeyClause,
deref_clause: Option<DeferSubclause>,
},
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TableOptions: u8 {
const NONE = 0;
const WITHOUT_ROWID = 1;
const STRICT = 2;
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NullsOrder {
First,
Last,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ForeignKeyClause {
pub tbl_name: Name,
pub columns: Option<Vec<IndexedColumn>>,
pub args: Vec<RefArg>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RefArg {
OnDelete(RefAct),
OnInsert(RefAct),
OnUpdate(RefAct),
Match(Name),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RefAct {
SetNull,
SetDefault,
Cascade,
Restrict,
NoAction,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeferSubclause {
pub deferrable: bool,
pub init_deferred: Option<InitDeferredPred>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InitDeferredPred {
InitiallyDeferred,
InitiallyImmediate, }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct IndexedColumn {
pub col_name: Name,
pub collation_name: Option<Name>, pub order: Option<SortOrder>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Indexed {
IndexedBy(Name),
NotIndexed,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SortedColumn {
pub expr: Expr,
pub order: Option<SortOrder>,
pub nulls: Option<NullsOrder>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Limit {
pub expr: Expr,
pub offset: Option<Expr>, }
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InsertBody {
Select(Box<Select>, Option<Upsert>),
DefaultValues,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Set {
pub col_names: DistinctNames,
pub expr: Expr,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PragmaBody {
Equals(PragmaValue),
Call(PragmaValue),
}
pub type PragmaValue = Expr;
#[derive(Clone, Debug, PartialEq, Eq, EnumIter, EnumString, strum::Display)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PragmaName {
AutoVacuum,
CacheSize,
IntegrityCheck,
JournalMode,
LegacyFileFormat,
PageCount,
PageSize,
SchemaVersion,
TableInfo,
UserVersion,
WalCheckpoint,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TriggerTime {
Before, After,
InsteadOf,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TriggerEvent {
Delete,
Insert,
Update,
UpdateOf(DistinctNames),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TriggerCmd {
Update(Box<TriggerCmdUpdate>),
Insert(Box<TriggerCmdInsert>),
Delete(Box<TriggerCmdDelete>),
Select(Box<Select>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TriggerCmdUpdate {
pub or_conflict: Option<ResolveType>,
pub tbl_name: Name,
pub sets: Vec<Set>,
pub from: Option<FromClause>,
pub where_clause: Option<Expr>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TriggerCmdInsert {
pub or_conflict: Option<ResolveType>,
pub tbl_name: Name,
pub col_names: Option<DistinctNames>,
pub select: Box<Select>,
pub upsert: Option<Upsert>,
pub returning: Option<Vec<ResultColumn>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TriggerCmdDelete {
pub tbl_name: Name,
pub where_clause: Option<Expr>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResolveType {
Rollback,
Abort, Fail,
Ignore,
Replace,
}
impl ResolveType {
pub fn bit_value(&self) -> usize {
match self {
ResolveType::Rollback => 1,
ResolveType::Abort => 2,
ResolveType::Fail => 3,
ResolveType::Ignore => 4,
ResolveType::Replace => 5,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct With {
pub recursive: bool,
pub ctes: Vec<CommonTableExpr>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Materialized {
Any,
Yes,
No,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommonTableExpr {
pub tbl_name: Name,
pub columns: Option<Vec<IndexedColumn>>, pub materialized: Materialized,
pub select: Box<Select>,
}
impl CommonTableExpr {
pub fn add_cte(ctes: &mut Vec<Self>, cte: Self) -> Result<(), ParserError> {
if ctes.iter().any(|c| c.tbl_name == cte.tbl_name) {
return Err(custom_err!("duplicate WITH table name: {}", cte.tbl_name));
}
ctes.push(cte);
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Type {
pub name: String, pub size: Option<TypeSize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TypeSize {
MaxSize(Box<Expr>),
TypeSize(Box<Expr>, Box<Expr>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactionType {
Deferred, Immediate,
Exclusive,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Upsert {
pub index: Option<Box<UpsertIndex>>,
pub do_clause: Box<UpsertDo>,
pub next: Option<Box<Upsert>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UpsertIndex {
pub targets: Vec<SortedColumn>,
pub where_clause: Option<Expr>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UpsertDo {
Set {
sets: Vec<Set>,
where_clause: Option<Expr>,
},
Nothing,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FunctionTail {
pub filter_clause: Option<Box<Expr>>,
pub over_clause: Option<Box<Over>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Over {
Window(Window),
Name(Name),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowDef {
pub name: Name,
pub window: Window,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Window {
pub base: Option<Name>,
pub partition_by: Option<Vec<Expr>>,
pub order_by: Option<Vec<SortedColumn>>,
pub frame_clause: Option<FrameClause>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FrameClause {
pub mode: FrameMode,
pub start: FrameBound,
pub end: Option<FrameBound>,
pub exclude: Option<FrameExclude>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FrameMode {
Groups,
Range,
Rows,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FrameBound {
CurrentRow,
Following(Box<Expr>),
Preceding(Box<Expr>),
UnboundedFollowing,
UnboundedPreceding,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FrameExclude {
NoOthers,
CurrentRow,
Group,
Ties,
}
#[cfg(test)]
mod test {
use super::{Name, PragmaName};
use strum::IntoEnumIterator;
#[test]
fn test_dequote() {
assert_eq!(name("x"), "x");
assert_eq!(name("`x`"), "x");
assert_eq!(name("`x``y`"), "x`y");
assert_eq!(name(r#""x""#), "x");
assert_eq!(name(r#""x""y""#), "x\"y");
assert_eq!(name("[x]"), "x");
}
#[test]
fn pragma_list_sorted() {
let pragma_strings: Vec<String> = PragmaName::iter().map(|x| x.to_string()).collect();
let mut pragma_strings_sorted = pragma_strings.clone();
pragma_strings_sorted.sort();
assert_eq!(pragma_strings, pragma_strings_sorted);
}
fn name(s: &'static str) -> Name {
Name(s.to_owned())
}
}