fix: Correct the AUTOINCREMENT statement and PRIMARY KEY place in declaration
Some checks failed
Build project / SonarQube Trigger (push) Has been cancelled
Build project / quality-and-build (push) Has been cancelled

This commit is contained in:
Namu
2026-06-16 21:19:11 +02:00
parent e77d8e672d
commit f922ca16f9
6 changed files with 42 additions and 15 deletions

2
Cargo.lock generated
View File

@@ -44,7 +44,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "db_builder"
version = "0.0.1"
version = "0.0.2"
dependencies = [
"rusqlite",
"serde",

View File

@@ -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"

View File

@@ -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
}
]
}

View File

@@ -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]

View File

@@ -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());
}
}

View File

@@ -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<Column, String> {
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,