You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
use std::borrow::Cow;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct MariaDBDatabase<'a, C: 'a + CommandRunner> {
|
|
db_name: Cow<'a, str>,
|
|
seed_file: Cow<'a, str>,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> MariaDBDatabase<'a, C> {
|
|
pub fn new(db_name: Cow<'a, str>, seed_file: Cow<'a, str>, command_runner: &'a C) -> Self {
|
|
MariaDBDatabase {
|
|
db_name: db_name,
|
|
seed_file: seed_file,
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
|
|
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
|
|
let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
|
|
Ok(try!(String::from_utf8(b)))
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for MariaDBDatabase<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "MariaDB Database {}", self.db_name)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> Symbol for MariaDBDatabase<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
Ok(try!(self.run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name))).trim_right() == self.db_name)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
try!(self.run_sql(&format!("CREATE DATABASE {}", self.db_name)));
|
|
self.command_runner.run_successfully("sh", &["-c", &format!("mariadb '{}' < {}", self.db_name, self.seed_file)])
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
}
|