Skip to content

Commit

Permalink
Check CREATE TABLE STRICT with missing or unknown datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Jan 7, 2024
1 parent d45d03e commit 7f31aac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ fn create_table_without_rowid_missing_pk() {
panic!("unexpected error type")
};
}

#[test]
fn create_table_strict_missing_datatype() {
let mut parser = Parser::new(b"CREATE TABLE tbl (c1) STRICT");
let r = parser.next();
if let Error::ParserError(ParserError::Custom(ref msg), _) = r.unwrap_err() {
assert_eq!(msg, "missing datatype for tbl.c1");
} else {
panic!("unexpected error type")
};
}

#[test]
fn create_table_strict_unknown_datatype() {
let mut parser = Parser::new(b"CREATE TABLE tbl (c1 BOOL) STRICT");
let r = parser.next();
if let Error::ParserError(ParserError::Custom(ref msg), _) = r.unwrap_err() {
assert_eq!(msg, "unknown datatype for tbl.c1: \"BOOL\"");
} else {
panic!("unexpected error type")
};
}
22 changes: 16 additions & 6 deletions src/parser/ast/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,25 @@ impl CreateTableBody {
// TODO columns.constraints : check no duplicate names
// TODO constraints: check no duplicated names, use only valid column names
if options.contains(TableOptions::STRICT) {
// TODO check columns type
// Every column definition must specify a datatype for that column. The freedom to specify a column without a datatype is removed.
// The datatype must be one of following: INT INTEGER REAL TEXT BLOB ANY
for c in columns {
match &c.col_type {
Some(Type { name, .. })
if name.eq_ignore_ascii_case("INT")
|| name.eq_ignore_ascii_case("INTEGER") => {}
Some(Type { name, .. }) => {
// The datatype must be one of following: INT INTEGER REAL TEXT BLOB ANY
if !(name.eq_ignore_ascii_case("INT")
|| name.eq_ignore_ascii_case("INTEGER")
|| name.eq_ignore_ascii_case("REAL")
|| name.eq_ignore_ascii_case("TEXT")
|| name.eq_ignore_ascii_case("BLOB")
|| name.eq_ignore_ascii_case("ANY"))
{
return Err(ParserError::Custom(format!(
"unknown datatype for {}.{}: \"{}\"",
tbl_name, c.col_name, name
)));
}
}
_ => {
// Every column definition must specify a datatype for that column. The freedom to specify a column without a datatype is removed.
return Err(ParserError::Custom(format!(
"missing datatype for {}.{}",
tbl_name, c.col_name
Expand Down

0 comments on commit 7f31aac

Please sign in to comment.