2019-04-09 18:55:53 -04:00
|
|
|
//! Database Driver for SQLite
|
|
|
|
//!
|
2019-04-23 09:58:03 -04:00
|
|
|
//! Use of this driver requires enabling the `sqlite` feature.
|
|
|
|
//!
|
2019-07-18 09:36:59 -04:00
|
|
|
//! Uses the [rusqlite](https://crates.io/crates/rusqlite) crate for
|
|
|
|
//! interfacing with the database
|
2019-04-03 16:29:51 -04:00
|
|
|
use super::*;
|
|
|
|
|
2019-04-24 16:12:07 -04:00
|
|
|
use slite::NO_PARAMS;
|
2019-04-25 13:09:18 -04:00
|
|
|
use slite::{params, Connection, Result};
|
2019-04-23 09:58:03 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
2019-04-09 18:55:53 -04:00
|
|
|
/// The struct implementing the `DatabaseDriver` trait
|
2019-04-03 16:29:51 -04:00
|
|
|
#[derive(Debug)]
|
2019-04-23 09:58:03 -04:00
|
|
|
pub struct SQLiteDriver {
|
2019-04-25 13:09:18 -04:00
|
|
|
connection: RefCell<Option<Connection>>,
|
2019-04-23 09:58:03 -04:00
|
|
|
}
|
2019-04-02 16:35:52 -04:00
|
|
|
|
2019-04-12 17:09:59 -04:00
|
|
|
impl SQLiteDriver {
|
|
|
|
/// Create an SQLiteDriver driver
|
2019-04-24 16:12:07 -04:00
|
|
|
pub fn new(dsn: &str) -> Self {
|
|
|
|
let mut driver = SQLiteDriver {
|
2019-04-25 13:09:18 -04:00
|
|
|
connection: RefCell::new(None),
|
2019-04-24 16:12:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
driver.connect(dsn);
|
|
|
|
|
|
|
|
driver
|
2019-04-12 17:09:59 -04:00
|
|
|
}
|
2019-04-24 16:12:07 -04:00
|
|
|
|
|
|
|
fn connect(&mut self, dsn: &str) {
|
|
|
|
let connection = if dsn == ":memory:" {
|
|
|
|
Connection::open_in_memory().unwrap()
|
|
|
|
} else {
|
|
|
|
Connection::open(dsn).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
self.connection = RefCell::new(Some(connection));
|
|
|
|
}
|
2019-07-19 16:42:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DatabaseDriver for SQLiteDriver {
|
|
|
|
fn explain(&self, sql: &str) -> String {
|
|
|
|
format!("EXPLAIN QUERY PLAN {}", sql)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn random(&self) -> String {
|
|
|
|
String::from(" RANDOM()")
|
|
|
|
}
|
2019-07-18 09:36:59 -04:00
|
|
|
|
2019-07-19 16:42:36 -04:00
|
|
|
fn query(&self, sql: &str) -> Result<Box<dyn Any>, Box<dyn Any>> {
|
2019-07-18 09:36:59 -04:00
|
|
|
if self.connection.borrow().is_none() {
|
|
|
|
panic!("No database connection.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.connection
|
|
|
|
.borrow_mut()
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
2019-07-19 16:42:36 -04:00
|
|
|
.execute(sql, NO_PARAMS);
|
2019-04-12 17:09:59 -04:00
|
|
|
|
2019-07-19 16:42:36 -04:00
|
|
|
// TODO: map native result to generic result
|
|
|
|
unimplemented!();
|
2019-04-02 16:35:52 -04:00
|
|
|
}
|
2019-04-04 16:39:05 -04:00
|
|
|
}
|