51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
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);
|
|
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(), ":memory:".to_string()) {
|
|
Ok(_) => {assert!(true)},
|
|
Err(message) => {assert!(false, "{}", message)}
|
|
}
|
|
}
|
|
}
|