Skip to main content

reifydb_sql/
ast.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4#[derive(Debug, Clone)]
5pub enum Statement {
6	Select(SelectStatement),
7	Insert(InsertStatement),
8	Update(UpdateStatement),
9	Delete(DeleteStatement),
10	CreateTable(CreateTableStatement),
11}
12
13#[derive(Debug, Clone)]
14pub struct CteDefinition {
15	pub name: String,
16	pub query: SelectStatement,
17}
18
19#[derive(Debug, Clone)]
20pub struct SelectStatement {
21	pub ctes: Vec<CteDefinition>,
22	pub distinct: bool,
23	pub columns: Vec<SelectColumn>,
24	pub from: Option<FromClause>,
25	pub joins: Vec<JoinClause>,
26	pub where_clause: Option<Expr>,
27	pub group_by: Vec<Expr>,
28	pub having: Option<Expr>,
29	pub order_by: Vec<OrderByItem>,
30	pub limit: Option<u64>,
31	pub offset: Option<u64>,
32}
33
34#[derive(Debug, Clone)]
35pub enum SelectColumn {
36	AllColumns,
37	Expr {
38		expr: Expr,
39		alias: Option<String>,
40	},
41}
42
43#[derive(Debug, Clone)]
44pub enum FromClause {
45	Table {
46		name: String,
47		schema: Option<String>,
48	},
49	Subquery(Box<SelectStatement>),
50}
51
52#[derive(Debug, Clone)]
53pub struct JoinClause {
54	pub join_type: JoinType,
55	pub table: FromClause,
56	pub table_alias: Option<String>,
57	pub on: Expr,
58}
59
60#[derive(Debug, Clone)]
61pub enum JoinType {
62	Inner,
63	Left,
64}
65
66#[derive(Debug, Clone)]
67pub struct OrderByItem {
68	pub expr: Expr,
69	pub direction: OrderDirection,
70}
71
72#[derive(Debug, Clone)]
73pub enum OrderDirection {
74	Asc,
75	Desc,
76}
77
78#[derive(Debug, Clone)]
79pub struct InsertStatement {
80	pub table: String,
81	pub schema: Option<String>,
82	pub columns: Vec<String>,
83	pub values: Vec<Vec<Expr>>,
84}
85
86#[derive(Debug, Clone)]
87pub struct UpdateStatement {
88	pub table: String,
89	pub schema: Option<String>,
90	pub assignments: Vec<(String, Expr)>,
91	pub where_clause: Option<Expr>,
92}
93
94#[derive(Debug, Clone)]
95pub struct DeleteStatement {
96	pub table: String,
97	pub schema: Option<String>,
98	pub where_clause: Option<Expr>,
99}
100
101#[derive(Debug, Clone)]
102pub struct CreateTableStatement {
103	pub table: String,
104	pub schema: Option<String>,
105	pub columns: Vec<ColumnDef>,
106	pub primary_key: Vec<String>,
107}
108
109#[derive(Debug, Clone)]
110pub struct ColumnDef {
111	pub name: String,
112	pub data_type: SqlType,
113	pub nullable: bool,
114}
115
116#[derive(Debug, Clone)]
117pub enum SqlType {
118	Int,
119	Int2,
120	Int4,
121	Int8,
122	Smallint,
123	Integer,
124	Bigint,
125	Float4,
126	Float8,
127	Real,
128	Double,
129	Boolean,
130	Bool,
131	Varchar(Option<u64>),
132	Char(Option<u64>),
133	Text,
134	Utf8,
135	Blob,
136}
137
138#[derive(Debug, Clone)]
139pub enum Expr {
140	Identifier(String),
141	QualifiedIdentifier(String, String),
142	IntegerLiteral(i64),
143	FloatLiteral(f64),
144	StringLiteral(String),
145	BoolLiteral(bool),
146	Null,
147	BinaryOp {
148		left: Box<Expr>,
149		op: BinaryOp,
150		right: Box<Expr>,
151	},
152	UnaryOp {
153		op: UnaryOp,
154		expr: Box<Expr>,
155	},
156	FunctionCall {
157		name: String,
158		args: Vec<Expr>,
159	},
160	Between {
161		expr: Box<Expr>,
162		low: Box<Expr>,
163		high: Box<Expr>,
164		negated: bool,
165	},
166	InList {
167		expr: Box<Expr>,
168		list: Vec<Expr>,
169		negated: bool,
170	},
171	IsNull {
172		expr: Box<Expr>,
173		negated: bool,
174	},
175	Cast {
176		expr: Box<Expr>,
177		data_type: SqlType,
178	},
179	Nested(Box<Expr>),
180}
181
182#[derive(Debug, Clone)]
183pub enum BinaryOp {
184	Eq,
185	NotEq,
186	Lt,
187	Gt,
188	LtEq,
189	GtEq,
190	And,
191	Or,
192	Add,
193	Sub,
194	Mul,
195	Div,
196	Mod,
197}
198
199#[derive(Debug, Clone)]
200pub enum UnaryOp {
201	Not,
202	Neg,
203}