Skip to content
Merged
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
44 changes: 15 additions & 29 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::io;
use crate::db;
pub mod ast;
mod tokenizer;
use crate::interpreter::run_sql;

pub fn cli() {
pub fn cli(database: &mut db::database::Database) {
clear_screen();
println!("Welcome to the MollyDB CLI");
let mut line_count = 1;
let mut database = db::database::Database::new();

loop {
print!("({:03}) > ", line_count);
Expand All @@ -29,35 +27,23 @@ pub fn cli() {
continue;
}

let tokens = tokenizer::tokenize(input);
// println!("{:?}", tokens);
let ast = ast::generate(tokens);
for sql_statement in ast {
// println!("{:?}", sql_statement);
match sql_statement {
Ok(statement) => {
let result = database.execute(statement);
if let Ok(values) = result {
if let Some(rows) = values {
for row in rows {
println!("{:?}", row);
}
}
else {
println!("Executed Successfully");
}
}
else {
println!("Error: {}", result.unwrap_err());
}
},
Err(error) => {
println!("Error: {}", error);
},
let results = run_sql(database, input);
for result in results {
if let Ok(Some(rows)) = result {
for row in rows {
println!("{:?}", row);
}
}
else if let Ok(None) = result {
println!("Executed Successfully");
}
else {
println!("Error: {}", result.unwrap_err());
}
}
}
}

fn clear_screen() {
// Clear screen and move cursor to top-left
print!("\x1B[2J\x1B[1;1H");
Expand Down
4 changes: 2 additions & 2 deletions src/db/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::table::{Table, Value};
use crate::cli::ast::{SqlStatement, CreateTableStatement, InsertIntoStatement, SelectStatement, DeleteStatement, UpdateStatement};
use crate::interpreter::ast::{SqlStatement, CreateTableStatement, InsertIntoStatement, SelectStatement, DeleteStatement, UpdateStatement};
use crate::db::table::select;
use crate::db::table::insert;
use crate::db::table::delete;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Database {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::CreateTableStatement;
use crate::interpreter::ast::CreateTableStatement;
use crate::db::table::{ColumnDefinition, DataType};


Expand Down
6 changes: 3 additions & 3 deletions src/db/table/delete/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use crate::db::table::Table;
use crate::cli::ast::DeleteStatement;
use crate::interpreter::ast::DeleteStatement;
use crate::db::table::helpers::common::get_row_indicies_matching_clauses;


Expand Down Expand Up @@ -44,9 +44,9 @@ fn swap_remove_bulk(table: &mut Table, row_indicies: Vec<usize>) -> Result<(), S
mod tests {
use super::*;
use crate::db::table::Value;
use crate::cli::ast::{WhereStackElement, Operator, Operand, WhereCondition, OrderByDirection, OrderByClause};
use crate::interpreter::ast::{WhereStackElement, Operator, Operand, WhereCondition, OrderByDirection, OrderByClause};
use crate::db::table::test_utils::{default_table, assert_table_rows_eq_unordered};
use crate::cli::ast::LimitClause;
use crate::interpreter::ast::LimitClause;

#[test]
fn delete_from_table_works_correctly() {
Expand Down
2 changes: 1 addition & 1 deletion src/db/table/helpers/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::table::{Table, Value, DataType};
use crate::cli::ast::{SelectStatementColumns, WhereStackElement, OrderByClause, LimitClause};
use crate::interpreter::ast::{SelectStatementColumns, WhereStackElement, OrderByClause, LimitClause};
use crate::db::table::helpers::where_stack::matches_where_stack;
use crate::db::table::helpers::{order_by_clause::get_ordered_row_indicies, limit_clause::get_limited_row_indicies};

Expand Down
2 changes: 1 addition & 1 deletion src/db/table/helpers/limit_clause.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::min;

use crate::cli::ast::LimitClause;
use crate::interpreter::ast::LimitClause;
use crate::db::table::Value;


Expand Down
4 changes: 2 additions & 2 deletions src/db/table/helpers/order_by_clause.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;

use crate::cli::ast::OrderByClause;
use crate::interpreter::ast::OrderByClause;
use crate::db::table::Table;
use crate::db::table::Value;

Expand Down Expand Up @@ -36,7 +36,7 @@ fn perform_comparions(table: &Table, row1: &Vec<Value>, row2: &Vec<Value>, order
mod tests {
use super::*;
use crate::db::table::{Table, Value, DataType, ColumnDefinition};
use crate::cli::ast::OrderByDirection;
use crate::interpreter::ast::OrderByDirection;

fn default_table() -> Table {
Table {
Expand Down
4 changes: 2 additions & 2 deletions src/db/table/helpers/where_condition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::table::{Table, Value};
use crate::cli::ast::{Operator, Operand, WhereCondition};
use crate::interpreter::ast::{Operator, Operand, WhereCondition};
use crate::db::table::DataType;


Expand Down Expand Up @@ -105,7 +105,7 @@ fn expect_same_type(l_side: &Value, r_side: &Value) -> Result<(), String> {
mod tests {
use super::*;
use crate::db::table::{Table, Value, DataType, ColumnDefinition};
use crate::cli::ast::{Operator, Operand, WhereCondition};
use crate::interpreter::ast::{Operator, Operand, WhereCondition};

#[test]
fn matches_where_clause_returns_true_if_row_matches_where_clause() {
Expand Down
6 changes: 3 additions & 3 deletions src/db/table/helpers/where_stack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::ast::{WhereStackElement, LogicalOperator};
use crate::interpreter::ast::{WhereStackElement, LogicalOperator};
use crate::db::table::{Table, Value};
use crate::db::table::helpers::where_condition::matches_where_clause;

Expand Down Expand Up @@ -52,8 +52,8 @@ pub fn matches_where_stack(table: &Table, row: &Vec<Value>, where_stack: &Vec<Wh
mod tests {
use super::*;
use crate::db::table::{Table, Value, ColumnDefinition, DataType};
use crate::cli::ast::{WhereStackElement, LogicalOperator};
use crate::cli::ast::{Operator, Operand, WhereCondition};
use crate::interpreter::ast::{WhereStackElement, LogicalOperator};
use crate::interpreter::ast::{Operator, Operand, WhereCondition};

fn simple_condition(l_side: &str, operator: Operator, r_side: Value) -> WhereStackElement {
WhereStackElement::Condition(WhereCondition {l_side: Operand::Identifier(l_side.to_string()), operator, r_side: Operand::Value(r_side)})
Expand Down
2 changes: 1 addition & 1 deletion src/db/table/insert/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, VecDeque};

use crate::db::table::{Table, Value};
use crate::cli::ast::InsertIntoStatement;
use crate::interpreter::ast::InsertIntoStatement;
use crate::db::table::helpers::common::validate_and_clone_row;


Expand Down
2 changes: 1 addition & 1 deletion src/db/table/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;

use crate::cli::ast::OrderByDirection;
use crate::interpreter::ast::OrderByDirection;

pub mod select;
pub mod insert;
Expand Down
10 changes: 5 additions & 5 deletions src/db/table/select/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::table::{Table, Value};
use crate::cli::ast::{SelectStatement};
use crate::interpreter::ast::{SelectStatement};
use crate::db::table::helpers::common::{get_row_indicies_matching_clauses, get_row_columns_from_indicies};


Expand All @@ -14,10 +14,10 @@ pub fn select(table: &Table, statement: SelectStatement) -> Result<Vec<Vec<Value
mod tests {
use super::*;
use crate::db::table::Value;
use crate::cli::ast::{SelectStatementColumns, LimitClause, OrderByClause, OrderByDirection, Operator};
use crate::cli::ast::WhereStackElement;
use crate::cli::ast::WhereCondition;
use crate::cli::ast::Operand;
use crate::interpreter::ast::{SelectStatementColumns, LimitClause, OrderByClause, OrderByDirection, Operator};
use crate::interpreter::ast::WhereStackElement;
use crate::interpreter::ast::WhereCondition;
use crate::interpreter::ast::Operand;
use crate::db::table::test_utils::default_table;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/db/table/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
use crate::db::table::{Table, Value, DataType, ColumnDefinition};
#[cfg(test)]
use crate::cli::ast::OrderByDirection;
use crate::interpreter::ast::OrderByDirection;
#[cfg(test)]
use std::cmp::Ordering;

Expand Down
6 changes: 3 additions & 3 deletions src/db/table/update/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::table::Table;
use crate::cli::ast::{UpdateStatement, ColumnValue};
use crate::interpreter::ast::{UpdateStatement, ColumnValue};
use crate::db::table::helpers::common::get_row_indicies_matching_clauses;
use crate::db::table::DataType;

Expand All @@ -26,9 +26,9 @@ fn update_rows_from_indicies(table: &mut Table, row_indicies: Vec<usize>, update
mod tests {
use super::*;
use crate::db::table::{Value, DataType, ColumnDefinition};
use crate::cli::ast::ColumnValue;
use crate::interpreter::ast::ColumnValue;
use crate::db::table::test_utils::{default_table, assert_table_rows_eq_unordered};
use crate::cli::ast::{WhereStackElement, WhereCondition, Operand, Operator, OrderByClause, OrderByDirection, LimitClause};
use crate::interpreter::ast::{WhereStackElement, WhereCondition, Operand, Operator, OrderByClause, OrderByDirection, LimitClause};

#[test]
fn update_works_correctly() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{
use crate::interpreter::{
ast::{
parser::Parser, CreateTableStatement, SqlStatement::{self, CreateTable},
helpers::common::{expect_token_type, get_table_name}
Expand Down Expand Up @@ -103,7 +103,7 @@ fn index_statement(_parser: &mut Parser) -> Result<SqlStatement, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::test_utils::token;

#[test]
fn create_table_generates_proper_statement(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{
use crate::interpreter::{
ast::{
parser::Parser, SqlStatement, DeleteStatement,
helpers::{
Expand Down Expand Up @@ -29,14 +29,14 @@ pub fn build(parser: &mut Parser) -> Result<SqlStatement, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::cli::ast::OrderByClause;
use crate::cli::ast::OrderByDirection;
use crate::cli::ast::LimitClause;
use crate::cli::ast::Operator;
use crate::cli::ast::WhereStackElement;
use crate::cli::ast::WhereCondition;
use crate::cli::ast::Operand;
use crate::interpreter::ast::test_utils::token;
use crate::interpreter::ast::OrderByClause;
use crate::interpreter::ast::OrderByDirection;
use crate::interpreter::ast::LimitClause;
use crate::interpreter::ast::Operator;
use crate::interpreter::ast::WhereStackElement;
use crate::interpreter::ast::WhereCondition;
use crate::interpreter::ast::Operand;
use crate::db::table::Value;

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{ast::{parser::Parser}, tokenizer::token::TokenTypes};
use crate::interpreter::{ast::{parser::Parser}, tokenizer::token::TokenTypes};

use crate::db::table::Value;

Expand Down Expand Up @@ -91,9 +91,9 @@ fn decode(hex: &str) -> Result<Vec<u8>, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::cli::ast::parser::Parser;
use crate::cli::tokenizer::token::TokenTypes;
use crate::interpreter::ast::test_utils::token;
use crate::interpreter::ast::parser::Parser;
use crate::interpreter::tokenizer::token::TokenTypes;

#[test]
fn value_list_handles_single_value() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::ast::{parser::Parser, LimitClause};
use crate::cli::tokenizer::token::TokenTypes;
use crate::interpreter::ast::{parser::Parser, LimitClause};
use crate::interpreter::tokenizer::token::TokenTypes;
use crate::db::table::Value;
use crate::cli::ast::helpers::common::{expect_token_type, token_to_value};
use crate::interpreter::ast::helpers::common::{expect_token_type, token_to_value};

pub fn get_limit(parser: &mut Parser) -> Result<Option<LimitClause>, String> {
if expect_token_type(parser, TokenTypes::Limit).is_err() {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn get_limit(parser: &mut Parser) -> Result<Option<LimitClause>, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::test_utils::token;

#[test]
fn limit_clause_is_generated_correctly() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::ast::{parser::Parser, OrderByClause, OrderByDirection};
use crate::cli::tokenizer::token::TokenTypes;
use crate::interpreter::ast::{parser::Parser, OrderByClause, OrderByDirection};
use crate::interpreter::tokenizer::token::TokenTypes;

use crate::cli::ast::helpers::common::expect_token_type;
use crate::interpreter::ast::helpers::common::expect_token_type;

pub fn get_order_by(parser: &mut Parser) -> Result<Option<Vec<OrderByClause>>, String> {
if expect_token_type(parser, TokenTypes::Order).is_err() {
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn get_order_by(parser: &mut Parser) -> Result<Option<Vec<OrderByClause>>, S
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::test_utils::token;

#[test]
fn order_by_clause_is_generated_correctly() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cli::ast::{parser::Parser, WhereCondition, Operand, Operator};
use crate::cli::tokenizer::token::TokenTypes;
use crate::cli::ast::helpers::common::{expect_token_type, token_to_value, tokens_to_value_list};
use crate::interpreter::ast::{parser::Parser, WhereCondition, Operand, Operator};
use crate::interpreter::tokenizer::token::TokenTypes;
use crate::interpreter::ast::helpers::common::{expect_token_type, token_to_value, tokens_to_value_list};


pub fn get_condition(parser: &mut Parser) -> Result<WhereCondition, String> {
Expand Down Expand Up @@ -76,8 +76,8 @@ pub fn get_operand(parser: &mut Parser) -> Result<Operand, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::{Operator, WhereCondition, Operand};
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::{Operator, WhereCondition, Operand};
use crate::interpreter::ast::test_utils::token;
use crate::db::table::Value;

fn assert_where_condition(result: Result<WhereCondition, String>, expected: WhereCondition, parser: &mut Parser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::{ast::{
use crate::interpreter::{ast::{
helpers::{common::expect_token_type, where_condition::get_condition},
parser::Parser, LogicalOperator, WhereStackElement, WhereStackOperators, Parentheses}};
use crate::cli::tokenizer::token::TokenTypes;
use crate::interpreter::tokenizer::token::TokenTypes;

// The WhereStack is a the method that is used to store the order of operations with Reverse Polish Notation.
// This is built from the infix expression of the where clause. Using the shunting yard algorithm. Thanks Djikstra!
Expand Down Expand Up @@ -166,8 +166,8 @@ fn get_where_condition(parser: &mut Parser) -> Result<Option<WhereStackElement>,
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::{LogicalOperator, Operator, WhereCondition, Operand};
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::{LogicalOperator, Operator, WhereCondition, Operand};
use crate::interpreter::ast::test_utils::token;
use crate::db::table::Value;

fn simple_condition(l_side: &str, operator: Operator, r_side: Value) -> WhereStackElement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{
use crate::interpreter::{
ast::{
helpers::common::{token_to_value, expect_token_type, get_table_name},
parser::Parser, InsertIntoStatement, SqlStatement::{self, InsertInto}
Expand Down Expand Up @@ -136,7 +136,7 @@ fn or_statement(_parser: &mut Parser) -> Result<SqlStatement, String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ast::test_utils::token;
use crate::interpreter::ast::test_utils::token;

#[test]
fn single_row_insert_statement_is_generated_correctly() {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/ast/mod.rs → src/interpreter/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::tokenizer::{scanner::Token, token::TokenTypes};
use crate::interpreter::tokenizer::{scanner::Token, token::TokenTypes};
use crate::db::table::{ColumnDefinition, Value};

mod create_statement;
Expand Down
Loading
Loading