Skip to main content

linkrs_query/parser/ast/
stmt.rs

1use std::sync::Arc;
2
3pub use super::pattern::*;
4pub use super::types::*;
5use linkrs_core::types::expr::expression_context::ExpressionAnalysisContext;
6
7mod dcl;
8mod ddl;
9mod dml;
10mod dql;
11mod management;
12mod migrate;
13mod search;
14mod transaction;
15
16pub use dcl::*;
17pub use ddl::*;
18pub use dml::*;
19pub use dql::*;
20pub use management::*;
21pub use migrate::*;
22pub use search::*;
23pub use transaction::*;
24
25#[cfg(test)]
26mod tests;
27
28#[derive(Debug, Clone)]
29pub struct Ast {
30    pub stmt: Stmt,
31    pub expr_context: Arc<ExpressionAnalysisContext>,
32}
33
34impl Ast {
35    pub fn new(stmt: Stmt, expr_context: Arc<ExpressionAnalysisContext>) -> Self {
36        Self { stmt, expr_context }
37    }
38
39    pub fn stmt(&self) -> &Stmt {
40        &self.stmt
41    }
42
43    pub fn expr_context(&self) -> &Arc<ExpressionAnalysisContext> {
44        &self.expr_context
45    }
46
47    pub fn into_stmt(self) -> Stmt {
48        self.stmt
49    }
50}
51
52/// Coarse classification of a statement, used for routing/permission/statistics.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
54pub enum StmtCategory {
55    /// Subquery / compound statement wrappers (QUERY / PIPE / SET OPERATION / ASSIGNMENT).
56    Query,
57    /// Read & analytic statements (MATCH / GO / LOOKUP / FIND PATH / ... / RETURN / YIELD / LET).
58    Dql,
59    /// Write statements (INSERT / MERGE / UPDATE / DELETE / SET / REMOVE).
60    Dml,
61    /// Schema statements (CREATE / DROP / ALTER / DESC / SHOW CREATE / index DDL).
62    Ddl,
63    /// Access-control statements (CREATE USER / GRANT / REVOKE / ...).
64    Dcl,
65    /// Server / session administration (USE / SHOW / EXPLAIN / PROFILE / ANALYZE / KILL QUERY / ...).
66    Admin,
67    /// Full-text & vector search statements (SEARCH / SEARCH VECTOR / LOOKUP|MATCH FULLTEXT|VECTOR).
68    Search,
69    /// Transaction control (BEGIN / COMMIT / ROLLBACK / SAVEPOINT / RELEASE SAVEPOINT).
70    Transaction,
71}
72
73impl StmtCategory {
74    pub fn as_str(&self) -> &'static str {
75        match self {
76            StmtCategory::Query => "QUERY",
77            StmtCategory::Dql => "DQL",
78            StmtCategory::Dml => "DML",
79            StmtCategory::Ddl => "DDL",
80            StmtCategory::Dcl => "DCL",
81            StmtCategory::Admin => "ADMIN",
82            StmtCategory::Search => "SEARCH",
83            StmtCategory::Transaction => "TRANSACTION",
84        }
85    }
86}
87
88#[derive(Debug, Clone)]
89pub enum Stmt {
90    Query(QueryStmt),
91    Create(CreateStmt),
92    Match(MatchStmt),
93    Delete(DeleteStmt),
94    Update(UpdateStmt),
95    Go(GoStmt),
96    Fetch(FetchStmt),
97    Use(UseStmt),
98    Show(ShowStmt),
99    Explain(ExplainStmt),
100    Profile(ProfileStmt),
101    Analyze(AnalyzeStmt),
102    GroupBy(GroupByStmt),
103    Lookup(LookupStmt),
104    Subgraph(SubgraphStmt),
105    FindPath(FindPathStmt),
106    Insert(InsertStmt),
107    Merge(MergeStmt),
108    Unwind(UnwindStmt),
109    Return(ReturnStmt),
110    With(WithStmt),
111    Yield(YieldStmt),
112    Filter(FilterStmt),
113    Collect(CollectStmt),
114    Set(SetStmt),
115    Remove(RemoveStmt),
116    Pipe(PipeStmt),
117    Drop(DropStmt),
118    Desc(DescStmt),
119    Alter(AlterStmt),
120    CreateUser(CreateUserStmt),
121    AlterUser(AlterUserStmt),
122    DropUser(DropUserStmt),
123    ChangePassword(ChangePasswordStmt),
124    Grant(GrantStmt),
125    Revoke(RevokeStmt),
126    DescribeUser(DescribeUserStmt),
127    ShowUsers(ShowUsersStmt),
128    ShowRoles(ShowRolesStmt),
129    ShowCreate(ShowCreateStmt),
130    ShowSessions(ShowSessionsStmt),
131    ShowQueries(ShowQueriesStmt),
132    KillQuery(KillQueryStmt),
133    ShowConfigs(ShowConfigsStmt),
134    UpdateConfigs(UpdateConfigsStmt),
135    Assignment(AssignmentStmt),
136    SetOperation(SetOperationStmt),
137    ClearSpace(ClearSpaceStmt),
138    CreateFulltextIndex(CreateFulltextIndex),
139    DropFulltextIndex(DropFulltextIndex),
140    AlterFulltextIndex(AlterFulltextIndex),
141    ShowFulltextIndex(ShowFulltextIndex),
142    DescribeFulltextIndex(DescribeFulltextIndex),
143    Search(SearchStatement),
144    LookupFulltext(LookupFulltext),
145    MatchFulltext(MatchFulltext),
146    CreateVectorIndex(CreateVectorIndex),
147    DropVectorIndex(DropVectorIndex),
148    SearchVector(SearchVectorStatement),
149    LookupVector(LookupVector),
150    MatchVector(MatchVector),
151    BeginTransaction(BeginTransactionStmt),
152    CommitTransaction(CommitTransactionStmt),
153    RollbackTransaction(RollbackTransactionStmt),
154    Savepoint(SavepointStmt),
155    ReleaseSavepoint(ReleaseSavepointStmt),
156    AssignVariable(AssignVariableStmt),
157    Copy(CopyStmt),
158    Migrate(MigrateStmt),
159    Checkpoint(CheckpointStmt),
160    LoadFrom(LoadFromStmt),
161    InQueryCall(InQueryCallStmt),
162    ExportDatabase(ExportDatabaseStmt),
163    ImportDatabase(ImportDatabaseStmt),
164    CreateMacro(CreateMacroStmt),
165    DropMacro(DropMacroStmt),
166    CreateType(CreateTypeStmt),
167    DropType(DropTypeStmt),
168    Extension(ExtensionStmt),
169    AttachDatabase(AttachDatabaseStmt),
170    DetachDatabase(DetachDatabaseStmt),
171    PurgeExpired(PurgeExpiredStmt),
172}
173
174crate::define_stmt_helpers! {
175    Query => Query,
176    Create => Ddl,
177    Match => Dql,
178    Delete => Dml,
179    Update => Dml,
180    Go => Dql,
181    Fetch => Dql,
182    Use => Admin,
183    Show => Admin,
184    Explain => Admin,
185    Profile => Admin,
186    Analyze => Admin,
187    GroupBy => Dql,
188    Lookup => Dql,
189    Subgraph => Dql,
190    FindPath => Dql,
191    Insert => Dml,
192    Merge => Dml,
193    Unwind => Dql,
194    Return => Dql,
195    With => Dql,
196    Yield => Dql,
197    Filter => Dql,
198    Collect => Dql,
199    Set => Dml,
200    Remove => Dml,
201    Pipe => Query,
202    Drop => Ddl,
203    Desc => Ddl,
204    Alter => Ddl,
205    CreateUser => Dcl,
206    AlterUser => Dcl,
207    DropUser => Dcl,
208    ChangePassword => Dcl,
209    Grant => Dcl,
210    Revoke => Dcl,
211    DescribeUser => Dcl,
212    ShowUsers => Dcl,
213    ShowRoles => Dcl,
214    ShowCreate => Ddl,
215    ShowSessions => Admin,
216    ShowQueries => Admin,
217    KillQuery => Admin,
218    ShowConfigs => Admin,
219    UpdateConfigs => Admin,
220    Assignment => Query,
221    SetOperation => Query,
222    ClearSpace => Admin,
223    CreateFulltextIndex => Ddl,
224    DropFulltextIndex => Ddl,
225    AlterFulltextIndex => Ddl,
226    ShowFulltextIndex => Ddl,
227    DescribeFulltextIndex => Ddl,
228    Search => Search,
229    LookupFulltext => Search,
230    MatchFulltext => Search,
231    CreateVectorIndex => Ddl,
232    DropVectorIndex => Ddl,
233    SearchVector => Search,
234    LookupVector => Search,
235    MatchVector => Search,
236    BeginTransaction => Transaction,
237    CommitTransaction => Transaction,
238    RollbackTransaction => Transaction,
239    Savepoint => Transaction,
240    ReleaseSavepoint => Transaction,
241    AssignVariable => Dql,
242    Copy => Dml,
243    Checkpoint => Admin,
244    LoadFrom => Dql,
245    InQueryCall => Dql,
246    ExportDatabase => Admin,
247    ImportDatabase => Admin,
248    CreateMacro => Ddl,
249    DropMacro => Ddl,
250    CreateType => Ddl,
251    DropType => Ddl,
252    Extension => Admin,
253    AttachDatabase => Admin,
254    DetachDatabase => Admin,
255    PurgeExpired => Ddl,
256}
257
258impl Stmt {
259    pub fn kind(&self) -> &'static str {
260        match self {
261            Stmt::Query(_) => "QUERY",
262            Stmt::Create(_) => "CREATE",
263            Stmt::Match(_) => "MATCH",
264            Stmt::Delete(_) => "DELETE",
265            Stmt::Update(s) => {
266                if s.is_upsert {
267                    "UPSERT"
268                } else {
269                    "UPDATE"
270                }
271            }
272            Stmt::Go(_) => "GO",
273            Stmt::Fetch(_) => "FETCH",
274            Stmt::Use(_) => "USE",
275            Stmt::Show(_) => "SHOW",
276            Stmt::Explain(_) => "EXPLAIN",
277            Stmt::Profile(_) => "PROFILE",
278            Stmt::Analyze(_) => "ANALYZE",
279            Stmt::GroupBy(_) => "GROUP BY",
280            Stmt::Lookup(_) => "LOOKUP",
281            Stmt::Subgraph(_) => "SUBGRAPH",
282            Stmt::FindPath(_) => "FIND PATH",
283            Stmt::Insert(_) => "INSERT",
284            Stmt::Merge(_) => "MERGE",
285            Stmt::Unwind(_) => "UNWIND",
286            Stmt::Return(_) => "RETURN",
287            Stmt::With(_) => "WITH",
288            Stmt::Yield(_) => "YIELD",
289            Stmt::Filter(_) => "WHERE",
290            Stmt::Collect(_) => "COLLECT",
291            Stmt::Set(_) => "SET",
292            Stmt::Remove(_) => "REMOVE",
293            Stmt::Pipe(_) => "PIPE",
294            Stmt::Drop(_) => "DROP",
295            Stmt::Desc(_) => "DESC",
296            Stmt::Alter(_) => "ALTER",
297            Stmt::CreateUser(_) => "CREATE USER",
298            Stmt::AlterUser(_) => "ALTER USER",
299            Stmt::DropUser(_) => "DROP USER",
300            Stmt::ChangePassword(_) => "CHANGE PASSWORD",
301            Stmt::Grant(_) => "GRANT",
302            Stmt::Revoke(_) => "REVOKE",
303            Stmt::DescribeUser(_) => "DESCRIBE USER",
304            Stmt::ShowUsers(_) => "SHOW USERS",
305            Stmt::ShowRoles(_) => "SHOW ROLES",
306            Stmt::ShowCreate(_) => "SHOW CREATE",
307            Stmt::ShowSessions(_) => "SHOW SESSIONS",
308            Stmt::ShowQueries(_) => "SHOW QUERIES",
309            Stmt::KillQuery(_) => "KILL QUERY",
310            Stmt::ShowConfigs(_) => "SHOW CONFIGS",
311            Stmt::UpdateConfigs(_) => "UPDATE CONFIGS",
312            Stmt::Assignment(_) => "ASSIGNMENT",
313            Stmt::SetOperation(_) => "SET OPERATION",
314            Stmt::ClearSpace(_) => "CLEAR SPACE",
315            Stmt::CreateFulltextIndex(_) => "CREATE FULLTEXT INDEX",
316            Stmt::DropFulltextIndex(_) => "DROP FULLTEXT INDEX",
317            Stmt::AlterFulltextIndex(_) => "ALTER FULLTEXT INDEX",
318            Stmt::ShowFulltextIndex(_) => "SHOW FULLTEXT INDEX",
319            Stmt::DescribeFulltextIndex(_) => "DESCRIBE FULLTEXT INDEX",
320            Stmt::Search(_) => "SEARCH",
321            Stmt::LookupFulltext(_) => "LOOKUP FULLTEXT",
322            Stmt::MatchFulltext(_) => "MATCH FULLTEXT",
323            Stmt::CreateVectorIndex(_) => "CREATE VECTOR INDEX",
324            Stmt::DropVectorIndex(_) => "DROP VECTOR INDEX",
325            Stmt::SearchVector(_) => "SEARCH VECTOR",
326            Stmt::LookupVector(_) => "LOOKUP VECTOR",
327            Stmt::MatchVector(_) => "MATCH VECTOR",
328            Stmt::BeginTransaction(stmt) => match stmt.read_only {
329                Some(true) => "BEGIN TRANSACTION READ ONLY",
330                Some(false) => "BEGIN TRANSACTION READ WRITE",
331                None => "BEGIN TRANSACTION",
332            },
333            Stmt::CommitTransaction(_) => "COMMIT TRANSACTION",
334            Stmt::RollbackTransaction(stmt) => {
335                if stmt.savepoint_name.is_some() {
336                    "ROLLBACK TRANSACTION TO SAVEPOINT"
337                } else {
338                    "ROLLBACK TRANSACTION"
339                }
340            }
341            Stmt::Savepoint(_) => "SAVEPOINT",
342            Stmt::ReleaseSavepoint(_) => "RELEASE SAVEPOINT",
343            Stmt::AssignVariable(_) => "LET",
344            Stmt::Copy(_) => "COPY",
345            Stmt::Migrate(m) => match m {
346                MigrateStmt::Plan(_) => "MIGRATE PLAN",
347                MigrateStmt::Execute(_) => "MIGRATE EXECUTE",
348                MigrateStmt::Rollback(_) => "MIGRATE ROLLBACK",
349            },
350            Stmt::Checkpoint(_) => "CHECKPOINT",
351            Stmt::LoadFrom(_) => "LOAD FROM",
352            Stmt::InQueryCall(_) => "CALL",
353            Stmt::ExportDatabase(_) => "EXPORT DATABASE",
354            Stmt::ImportDatabase(_) => "IMPORT DATABASE",
355            Stmt::CreateMacro(_) => "CREATE MACRO",
356            Stmt::DropMacro(_) => "DROP MACRO",
357            Stmt::CreateType(_) => "CREATE TYPE",
358            Stmt::DropType(_) => "DROP TYPE",
359            Stmt::Extension(s) => match s.action {
360                ExtensionAction::Load => "LOAD EXTENSION",
361                ExtensionAction::Install => "INSTALL EXTENSION",
362                ExtensionAction::Uninstall => "UNINSTALL EXTENSION",
363                ExtensionAction::Update => "UPDATE EXTENSION",
364            },
365            Stmt::AttachDatabase(_) => "ATTACH DATABASE",
366            Stmt::DetachDatabase(_) => "DETACH DATABASE",
367            Stmt::PurgeExpired(_) => "PURGE EXPIRED",
368        }
369    }
370
371    pub fn as_explain(&self) -> Option<&ExplainStmt> {
372        match self {
373            Stmt::Explain(s) => Some(s),
374            _ => None,
375        }
376    }
377}