use crate::span::Span;
use crate::types::CqlType;
pub use crate::types::{Order, QueryKind, RelOp};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ident {
pub name: String,
pub span: Span,
pub quoted: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableName {
pub keyspace: Option<Ident>,
pub name: Ident,
}
impl TableName {
pub fn span(&self) -> Span {
match &self.keyspace {
Some(ks) => ks.span.to(self.name.span),
None => self.name.span,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
CreateKeyspace(CreateKeyspace),
CreateTable(CreateTable),
CreateType(CreateType),
CreateIndex(CreateIndex),
CreateView(CreateView),
CreateFunction(CreateFunction),
CreateAggregate(CreateAggregate),
AlterTable(AlterTable),
AlterType(AlterType),
Drop(Drop),
Ignored(Ignored),
Select(Select),
Insert(Insert),
Update(Update),
Delete(Delete),
}
impl Statement {
pub fn is_ddl(&self) -> bool {
!matches!(
self,
Statement::Select(_)
| Statement::Insert(_)
| Statement::Update(_)
| Statement::Delete(_)
)
}
pub fn is_conditional(&self) -> bool {
match self {
Statement::Insert(i) => i.if_not_exists,
Statement::Update(u) => u.if_clause.is_some(),
Statement::Delete(d) => d.if_clause.is_some(),
_ => false,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateKeyspace {
pub name: Ident,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ColumnDef {
pub name: Ident,
pub ty: CqlType,
pub ty_span: Span,
pub is_static: bool,
pub inline_primary_key: bool,
pub masked: bool,
pub has_check: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PrimaryKeyDef {
pub partition: Vec<Ident>,
pub clustering: Vec<Ident>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateTable {
pub name: TableName,
pub columns: Vec<ColumnDef>,
pub primary_key: Option<PrimaryKeyDef>,
pub clustering_order: Vec<(Ident, Order)>,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IndexKind {
Values,
Keys,
Entries,
Full,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateIndex {
pub name: Option<Ident>,
pub table: TableName,
pub column: Ident,
pub kind: IndexKind,
pub class: Option<String>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateView {
pub name: TableName,
pub base: TableName,
pub columns: Option<Vec<Ident>>,
pub primary_key: PrimaryKeyDef,
pub clustering_order: Vec<(Ident, Order)>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateFunction {
pub name: TableName,
pub args: Vec<CqlType>,
pub returns: CqlType,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateAggregate {
pub name: TableName,
pub args: Vec<CqlType>,
pub state_type: CqlType,
pub final_func: Option<Ident>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AlterTableAction {
Add(Vec<ColumnDef>),
Drop(Vec<Ident>),
Rename(Vec<(Ident, Ident)>),
Other,
}
#[derive(Clone, Debug, PartialEq)]
pub struct AlterTable {
pub table: TableName,
pub action: AlterTableAction,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AlterTypeAction {
Add(Ident, CqlType),
Rename(Vec<(Ident, Ident)>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct AlterType {
pub name: TableName,
pub action: AlterTypeAction,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DropKind {
Table,
Type,
Index,
View,
Keyspace,
Function,
Aggregate,
Other,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Drop {
pub kind: DropKind,
pub name: TableName,
pub if_exists: bool,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Ignored {
pub what: String,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateType {
pub name: TableName,
pub fields: Vec<(Ident, CqlType)>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
String(String),
Integer(String),
Float(String),
Bool(bool),
Uuid(String),
Blob(String),
Duration(String),
Null,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Expr {
pub kind: ExprKind,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ExprKind {
Marker(usize),
Literal(Literal),
Column(Ident),
Field(Box<Expr>, Ident),
Index(Box<Expr>, Box<Expr>),
Call {
name: Ident,
args: Vec<Expr>,
star: bool,
},
Cast(Box<Expr>, CqlType),
Binary(BinOp, Box<Expr>, Box<Expr>),
Neg(Box<Expr>),
List(Vec<Expr>),
Tuple(Vec<Expr>),
Set(Vec<Expr>),
Map(Vec<(Expr, Expr)>),
Udt(Vec<(Ident, Expr)>),
EmptyBraces,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Rem,
}
impl BinOp {
pub fn symbol(self) -> &'static str {
match self {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Rem => "%",
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum RelLhs {
Column(Ident),
Token(Vec<Ident>),
Tuple(Vec<Ident>),
Element(Ident, Expr),
Field(Ident, Ident),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Rhs {
Expr(Expr),
InList(Vec<Expr>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Relation {
pub lhs: RelLhs,
pub op: RelOp,
pub rhs: Rhs,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum IfClause {
Exists,
Conditions(Vec<Relation>),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Using {
pub ttl: Option<Expr>,
pub timestamp: Option<Expr>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SelectItem {
pub expr: Expr,
pub alias: Option<Ident>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Selection {
Star(Span),
Items(Vec<SelectItem>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SelectMode {
Normal,
Json,
Distinct,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Select {
pub mode: SelectMode,
pub selection: Selection,
pub table: TableName,
pub where_clause: Vec<Relation>,
pub group_by: Vec<Ident>,
pub order_by: Vec<(Ident, Order)>,
pub ann: Option<(Ident, Expr)>,
pub per_partition_limit: Option<Expr>,
pub limit: Option<Expr>,
pub allow_filtering: bool,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub enum InsertSource {
Values {
columns: Vec<Ident>,
values: Vec<Expr>,
},
Json(Expr),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Insert {
pub table: TableName,
pub source: InsertSource,
pub if_not_exists: bool,
pub using: Using,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AssignOp {
Set,
Add,
Sub,
Prepend,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Target {
pub column: Ident,
pub key: Option<Expr>,
pub field: Option<Ident>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Assignment {
pub target: Target,
pub op: AssignOp,
pub value: Expr,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Update {
pub table: TableName,
pub using: Using,
pub assignments: Vec<Assignment>,
pub where_clause: Vec<Relation>,
pub if_clause: Option<IfClause>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Delete {
pub targets: Vec<Target>,
pub table: TableName,
pub using: Using,
pub where_clause: Vec<Relation>,
pub if_clause: Option<IfClause>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct QueryDef {
pub name: Ident,
pub kind: QueryKind,
pub statement: Statement,
pub cql: String,
pub marker_count: usize,
pub allow: Vec<crate::diagnostic::DiagnosticCode>,
pub span: Span,
}