pub struct Parser<'a> { /* private fields */ }Expand description
SQL Parser.
The parser converts SQL text into an Abstract Syntax Tree (AST).
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn parse_expression(&mut self) -> Result<Box<Expr>>
pub fn parse_expression(&mut self) -> Result<Box<Expr>>
Parse an expression.
Sourcepub fn parse_data_type(&mut self) -> Result<DataTypeSpec>
pub fn parse_data_type(&mut self) -> Result<DataTypeSpec>
Parse a data type specification.
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn parse_query(&mut self) -> Result<Query>
pub fn parse_query(&mut self) -> Result<Query>
Parse a complete query (WITH clause, SELECT, set operations, ORDER BY, LIMIT).
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn parse_insert(&mut self) -> Result<StatementKind>
pub fn parse_insert(&mut self) -> Result<StatementKind>
Parse INSERT statement.
Sourcepub fn parse_update(&mut self) -> Result<StatementKind>
pub fn parse_update(&mut self) -> Result<StatementKind>
Parse UPDATE statement.
Sourcepub fn parse_delete(&mut self) -> Result<StatementKind>
pub fn parse_delete(&mut self) -> Result<StatementKind>
Parse DELETE statement.
Sourcepub fn parse_merge(&mut self) -> Result<StatementKind>
pub fn parse_merge(&mut self) -> Result<StatementKind>
Parse MERGE statement.
Sourcepub fn parse_create(&mut self) -> Result<StatementKind>
pub fn parse_create(&mut self) -> Result<StatementKind>
Parse CREATE statement.
Sourcepub fn parse_alter(&mut self) -> Result<StatementKind>
pub fn parse_alter(&mut self) -> Result<StatementKind>
Parse ALTER statement.
Sourcepub fn parse_drop(&mut self) -> Result<StatementKind>
pub fn parse_drop(&mut self) -> Result<StatementKind>
Parse DROP statement.
Sourcepub fn parse_truncate(&mut self) -> Result<StatementKind>
pub fn parse_truncate(&mut self) -> Result<StatementKind>
Parse TRUNCATE statement.
Sourcepub fn parse_begin(&mut self) -> Result<StatementKind>
pub fn parse_begin(&mut self) -> Result<StatementKind>
Parse BEGIN statement.
Sourcepub fn parse_rollback(&mut self) -> Result<StatementKind>
pub fn parse_rollback(&mut self) -> Result<StatementKind>
Parse ROLLBACK statement.
Sourcepub fn parse_explain(&mut self) -> Result<StatementKind>
pub fn parse_explain(&mut self) -> Result<StatementKind>
Parse EXPLAIN statement.
Sourcepub fn parse_describe(&mut self) -> Result<StatementKind>
pub fn parse_describe(&mut self) -> Result<StatementKind>
Parse DESCRIBE statement.
Sourcepub fn parse_show(&mut self) -> Result<StatementKind>
pub fn parse_show(&mut self) -> Result<StatementKind>
Parse SHOW statement.
Sourcepub fn parse_set(&mut self) -> Result<StatementKind>
pub fn parse_set(&mut self) -> Result<StatementKind>
Parse SET statement.
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn new(input: &'a str) -> Self
pub fn new(input: &'a str) -> Self
Create a new parser for the given input.
Examples found in repository?
examples/csv_database/database.rs (line 174)
173 pub fn execute(&mut self, sql: &str) -> Result<QueryResult, String> {
174 let mut parser = Parser::new(sql);
175 let statements = parser.parse().map_err(|e| format!("Parse error: {}", e))?;
176
177 if statements.is_empty() {
178 return Err("No statements to execute".to_string());
179 }
180
181 let stmt = &statements[0];
182
183 match &stmt.kind {
184 StatementKind::Query(query) => self.execute_query(query),
185 StatementKind::Insert(insert) => {
186 let table_name = insert
187 .table
188 .parts
189 .last()
190 .map(|i| i.value.clone())
191 .unwrap_or_default();
192
193 if let vibesql::InsertSource::Values(rows) = &insert.source {
194 for row in rows {
195 let values: Vec<String> =
196 row.iter().map(|expr| self.eval_literal(expr)).collect();
197
198 if let Some(table_data) = self.tables.get_mut(&table_name) {
199 table_data.push(values.clone());
200 }
201 }
202 self.save_table(&table_name)?;
203 Ok(QueryResult::new(
204 vec!["result".to_string()],
205 vec![vec!["Inserted".to_string()]],
206 ))
207 } else {
208 Err("Only INSERT VALUES is supported".to_string())
209 }
210 }
211 StatementKind::CreateTable(create) => {
212 let table_name = create
213 .name
214 .parts
215 .last()
216 .map(|i| i.value.clone())
217 .unwrap_or_default();
218
219 let mut builder = TableSchemaBuilder::new(&table_name);
220 for col in &create.columns {
221 builder = builder.column(ColumnSchema::new(&col.name.value, SqlType::Varchar));
222 }
223 let schema = builder.build();
224 self.catalog.add_table(schema);
225
226 self.tables.insert(table_name.clone(), Vec::new());
227 self.save_table(&table_name)?;
228
229 Ok(QueryResult::new(
230 vec!["result".to_string()],
231 vec![vec![format!("Created table {}", table_name)]],
232 ))
233 }
234 _ => Err("Unsupported statement type".to_string()),
235 }
236 }Sourcepub fn parse(&mut self) -> Result<Vec<Statement>>
pub fn parse(&mut self) -> Result<Vec<Statement>>
Parse all statements from the input.
Examples found in repository?
examples/csv_database/database.rs (line 175)
173 pub fn execute(&mut self, sql: &str) -> Result<QueryResult, String> {
174 let mut parser = Parser::new(sql);
175 let statements = parser.parse().map_err(|e| format!("Parse error: {}", e))?;
176
177 if statements.is_empty() {
178 return Err("No statements to execute".to_string());
179 }
180
181 let stmt = &statements[0];
182
183 match &stmt.kind {
184 StatementKind::Query(query) => self.execute_query(query),
185 StatementKind::Insert(insert) => {
186 let table_name = insert
187 .table
188 .parts
189 .last()
190 .map(|i| i.value.clone())
191 .unwrap_or_default();
192
193 if let vibesql::InsertSource::Values(rows) = &insert.source {
194 for row in rows {
195 let values: Vec<String> =
196 row.iter().map(|expr| self.eval_literal(expr)).collect();
197
198 if let Some(table_data) = self.tables.get_mut(&table_name) {
199 table_data.push(values.clone());
200 }
201 }
202 self.save_table(&table_name)?;
203 Ok(QueryResult::new(
204 vec!["result".to_string()],
205 vec![vec!["Inserted".to_string()]],
206 ))
207 } else {
208 Err("Only INSERT VALUES is supported".to_string())
209 }
210 }
211 StatementKind::CreateTable(create) => {
212 let table_name = create
213 .name
214 .parts
215 .last()
216 .map(|i| i.value.clone())
217 .unwrap_or_default();
218
219 let mut builder = TableSchemaBuilder::new(&table_name);
220 for col in &create.columns {
221 builder = builder.column(ColumnSchema::new(&col.name.value, SqlType::Varchar));
222 }
223 let schema = builder.build();
224 self.catalog.add_table(schema);
225
226 self.tables.insert(table_name.clone(), Vec::new());
227 self.save_table(&table_name)?;
228
229 Ok(QueryResult::new(
230 vec!["result".to_string()],
231 vec![vec![format!("Created table {}", table_name)]],
232 ))
233 }
234 _ => Err("Unsupported statement type".to_string()),
235 }
236 }Sourcepub fn parse_statement(&mut self) -> Result<Statement>
pub fn parse_statement(&mut self) -> Result<Statement>
Parse a single statement.
Auto Trait Implementations§
impl<'a> Freeze for Parser<'a>
impl<'a> RefUnwindSafe for Parser<'a>
impl<'a> Send for Parser<'a>
impl<'a> Sync for Parser<'a>
impl<'a> Unpin for Parser<'a>
impl<'a> UnwindSafe for Parser<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more