keep track of notnull constraint on column creation

This commit is contained in:
Glauber Costa
2025-01-30 17:03:49 -05:00
parent 42f93e9bea
commit 69d3fbc797
4 changed files with 35 additions and 0 deletions

View File

@@ -178,6 +178,7 @@ impl PseudoTable {
ty,
primary_key,
is_rowid_alias: false,
notnull: false,
default: None,
});
}
@@ -272,11 +273,15 @@ fn create_table(
let mut default = None;
let mut primary_key = false;
let mut notnull = false;
for c_def in &col_def.constraints {
match &c_def.constraint {
sqlite3_parser::ast::ColumnConstraint::PrimaryKey { .. } => {
primary_key = true;
}
sqlite3_parser::ast::ColumnConstraint::NotNull { .. } => {
notnull = true;
}
sqlite3_parser::ast::ColumnConstraint::Default(expr) => {
default = Some(expr.clone())
}
@@ -295,6 +300,7 @@ fn create_table(
ty,
primary_key,
is_rowid_alias: typename_exactly_integer && primary_key,
notnull,
default,
});
}
@@ -344,6 +350,7 @@ pub struct Column {
pub ty: Type,
pub primary_key: bool,
pub is_rowid_alias: bool,
pub notnull: bool,
pub default: Option<Expr>,
}
@@ -383,6 +390,7 @@ pub fn sqlite_schema_table() -> BTreeTable {
ty: Type::Text,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
},
Column {
@@ -390,6 +398,7 @@ pub fn sqlite_schema_table() -> BTreeTable {
ty: Type::Text,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
},
Column {
@@ -397,6 +406,7 @@ pub fn sqlite_schema_table() -> BTreeTable {
ty: Type::Text,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
},
Column {
@@ -404,6 +414,7 @@ pub fn sqlite_schema_table() -> BTreeTable {
ty: Type::Integer,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
},
Column {
@@ -411,6 +422,7 @@ pub fn sqlite_schema_table() -> BTreeTable {
ty: Type::Text,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
},
],
@@ -739,6 +751,24 @@ mod tests {
Ok(())
}
#[test]
pub fn test_col_notnull() -> Result<()> {
let sql = r#"CREATE TABLE t1 (a INTEGER NOT NULL);"#;
let table = BTreeTable::from_sql(sql, 0)?;
let column = table.get_column("a").unwrap().1;
assert_eq!(column.notnull, true);
Ok(())
}
#[test]
pub fn test_col_notnull_negative() -> Result<()> {
let sql = r#"CREATE TABLE t1 (a INTEGER);"#;
let table = BTreeTable::from_sql(sql, 0)?;
let column = table.get_column("a").unwrap().1;
assert_eq!(column.notnull, false);
Ok(())
}
#[test]
pub fn test_sqlite_schema() {
let expected = r#"CREATE TABLE sqlite_schema (
@@ -813,6 +843,7 @@ mod tests {
ty: Type::Integer,
primary_key: false,
is_rowid_alias: false,
notnull: false,
default: None,
}],
};

View File

@@ -171,6 +171,7 @@ pub fn emit_group_by<'a>(
primary_key: false,
ty: crate::schema::Type::Null,
is_rowid_alias: false,
notnull: false,
default: None,
})
.collect::<Vec<_>>();

View File

@@ -73,6 +73,7 @@ pub fn emit_order_by(
primary_key: false,
ty: crate::schema::Type::Null,
is_rowid_alias: false,
notnull: false,
default: None,
});
}
@@ -88,6 +89,7 @@ pub fn emit_order_by(
primary_key: false,
ty: crate::schema::Type::Null,
is_rowid_alias: false,
notnull: false,
default: None,
});
}

View File

@@ -275,6 +275,7 @@ impl TableReference {
ty: Type::Text, // FIXME: infer proper type
is_rowid_alias: false,
primary_key: false,
notnull: false,
default: None,
})
.collect(),