//! Database Driver for MySQL //! //! Contains database-specific query data use super::*; /// The struct implementing the `DatabaseDriver` trait #[derive(Debug)] pub struct MySQLDriver; impl MySQLDriver { /// Create a MySQLDriver driver pub fn new() -> Self { MySQLDriver {} } } impl DatabaseDriver for MySQLDriver { /// Get which characters are used to delimit identifiers /// such as tables, and columns fn _quotes(&self) -> (char, char) { ('`', '`') } fn limit(&self, sql: &str, limit: Option, offset: Option) -> 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 { return format!("EXPLAIN EXTENDED {}", sql) } fn random(&self) -> String { String::from(" RAND() DESC") } } #[cfg(test)] mod tests { use super::*; #[test] fn test_quote_identifier_backtick_quote() { let driver = MySQLDriver::new(); 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() { let driver = MySQLDriver::new(); assert_eq!( driver.quote_identifiers(vec![ "\tfoo. bar".to_string(), "baz".to_string(), "fizz.\n\tbuzz.baz".to_string(), ]), vec![ "`foo`.`bar`".to_string(), "`baz`".to_string(), "`fizz`.`buzz`.`baz`".to_string(), ] ); } }