fix: Correct the AUTOINCREMENT statement and PRIMARY KEY place in declaration
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -44,7 +44,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "db_builder"
|
name = "db_builder"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "db_builder"
|
name = "db_builder"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
description = "This crate enables you to have a lib that create a sqlite database from a json file schema"
|
description = "This crate enables you to have a lib that create a sqlite database from a json file schema"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
29
example.json
29
example.json
@@ -6,7 +6,34 @@
|
|||||||
{
|
{
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"datatype": "Integer",
|
"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
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,12 @@ impl ColumnSqlSerializer {
|
|||||||
query += "UNIQUE " // Notice the space at the end
|
query += "UNIQUE " // Notice the space at the end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if column.primary_key {
|
||||||
|
query += "PRIMARY KEY ";
|
||||||
|
}
|
||||||
|
|
||||||
if column.auto_increment {
|
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 {
|
if let Some(default) = column.default {
|
||||||
@@ -42,10 +46,6 @@ impl ColumnSqlSerializer {
|
|||||||
query += &format!("CHECK {} ", check_constraint);
|
query += &format!("CHECK {} ", check_constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
if column.primary_key {
|
|
||||||
query += "PRIMARY KEY ";
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(query.trim_end().to_string())
|
Ok(query.trim_end().to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ mod tests {
|
|||||||
let serializer = ColumnSqlSerializer{};
|
let serializer = ColumnSqlSerializer{};
|
||||||
let result = serializer.serialize(column).unwrap();
|
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]
|
#[test]
|
||||||
|
|||||||
@@ -40,6 +40,6 @@ mod test {
|
|||||||
|
|
||||||
let result = serializer.serialize(table);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ pub struct ColumnBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ColumnBuilder {
|
impl ColumnBuilder {
|
||||||
pub fn new() -> ColumnBuilder {
|
pub fn new() -> Self {
|
||||||
ColumnBuilder {
|
Self {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
datatype: SQLiteDatatypes::Text,
|
datatype: SQLiteDatatypes::Text,
|
||||||
nullable: false,
|
nullable: false,
|
||||||
@@ -68,7 +68,7 @@ impl ColumnBuilder {
|
|||||||
|
|
||||||
pub fn build(self) -> Result<Column, String> {
|
pub fn build(self) -> Result<Column, String> {
|
||||||
if self.auto_increment && self.datatype != SQLiteDatatypes::Integer {
|
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 {
|
Ok(Column {
|
||||||
@@ -97,8 +97,8 @@ pub struct TableBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TableBuilder {
|
impl TableBuilder {
|
||||||
pub fn new() -> TableBuilder {
|
pub fn new() -> Self {
|
||||||
TableBuilder {
|
Self {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
columns: Vec::new(),
|
columns: Vec::new(),
|
||||||
strict: false,
|
strict: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user