Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Remember previously used NS and DB #35 #1311

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/cli/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ use reqwest::header::ACCEPT;
use rustyline::error::ReadlineError;
use rustyline::Editor;
use serde_json::Value;
use surrealdb::sql::parse;
use surrealdb::sql::statements::UseStatement;
use surrealdb::sql::Statement;

struct SqlContext {
ns: Option<String>,
db: Option<String>,
}

impl SqlContext {
fn using(&mut self, statement: &UseStatement) {
match statement.ns.clone() {
Some(ns) => self.ns = Some(ns),
None => {}
};
match statement.db.clone() {
Some(db) => self.db = Some(db),
None => {}
};
}
}

pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Set the default logging level
Expand All @@ -13,8 +34,10 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
let user = matches.value_of("user").unwrap();
let pass = matches.value_of("pass").unwrap();
let conn = matches.value_of("conn").unwrap();
let ns = matches.value_of("ns");
let db = matches.value_of("db");
let mut ctx = SqlContext {
ns: matches.value_of("ns").map(|s| s.to_string()),
db: matches.value_of("db").map(|s| s.to_string()),
};

// If we should pretty-print responses
let pretty = matches.is_present("pretty");
Expand Down Expand Up @@ -44,20 +67,33 @@ pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
.header(ACCEPT, "application/json")
.basic_auth(user, Some(pass));
// Add NS header if specified
let res = match ns {
let res = match ctx.ns.clone() {
Some(ns) => res.header("NS", ns),
None => res,
};
// Add DB header if specified
let res = match db {
let res = match ctx.db.clone() {
Some(db) => res.header("DB", db),
None => res,
};
// Compile the request for later
let ast = parse(line.as_str());
// Complete request
let res = res.body(line).send();
// Get the request response
match process(pretty, res) {
Ok(v) => println!("{}", v),
Ok(v) => {
println!("{}", v);
if let Ok(ok_ast) = ast {
for stmt in ok_ast.iter() {
// If there's a use statement, update the context
match stmt {
Statement::Use(using) => ctx.using(using),
_ => {}
}
}
};
}
Err(e) => eprintln!("{}", e),
}
}
Expand Down