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-04-09 18:55:53 -04:00
|
|
|
//! Contains database-specific query data
|
2019-04-03 16:29:51 -04:00
|
|
|
use super::*;
|
|
|
|
|
2019-04-24 16:12:07 -04:00
|
|
|
use slite::{params, Connection, Result};
|
|
|
|
use slite::NO_PARAMS;
|
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 {
|
|
|
|
connection: RefCell<Option<Connection>>
|
|
|
|
}
|
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-23 09:58:03 -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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// pub fn query(&self, sql: &str) -> Result<usize> {
|
|
|
|
//
|
|
|
|
// }
|
2019-04-12 17:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DatabaseDriver for SQLiteDriver {
|
2019-04-23 09:58:03 -04:00
|
|
|
// fn query(&self, sql: &str) -> Result<(), ()> {
|
|
|
|
// unimplemented!();
|
|
|
|
// }
|
|
|
|
|
2019-04-12 17:09:59 -04:00
|
|
|
fn explain(&self, sql: &str) -> String {
|
2019-04-17 09:09:31 -04:00
|
|
|
format!("EXPLAIN QUERY PLAN {}", sql)
|
2019-04-12 17:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn random(&self) -> String {
|
|
|
|
String::from(" RANDOM()")
|
2019-04-02 16:35:52 -04:00
|
|
|
}
|
2019-04-04 16:39:05 -04:00
|
|
|
}
|