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.
52 lines
1.5 KiB
52 lines
1.5 KiB
use std::borrow::Cow;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::Symbol;
|
|
|
|
pub struct MariaDBDatabase<'a> {
|
|
db_name: Cow<'a, str>,
|
|
seed_file: &'a str,
|
|
command_runner: &'a CommandRunner
|
|
}
|
|
|
|
impl<'a> MariaDBDatabase<'a> {
|
|
pub fn new(db_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBDatabase<'a> {
|
|
MariaDBDatabase {
|
|
db_name: db_name,
|
|
seed_file: "/root/seedfile.sql",
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
|
|
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
|
|
let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-B", "-e", sql]));
|
|
if output.status.code() != Some(0) {
|
|
return Err(try!(String::from_utf8(output.stderr)).into());
|
|
}
|
|
Ok(try!(String::from_utf8(output.stdout)))
|
|
}
|
|
}
|
|
|
|
impl<'a> fmt::Display for MariaDBDatabase<'a> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "MariaDB Database {}", self.db_name)
|
|
}
|
|
}
|
|
|
|
impl<'a> Symbol for MariaDBDatabase<'a> {
|
|
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)));
|
|
try!(self.command_runner.run_with_args("sh", &["-c", &format!("mariadb '{}' < {}", self.db_name, self.seed_file)]));
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
}
|