Database commands (#5343)

* dabase access commands

* select expression

* select using expressions

* cargo fmt
This commit is contained in:
Fernando Herrera
2022-04-27 11:52:31 +01:00
committed by GitHub
parent cd5199de31
commit 5c9fe85ec4
26 changed files with 745 additions and 154 deletions

View File

@ -6,19 +6,23 @@ mod open;
mod query;
mod schema;
mod select;
mod utils;
// Temporal module to create Query objects
mod testing;
use testing::TestingDb;
use nu_protocol::engine::StateWorkingSet;
use collect::CollectDb;
use command::Database;
use describe::DescribeDb;
use from::FromDb;
use nu_protocol::engine::StateWorkingSet;
use open::OpenDb;
use query::QueryDb;
use schema::SchemaDb;
use select::SelectDb;
use select::ProjectionDb;
pub fn add_database_decls(working_set: &mut StateWorkingSet) {
pub fn add_commands_decls(working_set: &mut StateWorkingSet) {
macro_rules! bind_command {
( $command:expr ) => {
working_set.add_decl(Box::new($command));
@ -29,5 +33,15 @@ pub fn add_database_decls(working_set: &mut StateWorkingSet) {
}
// Series commands
bind_command!(CollectDb, Database, DescribeDb, FromDb, QueryDb, SelectDb, OpenDb, SchemaDb);
bind_command!(
CollectDb,
Database,
DescribeDb,
FromDb,
QueryDb,
ProjectionDb,
OpenDb,
SchemaDb,
TestingDb
);
}

View File

@ -1,5 +1,5 @@
use super::super::SQLiteDatabase;
use crate::database::values::db_row::DbRow;
use crate::database::values::definitions::db_row::DbRow;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},

View File

@ -1,16 +1,16 @@
use super::{super::SQLiteDatabase, utils::extract_strings};
use super::{super::values::dsl::SelectDb, super::SQLiteDatabase};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
};
use sqlparser::ast::{Expr, Ident, Query, Select, SelectItem, SetExpr};
use sqlparser::ast::{Query, Select, SelectItem, SetExpr};
#[derive(Clone)]
pub struct SelectDb;
pub struct ProjectionDb;
impl Command for SelectDb {
impl Command for ProjectionDb {
fn name(&self) -> &str {
"db select"
}
@ -21,7 +21,7 @@ impl Command for SelectDb {
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
.rest(
"select",
SyntaxShape::Any,
"Select expression(s) on the table",
@ -42,7 +42,7 @@ impl Command for SelectDb {
},
Example {
description: "selects columns from a database",
example: "db open db.mysql | db select [a, b, c]",
example: "db open db.mysql | db select a b c",
result: None,
},
]
@ -55,20 +55,24 @@ impl Command for SelectDb {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value: Value = call.req(engine_state, stack, 0)?;
let expressions = extract_strings(value)?;
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
let value = Value::List {
vals,
span: call.head,
};
let projection = SelectDb::extract_selects(value)?;
let mut db = SQLiteDatabase::try_from_pipeline(input, call.head)?;
db.query = match db.query {
None => Some(create_query(expressions)),
Some(query) => Some(modify_query(query, expressions)),
None => Some(create_query(projection)),
Some(query) => Some(modify_query(query, projection)),
};
Ok(db.into_value(call.head).into_pipeline_data())
}
}
fn create_query(expressions: Vec<String>) -> Query {
fn create_query(expressions: Vec<SelectItem>) -> Query {
Query {
with: None,
body: SetExpr::Select(Box::new(create_select(expressions))),
@ -80,7 +84,7 @@ fn create_query(expressions: Vec<String>) -> Query {
}
}
fn modify_query(mut query: Query, expressions: Vec<String>) -> Query {
fn modify_query(mut query: Query, expressions: Vec<SelectItem>) -> Query {
query.body = match query.body {
SetExpr::Select(select) => SetExpr::Select(Box::new(modify_select(select, expressions))),
_ => SetExpr::Select(Box::new(create_select(expressions))),
@ -89,18 +93,18 @@ fn modify_query(mut query: Query, expressions: Vec<String>) -> Query {
query
}
fn modify_select(select: Box<Select>, expressions: Vec<String>) -> Select {
fn modify_select(select: Box<Select>, projection: Vec<SelectItem>) -> Select {
Select {
projection: create_projection(expressions),
projection,
..select.as_ref().clone()
}
}
fn create_select(expressions: Vec<String>) -> Select {
fn create_select(projection: Vec<SelectItem>) -> Select {
Select {
distinct: false,
top: None,
projection: create_projection(expressions),
projection,
into: None,
from: Vec::new(),
lateral_views: Vec::new(),
@ -112,20 +116,3 @@ fn create_select(expressions: Vec<String>) -> Select {
having: None,
}
}
// This function needs more work
// It needs to define alias and functions in the columns
// I assume we will need to define expressions for the columns instead of strings
fn create_projection(expressions: Vec<String>) -> Vec<SelectItem> {
expressions
.into_iter()
.map(|expression| {
let expr = Expr::Identifier(Ident {
value: expression,
quote_style: None,
});
SelectItem::UnnamedExpr(expr)
})
.collect()
}

View File

@ -0,0 +1,76 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Value,
};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
#[derive(Clone)]
pub struct TestingDb;
impl Command for TestingDb {
fn name(&self) -> &str {
"db testing"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"query",
SyntaxShape::String,
"SQL to execute to create the query object",
)
.category(Category::Custom("database".into()))
}
fn usage(&self) -> &str {
"Create query object"
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "",
example: "",
result: None,
}]
}
fn search_terms(&self) -> Vec<&str> {
vec!["database", "SQLite"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let sql: Spanned<String> = call.req(engine_state, stack, 0)?;
let dialect = GenericDialect {}; // or AnsiDialect, or your own dialect ...
let ast = Parser::parse_sql(&dialect, sql.item.as_str()).map_err(|e| {
ShellError::GenericError(
"Error creating AST".into(),
e.to_string(),
Some(sql.span),
None,
Vec::new(),
)
})?;
let value = match ast.get(0) {
None => Value::nothing(call.head),
Some(statement) => Value::String {
val: format!("{:#?}", statement),
span: call.head,
},
};
Ok(value.into_pipeline_data())
}
}

View File

@ -1,15 +0,0 @@
use nu_protocol::{FromValue, ShellError, Value};
pub fn extract_strings(value: Value) -> Result<Vec<String>, ShellError> {
match (
<String as FromValue>::from_value(&value),
<Vec<String> as FromValue>::from_value(&value),
) {
(Ok(col), Err(_)) => Ok(vec![col]),
(Err(_), Ok(cols)) => Ok(cols),
_ => Err(ShellError::IncompatibleParametersSingle(
"Expected a string or list of strings".into(),
value.span()?,
)),
}
}