forked from extern/nushell
Database commands (#5417)
* dabase access commands * select expression * select using expressions * cargo fmt * alias for database * database where command * expression operations * and and or operators * limit and sort by commands
This commit is contained in:
143
crates/nu-command/src/database/commands/and.rs
Normal file
143
crates/nu-command/src/database/commands/and.rs
Normal file
@ -0,0 +1,143 @@
|
||||
use crate::database::values::dsl::ExprDb;
|
||||
|
||||
use super::super::SQLiteDatabase;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Value,
|
||||
};
|
||||
use sqlparser::ast::{BinaryOperator, Expr, Query, Select, SetExpr};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AndDb;
|
||||
|
||||
impl Command for AndDb {
|
||||
fn name(&self) -> &str {
|
||||
"db and"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Includes an AND clause for a query or expression"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.required("where", SyntaxShape::Any, "Where expression on the table")
|
||||
.category(Category::Custom("database".into()))
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["database", "where"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "selects a column from a database with a where clause",
|
||||
example: r#"db open db.mysql
|
||||
| db select a
|
||||
| db from table_1
|
||||
| db where ((db col a) > 1)
|
||||
| db and ((db col b) == 1)
|
||||
| db describe"#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Creates a nested where clause",
|
||||
example: r#"db open db.mysql
|
||||
| db select a
|
||||
| db from table_1
|
||||
| db where ((db col a) > 1 | db and ((db col a) < 10))
|
||||
| db describe"#,
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let value: Value = call.req(engine_state, stack, 0)?;
|
||||
let expr = ExprDb::try_from_value(&value)?.into_native();
|
||||
|
||||
let value = input.into_value(call.head);
|
||||
if let Ok(expression) = ExprDb::try_from_value(&value) {
|
||||
let expression = Expr::BinaryOp {
|
||||
left: Box::new(expression.into_native()),
|
||||
op: BinaryOperator::And,
|
||||
right: Box::new(expr),
|
||||
};
|
||||
|
||||
let expression: ExprDb = Expr::Nested(Box::new(expression)).into();
|
||||
|
||||
Ok(expression.into_value(call.head).into_pipeline_data())
|
||||
} else if let Ok(mut db) = SQLiteDatabase::try_from_value(value.clone()) {
|
||||
db.query = match db.query {
|
||||
Some(query) => Some(modify_query(query, expr, call.head)?),
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Connection without query".into(),
|
||||
"Missing query in the connection".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(db.into_value(call.head).into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::CantConvert(
|
||||
"expression or query".into(),
|
||||
value.get_type().to_string(),
|
||||
value.span()?,
|
||||
None,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn modify_query(mut query: Query, expression: Expr, span: Span) -> Result<Query, ShellError> {
|
||||
query.body = match query.body {
|
||||
SetExpr::Select(select) => Ok(SetExpr::Select(modify_select(select, expression, span)?)),
|
||||
_ => Err(ShellError::GenericError(
|
||||
"Query without a select".into(),
|
||||
"Missing a WHERE clause before an AND clause".into(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
Ok(query)
|
||||
}
|
||||
|
||||
fn modify_select(
|
||||
mut select: Box<Select>,
|
||||
expression: Expr,
|
||||
span: Span,
|
||||
) -> Result<Box<Select>, ShellError> {
|
||||
let new_expression = match &select.selection {
|
||||
Some(expr) => Ok(Expr::BinaryOp {
|
||||
left: Box::new(expr.clone()),
|
||||
op: BinaryOperator::And,
|
||||
right: Box::new(expression),
|
||||
}),
|
||||
None => Err(ShellError::GenericError(
|
||||
"Query without a select".into(),
|
||||
"Missing a WHERE clause before an AND clause".into(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
select.as_mut().selection = Some(new_expression);
|
||||
Ok(select)
|
||||
}
|
@ -74,18 +74,16 @@ fn create_query(table: String) -> Query {
|
||||
|
||||
fn modify_query(mut query: Query, table: String) -> Query {
|
||||
query.body = match query.body {
|
||||
SetExpr::Select(select) => SetExpr::Select(Box::new(modify_select(select, table))),
|
||||
SetExpr::Select(select) => SetExpr::Select(modify_select(select, table)),
|
||||
_ => SetExpr::Select(Box::new(create_select(table))),
|
||||
};
|
||||
|
||||
query
|
||||
}
|
||||
|
||||
fn modify_select(select: Box<Select>, table: String) -> Select {
|
||||
Select {
|
||||
from: create_from(table),
|
||||
..select.as_ref().clone()
|
||||
}
|
||||
fn modify_select(mut select: Box<Select>, table: String) -> Box<Select> {
|
||||
select.as_mut().from = create_from(table);
|
||||
select
|
||||
}
|
||||
|
||||
fn create_select(table: String) -> Select {
|
||||
|
77
crates/nu-command/src/database/commands/limit.rs
Normal file
77
crates/nu-command/src/database/commands/limit.rs
Normal file
@ -0,0 +1,77 @@
|
||||
use super::super::SQLiteDatabase;
|
||||
use crate::database::values::dsl::ExprDb;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LimitDb;
|
||||
|
||||
impl Command for LimitDb {
|
||||
fn name(&self) -> &str {
|
||||
"db limit"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Limit result from query"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.required(
|
||||
"limit",
|
||||
SyntaxShape::Int,
|
||||
"Number of rows to extract for query",
|
||||
)
|
||||
.category(Category::Custom("database".into()))
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["database", "limit"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Limits selection from table",
|
||||
example: r#"db open db.mysql
|
||||
| db from table_a
|
||||
| db select a
|
||||
| db limit 10
|
||||
| db describe"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let limit: Value = call.req(engine_state, stack, 0)?;
|
||||
let expr = ExprDb::try_from_value(&limit)?.into_native();
|
||||
|
||||
let mut db = SQLiteDatabase::try_from_pipeline(input, call.head)?;
|
||||
db.query = match db.query {
|
||||
Some(mut query) => {
|
||||
query.limit = Some(expr);
|
||||
Some(query)
|
||||
}
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Connection without query".into(),
|
||||
"The connection needs a query defined".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(db.into_value(call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
mod and;
|
||||
mod collect;
|
||||
mod command;
|
||||
mod describe;
|
||||
mod from;
|
||||
mod limit;
|
||||
mod open;
|
||||
mod or;
|
||||
mod order_by;
|
||||
mod query;
|
||||
mod schema;
|
||||
mod select;
|
||||
mod where_;
|
||||
|
||||
// Temporal module to create Query objects
|
||||
mod testing;
|
||||
@ -13,14 +18,19 @@ use testing::TestingDb;
|
||||
|
||||
use nu_protocol::engine::StateWorkingSet;
|
||||
|
||||
use and::AndDb;
|
||||
use collect::CollectDb;
|
||||
use command::Database;
|
||||
use describe::DescribeDb;
|
||||
use from::FromDb;
|
||||
use limit::LimitDb;
|
||||
use open::OpenDb;
|
||||
use or::OrDb;
|
||||
use order_by::OrderByDb;
|
||||
use query::QueryDb;
|
||||
use schema::SchemaDb;
|
||||
use select::ProjectionDb;
|
||||
use where_::WhereDb;
|
||||
|
||||
pub fn add_commands_decls(working_set: &mut StateWorkingSet) {
|
||||
macro_rules! bind_command {
|
||||
@ -34,14 +44,19 @@ pub fn add_commands_decls(working_set: &mut StateWorkingSet) {
|
||||
|
||||
// Series commands
|
||||
bind_command!(
|
||||
AndDb,
|
||||
CollectDb,
|
||||
Database,
|
||||
DescribeDb,
|
||||
FromDb,
|
||||
QueryDb,
|
||||
LimitDb,
|
||||
ProjectionDb,
|
||||
OpenDb,
|
||||
OrderByDb,
|
||||
OrDb,
|
||||
SchemaDb,
|
||||
TestingDb
|
||||
TestingDb,
|
||||
WhereDb
|
||||
);
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ impl Command for OpenDb {
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "",
|
||||
example: r#"""#,
|
||||
description: "Open a sqlite file",
|
||||
example: r#"db open file.sqlite"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
143
crates/nu-command/src/database/commands/or.rs
Normal file
143
crates/nu-command/src/database/commands/or.rs
Normal file
@ -0,0 +1,143 @@
|
||||
use crate::database::values::dsl::ExprDb;
|
||||
|
||||
use super::super::SQLiteDatabase;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Value,
|
||||
};
|
||||
use sqlparser::ast::{BinaryOperator, Expr, Query, Select, SetExpr};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OrDb;
|
||||
|
||||
impl Command for OrDb {
|
||||
fn name(&self) -> &str {
|
||||
"db or"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Includes an OR clause for a query or expression"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.required("where", SyntaxShape::Any, "Where expression on the table")
|
||||
.category(Category::Custom("database".into()))
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["database", "where"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "selects a column from a database with a where clause",
|
||||
example: r#"db open db.mysql
|
||||
| db select a
|
||||
| db from table_1
|
||||
| db where ((db col a) > 1)
|
||||
| db or ((db col b) == 1)
|
||||
| db describe"#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Creates a nested where clause",
|
||||
example: r#"db open db.mysql
|
||||
| db select a
|
||||
| db from table_1
|
||||
| db where ((db col a) > 1 | db or ((db col a) < 10))
|
||||
| db describe"#,
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let value: Value = call.req(engine_state, stack, 0)?;
|
||||
let expr = ExprDb::try_from_value(&value)?.into_native();
|
||||
|
||||
let value = input.into_value(call.head);
|
||||
if let Ok(expression) = ExprDb::try_from_value(&value) {
|
||||
let expression = Expr::BinaryOp {
|
||||
left: Box::new(expression.into_native()),
|
||||
op: BinaryOperator::Or,
|
||||
right: Box::new(expr),
|
||||
};
|
||||
|
||||
let expression: ExprDb = Expr::Nested(Box::new(expression)).into();
|
||||
|
||||
Ok(expression.into_value(call.head).into_pipeline_data())
|
||||
} else if let Ok(mut db) = SQLiteDatabase::try_from_value(value.clone()) {
|
||||
db.query = match db.query {
|
||||
Some(query) => Some(modify_query(query, expr, call.head)?),
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Connection without query".into(),
|
||||
"Missing query in the connection".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(db.into_value(call.head).into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::CantConvert(
|
||||
"expression or query".into(),
|
||||
value.get_type().to_string(),
|
||||
value.span()?,
|
||||
None,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn modify_query(mut query: Query, expression: Expr, span: Span) -> Result<Query, ShellError> {
|
||||
query.body = match query.body {
|
||||
SetExpr::Select(select) => Ok(SetExpr::Select(modify_select(select, expression, span)?)),
|
||||
_ => Err(ShellError::GenericError(
|
||||
"Query without a select".into(),
|
||||
"Missing a WHERE clause before an OR clause".into(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
Ok(query)
|
||||
}
|
||||
|
||||
fn modify_select(
|
||||
mut select: Box<Select>,
|
||||
expression: Expr,
|
||||
span: Span,
|
||||
) -> Result<Box<Select>, ShellError> {
|
||||
let new_expression = match &select.selection {
|
||||
Some(expr) => Ok(Expr::BinaryOp {
|
||||
left: Box::new(expr.clone()),
|
||||
op: BinaryOperator::Or,
|
||||
right: Box::new(expression),
|
||||
}),
|
||||
None => Err(ShellError::GenericError(
|
||||
"Query without a select".into(),
|
||||
"Missing a WHERE clause before an OR clause".into(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
select.as_mut().selection = Some(new_expression);
|
||||
Ok(select)
|
||||
}
|
97
crates/nu-command/src/database/commands/order_by.rs
Normal file
97
crates/nu-command/src/database/commands/order_by.rs
Normal file
@ -0,0 +1,97 @@
|
||||
use crate::database::values::dsl::ExprDb;
|
||||
|
||||
use super::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::OrderByExpr;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OrderByDb;
|
||||
|
||||
impl Command for OrderByDb {
|
||||
fn name(&self) -> &str {
|
||||
"db order-by"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Orders by query"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.switch("ascending", "Order by ascending values", Some('a'))
|
||||
.switch("nulls_first", "Show nulls first in order", Some('n'))
|
||||
.rest(
|
||||
"select",
|
||||
SyntaxShape::Any,
|
||||
"Select expression(s) on the table",
|
||||
)
|
||||
.category(Category::Custom("database".into()))
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["database", "select"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "orders query by a column",
|
||||
example: r#"db open db.mysql
|
||||
| db from table_a
|
||||
| db select a
|
||||
| db order-by a
|
||||
| db describe"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let asc = call.has_flag("ascending");
|
||||
let nulls_first = call.has_flag("nulls_first");
|
||||
|
||||
let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
||||
let value = Value::List {
|
||||
vals,
|
||||
span: call.head,
|
||||
};
|
||||
let expressions = ExprDb::extract_exprs(value)?;
|
||||
|
||||
let mut db = SQLiteDatabase::try_from_pipeline(input, call.head)?;
|
||||
db.query = match db.query {
|
||||
Some(mut query) => {
|
||||
let mut order_expr: Vec<OrderByExpr> = expressions
|
||||
.into_iter()
|
||||
.map(|expr| OrderByExpr {
|
||||
expr,
|
||||
asc: if asc { Some(asc) } else { None },
|
||||
nulls_first: if nulls_first { Some(nulls_first) } else { None },
|
||||
})
|
||||
.collect();
|
||||
|
||||
query.order_by.append(&mut order_expr);
|
||||
Some(query)
|
||||
}
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Connection without query".into(),
|
||||
"The connection needs a query defined".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(db.into_value(call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
@ -37,12 +37,12 @@ impl Command for ProjectionDb {
|
||||
vec![
|
||||
Example {
|
||||
description: "selects a column from a database",
|
||||
example: "db open db.mysql | db select a",
|
||||
example: "db open db.mysql | db select a | db describe",
|
||||
result: None,
|
||||
},
|
||||
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 | db describe",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
@ -86,18 +86,16 @@ fn create_query(expressions: Vec<SelectItem>) -> 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(select) => SetExpr::Select(modify_select(select, expressions)),
|
||||
_ => SetExpr::Select(Box::new(create_select(expressions))),
|
||||
};
|
||||
|
||||
query
|
||||
}
|
||||
|
||||
fn modify_select(select: Box<Select>, projection: Vec<SelectItem>) -> Select {
|
||||
Select {
|
||||
projection,
|
||||
..select.as_ref().clone()
|
||||
}
|
||||
fn modify_select(mut select: Box<Select>, projection: Vec<SelectItem>) -> Box<Select> {
|
||||
select.as_mut().projection = projection;
|
||||
select
|
||||
}
|
||||
|
||||
fn create_select(projection: Vec<SelectItem>) -> Select {
|
||||
|
103
crates/nu-command/src/database/commands/where_.rs
Normal file
103
crates/nu-command/src/database/commands/where_.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use crate::database::values::dsl::ExprDb;
|
||||
|
||||
use super::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, Query, Select, SetExpr};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WhereDb;
|
||||
|
||||
impl Command for WhereDb {
|
||||
fn name(&self) -> &str {
|
||||
"db where"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Includes a where statement for a query"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name())
|
||||
.required("where", SyntaxShape::Any, "Where expression on the table")
|
||||
.category(Category::Custom("database".into()))
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["database", "where"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "selects a column from a database with a where clause",
|
||||
example: r#"db open db.mysql
|
||||
| db select a
|
||||
| db from table_1
|
||||
| db where ((db col a) > 1)
|
||||
| db describe"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let value: Value = call.req(engine_state, stack, 0)?;
|
||||
let expr = ExprDb::try_from_value(&value)?.into_native();
|
||||
|
||||
let mut db = SQLiteDatabase::try_from_pipeline(input, call.head)?;
|
||||
db.query = match db.query {
|
||||
Some(query) => Some(modify_query(query, expr)),
|
||||
None => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Connection without query".into(),
|
||||
"The connection needs a query defined".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(db.into_value(call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
fn modify_query(mut query: Query, expression: Expr) -> Query {
|
||||
query.body = match query.body {
|
||||
SetExpr::Select(select) => SetExpr::Select(modify_select(select, expression)),
|
||||
_ => SetExpr::Select(Box::new(create_select(expression))),
|
||||
};
|
||||
|
||||
query
|
||||
}
|
||||
|
||||
fn modify_select(mut select: Box<Select>, expression: Expr) -> Box<Select> {
|
||||
select.as_mut().selection = Some(expression);
|
||||
select
|
||||
}
|
||||
|
||||
fn create_select(expression: Expr) -> Select {
|
||||
Select {
|
||||
distinct: false,
|
||||
top: None,
|
||||
into: None,
|
||||
projection: Vec::new(),
|
||||
from: Vec::new(),
|
||||
lateral_views: Vec::new(),
|
||||
selection: Some(expression),
|
||||
group_by: Vec::new(),
|
||||
cluster_by: Vec::new(),
|
||||
distribute_by: Vec::new(),
|
||||
sort_by: Vec::new(),
|
||||
having: None,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user