Skip to content

Commit

Permalink
Updating QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mjovanc committed Oct 30, 2023
1 parent 4d77d02 commit 3c48d30
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
14 changes: 12 additions & 2 deletions njord/src/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::table::Table;

use rusqlite::{Connection, Result};

use log::info;

use super::Condition;

pub struct QueryBuilder<'a> {
Expand Down Expand Up @@ -39,7 +41,7 @@ impl<'a> QueryBuilder<'a> {
self
}

pub fn build(self) -> Result<String> {
pub fn build(mut self) -> rusqlite::Result<()> {
let columns_str = self.columns.join(", ");
let table_name = self
.table
Expand Down Expand Up @@ -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(())
}
}
25 changes: 25 additions & 0 deletions njord/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Table>> {
#[derive(Table, Debug, Default)]
struct TableA {
Expand Down
15 changes: 11 additions & 4 deletions njord/tests/sqlite_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand All @@ -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),
};
Expand Down

0 comments on commit 3c48d30

Please sign in to comment.