Skip to content

Commit

Permalink
Check GENERATED column
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed May 11, 2024
1 parent 0003c25 commit 298f4d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ fn auto_increment() {
);
}

#[test]
fn generated() {
expect_parser_err_msg(
b"CREATE TABLE x(a PRIMARY KEY AS ('id'));",
"generated columns cannot be part of the PRIMARY KEY",
)
}

#[test]
fn vtab_args() -> Result<(), Error> {
let sql = b"CREATE VIRTUAL TABLE mail USING fts3(
Expand Down
5 changes: 0 additions & 5 deletions src/parser/ast/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ impl Stmt {
body: InsertBody::DefaultValues,
..
} => Err(custom_err!("0 values for {} columns", columns.len())),
Stmt::Update {
order_by: Some(_),
limit: None,
..
} => Err(custom_err!("ORDER BY without LIMIT on UPDATE")),
_ => Ok(()),
}
}
Expand Down
25 changes: 16 additions & 9 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub enum Stmt {

impl Stmt {
/// constructor
#[allow(clippy::too_many_arguments)]
pub fn update(
with: Option<With>,
or_conflict: Option<ResolveType>,
Expand Down Expand Up @@ -1372,21 +1373,20 @@ impl ColumnDefinition {
col_type.name = new_type.join(" ");
}
}
let (mut primary_key, mut generated) = (false, false);
for constraint in &cd.constraints {
if let ColumnConstraint::PrimaryKey {
auto_increment: true,
..
} = &constraint.constraint
{
if cd
.col_type
.as_ref()
.map_or(true, |t| !t.name.eq_ignore_ascii_case("INTEGER"))
if let ColumnConstraint::PrimaryKey { auto_increment, .. } = &constraint.constraint {
if *auto_increment
&& cd
.col_type
.as_ref()
.map_or(true, |t| !t.name.eq_ignore_ascii_case("INTEGER"))
{
return Err(custom_err!(
"AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY"
));
}
primary_key = true;
} else if let ColumnConstraint::ForeignKey {
clause:
ForeignKeyClause {
Expand All @@ -1403,8 +1403,15 @@ impl ColumnDefinition {
tbl_name
));
}
} else if let ColumnConstraint::Generated { .. } = &constraint.constraint {
generated = true;
}
}
if primary_key && generated {
return Err(custom_err!(
"generated columns cannot be part of the PRIMARY KEY"
));
}
columns.insert(col_name.clone(), cd);
Ok(())
}
Expand Down

0 comments on commit 298f4d0

Please sign in to comment.