refactor: make public the code that needs to be exposed, remove unused code

This commit is contained in:
Namu
2026-04-02 00:22:43 +02:00
parent ef0767f33a
commit 27e6193b8d
10 changed files with 231 additions and 68 deletions

View File

@@ -1,3 +1,50 @@
mod mappings;
mod schemas;
mod query_builders;
use crate::databases::{connect, disconnect, run_command};
use crate::query_builders::table_sql_serializer::TableSqlSerializer;
use crate::schemas::reader::SchemaParsingFacade;
pub mod mappings;
pub mod schemas;
pub mod query_builders;
mod databases;
pub struct DbBuilderFacade {}
impl DbBuilderFacade {
pub fn build(self, json_schema_path: String, db_connection: String) -> Result<(), String> {
let schema_parsing_facade = SchemaParsingFacade{};
let tables = schema_parsing_facade.parse(json_schema_path);
let table_serializer = TableSqlSerializer{};
let mut sql_tables = String::new();
for table in tables {
sql_tables += &table_serializer.serialize(table);
}
let connection_result = connect(db_connection);
if let Ok(connection) = connection_result {
run_command(&connection, sql_tables).expect("Failed to execute query");
disconnect(connection).expect("Failed to disconnect from sqlite");
Ok(())
} else {
Err("Failed to connect to the server".to_string())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_db_builder() {
let facade = DbBuilderFacade{};
match facade.build("./example.json".to_string(), "test_db".to_string()) {
Ok(_) => {assert!(true)},
Err(message) => {assert!(false, "{}", message)}
}
}
}

View File

@@ -5,10 +5,6 @@ use crate::schemas::entities::Column;
pub struct ColumnSqlSerializer {}
impl ColumnSqlSerializer {
pub fn new() -> Self {
ColumnSqlSerializer {}
}
pub fn serialize(self, column: Column) -> Result<String, String> {
let mut query = String::from(column.name);
query.push(' ');
@@ -71,7 +67,7 @@ mod tests {
.build()
.unwrap();
let serializer = ColumnSqlSerializer::new();
let serializer = ColumnSqlSerializer{};
let result = serializer.serialize(column).unwrap();
assert_eq!(result, "id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY ,".to_string());
@@ -86,7 +82,7 @@ mod tests {
.build()
.unwrap();
let serializer = ColumnSqlSerializer::new();
let serializer = ColumnSqlSerializer{};
let result = serializer.serialize(column).unwrap();
assert_eq!(result, "name TEXT NOT NULL DEFAULT 'None' ,".to_string());
@@ -101,7 +97,7 @@ mod tests {
.build()
.unwrap();
let serializer = ColumnSqlSerializer::new();
let serializer = ColumnSqlSerializer {};
let result = serializer.serialize(column).unwrap();
assert_eq!(result, "age INTEGER NOT NULL DEFAULT 18 ,".to_string());

View File

@@ -1,2 +1,2 @@
mod column_sql_serializer;
mod table_sql_serializer;
pub(crate) mod column_sql_serializer;
pub(crate) mod table_sql_serializer;

View File

@@ -5,15 +5,11 @@ use crate::schemas::entities::Table;
pub struct TableSqlSerializer {}
impl TableSqlSerializer {
pub fn new() -> Self {
TableSqlSerializer{}
}
pub fn serialize(self, table: Table) -> String {
let mut query = String::from("CREATE TABLE ");
query += &format!("{} (\n", table.name);
let column_serializer = ColumnSqlSerializer::new();
let column_serializer = ColumnSqlSerializer{};
for column in table.columns {
query += &column_serializer.serialize(column).unwrap();
}
@@ -43,7 +39,7 @@ mod test {
.with_column(column)
.build();
let serializer = TableSqlSerializer::new();
let serializer = TableSqlSerializer{};
let result = serializer.serialize(table);

View File

@@ -71,16 +71,16 @@ impl ColumnBuilder {
return Err("Cannot set AUTO_INCREMENT on non-INTEGER column".to_string());
}
Ok(Column::new(
self.name,
self.datatype,
self.nullable,
self.default,
self.auto_increment,
self.unique,
self.check_constraint,
self.primary_key,
))
Ok(Column {
name: self.name,
datatype: self.datatype,
nullable: self.nullable,
default: self.default,
auto_increment: self.auto_increment,
unique: self.unique,
check_constraint: self.check_constraint,
primary_key: self.primary_key,
})
}
}
@@ -115,7 +115,7 @@ impl TableBuilder {
}
pub fn build(self) -> Table {
Table::new(self.name, self.columns, self.strict)
Table { name: self.name, columns: self.columns, strict: self.strict }
}
}

View File

@@ -1,4 +1,3 @@
pub(crate) mod reader;
pub(crate) mod entities;
pub(crate) mod builders;
mod writer;
pub mod entities;
pub mod builders;

View File

@@ -1,13 +1,10 @@
use std::fs;
use crate::schemas::entities::Table;
/// SchemaParsingFacade aim to hide the logic of reading and parsing the schema json file
pub struct SchemaParsingFacade {}
impl SchemaParsingFacade {
pub fn new() -> SchemaParsingFacade {
SchemaParsingFacade{}
}
pub fn parse(&self, path: String) -> Vec<Table> {
let json_schema = self.read_schema_file(path);
self.parse_schema_file(json_schema)

View File

@@ -1,32 +0,0 @@
use std::fs::File;
use std::io::Write;
use crate::schemas::entities::Table;
pub struct SchemaWriter {
tables: Vec<Table>
}
impl SchemaWriter {
pub fn new() -> Self {
SchemaWriter{
tables: Vec::new()
}
}
pub fn add_table(mut self, table: Table) -> Self {
self.tables.push(table);
self
}
// create_schemas create the schema as a JSON file. the name doesn't need to have the ".json"
pub fn create_schemas(self, schema_name: String) -> Result<(), String> {
let schema_file_path = format!("{}.json", schema_name);
let content_result = serde_json::to_string(self.tables.as_slice()).expect("Error serializing tables");
let mut schema_file = File::create(schema_file_path).expect("Failed to create schema file");
schema_file.write_all(content_result.as_bytes()).expect("Error writing in schema file");
Ok(())
}
}