Skip to content

Commit

Permalink
Replace TryFrom for AstLiteral => TryFrom for Expr, add more test…
Browse files Browse the repository at this point in the history
… to dump (#990)

Replace TryFrom for AstLiteral => TryFrom for Expr
Add more data types to E2E test case (except F64 and Decimal due to floating point issue)
Replace double to single quote at interval
  • Loading branch information
devgony authored Nov 8, 2022
1 parent 427b827 commit 9cf5593
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 192 deletions.
4 changes: 2 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
clap::Parser,
futures::executor::block_on,
gluesql_core::{
ast::{AstLiteral, Expr, SetExpr, Statement, ToSql, Values},
ast::{Expr, SetExpr, Statement, ToSql, Values},
prelude::Row,
store::Transaction,
store::{GStore, GStoreMut, Store},
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn dump_database(storage: SledStorage, dump_path: PathBuf) -> Result<SledSto
result.map(|Row(values)| {
values
.into_iter()
.map(|value| Ok(Expr::Literal(AstLiteral::try_from(value)?)))
.map(|value| Ok(Expr::try_from(value)?))
.collect::<Result<Vec<_>>>()
})?
})
Expand Down
44 changes: 40 additions & 4 deletions cli/tests/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,52 @@ async fn dump_and_import() {
let mut source_glue = Glue::new(source_storage);

let sqls = vec![
"CREATE TABLE User (id INT, name TEXT);",
"CREATE INDEX User_id ON User (id);",
"INSERT INTO User SELECT N, 'a' FROM SERIES(101);",
"CREATE TABLE Foo (
boolean BOOLEAN,
int8 INT8,
int16 INT16,
int32 INT32,
int INT,
int128 INT128,
uinti8 UINT8,
text TEXT,
bytea BYTEA,
date DATE,
timestamp TIMESTAMP,
time TIME,
interval INTERVAL,
uuid UUID,
map MAP,
list LIST,
);",
r#"INSERT INTO Foo
VALUES (
true,
1,
2,
3,
4,
5,
6,
'a',
X'123456',
DATE '2022-11-01',
TIMESTAMP '2022-11-02',
TIME '23:59:59',
INTERVAL '1' DAY,
'550e8400-e29b-41d4-a716-446655440000',
'{"a": {"red": "apple", "blue": 1}, "b": 10}',
'[{ "foo": 100, "bar": [true, 0, [10.5, false] ] }, 10, 20]'
);"#,
"CREATE INDEX Foo_int ON Foo (int);",
"CREATE TABLE Bar AS SELECT N FROM SERIES(101);",
];

for sql in sqls {
source_glue.execute(sql).unwrap();
}

let sql = "SELECT * FROM User;";
let sql = "SELECT * FROM Foo JOIN Bar;";
let source_data = source_glue.execute(sql).unwrap();

let source_storage = dump_database(source_glue.storage.unwrap(), dump_path.clone()).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions core/src/data/interval/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl From<&Interval> for String {
let month = v % 12;

match (year, month) {
(_, 0) if year != 0 => format!(r#""{}{}" YEAR"#, sign, year),
(0, _) => format!(r#""{}{}" MONTH"#, sign, month),
_ => format!(r#""{}{}-{}" YEAR TO MONTH"#, sign, year, month),
(_, 0) if year != 0 => format!("'{}{}' YEAR", sign, year),
(0, _) => format!("'{}{}' MONTH", sign, month),
_ => format!("'{}{}-{}' YEAR TO MONTH", sign, year, month),
}
}
Interval::Microsecond(v) => {
Expand All @@ -67,7 +67,7 @@ impl From<&Interval> for String {

macro_rules! f {
($template: literal; $( $value: expr )*; $from_to: literal) => {
format!(r#""{}{}" {}"#, sign, format!($template, $( $value ),*), $from_to)
format!("'{}{}' {}", sign, format!($template, $( $value ),*), $from_to)
};

(DAY $template: literal) => {
Expand Down Expand Up @@ -162,15 +162,15 @@ mod tests {
macro_rules! test {
($( $value: literal $duration: ident ),* => $result: literal $from_to: tt) => {
let interval = interval!($( $value $duration ),*);
let interval_str = format!(r#""{}" {}"#, $result, stringify!($from_to));
let interval_str = format!("'{}' {}", $result, stringify!($from_to));

assert_eq!(Ok(interval), Interval::try_from(interval_str.as_str()));
assert_eq!(String::from(interval), interval_str);
};
($( $value: literal $duration: ident ),* => $result: literal $from: tt TO $to: tt) => {
let interval = interval!($( $value $duration ),*);
let interval_str = format!(
r#""{}" {} TO {}"#,
"'{}' {} TO {}",
$result,
stringify!($from),
stringify!($to),
Expand Down
176 changes: 0 additions & 176 deletions core/src/data/value/ast_literal.rs

This file was deleted.

4 changes: 2 additions & 2 deletions core/src/data/value/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ pub enum ValueError {
#[error("unsupported value by position function: from_str(from_str:?), sub_str(sub_str:?)")]
UnSupportedValueByPositionFunction { from_str: Value, sub_str: Value },

#[error("failed to convert Value to AstLiteral")]
ValueToAstLiteralConversionFailure,
#[error("failed to convert Value to Expr")]
ValueToExprConversionFailure,
}

#[derive(Debug, PartialEq, Eq, Serialize, Display)]
Expand Down
Loading

0 comments on commit 9cf5593

Please sign in to comment.