Skip to main content

oak_sql/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the SQL language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum SqlElementType {
7    /// Root node.
8    Root,
9    /// EXPLAIN statement.
10    ExplainStatement,
11    /// TRANSACTION statement.
12    TransactionStatement,
13    /// PRAGMA statement.
14    PragmaStatement,
15    /// SHOW statement.
16    ShowStatement,
17    /// SELECT statement.
18    SelectStatement,
19    /// VECTOR search clause.
20    VectorSearch,
21    /// Identifier.
22    Identifier,
23    /// Expression.
24    Expression,
25    /// Error node.
26    ErrorNode,
27    /// INSERT statement.
28    InsertStatement,
29    /// UPDATE statement.
30    UpdateStatement,
31    /// DELETE statement.
32    DeleteStatement,
33    /// CREATE statement.
34    CreateStatement,
35    /// DROP statement.
36    DropStatement,
37    /// ALTER statement.
38    AlterStatement,
39    /// JOIN clause.
40    JoinClause,
41    /// GROUP BY clause.
42    GroupByClause,
43    /// HAVING clause.
44    HavingClause,
45    /// ORDER BY clause.
46    OrderByClause,
47    /// LIMIT clause.
48    LimitClause,
49    /// Table name.
50    TableName,
51    /// Column name.
52    ColumnName,
53    /// Select item.
54    SelectItem,
55    /// Alias.
56    Alias,
57    /// Column definition.
58    ColumnDefinition,
59    /// Value list.
60    ValueList,
61    /// Assignment.
62    Assignment,
63    /// Alter action.
64    AlterAction,
65    /// RETURNING clause.
66    ReturningClause,
67    /// ON CONFLICT clause.
68    ConflictClause,
69    /// Subquery.
70    Subquery,
71    /// Function call.
72    FunctionCall,
73}
74
75impl ElementType for SqlElementType {
76    type Role = UniversalElementRole;
77
78    fn role(&self) -> UniversalElementRole {
79        use UniversalElementRole::*;
80        match self {
81            Self::Root => Root,
82            Self::Identifier | Self::TableName | Self::ColumnName | Self::Alias => Name,
83            Self::Expression | Self::FunctionCall => Expression,
84            Self::ErrorNode => Error,
85            Self::SelectStatement
86            | Self::InsertStatement
87            | Self::UpdateStatement
88            | Self::DeleteStatement
89            | Self::CreateStatement
90            | Self::DropStatement
91            | Self::AlterStatement
92            | Self::ExplainStatement
93            | Self::TransactionStatement
94            | Self::PragmaStatement
95            | Self::ShowStatement => Statement,
96            Self::JoinClause
97            | Self::GroupByClause
98            | Self::HavingClause
99            | Self::OrderByClause
100            | Self::LimitClause
101            | Self::SelectItem
102            | Self::ColumnDefinition
103            | Self::ValueList
104            | Self::Assignment
105            | Self::AlterAction
106            | Self::ReturningClause
107            | Self::ConflictClause
108            | Self::VectorSearch
109            | Self::Subquery => Statement,
110        }
111    }
112}
113
114impl From<crate::lexer::token_type::SqlTokenType> for SqlElementType {
115    fn from(token: crate::lexer::token_type::SqlTokenType) -> Self {
116        match token {
117            crate::lexer::token_type::SqlTokenType::Root => Self::Root,
118            crate::lexer::token_type::SqlTokenType::ExplainStatement => Self::ExplainStatement,
119            crate::lexer::token_type::SqlTokenType::TransactionStatement => Self::TransactionStatement,
120            crate::lexer::token_type::SqlTokenType::PragmaStatement => Self::PragmaStatement,
121            crate::lexer::token_type::SqlTokenType::ShowStatement => Self::ShowStatement,
122            crate::lexer::token_type::SqlTokenType::SelectStatement => Self::SelectStatement,
123            crate::lexer::token_type::SqlTokenType::InsertStatement => Self::InsertStatement,
124            crate::lexer::token_type::SqlTokenType::UpdateStatement => Self::UpdateStatement,
125            crate::lexer::token_type::SqlTokenType::DeleteStatement => Self::DeleteStatement,
126            crate::lexer::token_type::SqlTokenType::CreateStatement => Self::CreateStatement,
127            crate::lexer::token_type::SqlTokenType::DropStatement => Self::DropStatement,
128            crate::lexer::token_type::SqlTokenType::AlterStatement => Self::AlterStatement,
129            crate::lexer::token_type::SqlTokenType::Expression => Self::Expression,
130            crate::lexer::token_type::SqlTokenType::Identifier => Self::Identifier,
131            crate::lexer::token_type::SqlTokenType::TableName => Self::TableName,
132            crate::lexer::token_type::SqlTokenType::ColumnName => Self::ColumnName,
133            crate::lexer::token_type::SqlTokenType::JoinClause => Self::JoinClause,
134            crate::lexer::token_type::SqlTokenType::GroupByClause => Self::GroupByClause,
135            crate::lexer::token_type::SqlTokenType::HavingClause => Self::HavingClause,
136            crate::lexer::token_type::SqlTokenType::OrderByClause => Self::OrderByClause,
137            crate::lexer::token_type::SqlTokenType::LimitClause => Self::LimitClause,
138            crate::lexer::token_type::SqlTokenType::SelectItem => Self::SelectItem,
139            crate::lexer::token_type::SqlTokenType::Alias => Self::Alias,
140            crate::lexer::token_type::SqlTokenType::ColumnDefinition => Self::ColumnDefinition,
141            crate::lexer::token_type::SqlTokenType::ValueList => Self::ValueList,
142            crate::lexer::token_type::SqlTokenType::Assignment => Self::Assignment,
143            crate::lexer::token_type::SqlTokenType::AlterAction => Self::AlterAction,
144            crate::lexer::token_type::SqlTokenType::ErrorNode => Self::ErrorNode,
145            _ => Self::ErrorNode,
146        }
147    }
148}