Semi-functional sqlite demo

This commit is contained in:
William Brown 2018-11-06 22:09:48 +10:00
parent 4c1a4406bb
commit 8844293a48
3 changed files with 45 additions and 5 deletions

View file

@ -26,6 +26,7 @@ serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
rusqlite = "0.15"
r2d2 = "0.8" r2d2 = "0.8"
r2d2_sqlite = "0.7" r2d2_sqlite = "0.7"

View file

@ -4,6 +4,8 @@ use actix::prelude::*;
use serde_json; use serde_json;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
use r2d2::Pool; use r2d2::Pool;
use rusqlite::NO_PARAMS;
use rusqlite::types::ToSql;
// use uuid; // use uuid;
use super::log::EventLog; use super::log::EventLog;
@ -30,6 +32,12 @@ impl BackendAuditEvent {
} }
} }
#[derive(Debug)]
struct IdEntry {
id: i32,
data: String
}
pub enum BackendType { pub enum BackendType {
Memory, // isn't memory just sqlite with file :memory: ? Memory, // isn't memory just sqlite with file :memory: ?
SQLite, SQLite,
@ -59,9 +67,18 @@ impl Backend {
.expect("Failed to create pool"); .expect("Failed to create pool");
{ {
let conn = pool.get().unwrap();
// Perform any migrations as required? // Perform any migrations as required?
// I think we only need the core table here, indexing will do it's own // I think we only need the core table here, indexing will do it's own
// thing later // thing later
// conn.execute("PRAGMA journal_mode=WAL;", NO_PARAMS).unwrap();
conn.execute(
"CREATE TABLE IF NOT EXISTS id2entry (
id INTEGER PRIMARY KEY ASC,
data TEXT NOT NULL
)
", NO_PARAMS
).unwrap();
// Create a version table for migration indication // Create a version table for migration indication
@ -99,10 +116,31 @@ impl Backend {
log_event!(self.log, "serialising: {:?}", ser_entries); log_event!(self.log, "serialising: {:?}", ser_entries);
// THIS IS PROBABLY THE BIT WHERE YOU NEED DB ABSTRACTION // THIS IS PROBABLY THE BIT WHERE YOU NEED DB ABSTRACTION
// Start a txn {
// write them all let conn = self.pool.get().unwrap();
// TODO: update indexes (as needed) // Start a txn
// Commit the txn conn.execute("BEGIN TRANSACTION", NO_PARAMS).unwrap();
for ser_entry in ser_entries {
conn.execute("INSERT INTO id2entry (data) VALUES (?1)", &[&ser_entry as &ToSql]).unwrap();
}
// write them all
let mut stmt = conn.prepare("SELECT id, data FROM id2entry").unwrap();
let id2entry_iter = stmt
.query_map(NO_PARAMS, |row|
IdEntry {
id: row.get(0),
data: row.get(1)
}
).unwrap();
for row in id2entry_iter {
println!("{:?}", row);
}
// TODO: update indexes (as needed)
// Commit the txn
conn.execute("COMMIT TRANSACTION", NO_PARAMS).unwrap();
}
log_event!(self.log, "End create"); log_event!(self.log, "End create");
// End the timer? // End the timer?
@ -153,7 +191,7 @@ mod tests {
System::run(|| { System::run(|| {
let test_log = log::start(); let test_log = log::start();
let mut be = Backend::new(test_log.clone(), ""); let mut be = Backend::new(test_log.clone(), "/tmp/test.db");
// Could wrap another future here for the future::ok bit... // Could wrap another future here for the future::ok bit...
let fut = $test_fn(test_log, be); let fut = $test_fn(test_log, be);

View file

@ -5,6 +5,7 @@ extern crate serde_derive;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate futures; extern crate futures;
extern crate rusqlite;
extern crate r2d2; extern crate r2d2;
extern crate r2d2_sqlite; extern crate r2d2_sqlite;
extern crate uuid; extern crate uuid;