Page MenuHomeDevCentral

D704.id1774.diff
No OneTemporary

D704.id1774.diff

Index: src/main.rs
===================================================================
--- src/main.rs
+++ src/main.rs
@@ -20,10 +20,13 @@
#[macro_use]
extern crate router;
+extern crate sqlite3;
+
/* -------------------------------------------------------------
Modules
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+mod store;
mod http_server;
mod web_handlers;
Index: src/store.rs
===================================================================
--- /dev/null
+++ src/store.rs
@@ -0,0 +1,158 @@
+/* -------------------------------------------------------------
+ Servers log microservice
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ Project: Nasqueron
+ Created: 2016-11-11
+ License: BSD-2-Clause
+ ------------------------------------------------------------- */
+
+use sqlite3::DatabaseConnection;
+use sqlite3::SqliteResult;
+use sqlite3::StatementUpdate;
+use sqlite3::ToSql;
+use sqlite3::access::ByFilename;
+
+use std::env;
+use std::fs;
+use std::io;
+use std::path::Path;
+
+/* -------------------------------------------------------------
+ Log entry
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+#[derive(Debug)]
+struct LogEntry {
+ date: String,
+ emitter: String,
+ source: String,
+ component: String,
+ entry: String
+}
+
+/* -------------------------------------------------------------
+ Helper functions
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// Gets the path to the store to use.
+fn get_path() -> String {
+ env::var("STORE").unwrap_or("log.db".to_string())
+}
+
+/* -------------------------------------------------------------
+ Log store
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// Determines the store exists.
+fn exists() -> bool {
+ Path::new(&get_path()).exists()
+}
+
+/// Creates or opens the store.
+fn get () -> Database {
+ let mut db = Database::new().unwrap();
+ db.init(get_schema());
+ db
+}
+
+/// Gets the SQL tables schema for the log store.
+fn get_schema () -> &'static str {
+ "CREATE TABLE IF NOT EXISTS log (
+ date VARCHAR(20),
+ emitter VARCHAR(32),
+ source VARCHAR(32),
+ component VARCHAR(32),
+ entry TEXT
+ );"
+}
+
+fn insert (mut db: Database, entry: LogEntry) -> Result<(), String> {
+ db.exec_prepared_statement(
+ "INSERT INTO log (date, emitter, source, component, entry)
+ VALUES ($1, $2, $3, $4, $5);",
+ &[&entry.date, &entry.emitter, &entry.source, &entry.component, &entry.entry]
+ )
+}
+
+/// Destroys the store.
+fn destroy() -> Result<(), io::Error> {
+ try!(fs::remove_file(&get_path()));
+ info!("Destroyed {}", get_path());
+ Ok(())
+}
+
+/// Opens a connection to the database.
+fn get_connection () -> SqliteResult<DatabaseConnection> {
+ DatabaseConnection::new(
+ ByFilename {
+ filename: &*get_path(),
+ flags: Default::default(),
+ }
+ )
+}
+
+/* -------------------------------------------------------------
+ Database layer
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+pub struct Database {
+ connection: DatabaseConnection
+}
+
+impl Database {
+ /// Initializes a new instance of the Database object.
+ pub fn new() -> Result<Database, String> {
+ info!("Open database {}", get_path());
+ match ::store::get_connection() {
+ Ok(database_connection) => Ok(Database { connection: database_connection }),
+ Err(err) => Err(err.desc.to_string())
+ }
+ }
+
+ /// Ensures database contains required table
+ pub fn init(&mut self, sql_schema_statements: &str) {
+ self.connection.exec(sql_schema_statements).unwrap();
+ }
+
+ /// Executes a prepared statement
+ /// - query: the SQL query, each parameter replaced by $1, $2, etc.
+ /// - parameters: the parameters to put in the query
+ pub fn exec_prepared_statement (&mut self, query: &str, parameters: &[&ToSql]) -> Result<(), String> {
+ let mut statement = self.connection.prepare(query).unwrap();
+ match statement.update(parameters) {
+ Ok(_) => Ok(()),
+ Err(err) => Err(err.desc.to_string()),
+ }
+ }
+}
+
+/* -------------------------------------------------------------
+ Tests
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+#[cfg(test)]
+mod tests {
+
+ use super::get;
+ use super::destroy;
+ use super::get_path;
+ use super::exists;
+
+ #[test]
+ fn get_store_path_returns_expected_default_value() {
+ assert_eq!("log.db", get_path());
+ }
+
+ #[test]
+ fn store_exists_when_store_does_not() {
+ assert_eq!(false, exists());
+ }
+
+ #[test]
+ fn store_exists_when_store_does() {
+ get();
+ assert_eq!(true, exists());
+ destroy().unwrap();
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 23:00 (19 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259106
Default Alt Text
D704.id1774.diff (4 KB)

Event Timeline