Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions src/language/parser/conditional.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;

use crate::{
language::{
parser::helpers::{
Expand Down Expand Up @@ -435,6 +436,58 @@ fn recurse_down(
}

impl ConditionNode {
/// Reconstructs conditional literal from provided tokens
/// and bounds.
fn reconstruct_literal (
tokens: &Vec<Token>,
start_idx: usize,
end_idx: usize,
) -> String {
// Reconstruct literal
let mut i: usize = start_idx;
let mut literal = String::new();

while i < end_idx {
match tokens[i].token_type {
TokenType::OpenParen | TokenType::CloseParen => {
literal.push_str(&tokens[i].lexeme);
i += 1;
}
TokenType::And | TokenType::Or => {
literal.push_str(&format!(" {} ", &tokens[i].lexeme));
i += 1;
}
TokenType::Identifier => {
let mut j = i;
let mut parts = Vec::new();

while parts.len() < 3
{
parts.push(if tokens[j].token_type == TokenType::Equal {
"=".to_string()
} else {
tokens[j].lexeme.clone()
}
);
j += 1;
}

if !parts.is_empty() {
literal.push_str(&parts.join(" "));
}

i = j;
}
_ => {
literal.push_str(&tokens[i].lexeme);
i += 1;
}
}
}

literal
}

/// Takes current node type and given the current location in the
/// query defined by the borrowed index, makes an attempt to parse
/// this node and associated subnodes for the Abstract Syntax Tree.
Expand Down Expand Up @@ -482,20 +535,12 @@ impl ConditionNode {
return Err("Conditional had unclosed parentheses".to_string());
}


return Ok(
ConditionNode {
_condition: ret,
_depth: depth,
_literal: {
tokens[start_idx..*idx].iter()
.map(|v| if v.token_type == TokenType::Equal {
"="
} else {
v.lexeme.as_str()
})
.collect::<Vec<&str>>()
.join(" ")
}
_literal: ConditionNode::reconstruct_literal(tokens, start_idx, *idx)
}
)
}
Expand Down
9 changes: 2 additions & 7 deletions src/language/parser/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,7 @@ impl FilterNode {
Some(FilterNode {
condition: condition_node,

_literal: {
tokens[start_idx..*idx].iter()
.map(|v| v.lexeme.as_str())
.collect::<Vec<&str>>()
.join(" ")
},
_literal: tokens[start_idx].lexeme.clone(),
_depth: depth
}));
}
Expand All @@ -408,7 +403,7 @@ impl FilterNode {
&self
) -> (String, String) {
(
colorize(&self._literal, AnsiColor::Cyan),
colorize(&format!("{} {}", self._literal, self.condition.transpile_raw()), AnsiColor::Cyan),
colorize(&format!("WHERE {}", self.condition.transpile_color()), AnsiColor::Cyan)
)
}
Expand Down
38 changes: 19 additions & 19 deletions tests/transpiler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ use eaql::transpiler::engine;
// Database Query Tests (Validator)
// Normal
#[test]
fn integration_test_db_create_normal() {
fn transpile_integration_test_db_create_normal() {
// Create keyword tests
assert_eq!(engine("create database test;"), Ok("CREATE DATABASE test;".to_string()));
assert_eq!(engine("make database test."), Ok("CREATE DATABASE test;".to_string()));
}

#[test]
fn integration_test_db_use_normal() {
fn transpile_integration_test_db_use_normal() {
// Use keyword tests
assert_eq!(engine("use database test;"), Ok("USE DATABASE test;".to_string()));
assert_eq!(engine("enter database test;"), Ok("USE DATABASE test;".to_string()));
}

#[test]
fn integration_test_db_show_normal() {
fn transpile_integration_test_db_show_normal() {
// Show keyword tests
assert_eq!(engine("show database;"), Ok("SHOW DATABASES;".to_string()));
assert_eq!(engine("list databases."), Ok("SHOW DATABASES;".to_string()));
}

#[test]
fn integration_test_db_destroy_normal() {
fn transpile_integration_test_db_destroy_normal() {
// Destroy
assert_eq!(engine("remove database db1!"), Ok("DROP DATABASE db1;".to_string()));
assert_eq!(engine("destroy database db1!"), Ok("DROP DATABASE db1;".to_string()));
Expand All @@ -37,31 +37,31 @@ fn integration_test_db_destroy_normal() {

// Error
#[test]
fn integration_test_db_create_error() {
fn transpile_integration_test_db_create_error() {
// Generic Error Test
assert!(engine("create database test").is_err());
assert!(engine("Create the test!").is_err());
assert!(engine("Make the database \"test\".").is_err());
}

#[test]
fn integration_test_db_use_error() {
fn transpile_integration_test_db_use_error() {
// Generic Error Test
assert!(engine("use database test").is_err());
assert!(engine("Enter test!").is_err());
assert!(engine("enter the database \"test\".").is_err());
}

#[test]
fn integration_test_db_show_error() {
fn transpile_integration_test_db_show_error() {
// Generic Error Test
assert!(engine("show database").is_err());
assert!(engine("show!").is_err());
assert!(engine("List the \"databases\".").is_err());
}

#[test]
fn integration_test_db_destroy_error() {
fn transpile_integration_test_db_destroy_error() {
// Generic Error Test
assert!(engine("delete databases db1, db2, db3").is_err());
assert!(engine("Delete db1!").is_err());
Expand All @@ -72,7 +72,7 @@ fn integration_test_db_destroy_error() {
// Table Accessor Query Tests (Validator)
// Normal
#[test]
fn integration_test_table_accessor_normal_get() {
fn transpile_integration_test_table_accessor_normal_get() {
// Test "wildcard" keywords
assert_eq!(engine("get all from test_table;"), Ok("SELECT * FROM test_table;".to_string()));
assert_eq!(engine("get any from test_table;"), Ok("SELECT * FROM test_table;".to_string()));
Expand All @@ -89,7 +89,7 @@ fn integration_test_table_accessor_normal_get() {
}

#[test]
fn integration_test_table_accessor_normal_filter() {
fn transpile_integration_test_table_accessor_normal_filter() {
// Test "filter entrance" keywords
assert_eq!(engine("get all from test_table where id = 3;"), Ok("SELECT * FROM test_table WHERE id = 3;".to_string()));
assert_eq!(engine("get all from test_table wherever id = 3;"), Ok("SELECT * FROM test_table WHERE id = 3;".to_string()));
Expand All @@ -99,35 +99,35 @@ fn integration_test_table_accessor_normal_filter() {
assert_eq!(engine("get all from test_table where id = 3 and price = 2.0."),
Ok("SELECT * FROM test_table WHERE id = 3 and price = 2.0;".to_string()));
assert_eq!(engine("get all from test_table where id = 3 or (price <= 2 and name is \"3\")!"),
Ok("SELECT * FROM test_table WHERE id = 3 or ( price <= 2 and name = \"3\" );".to_string()));
Ok("SELECT * FROM test_table WHERE id = 3 or (price <= 2 and name = \"3\");".to_string()));
assert_eq!(engine("get all from test_table where (price < 3 or name is \"test\" and (id = 3 or (value < 4 and time >= 5)));"),
Ok("SELECT * FROM test_table WHERE ( price < 3 or name = \"test\" and ( id = 3 or ( value < 4 and time >= 5 ) ) );".to_string()));
Ok("SELECT * FROM test_table WHERE (price < 3 or name = \"test\" and (id = 3 or (value < 4 and time >= 5)));".to_string()));
}


#[test]
fn integration_test_table_accessor_normal_postprocessor() {
fn transpile_integration_test_table_accessor_normal_postprocessor() {
// Test "post-processor entrance" keywords
assert_eq!(engine("get all from test_table then limit 5;"), Ok("SELECT * FROM test_table LIMIT 5;".to_string()));
assert_eq!(engine("get all from test_table afterwords limit 5;"), Ok("SELECT * FROM test_table LIMIT 5;".to_string()));
assert_eq!(engine("get all from test_table after limit 5;"), Ok("SELECT * FROM test_table LIMIT 5;".to_string()));

// Test post-processor w/ filter
assert_eq!(engine("get all from test_table where id = 3 or (price <= 2 and name is \"3\") then limit 5."),
Ok("SELECT * FROM test_table WHERE id = 3 or ( price <= 2 and name = \"3\" ) LIMIT 5;".to_string()));
Ok("SELECT * FROM test_table WHERE id = 3 or (price <= 2 and name = \"3\") LIMIT 5;".to_string()));
}


#[test]
fn integration_test_table_accessor_normal_postprocessor_limit() {
fn transpile_integration_test_table_accessor_normal_postprocessor_limit() {
// Test "post-processor limit" keywords
assert_eq!(engine("get all from test_table then limit 5;"), Ok("SELECT * FROM test_table LIMIT 5;".to_string()));
assert_eq!(engine("get all from test_table then limit it to 5;"), Ok("SELECT * FROM test_table LIMIT 5;".to_string()));
}

// Error
#[test]
fn integration_test_table_accessor_error_get() {
fn transpile_integration_test_table_accessor_error_get() {
// Test improper tokens
assert!(engine("get all from \"test_table\";").is_err());
assert!(engine("get all from test_table").is_err());
Expand All @@ -139,7 +139,7 @@ fn integration_test_table_accessor_error_get() {
}

#[test]
fn integration_test_table_accessor_error_filter() {
fn transpile_integration_test_table_accessor_error_filter() {
// Test bad conditions
assert!(engine("get all from test_table where id = 3").is_err());
assert!(engine("get all from test_table where id is equal to 3;").is_err());
Expand All @@ -152,7 +152,7 @@ fn integration_test_table_accessor_error_filter() {
}

#[test]
fn integration_test_table_accessor_error_postprocessor() {
fn transpile_integration_test_table_accessor_error_postprocessor() {
// Generic tests
assert!(engine("get all from test_table then limit 5").is_err());

Expand All @@ -164,7 +164,7 @@ fn integration_test_table_accessor_error_postprocessor() {
}

#[test]
fn integration_test_table_accessor_error_postprocessor_limit() {
fn transpile_integration_test_table_accessor_error_postprocessor_limit() {
// Test bad limit
assert!(engine("get all from test_table then limit = 5;").is_err());
assert!(engine("get all from test_table then limit id;").is_err());
Expand Down
Loading