diff --git a/core/schema.rs b/core/schema.rs index 32dc631c5..f83e27cf1 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -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, } @@ -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, }], }; diff --git a/core/translate/group_by.rs b/core/translate/group_by.rs index 4c593643c..5f7aa7aae 100644 --- a/core/translate/group_by.rs +++ b/core/translate/group_by.rs @@ -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::>(); diff --git a/core/translate/order_by.rs b/core/translate/order_by.rs index 04e29035b..1435510d3 100644 --- a/core/translate/order_by.rs +++ b/core/translate/order_by.rs @@ -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, }); } diff --git a/core/translate/plan.rs b/core/translate/plan.rs index e151ea9e4..76d0a334c 100644 --- a/core/translate/plan.rs +++ b/core/translate/plan.rs @@ -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(),