From 3c48d3091f621046c3008b8a4ef74be20ff81e05 Mon Sep 17 00:00:00 2001 From: mjovanc Date: Mon, 30 Oct 2023 20:27:37 +0100 Subject: [PATCH] Updating QueryBuilder --- njord/src/sqlite/query.rs | 14 ++++++++++++-- njord/tests/common/mod.rs | 25 +++++++++++++++++++++++++ njord/tests/sqlite_test.rs | 15 +++++++++++---- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/njord/src/sqlite/query.rs b/njord/src/sqlite/query.rs index 8dfaf2f4..a7356c68 100644 --- a/njord/src/sqlite/query.rs +++ b/njord/src/sqlite/query.rs @@ -2,6 +2,8 @@ use crate::table::Table; use rusqlite::{Connection, Result}; +use log::info; + use super::Condition; pub struct QueryBuilder<'a> { @@ -39,7 +41,7 @@ impl<'a> QueryBuilder<'a> { self } - pub fn build(self) -> Result { + pub fn build(mut self) -> rusqlite::Result<()> { let columns_str = self.columns.join(", "); let table_name = self .table @@ -80,6 +82,14 @@ impl<'a> QueryBuilder<'a> { } } } - Ok(query) + + // info!("INSERT SQL: {}", query); + println!("INSERT SQL: {}", query); + + let tx = self.conn.transaction()?; + tx.execute(&query, [])?; + tx.commit()?; + + Ok(()) } } diff --git a/njord/tests/common/mod.rs b/njord/tests/common/mod.rs index 60d090da..394a5933 100644 --- a/njord/tests/common/mod.rs +++ b/njord/tests/common/mod.rs @@ -27,6 +27,31 @@ pub fn initialize_tables_sqlite(db_name: &str) -> Result<(), Error> { result } +pub fn insert_rows_sqlite(db_name: &str) -> Result<(), Error> { + let conn = open_db_sqlite(db_name)?; + + #[derive(Table, Debug)] + struct TableA { + title: String, + description: String, + amount: u32, + } + + let table_row: TableA = TableA { + title: "Table A".to_string(), + description: "Some description for Table A".to_string(), + amount: 0, + }; + + let result = sqlite::insert(conn, &table_row); + + println!("Result: {:?}", result); + + assert!(result.is_ok()); + + result +} + pub fn generate_tables_sqlite() -> Vec> { #[derive(Table, Debug, Default)] struct TableA { diff --git a/njord/tests/sqlite_test.rs b/njord/tests/sqlite_test.rs index 03835eff..6e3df7fd 100644 --- a/njord/tests/sqlite_test.rs +++ b/njord/tests/sqlite_test.rs @@ -91,6 +91,7 @@ fn select() { let _ = common::drop_db_sqlite(db_name); let conn = common::open_db_sqlite(db_name).unwrap(); let init_tables_result = common::initialize_tables_sqlite(db_name); + let insert_rows_result = common::insert_rows_sqlite(db_name); match init_tables_result { Ok(_) => { @@ -101,15 +102,21 @@ fn select() { amount: u32, } let columns = vec!["title".to_string(), "description".to_string()]; - let condition = Condition::Eq("title".to_string(), "Some title".to_string()); + let condition = Condition::Eq( + "description".to_string(), + "Some description for Table A".to_string(), + ); let result = sqlite::select(conn, columns) .from(&TableA::default()) .where_clause(condition) - .build() - .unwrap(); + .build(); - println!("SELECT SQL QUERY: {:?}", result); + // currently returns error with "ExecuteReturnedResults" + match result { + Ok(result) => println!("SELECT SQL QUERY: {:?}", result), + Err(error) => panic!("Failed to select: {:?}", error), + }; } Err(error) => panic!("Failed to select: {:?}", error), };