stringqb/src/drivers/mysql.rs

115 lines
2.7 KiB
Rust
Raw Permalink Normal View History

2019-04-09 18:55:53 -04:00
//! Database Driver for MySQL
//!
2019-04-22 08:59:40 -04:00
//! Use of this driver requires enabling the `mysql` feature.
//!
2019-04-09 18:55:53 -04:00
//! Contains database-specific query data
2019-07-24 09:51:53 -04:00
//! Uses the [Mysql](https://crates.io/crates/mysql) crate for
//! interfacing with the database
2019-04-03 20:58:22 -04:00
use super::*;
2019-07-24 09:51:53 -04:00
use std::any::Any;
use std::cell::RefCell;
use my::{Pool};
2019-04-09 18:55:53 -04:00
/// The struct implementing the `DatabaseDriver` trait
2019-04-03 20:58:22 -04:00
#[derive(Debug)]
2019-07-24 09:51:53 -04:00
pub struct MySQLDriver {
connection: RefCell<Option<Pool>>,
}
2019-04-03 20:58:22 -04:00
2019-04-12 17:09:59 -04:00
impl MySQLDriver {
/// Create a MySQLDriver driver
2019-07-24 09:51:53 -04:00
pub fn new(dsn: &str) -> Self {
let mut driver = MySQLDriver {
connection: RefCell::new(None)
};
driver.connect(dsn);
driver
}
fn test_new() -> Self {
MySQLDriver {
connection: RefCell::new(None)
}
}
fn connect(&mut self, dsn: &str) {
let connection = Pool::new(dsn).unwrap();
self.connection = RefCell::new(Some(connection));
2019-04-09 14:13:37 -04:00
}
}
2019-04-12 17:09:59 -04:00
impl DatabaseDriver for MySQLDriver {
2019-04-05 14:51:31 -04:00
/// Get which characters are used to delimit identifiers
/// such as tables, and columns
fn _quotes(&self) -> (char, char) {
2019-04-05 20:46:07 -04:00
('`', '`')
2019-04-03 20:58:22 -04:00
}
2019-04-12 17:09:59 -04:00
2019-07-24 09:51:53 -04:00
fn query(&self, sql: &str) -> Result<Box<dyn Any>, Box<dyn Any>> {
unimplemented!();
}
2019-04-12 17:09:59 -04:00
fn limit(&self, sql: &str, limit: Option<usize>, offset: Option<usize>) -> String {
if limit.is_none() {
return sql.to_string();
}
// Limit and Offset are defined
if offset.is_some() {
return format!("{} LIMIT {}.{}", sql, offset.unwrap(), limit.unwrap());
}
// Limit is defined
format!("{} LIMIT {}", sql, limit.unwrap())
}
fn explain(&self, sql: &str) -> String {
2019-04-17 09:09:31 -04:00
format!("EXPLAIN EXTENDED {}", sql)
2019-04-12 17:09:59 -04:00
}
fn random(&self) -> String {
String::from(" RAND() DESC")
}
2019-04-03 20:58:22 -04:00
}
2019-04-05 20:46:07 -04:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quote_identifier_backtick_quote() {
2019-07-24 09:51:53 -04:00
let driver = MySQLDriver::test_new();
2019-04-05 20:46:07 -04:00
assert_eq!(
driver.quote_identifier("foo, bar, baz"),
"`foo`,`bar`,`baz`"
);
assert_eq!(
driver.quote_identifier("foo.bar, baz, fizz"),
"`foo`.`bar`,`baz`,`fizz`"
);
}
#[test]
fn test_quote_identifiers_backtick_quote() {
2019-07-24 09:51:53 -04:00
let driver = MySQLDriver::test_new();
2019-04-05 20:46:07 -04:00
assert_eq!(
2019-04-11 11:44:06 -04:00
driver.quote_identifiers(vec![
"\tfoo. bar".to_string(),
"baz".to_string(),
"fizz.\n\tbuzz.baz".to_string(),
]),
2019-04-05 20:46:07 -04:00
vec![
"`foo`.`bar`".to_string(),
"`baz`".to_string(),
"`fizz`.`buzz`.`baz`".to_string(),
]
);
}
}