#[cfg(feature = "extra_checks")]
pub mod check;
pub mod fmt;
use std::num::ParseIntError;
use std::ops::Deref;
use std::str::{self, Bytes, FromStr as _};
#[cfg(feature = "extra_checks")]
use check::ColumnCount;
use fmt::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: u16,
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 = u16::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)]
pub enum Stmt {
AlterTable(QualifiedName, AlterTableBody),
Analyze(Option<QualifiedName>),
Attach {
expr: Expr,
db_name: Expr,
key: Option<Expr>,
},
Begin(Option<TransactionType>, Option<Name>),
Commit(Option<Name>), CreateIndex {
unique: bool,
if_not_exists: bool,
idx_name: QualifiedName,
tbl_name: Name,
columns: Vec<SortedColumn>,
where_clause: Option<Expr>,
},
CreateTable {
temporary: bool, if_not_exists: bool,
tbl_name: QualifiedName,
body: CreateTableBody,
},
CreateTrigger {
temporary: bool,
if_not_exists: bool,
trigger_name: QualifiedName,
time: Option<TriggerTime>,
event: TriggerEvent,
tbl_name: QualifiedName,
for_each_row: bool,
when_clause: Option<Expr>,
commands: Vec<TriggerCmd>,
},
CreateView {
temporary: bool,
if_not_exists: bool,
view_name: QualifiedName,
columns: Option<Vec<IndexedColumn>>, select: Box<Select>,
},
CreateVirtualTable {
if_not_exists: bool,
tbl_name: QualifiedName,
module_name: Name,
args: Option<Vec<Box<str>>>,
},
Delete {
with: Option<With>, tbl_name: QualifiedName,
indexed: Option<Indexed>,
where_clause: Option<Expr>,
returning: Option<Vec<ResultColumn>>,
order_by: Option<Vec<SortedColumn>>,
limit: Option<Limit>,
},
Detach(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 {
with: Option<With>, or_conflict: Option<ResolveType>, tbl_name: QualifiedName,
columns: Option<DistinctNames>,
body: InsertBody,
returning: Option<Vec<ResultColumn>>,
},
Pragma(QualifiedName, Option<PragmaBody>),
Reindex {
obj_name: Option<QualifiedName>,
},
Release(Name), Rollback {
tx_name: Option<Name>,
savepoint_name: Option<Name>, },
Savepoint(Name),
Select(Box<Select>),
Update {
with: Option<With>, or_conflict: Option<ResolveType>,
tbl_name: QualifiedName,
indexed: Option<Indexed>,
sets: Vec<Set>, from: Option<FromClause>,
where_clause: Option<Expr>,
returning: Option<Vec<ResultColumn>>,
order_by: Option<Vec<SortedColumn>>,
limit: Option<Limit>,
},
Vacuum(Option<Name>, Option<Expr>),
}
impl Stmt {
pub fn create_index(
unique: bool,
if_not_exists: bool,
idx_name: QualifiedName,
tbl_name: Name,
columns: Vec<SortedColumn>,
where_clause: Option<Expr>,
) -> Result<Self, ParserError> {
has_explicit_nulls(&columns)?;
Ok(Self::CreateIndex {
unique,
if_not_exists,
idx_name,
tbl_name,
columns,
where_clause,
})
}
#[allow(clippy::too_many_arguments)]
pub fn update(
with: Option<With>,
or_conflict: Option<ResolveType>,
tbl_name: QualifiedName,
indexed: Option<Indexed>,
sets: Vec<Set>, from: Option<FromClause>,
where_clause: Option<Expr>,
returning: Option<Vec<ResultColumn>>,
order_by: Option<Vec<SortedColumn>>,
limit: Option<Limit>,
) -> Result<Self, ParserError> {
#[cfg(feature = "extra_checks")]
if let Some(FromClause {
select: Some(ref select),
ref joins,
..
}) = from
{
if matches!(select.as_ref(),
SelectTable::Table(qn, _, _) | SelectTable::TableCall(qn, _, _)
if *qn == tbl_name)
|| joins.as_ref().is_some_and(|js| js.iter().any(|j|
matches!(j.table, SelectTable::Table(ref qn, _, _) | SelectTable::TableCall(ref qn, _, _)
if *qn == tbl_name)))
{
return Err(custom_err!(
"target object/alias may not appear in FROM clause",
));
}
}
#[cfg(feature = "extra_checks")]
if order_by.is_some() && limit.is_none() {
return Err(custom_err!("ORDER BY without LIMIT on UPDATE"));
}
Ok(Self::Update {
with,
or_conflict,
tbl_name,
indexed,
sets,
from,
where_clause,
returning,
order_by,
limit,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr {
Between {
lhs: Box<Self>,
not: bool,
start: Box<Self>,
end: Box<Self>,
},
Binary(Box<Self>, Operator, Box<Self>),
Case {
base: Option<Box<Self>>,
when_then_pairs: Vec<(Self, Self)>,
else_expr: Option<Box<Self>>,
},
Cast {
expr: Box<Self>,
type_name: Option<Type>,
},
Collate(Box<Self>, Box<str>),
DoublyQualified(Name, Name, Name),
Exists(Box<Select>),
FunctionCall {
name: Id,
distinctness: Option<Distinctness>,
args: Option<Vec<Self>>,
order_by: Option<FunctionCallOrder>,
filter_over: Option<FunctionTail>,
},
FunctionCallStar {
name: Id,
filter_over: Option<FunctionTail>,
},
Id(Id),
InList {
lhs: Box<Self>,
not: bool,
rhs: Option<Vec<Self>>,
},
InSelect {
lhs: Box<Self>,
not: bool,
rhs: Box<Select>,
},
InTable {
lhs: Box<Self>,
not: bool,
rhs: QualifiedName,
args: Option<Vec<Self>>,
},
IsNull(Box<Self>),
Like {
lhs: Box<Self>,
not: bool,
op: LikeOperator,
rhs: Box<Self>,
escape: Option<Box<Self>>,
},
Literal(Literal),
Name(Name),
NotNull(Box<Self>),
Parenthesized(Vec<Self>),
Qualified(Name, Name),
Raise(ResolveType, Option<Box<Self>>),
Subquery(Box<Select>),
Unary(UnaryOperator, Box<Self>),
Variable(Box<str>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FunctionCallOrder {
SortList(Vec<SortedColumn>),
#[cfg(feature = "SQLITE_ENABLE_ORDERED_SET_AGGREGATES")]
WithinGroup(Box<Expr>),
}
impl FunctionCallOrder {
#[cfg(feature = "SQLITE_ENABLE_ORDERED_SET_AGGREGATES")]
pub fn within_group(expr: Expr) -> Self {
Self::WithinGroup(Box::new(expr))
}
}
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))
}
pub fn function_call(
xt: YYCODETYPE,
x: Token,
distinctness: Option<Distinctness>,
args: Option<Vec<Self>>,
order_by: Option<FunctionCallOrder>,
filter_over: Option<FunctionTail>,
) -> Result<Self, ParserError> {
#[cfg(feature = "extra_checks")]
if let Some(Distinctness::Distinct) = distinctness {
if args.as_ref().map_or(0, Vec::len) != 1 {
return Err(custom_err!(
"DISTINCT aggregates must have exactly one argument"
));
}
}
Ok(Self::FunctionCall {
name: Id::from_token(xt, x),
distinctness,
args,
order_by,
filter_over,
})
}
pub fn is_integer(&self) -> Option<i64> {
if let Self::Literal(Literal::Numeric(s)) = self {
i64::from_str(s).ok()
} else if let Self::Unary(UnaryOperator::Positive, e) = self {
e.is_integer()
} else if let Self::Unary(UnaryOperator::Negative, e) = self {
e.is_integer().map(i64::saturating_neg)
} else {
None
}
}
#[cfg(feature = "extra_checks")]
fn check_range(&self, term: &str, mx: u16) -> Result<(), ParserError> {
if let Some(i) = self.is_integer() {
if i < 1 || i > mx as i64 {
return Err(custom_err!(
"{} BY term out of range - should be between 1 and {}",
term,
mx
));
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Literal {
Numeric(Box<str>),
String(Box<str>),
Blob(Box<str>),
Keyword(Box<str>),
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)]
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)]
pub enum Operator {
Add,
And,
ArrowRight,
ArrowRightShift,
BitwiseAnd,
BitwiseOr,
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_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!(),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
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)]
pub struct Select {
pub with: Option<With>, pub body: SelectBody,
pub order_by: Option<Vec<SortedColumn>>, pub limit: Option<Limit>,
}
impl Select {
pub fn new(
with: Option<With>,
body: SelectBody,
order_by: Option<Vec<SortedColumn>>,
limit: Option<Limit>,
) -> Result<Self, ParserError> {
let select = Self {
with,
body,
order_by,
limit,
};
#[cfg(feature = "extra_checks")]
if let Self {
order_by: Some(ref scs),
..
} = select
{
if let ColumnCount::Fixed(n) = select.column_count() {
for sc in scs {
sc.expr.check_range("ORDER", n)?;
}
}
}
Ok(select)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SelectBody {
pub select: OneSelect,
pub compounds: Option<Vec<CompoundSelect>>,
}
impl SelectBody {
pub(crate) fn push(&mut self, cs: CompoundSelect) -> Result<(), ParserError> {
#[cfg(feature = "extra_checks")]
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)]
pub struct CompoundSelect {
pub operator: CompoundOperator,
pub select: OneSelect,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CompoundOperator {
Union,
UnionAll,
Except,
Intersect,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OneSelect {
Select {
distinctness: Option<Distinctness>,
columns: Vec<ResultColumn>,
from: Option<FromClause>,
where_clause: Option<Box<Expr>>,
group_by: Option<Vec<Expr>>,
having: Option<Box<Expr>>, window_clause: Option<Vec<WindowDef>>,
},
Values(Vec<Vec<Expr>>),
}
impl OneSelect {
pub fn new(
distinctness: Option<Distinctness>,
columns: Vec<ResultColumn>,
from: Option<FromClause>,
where_clause: Option<Expr>,
group_by: Option<Vec<Expr>>,
having: Option<Expr>,
window_clause: Option<Vec<WindowDef>>,
) -> Result<Self, ParserError> {
#[cfg(feature = "extra_checks")]
if from.is_none()
&& columns
.iter()
.any(|rc| matches!(rc, ResultColumn::Star | ResultColumn::TableStar(_)))
{
return Err(custom_err!("no tables specified"));
}
let select = Self::Select {
distinctness,
columns,
from,
where_clause: where_clause.map(Box::new),
group_by,
having: having.map(Box::new),
window_clause,
};
#[cfg(feature = "extra_checks")]
if let Self::Select {
group_by: Some(ref gb),
..
} = select
{
if let ColumnCount::Fixed(n) = select.column_count() {
for expr in gb {
expr.check_range("GROUP", n)?;
}
}
}
Ok(select)
}
pub fn push(values: &mut Vec<Vec<Expr>>, v: Vec<Expr>) -> Result<(), ParserError> {
#[cfg(feature = "extra_checks")]
if values[0].len() != v.len() {
return Err(custom_err!("all VALUES must have the same number of terms"));
}
values.push(v);
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
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)]
pub enum Distinctness {
Distinct,
All,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ResultColumn {
Expr(Expr, Option<As>),
Star,
TableStar(Name),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum As {
As(Name),
Elided(Name), }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct JoinedSelectTable {
pub operator: JoinOperator,
pub table: SelectTable,
pub constraint: Option<JoinConstraint>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
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)]
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().as_bytes())?;
}
if (jt & (JoinType::INNER | JoinType::OUTER)) == (JoinType::INNER | JoinType::OUTER)
|| (jt & (JoinType::OUTER | JoinType::LEFT | JoinType::RIGHT)) == JoinType::OUTER
{
return Err(custom_err!(
"unknown join type: {} {} {}",
str::from_utf8(token.1).unwrap_or("invalid utf8"),
n1.as_ref().map_or("", |n| n.0.as_ref()),
n2.as_ref().map_or("", |n| n.0.as_ref())
));
}
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)]
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!(
"unknown join type: {}",
str::from_utf8(s).unwrap_or("invalid utf8")
))
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum JoinConstraint {
On(Expr),
Using(DistinctNames),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Id(pub Box<str>);
impl Id {
pub fn from_token(ty: YYCODETYPE, token: Token) -> Self {
Self(from_token(ty, token))
}
}
#[derive(Clone, Debug, Eq)]
pub struct Name(pub Box<str>);
pub(crate) fn unquote(s: &str) -> (&str, u8) {
if s.is_empty() {
return (s, 0);
}
let bytes = s.as_bytes();
let mut quote = bytes[0];
if quote != b'"' && quote != b'`' && quote != b'\'' && quote != b'[' {
return (s, 0);
} else if quote == b'[' {
quote = b']';
}
debug_assert!(bytes.len() > 1);
debug_assert_eq!(quote, bytes[bytes.len() - 1]);
let sub = &s[1..bytes.len() - 1];
if quote == b']' || sub.len() < 2 {
(sub, 0)
} else {
(sub, quote)
}
}
impl Name {
pub fn from_token(ty: YYCODETYPE, token: Token) -> Self {
Self(from_token(ty, token))
}
fn as_bytes(&self) -> QuotedIterator<'_> {
let (sub, quote) = unquote(self.0.as_ref());
QuotedIterator(sub.bytes(), quote)
}
#[cfg(feature = "extra_checks")]
fn is_reserved(&self) -> bool {
let bytes = self.as_bytes();
let reserved = QuotedIterator("sqlite_".bytes(), 0);
bytes.zip(reserved).fold(0u8, |acc, (b1, b2)| {
acc + if b1.eq_ignore_ascii_case(&b2) { 1 } else { 0 }
}) == 7u8
}
}
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)]
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)]
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)]
pub enum AlterTableBody {
RenameTo(Name),
AddColumn(ColumnDefinition), DropColumnNotNull(Name), SetColumnNotNull(Name, Option<ResolveType>), RenameColumn {
old: Name,
new: Name,
},
DropColumn(Name), AddConstraint(NamedTableConstraint), DropConstraint(Name),
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct TabFlags: u32 {
const HasHidden = 0x00000002;
const HasPrimaryKey = 0x00000004;
const Autoincrement = 0x00000008;
const HasVirtual = 0x00000020;
const HasStored = 0x00000040;
const HasGenerated = 0x00000060;
const WithoutRowid = 0x00000080;
const HasNotNull = 0x00000800;
const Strict = 0x00010000;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CreateTableBody {
ColumnsAndConstraints {
columns: IndexMap<Name, ColumnDefinition>,
constraints: Option<Vec<NamedTableConstraint>>,
flags: TabFlags,
},
AsSelect(Box<Select>),
}
impl CreateTableBody {
pub fn columns_and_constraints(
columns: IndexMap<Name, ColumnDefinition>,
constraints: Option<Vec<NamedTableConstraint>>,
mut flags: TabFlags,
) -> Result<Self, ParserError> {
for col in columns.values() {
if col.flags.contains(ColFlags::PRIMKEY) {
flags |= TabFlags::HasPrimaryKey;
}
}
if let Some(ref constraints) = constraints {
for c in constraints {
if let NamedTableConstraint {
constraint: TableConstraint::PrimaryKey { .. },
..
} = c
{
if flags.contains(TabFlags::HasPrimaryKey) {
#[cfg(feature = "extra_checks")]
return Err(custom_err!("table has more than one primary key"));
} else {
flags |= TabFlags::HasPrimaryKey;
}
}
}
}
Ok(Self::ColumnsAndConstraints {
columns,
constraints,
flags,
})
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ColFlags: u16 {
const PRIMKEY = 0x0001;
const HASTYPE = 0x0004;
const UNIQUE = 0x0008;
const VIRTUAL = 0x0020;
const STORED = 0x0040;
const HASCOLL = 0x0200;
const GENERATED = Self::STORED.bits() | Self::VIRTUAL.bits();
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColumnDefinition {
pub col_name: Name,
pub col_type: Option<Type>,
pub constraints: Vec<NamedColumnConstraint>,
pub flags: ColFlags,
}
impl ColumnDefinition {
pub fn new(
col_name: Name,
mut col_type: Option<Type>,
constraints: Vec<NamedColumnConstraint>,
) -> Result<Self, ParserError> {
let mut flags = ColFlags::empty();
#[allow(unused_variables)]
let mut default = false;
for constraint in &constraints {
match &constraint.constraint {
#[allow(unused_assignments)]
ColumnConstraint::Default(..) => {
default = true;
}
ColumnConstraint::Collate { .. } => {
flags |= ColFlags::HASCOLL;
}
ColumnConstraint::Generated { typ, .. } => {
flags |= ColFlags::VIRTUAL;
if let Some(id) = typ {
if id.0.eq_ignore_ascii_case("STORED") {
flags |= ColFlags::STORED;
}
}
}
#[cfg(feature = "extra_checks")]
ColumnConstraint::ForeignKey {
clause:
ForeignKeyClause {
tbl_name, columns, ..
},
..
} => {
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
));
}
}
#[allow(unused_variables)]
ColumnConstraint::PrimaryKey { auto_increment, .. } => {
#[cfg(feature = "extra_checks")]
if *auto_increment
&& col_type.as_ref().is_none_or(|t| {
!unquote(t.name.as_str()).0.eq_ignore_ascii_case("INTEGER")
})
{
return Err(custom_err!(
"AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY"
));
}
flags |= ColFlags::PRIMKEY | ColFlags::UNIQUE;
}
ColumnConstraint::Unique(..) => {
flags |= ColFlags::UNIQUE;
}
_ => {}
}
}
#[cfg(feature = "extra_checks")]
if flags.contains(ColFlags::PRIMKEY) && flags.intersects(ColFlags::GENERATED) {
return Err(custom_err!(
"generated columns cannot be part of the PRIMARY KEY"
));
} else if default && flags.intersects(ColFlags::GENERATED) {
return Err(custom_err!("cannot use DEFAULT on a generated column"));
}
if flags.intersects(ColFlags::GENERATED) {
if let Some(ref mut col_type) = col_type {
let mut split = col_type.name.split_ascii_whitespace();
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 new_type: Vec<&str> = split.collect();
col_type.name = new_type.join(" ");
}
}
}
if col_type.as_ref().is_some_and(|t| !t.name.is_empty()) {
flags |= ColFlags::HASTYPE;
}
Ok(Self {
col_name,
col_type,
constraints,
flags,
})
}
pub fn add_column(columns: &mut IndexMap<Name, Self>, 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));
} else if cd.flags.contains(ColFlags::PRIMKEY)
&& columns
.values()
.any(|c| c.flags.contains(ColFlags::PRIMKEY))
{
#[cfg(feature = "extra_checks")]
return Err(custom_err!("table has more than one primary key")); }
columns.insert(col_name.clone(), cd);
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NamedColumnConstraint {
pub name: Option<Name>,
pub constraint: ColumnConstraint,
}
#[derive(Clone, Debug, PartialEq, Eq)]
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,
defer_clause: Option<DeferSubclause>,
},
Generated {
expr: Expr,
typ: Option<Id>,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NamedTableConstraint {
pub name: Option<Name>,
pub constraint: TableConstraint,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TableConstraint {
PrimaryKey {
columns: Vec<SortedColumn>,
auto_increment: bool,
conflict_clause: Option<ResolveType>,
},
Unique {
columns: Vec<SortedColumn>,
conflict_clause: Option<ResolveType>,
},
Check(Expr, Option<ResolveType>),
ForeignKey {
columns: Vec<IndexedColumn>,
clause: ForeignKeyClause,
defer_clause: Option<DeferSubclause>,
},
}
impl TableConstraint {
pub fn primary_key(
columns: Vec<SortedColumn>,
auto_increment: bool,
conflict_clause: Option<ResolveType>,
) -> Result<Self, ParserError> {
has_expression(&columns)?;
has_explicit_nulls(&columns)?;
Ok(Self::PrimaryKey {
columns,
auto_increment,
conflict_clause,
})
}
pub fn unique(
columns: Vec<SortedColumn>,
conflict_clause: Option<ResolveType>,
) -> Result<Self, ParserError> {
has_expression(&columns)?;
has_explicit_nulls(&columns)?;
Ok(Self::Unique {
columns,
conflict_clause,
})
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum NullsOrder {
First,
Last,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ForeignKeyClause {
pub tbl_name: Name,
pub columns: Option<Vec<IndexedColumn>>,
pub args: Vec<RefArg>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RefArg {
OnDelete(RefAct),
OnInsert(RefAct),
OnUpdate(RefAct),
Match(Name),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RefAct {
SetNull,
SetDefault,
Cascade,
Restrict,
NoAction,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeferSubclause {
pub deferrable: bool,
pub init_deferred: Option<InitDeferredPred>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum InitDeferredPred {
InitiallyDeferred,
InitiallyImmediate, }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexedColumn {
pub col_name: Name,
pub collation_name: Option<Name>, pub order: Option<SortOrder>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Indexed {
IndexedBy(Name),
NotIndexed,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SortedColumn {
pub expr: Expr,
pub order: Option<SortOrder>,
pub nulls: Option<NullsOrder>,
}
fn has_expression(columns: &Vec<SortedColumn>) -> Result<(), ParserError> {
for _column in columns {
if false {
return Err(custom_err!(
"expressions prohibited in PRIMARY KEY and UNIQUE constraints"
));
}
}
Ok(())
}
#[allow(unused_variables)]
fn has_explicit_nulls(columns: &[SortedColumn]) -> Result<(), ParserError> {
#[cfg(feature = "extra_checks")]
for column in columns {
if let Some(ref nulls) = column.nulls {
return Err(custom_err!(
"unsupported use of NULLS {}",
if *nulls == NullsOrder::First {
"FIRST"
} else {
"LAST"
}
));
}
}
Ok(())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Limit {
pub expr: Expr,
pub offset: Option<Expr>, }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InsertBody {
Select(Box<Select>, Option<Box<Upsert>>),
DefaultValues,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Set {
pub col_names: DistinctNames,
pub expr: Expr,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PragmaBody {
Equals(PragmaValue),
Call(PragmaValue),
}
pub type PragmaValue = Expr;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TriggerTime {
Before, After,
InsteadOf,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TriggerEvent {
Delete,
Insert,
Update,
UpdateOf(DistinctNames),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TriggerCmd {
Update {
or_conflict: Option<ResolveType>,
tbl_name: QualifiedName,
sets: Vec<Set>, from: Option<FromClause>,
where_clause: Option<Expr>,
},
Insert {
or_conflict: Option<ResolveType>,
tbl_name: QualifiedName,
col_names: Option<DistinctNames>,
select: Box<Select>,
upsert: Option<Box<Upsert>>,
},
Delete {
tbl_name: QualifiedName,
where_clause: Option<Expr>,
},
Select(Box<Select>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ResolveType {
Rollback,
Abort, Fail,
Ignore,
Replace,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct With {
pub recursive: bool,
pub ctes: Vec<CommonTableExpr>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Materialized {
Any,
Yes,
No,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommonTableExpr {
pub tbl_name: Name,
pub columns: Option<Vec<IndexedColumn>>, pub materialized: Materialized,
pub select: Box<Select>,
}
impl CommonTableExpr {
pub fn new(
tbl_name: Name,
columns: Option<Vec<IndexedColumn>>,
materialized: Materialized,
select: Select,
) -> Result<Self, ParserError> {
#[cfg(feature = "extra_checks")]
if let Some(ref columns) = columns {
if let check::ColumnCount::Fixed(cc) = select.column_count() {
if cc as usize != columns.len() {
return Err(custom_err!(
"table {} has {} values for {} columns",
tbl_name,
cc,
columns.len()
));
}
}
}
Ok(Self {
tbl_name,
columns,
materialized,
select: Box::new(select),
})
}
pub fn add_cte(ctes: &mut Vec<Self>, cte: Self) -> Result<(), ParserError> {
#[cfg(feature = "extra_checks")]
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)]
pub struct Type {
pub name: String, pub size: Option<TypeSize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TypeSize {
MaxSize(Box<Expr>),
TypeSize(Box<Expr>, Box<Expr>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TransactionType {
Deferred, Immediate,
Exclusive,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Upsert {
pub index: Option<UpsertIndex>,
pub do_clause: UpsertDo,
pub next: Option<Box<Self>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpsertIndex {
pub targets: Vec<SortedColumn>,
pub where_clause: Option<Expr>,
}
impl UpsertIndex {
pub fn new(
targets: Vec<SortedColumn>,
where_clause: Option<Expr>,
) -> Result<Self, ParserError> {
has_explicit_nulls(&targets)?;
Ok(Self {
targets,
where_clause,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum UpsertDo {
Set {
sets: Vec<Set>,
where_clause: Option<Expr>,
},
Nothing,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionTail {
pub filter_clause: Option<Box<Expr>>,
pub over_clause: Option<Box<Over>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Over {
Window(Box<Window>),
Name(Name),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WindowDef {
pub name: Name,
pub window: Window,
}
#[derive(Clone, Debug, PartialEq, Eq)]
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)]
pub struct FrameClause {
pub mode: FrameMode,
pub start: FrameBound,
pub end: Option<FrameBound>,
pub exclude: Option<FrameExclude>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum FrameMode {
Groups,
Range,
Rows,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FrameBound {
CurrentRow,
Following(Expr),
Preceding(Expr),
UnboundedFollowing,
UnboundedPreceding,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FrameExclude {
NoOthers,
CurrentRow,
Group,
Ties,
}
#[cfg(test)]
mod test {
use super::Name;
#[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");
}
fn name(s: &'static str) -> Name {
Name(s.into())
}
}