Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into release-v0.13.1-to-…
Browse files Browse the repository at this point in the history
…main
  • Loading branch information
devgony committed Nov 8, 2022
2 parents f2c9f33 + 2a8e386 commit 2f14d28
Show file tree
Hide file tree
Showing 103 changed files with 2,673 additions and 1,245 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
--branch \
--ignore-not-existing \
--ignore "/*" \
--ignore "examples/*.rs" \
--ignore "pkg/rust/examples/*.rs" \
--ignore "cli/src/{cli,helper,lib,main}.rs" \
--excl-line "#\\[derive\\(" \
-o ./coverage/lcov.info
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ default-members = [
"test-suite",
"utils",
]

# ref. https://github.com/rustwasm/wasm-pack/issues/1111
# enable this only for gluesql-js build
# [profile.release]
# opt-level = "s"
68 changes: 17 additions & 51 deletions cli/src/print.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use {
crate::command::{SetOption, ShowOption},
gluesql_core::{
ast::ToSql,
prelude::{Payload, PayloadVariable},
},
gluesql_core::prelude::{Payload, PayloadVariable},
std::{
fmt::Display,
fs::File,
Expand Down Expand Up @@ -105,6 +102,14 @@ impl<'a, W: Write> Print<W> {
};

match payload {
Payload::Create => self.write("Table created")?,
Payload::DropTable => self.write("Table dropped")?,
Payload::AlterTable => self.write("Table altered")?,
Payload::CreateIndex => self.write("Index created")?,
Payload::DropIndex => self.write("Index dropped")?,
Payload::Commit => self.write("Commit completed")?,
Payload::Rollback => self.write("Rollback completed")?,
Payload::StartTransaction => self.write("Transaction started")?,
Payload::Insert(n) => affected(*n, "inserted")?,
Payload::Delete(n) => affected(*n, "deleted")?,
Payload::Update(n) => affected(*n, "updated")?,
Expand All @@ -125,18 +130,6 @@ impl<'a, W: Write> Print<W> {
let table = self.build_table(table);
self.write(table)?;
}
Payload::ShowIndexes(indexes) => {
let mut table = self.get_table(vec!["Index Name", "Order", "Description"]);
for index in indexes {
table.add_record([
index.name.to_owned(),
index.order.to_string(),
index.expr.to_sql(),
]);
}
let table = self.build_table(table);
self.write(table)?;
}
Payload::Select { labels, rows } => {
let PrintOption {
tabular,
Expand Down Expand Up @@ -179,7 +172,6 @@ impl<'a, W: Write> Print<W> {
}
}
}
_ => {}
};

Ok(())
Expand Down Expand Up @@ -262,10 +254,6 @@ mod tests {
use {
super::Print,
crate::command::{SetOption, ShowOption},
gluesql_core::{
ast::{BinaryOperator, Expr},
data::{SchemaIndex, SchemaIndexOrd},
},
};

#[test]
Expand Down Expand Up @@ -323,6 +311,14 @@ mod tests {
};
}

test!(&Payload::Create, "Table created");
test!(&Payload::DropTable, "Table dropped");
test!(&Payload::AlterTable, "Table altered");
test!(&Payload::CreateIndex, "Index created");
test!(&Payload::DropIndex, "Index dropped");
test!(&Payload::Commit, "Commit completed");
test!(&Payload::Rollback, "Rollback completed");
test!(&Payload::StartTransaction, "Transaction started");
test!(&Payload::Insert(0), "0 row inserted");
test!(&Payload::Insert(1), "1 row inserted");
test!(&Payload::Insert(7), "7 rows inserted");
Expand Down Expand Up @@ -426,36 +422,6 @@ mod tests {
| 5 | kim | TRUE |"
);

