Parser

Struct Parser 

Source
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>

Source

pub fn parse_expression(&mut self) -> Result<Box<Expr>>

Parse an expression.

Source

pub fn parse_data_type(&mut self) -> Result<DataTypeSpec>

Parse a data type specification.

Source§

impl<'a> Parser<'a>

Source

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>

Source

pub fn parse_insert(&mut self) -> Result<StatementKind>

Parse INSERT statement.

Source

pub fn parse_update(&mut self) -> Result<StatementKind>

Parse UPDATE statement.

Source

pub fn parse_delete(&mut self) -> Result<StatementKind>

Parse DELETE statement.

Source

pub fn parse_merge(&mut self) -> Result<StatementKind>

Parse MERGE statement.

Source

pub fn parse_create(&mut self) -> Result<StatementKind>

Parse CREATE statement.

Source

pub fn parse_alter(&mut self) -> Result<StatementKind>

Parse ALTER statement.

Source

pub fn parse_drop(&mut self) -> Result<StatementKind>

Parse DROP statement.

Source

pub fn parse_truncate(&mut self) -> Result<StatementKind>

Parse TRUNCATE statement.

Source

pub fn parse_begin(&mut self) -> Result<StatementKind>

Parse BEGIN statement.

Source

pub fn parse_rollback(&mut self) -> Result<StatementKind>

Parse ROLLBACK statement.

Source

pub fn parse_explain(&mut self) -> Result<StatementKind>

Parse EXPLAIN statement.

Source

pub fn parse_describe(&mut self) -> Result<StatementKind>

Parse DESCRIBE statement.

Source

pub fn parse_show(&mut self) -> Result<StatementKind>

Parse SHOW statement.

Source

pub fn parse_set(&mut self) -> Result<StatementKind>

Parse SET statement.

Source§

impl<'a> Parser<'a>

Source

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    }
Source

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    }
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.