Skip to content

Commit

Permalink
Make possible to perform extra consistency checks
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Jan 2, 2024
1 parent 799722f commit 13c31f0
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ impl Display for Cmd {
}

impl Cmd {
/// Statement accessor
pub fn stmt(&self) -> &Stmt {
match self {
Cmd::Explain(stmt) => stmt,
Cmd::ExplainQueryPlan(stmt) => stmt,
Cmd::Stmt(stmt) => stmt,
}
}

/// Like `sqlite3_column_count` but more limited
pub fn column_count(&self) -> ColumnCount {
match self {
Expand All @@ -166,11 +175,12 @@ impl Cmd {

/// Like `sqlite3_stmt_readonly`
pub fn readonly(&self) -> bool {
match self {
Cmd::Explain(stmt) => stmt.readonly(),
Cmd::ExplainQueryPlan(stmt) => stmt.readonly(),
Cmd::Stmt(stmt) => stmt.readonly(),
}
self.stmt().readonly()
}

/// check for extra rules
pub fn check(&self) -> Result<(), ParserError> {
self.stmt().check()
}
}

Expand Down Expand Up @@ -767,6 +777,38 @@ impl Stmt {
_ => false,
}
}

/// check for extra rules
pub fn check(&self) -> Result<(), ParserError> {
match self {
Stmt::CreateTable { body, .. } => Ok(()), // TODO ...
Stmt::CreateView {
columns: Some(columns),
select,
..
} => {
match select.body.select.column_count() {
ColumnCount::Fixed(n) if n != columns.len() => Err(ParserError::Custom(
format!("incoherent column numbers {} <> {}", columns.len(), n),
)), // TODO find original SQLite error msg
_ => Ok(()),
}
}
Stmt::Insert {
columns: Some(columns),
body: InsertBody::Select(select, ..),
..
} => {
match select.body.select.column_count() {
ColumnCount::Fixed(n) if n != columns.len() => Err(ParserError::Custom(
format!("incoherent column numbers {} <> {}", columns.len(), n),
)), // TODO find original SQLite error msg
_ => Ok(()),
}
}
_ => Ok(()),
}
}
}

// https://sqlite.org/syntax/expr.html
Expand Down

0 comments on commit 13c31f0

Please sign in to comment.