test!(
&Payload::ShowIndexes(vec![
SchemaIndex {
name: "id_ndx".to_owned(),
order: SchemaIndexOrd::Asc,
expr: Expr::Identifier("id".to_owned())
},
SchemaIndex {
name: "name_ndx".to_owned(),
order: SchemaIndexOrd::Desc,
expr: Expr::Identifier("name".to_owned())
},
SchemaIndex {
name: "expr_ndx".to_owned(),
order: SchemaIndexOrd::Both,
expr: Expr::BinaryOp {
left: Box::new(Expr::Identifier("expr1".to_owned())),
op: BinaryOperator::Minus,
right: Box::new(Expr::Identifier("expr2".to_owned()))
}
}
],),
"
| Index Name | Order | Description |
|------------|-------|---------------|
| id_ndx | ASC | id |
| name_ndx | DESC | name |
| expr_ndx | BOTH | expr1 - expr2 |"
);

test!(
&Payload::ShowColumns(vec![
("id".to_owned(), DataType::Int),
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum DataType {
Int,
Int128,
Uint8,
Uint16,
Float,
Text,
Bytea,
Expand Down
39 changes: 10 additions & 29 deletions core/src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ pub enum AlterTableOperation {
pub struct ColumnDef {
pub name: String,
pub data_type: DataType,
pub options: Vec<ColumnOptionDef>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ColumnOptionDef {
pub name: Option<String>,
pub option: ColumnOption,
pub options: Vec<ColumnOption>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -81,7 +75,7 @@ impl ToSql for ColumnDef {
{
let options = options
.iter()
.map(|ColumnOptionDef { option, .. }| option.to_sql())
.map(|option| option.to_sql())
.collect::<Vec<_>>()
.join(" ");

Expand All @@ -108,7 +102,7 @@ impl ToSql for ColumnOption {

#[cfg(test)]
mod tests {
use crate::ast::{AstLiteral, ColumnDef, ColumnOption, ColumnOptionDef, DataType, Expr, ToSql};
use crate::ast::{AstLiteral, ColumnDef, ColumnOption, DataType, Expr, ToSql};

#[test]
fn to_sql_column_def() {
Expand All @@ -117,10 +111,7 @@ mod tests {
ColumnDef {
name: "name".to_owned(),
data_type: DataType::Text,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: false }
}]
options: vec![ColumnOption::Unique { is_primary: false }]
}
.to_sql()
);
Expand All @@ -130,10 +121,7 @@ mod tests {
ColumnDef {
name: "accepted".to_owned(),
data_type: DataType::Boolean,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null
}]
options: vec![ColumnOption::Null]
}
.to_sql()
);
Expand All @@ -144,14 +132,8 @@ mod tests {
name: "id".to_owned(),
data_type: DataType::Int,
options: vec![
ColumnOptionDef {
name: None,
option: ColumnOption::NotNull
},
ColumnOptionDef {
name: None,
option: ColumnOption::Unique { is_primary: true }
}
ColumnOption::NotNull,
ColumnOption::Unique { is_primary: true }
]
}
.to_sql()
Expand All @@ -162,10 +144,9 @@ mod tests {
ColumnDef {
name: "accepted".to_owned(),
data_type: DataType::Boolean,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Default(Expr::Literal(AstLiteral::Boolean(false)))
}]
options: vec![ColumnOption::Default(Expr::Literal(AstLiteral::Boolean(
false
)))]
}
.to_sql()
);
Expand Down
25 changes: 25 additions & 0 deletions core/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub enum Function {
},
Ceil(Expr),
Concat(Vec<Expr>),
ConcatWs {
separator: Expr,
exprs: Vec<Expr>,
},
IfNull {
expr: Expr,
then: Expr,
Expand Down Expand Up @@ -180,6 +184,14 @@ impl ToSql for Function {
.join(", ");
format!("CONCAT({items})")
}
Function::ConcatWs { separator, exprs } => {
let exprs = exprs
.iter()
.map(ToSql::to_sql)
.collect::<Vec<_>>()
.join(", ");
format!("CONCAT_WS({}, {})", separator.to_sql(), exprs)
}
Function::IfNull { expr, then } => {
format!("IFNULL({}, {})", expr.to_sql(), then.to_sql())
}
Expand Down Expand Up @@ -462,6 +474,19 @@ mod tests {
.to_sql()
);

assert_eq!(
"CONCAT_WS(-, Tic, tac, toe)",
&Expr::Function(Box::new(Function::ConcatWs {
separator: Expr::Identifier("-".to_owned()),
exprs: vec![
Expr::Identifier("Tic".to_owned()),
Expr::Identifier("tac".to_owned()),
Expr::Identifier("toe".to_owned())
]
}))
.to_sql()
);

assert_eq!(
"IFNULL(updated_at, created_at)",
&Expr::Function(Box::new(Function::IfNull {
Expand Down
20 changes: 7 additions & 13 deletions core/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ mod tests {

use {
crate::ast::{
Assignment, AstLiteral, BinaryOperator, ColumnDef, ColumnOption, ColumnOptionDef,
DataType, Expr, Query, Select, SelectItem, SetExpr, Statement, TableFactor,
TableWithJoins, ToSql, Values, Variable,
Assignment, AstLiteral, BinaryOperator, ColumnDef, ColumnOption, DataType, Expr, Query,
Select, SelectItem, SetExpr, Statement, TableFactor, TableWithJoins, ToSql, Values,
Variable,
},
bigdecimal::BigDecimal,
std::str::FromStr,
Expand Down Expand Up @@ -374,10 +374,7 @@ mod tests {
ColumnDef {
name: "num".to_owned(),
data_type: DataType::Int,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null
}]
options: vec![ColumnOption::Null]
},
ColumnDef {
name: "name".to_owned(),
Expand Down Expand Up @@ -461,12 +458,9 @@ mod tests {
column_def: ColumnDef {
name: "amount".to_owned(),
data_type: DataType::Int,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Default(Expr::Literal(AstLiteral::Number(
BigDecimal::from_str("10").unwrap()
)))
}]
options: vec![ColumnOption::Default(Expr::Literal(AstLiteral::Number(
BigDecimal::from_str("10").unwrap()
)))]
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum Dictionary {
GlueTables,
GlueTableColumns,
GlueIndexes,
GlueObjects,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
10 changes: 5 additions & 5 deletions core/src/ast_builder/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ use crate::{
};

#[derive(Clone)]
pub enum AssignmentNode {
Expr(String, ExprNode),
pub enum AssignmentNode<'a> {
Expr(String, ExprNode<'a>),
Text(String),
}

impl From<&str> for AssignmentNode {
impl<'a> From<&str> for AssignmentNode<'a> {
fn from(expr: &str) -> Self {
Self::Text(expr.to_owned())
}
}

impl TryFrom<AssignmentNode> for Assignment {
impl<'a> TryFrom<AssignmentNode<'a>> for Assignment {
type Error = Error;

fn try_from(node: AssignmentNode) -> Result<Self> {
fn try_from(node: AssignmentNode<'a>) -> Result<Self> {
match node {
AssignmentNode::Text(expr) => {
let expr = parse_assignment(expr)
Expand Down
10 changes: 5 additions & 5 deletions core/src/ast_builder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ use {
};

#[derive(Clone)]
pub struct DeleteNode {
pub struct DeleteNode<'a> {
table_name: String,
filter_expr: Option<ExprNode>,
filter_expr: Option<ExprNode<'a>>,
}

impl DeleteNode {
impl<'a> DeleteNode<'a> {
pub fn new(table_name: String) -> Self {
Self {
table_name,
filter_expr: None,
}
}

pub fn filter<T: Into<ExprNode>>(mut self, expr: T) -> Self {
pub fn filter<T: Into<ExprNode<'a>>>(mut self, expr: T) -> Self {
self.filter_expr = Some(expr.into());

self
}
}

impl Build for DeleteNode {
impl<'a> Build for DeleteNode<'a> {
fn build(self) -> Result<Statement> {
let table_name = self.table_name;
let selection = self.filter_expr.map(Expr::try_from).transpose()?;
Expand Down

0 comments on commit 2f14d28

Please sign in to comment.