Skip to content

Commit

Permalink
Changing return to use a vector of rusqlite::Value for QueryBuilder.b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
mjovanc committed Nov 21, 2023
1 parent 62d6ec6 commit ddf0478
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions njord/src/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a> QueryBuilder<'a> {
}

// Result<Option<Vec<HashMap<String, rusqlite::types::Value>>>>
pub fn build(mut self) -> Result<Vec<HashMap<String, String>>> {
pub fn build(mut self) -> Result<Vec<HashMap<String, Value>>> {
let columns_str = self.columns.join(", ");
let table_name = self
.table
Expand Down Expand Up @@ -94,13 +94,13 @@ impl<'a> QueryBuilder<'a> {
let iter = stmt.query_map((), |row| {
let mut result_row = HashMap::new();
for (i, column) in self.columns.iter().enumerate() {
let value: String = format!("{:?}", row.get_unwrap::<usize, Value>(i));
let value = row.get_unwrap::<usize, Value>(i);
result_row.insert(column.clone(), value);
}
Ok(result_row)
})?;

let result: Result<Vec<HashMap<String, String>>> = iter.collect();
let result: Result<Vec<HashMap<String, Value>>> = iter.collect();
result.map_err(|err| err.into())
}
}
16 changes: 15 additions & 1 deletion njord/tests/sqlite_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// integrations tests for sqlite

use rusqlite::types::Value;
use njord::sqlite::{self, Condition};
use njord::table::Table;

Expand Down Expand Up @@ -114,7 +115,20 @@ fn select() {

// currently returns error with "ExecuteReturnedResults"
match result {
Ok(result) => println!("{:?}", result),
Ok(result) => {
for row in result {
for (column, value) in &row {
match value {
Value::Null => println!("{}: NULL", column),
Value::Integer(i) => println!("{}: {}", column, i),
Value::Real(f) => println!("{}: {}", column, f),
Value::Text(s) => println!("{}: {}", column, s),
Value::Blob(b) => println!("{}: <blob of length {}>", column, b.len()),
}
}
println!("---"); // Separate rows for clarity
}
},
Err(error) => panic!("Failed to SELECT: {:?}", error),
};
}
Expand Down

0 comments on commit ddf0478

Please sign in to comment.