diff --git a/Cargo.lock b/Cargo.lock index a4285ee..b1770ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,7 +44,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "db_builder" -version = "0.0.1" +version = "0.0.2" dependencies = [ "rusqlite", "serde", diff --git a/Cargo.toml b/Cargo.toml index c648c4d..39c5290 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "db_builder" -version = "0.0.1" +version = "0.0.2" edition = "2024" description = "This crate enables you to have a lib that create a sqlite database from a json file schema" license = "MIT" diff --git a/example.json b/example.json index 97a0605..dc8a8e2 100644 --- a/example.json +++ b/example.json @@ -6,7 +6,34 @@ { "name": "id", "datatype": "Integer", - "primary_key": true + "primary_key": true, + "auto_increment": true + } + ] + }, + { + "name": "users", + "strict": true, + "columns": [ + { + "name": "id", + "datatype": "Integer", + "primary_key": true, + "auto_increment": true + }, + { + "name": "name", + "datatype": "Text", + "unique": true + }, + { + "name": "password", + "datatype": "Text" + }, + { + "name": "email", + "datatype": "Text", + "unique": true } ] } diff --git a/src/query_builders/column_sql_serializer.rs b/src/query_builders/column_sql_serializer.rs index 9268b4e..2213051 100644 --- a/src/query_builders/column_sql_serializer.rs +++ b/src/query_builders/column_sql_serializer.rs @@ -20,8 +20,12 @@ impl ColumnSqlSerializer { query += "UNIQUE " // Notice the space at the end } + if column.primary_key { + query += "PRIMARY KEY "; + } + if column.auto_increment { - query += "AUTO_INCREMENT " // Notice the space at the end + query += "AUTOINCREMENT " // Notice the space at the end } if let Some(default) = column.default { @@ -42,10 +46,6 @@ impl ColumnSqlSerializer { query += &format!("CHECK {} ", check_constraint); } - if column.primary_key { - query += "PRIMARY KEY "; - } - Ok(query.trim_end().to_string()) } } @@ -69,7 +69,7 @@ mod tests { let serializer = ColumnSqlSerializer{}; let result = serializer.serialize(column).unwrap(); - assert_eq!(result, "id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY".to_string()); + assert_eq!(result, "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT".to_string()); } #[test] diff --git a/src/query_builders/table_sql_serializer.rs b/src/query_builders/table_sql_serializer.rs index c59cf3d..a666f89 100644 --- a/src/query_builders/table_sql_serializer.rs +++ b/src/query_builders/table_sql_serializer.rs @@ -40,6 +40,6 @@ mod test { let result = serializer.serialize(table); - assert_eq!(result, "CREATE TABLE test (id INTEGER NOT NULL AUTO_INCREMENT);".to_string()); + assert_eq!(result, "CREATE TABLE test (id INTEGER NOT NULL AUTOINCREMENT);".to_string()); } } diff --git a/src/schemas/builders.rs b/src/schemas/builders.rs index ddc3b43..eb404d7 100644 --- a/src/schemas/builders.rs +++ b/src/schemas/builders.rs @@ -13,8 +13,8 @@ pub struct ColumnBuilder { } impl ColumnBuilder { - pub fn new() -> ColumnBuilder { - ColumnBuilder { + pub fn new() -> Self { + Self { name: String::new(), datatype: SQLiteDatatypes::Text, nullable: false, @@ -68,7 +68,7 @@ impl ColumnBuilder { pub fn build(self) -> Result { if self.auto_increment && self.datatype != SQLiteDatatypes::Integer { - return Err("Cannot set AUTO_INCREMENT on non-INTEGER column".to_string()); + return Err("Cannot set AUTOINCREMENT on non-INTEGER column".to_string()); } Ok(Column { @@ -97,8 +97,8 @@ pub struct TableBuilder { } impl TableBuilder { - pub fn new() -> TableBuilder { - TableBuilder { + pub fn new() -> Self { + Self { name: String::new(), columns: Vec::new(), strict: false,