Execute queries and transacts passed in at startup
This commit is contained in:
parent
825726ddf9
commit
002edeae1e
2 changed files with 31 additions and 9 deletions
|
@ -42,6 +42,8 @@ pub fn run() -> i32 {
|
||||||
|
|
||||||
opts.optopt("d", "", "The path to a database to open", "DATABASE");
|
opts.optopt("d", "", "The path to a database to open", "DATABASE");
|
||||||
opts.optflag("h", "help", "Print this help message and exit");
|
opts.optflag("h", "help", "Print this help message and exit");
|
||||||
|
opts.optopt("q", "query", "Execute a query on startup. Queries are executed after any transacts.", "QUERY");
|
||||||
|
opts.optopt("t", "transact", "Execute a transact on startup. Transacts are executed before queries.", "TRANSACT");
|
||||||
opts.optflag("v", "version", "Print version and exit");
|
opts.optflag("v", "version", "Print version and exit");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
|
@ -64,9 +66,23 @@ pub fn run() -> i32 {
|
||||||
|
|
||||||
let db_name = matches.opt_str("d");
|
let db_name = matches.opt_str("d");
|
||||||
|
|
||||||
|
let transacts = matches.opt_strs("t");
|
||||||
|
let queries = matches.opt_strs("q");
|
||||||
|
|
||||||
|
let mut cmds = Vec::with_capacity(transacts.len() + queries.len());
|
||||||
|
|
||||||
|
for transact in transacts {
|
||||||
|
cmds.push(command_parser::Command::Transact(transact));
|
||||||
|
}
|
||||||
|
for query in queries {
|
||||||
|
cmds.push(command_parser::Command::Query(query));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let repl = repl::Repl::new(db_name);
|
let repl = repl::Repl::new(db_name);
|
||||||
if repl.is_ok() {
|
if repl.is_ok() {
|
||||||
repl.unwrap().run();
|
repl.unwrap().run(Some(cmds));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
println!("{}", repl.err().unwrap());
|
println!("{}", repl.err().unwrap());
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ use command_parser::{
|
||||||
QUERY_COMMAND,
|
QUERY_COMMAND,
|
||||||
ALT_QUERY_COMMAND,
|
ALT_QUERY_COMMAND,
|
||||||
TRANSACT_COMMAND,
|
TRANSACT_COMMAND,
|
||||||
ALT_TRANSACT_COMMAND
|
ALT_TRANSACT_COMMAND,
|
||||||
};
|
};
|
||||||
use input::InputReader;
|
use input::InputReader;
|
||||||
use input::InputResult::{
|
use input::InputResult::{
|
||||||
|
@ -49,22 +49,29 @@ lazy_static! {
|
||||||
|
|
||||||
/// Executes input and maintains state of persistent items.
|
/// Executes input and maintains state of persistent items.
|
||||||
pub struct Repl {
|
pub struct Repl {
|
||||||
store: Store,
|
store: Store
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Repl {
|
impl Repl {
|
||||||
/// Constructs a new `Repl`.
|
/// Constructs a new `Repl`.
|
||||||
pub fn new(db_name: Option<String>) -> Result<Repl, String> {
|
pub fn new(db_name: Option<String>) -> Result<Repl, String> {
|
||||||
let store = try!(Store::new(db_name.clone()).map_err(|e| e.to_string()));
|
let store = try!(Store::new(db_name.clone()).map_err(|e| e.to_string()));
|
||||||
|
println!("Database {:?} opened", db_output_name(&db_name.unwrap_or("".to_string())));
|
||||||
Ok(Repl{
|
Ok(Repl{
|
||||||
store: store,
|
store: store,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs the REPL interactively.
|
/// Runs the REPL interactively.
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self, startup_commands: Option<Vec<Command>>) {
|
||||||
let mut input = InputReader::new();
|
let mut input = InputReader::new();
|
||||||
|
|
||||||
|
if let Some(cmds) = startup_commands {
|
||||||
|
for command in cmds.iter() {
|
||||||
|
self.handle_command(command.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let res = input.read_input();
|
let res = input.read_input();
|
||||||
|
|
||||||
|
@ -103,9 +110,8 @@ impl Repl {
|
||||||
Err(e) => println!("{}", e.to_string())
|
Err(e) => println!("{}", e.to_string())
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
Command::Query(query) => self.query_command(query),
|
Command::Query(query) => self.execute_query(query),
|
||||||
Command::Transact(transaction) => self.transact_command(transaction),
|
Command::Transact(transaction) => self.execute_transact(transaction),
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +135,7 @@ impl Repl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_command(&self, query: String) {
|
pub fn execute_query(&self, query: String) {
|
||||||
let results = match self.store.query(query){
|
let results = match self.store.query(query){
|
||||||
Result::Ok(vals) => {
|
Result::Ok(vals) => {
|
||||||
vals
|
vals
|
||||||
|
@ -169,7 +175,7 @@ impl Repl {
|
||||||
println!("\n{}", output);
|
println!("\n{}", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transact_command(&mut self, transaction: String) {
|
pub fn execute_transact(&mut self, transaction: String) {
|
||||||
match self.store.transact(transaction) {
|
match self.store.transact(transaction) {
|
||||||
Result::Ok(report) => println!("{:?}", report),
|
Result::Ok(report) => println!("{:?}", report),
|
||||||
Result::Err(err) => println!("{:?}.", err),
|
Result::Err(err) => println!("{:?}.", err),
|
||||||
|
|
Loading…
Reference in a new issue