changes here and there
This commit is contained in:
parent
5e69ce1047
commit
5e4480c767
|
|
@ -7,15 +7,15 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
|
||||
|
||||
|
||||
project(msql)
|
||||
project(usql)
|
||||
|
||||
set(PROJECT_NAME msql)
|
||||
set(PROJECT_NAME usql)
|
||||
|
||||
set(SOURCE
|
||||
exception.cpp lexer.cpp parser.cpp executor.cpp main.cpp table.cpp table.h row.cpp row.h)
|
||||
exception.cpp lexer.cpp parser.cpp executor.cpp main.cpp table.cpp table.h row.cpp row.h csvreader.cpp csvreader.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCE})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} stdc++ m)
|
||||
|
||||
target_compile_options(msql PRIVATE -g)
|
||||
target_compile_options(usql PRIVATE -g)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
|
||||
### TODO
|
||||
- rename it to usql
|
||||
- rename Exception to UException, Table to UTable, Row to URow etc
|
||||
- remove newlines from lexed string tokens
|
||||
- unify using of float and double keywords
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
|
||||
#include "csvreader.h"
|
||||
#include <climits>
|
||||
|
||||
namespace usql {
|
||||
|
||||
CsvReader::CsvReader(bool skip_hdr, char field_sep, char quote_ch, char line_sep, char line_sep2) {
|
||||
skip_header = skip_hdr;
|
||||
field_separator = field_sep;
|
||||
quote_character = quote_ch;
|
||||
line_separator = line_sep;
|
||||
line_separator2 = line_sep2;
|
||||
|
||||
header_skiped = false;
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> CsvReader::parseCSV(const std::string &csvSource) {
|
||||
int linesRead = 0;
|
||||
bool inQuote(false);
|
||||
bool newLine(false);
|
||||
std::string field;
|
||||
|
||||
std::vector<std::vector<std::string>> parsed_data;
|
||||
parsed_data.reserve(128);
|
||||
|
||||
std::vector<std::string> line;
|
||||
line.reserve(32);
|
||||
|
||||
std::string::const_iterator aChar = csvSource.begin();
|
||||
while (aChar != csvSource.end()) {
|
||||
if (*aChar == quote_character) {
|
||||
newLine = false;
|
||||
inQuote = !inQuote;
|
||||
} else if (*aChar == field_separator) {
|
||||
newLine = false;
|
||||
if (inQuote == true) {
|
||||
field += *aChar;
|
||||
} else {
|
||||
line.push_back(field);
|
||||
field.clear();
|
||||
}
|
||||
} else if (*aChar == line_separator || *aChar == line_separator2) {
|
||||
if (inQuote == true) {
|
||||
field += *aChar;
|
||||
} else {
|
||||
if (newLine == false) {
|
||||
line.push_back(field);
|
||||
add_line(line, parsed_data);
|
||||
field.clear();
|
||||
line.clear();
|
||||
linesRead++;
|
||||
if (linesRead == 16) {
|
||||
int linesEstimation =
|
||||
csvSource.size() /
|
||||
(std::distance(csvSource.begin(), aChar) / linesRead);
|
||||
if (linesEstimation > parsed_data.capacity())
|
||||
parsed_data.reserve(linesEstimation);
|
||||
}
|
||||
newLine = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newLine = false;
|
||||
field.push_back(*aChar);
|
||||
}
|
||||
|
||||
aChar++;
|
||||
}
|
||||
|
||||
if (field.size())
|
||||
line.push_back(field);
|
||||
|
||||
add_line(line, parsed_data);
|
||||
|
||||
return parsed_data;
|
||||
}
|
||||
|
||||
|
||||
void CsvReader::add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines) {
|
||||
if (skip_header && !header_skiped) {
|
||||
header_skiped = true;
|
||||
} else {
|
||||
if (line.size())
|
||||
lines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class CsvReader {
|
||||
|
||||
private:
|
||||
char field_separator;
|
||||
char line_separator;
|
||||
char line_separator2;
|
||||
char quote_character;
|
||||
|
||||
bool skip_header;
|
||||
bool header_skiped;
|
||||
|
||||
public:
|
||||
CsvReader(bool skip_hdr = false, char field_sep = ',', char quote_ch = '"', char line_sep = '\r',
|
||||
char line_sep2 = '\n');
|
||||
|
||||
std::vector<std::vector<std::string>> parseCSV(const std::string &csvSource);
|
||||
|
||||
private:
|
||||
void add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines);
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Ticker,Price
|
||||
FDX,257.3
|
||||
C,59.85
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "exception.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Exception::Exception(const std::string &msg) {
|
||||
cause = msg;
|
||||
|
|
@ -7,3 +8,4 @@ Exception::Exception(const std::string &msg) {
|
|||
|
||||
|
||||
const char *Exception::what() const noexcept { return cause.c_str(); }
|
||||
}
|
||||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class Exception : public std::exception {
|
||||
private:
|
||||
std::string cause;
|
||||
|
|
@ -13,3 +15,5 @@ public:
|
|||
|
||||
const char *what() const noexcept;
|
||||
};
|
||||
|
||||
}
|
||||
261
executor.cpp
261
executor.cpp
|
|
@ -1,13 +1,17 @@
|
|||
#include "executor.h"
|
||||
#include "exception.h"
|
||||
#include "csvreader.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
namespace usql {
|
||||
|
||||
Executor::Executor() {
|
||||
m_tables.clear();
|
||||
}
|
||||
|
||||
|
||||
Table *Executor::find_table(const std::string name) {
|
||||
auto name_cmp = [name](Table t) { return t.m_name == name; };
|
||||
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
|
||||
|
|
@ -19,8 +23,24 @@ Table* Executor::find_table(const std::string name) {
|
|||
}
|
||||
|
||||
|
||||
bool Executor::execute(Node& node) {
|
||||
// TODO optimize node here
|
||||
std::unique_ptr<Table> Executor::create_stmt_result_table(int code, std::string text) {
|
||||
std::vector<ColDefNode> result_tbl_col_defs{};
|
||||
result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false));
|
||||
result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
|
||||
|
||||
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
new_row.setColumnValue(0, code);
|
||||
new_row.setColumnValue(1, text);
|
||||
table_def->addRow(new_row);
|
||||
|
||||
return std::move(table_def);
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> Executor::execute(Node &node) {
|
||||
// TODO optimize execution nodes here
|
||||
switch (node.node_type) {
|
||||
case NodeType::create_table:
|
||||
return execute_create_table(static_cast<CreateTableNode &>(node));
|
||||
|
|
@ -32,22 +52,25 @@ bool Executor::execute(Node& node) {
|
|||
return execute_delete(static_cast<DeleteFromTableNode &>(node));
|
||||
case NodeType::update_table:
|
||||
return execute_update(static_cast<UpdateTableNode &>(node));
|
||||
case NodeType::load_table:
|
||||
return execute_load(static_cast<LoadIntoTableNode &>(node));
|
||||
default:
|
||||
// TODO error message
|
||||
return false;
|
||||
return create_stmt_result_table(-1, "unknown statement");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool Executor::execute_create_table(CreateTableNode& node) {
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_create_table(CreateTableNode &node) {
|
||||
// TODO check table does not exists
|
||||
Table table{node.table_name, node.cols_defs};
|
||||
m_tables.push_back(table);
|
||||
|
||||
return true;
|
||||
return create_stmt_result_table(0, "table created");
|
||||
}
|
||||
|
||||
bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_insert_into_table(InsertIntoTableNode &node) {
|
||||
// TODO check column names.size = values.size
|
||||
|
||||
// find table
|
||||
|
|
@ -58,8 +81,7 @@ bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
|
|||
|
||||
// copy values
|
||||
for (size_t i = 0; i < node.cols_names.size(); i++) {
|
||||
auto colNameNode = node.cols_names[i];
|
||||
ColDefNode col_def = table_def->get_column_def(colNameNode.name);
|
||||
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
|
||||
|
||||
// TODO validate value
|
||||
|
||||
|
|
@ -72,15 +94,14 @@ bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO check not null columns
|
||||
|
||||
// append new_row
|
||||
table_def->addRow(new_row);
|
||||
|
||||
return true;
|
||||
return create_stmt_result_table(0, "insert succeded");
|
||||
}
|
||||
|
||||
bool Executor::execute_select(SelectFromTableNode& node) {
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_select(SelectFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -99,21 +120,22 @@ bool Executor::execute_select(SelectFromTableNode& node) {
|
|||
|
||||
i++;
|
||||
}
|
||||
Table result {"result", result_tbl_col_defs};
|
||||
auto result = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
// execute access plan
|
||||
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
|
||||
// eval where for row
|
||||
if (evalWhere(node.where.get(), table, row)) {
|
||||
// prepare empty row
|
||||
Row new_row = result.createEmptyRow();
|
||||
Row new_row = result->createEmptyRow();
|
||||
|
||||
// copy column values
|
||||
for(auto idx=0; idx<result.columns_count(); idx++) {
|
||||
for (auto idx = 0; idx < result->columns_count(); idx++) {
|
||||
auto row_col_index = source_table_col_index[idx];
|
||||
ColValue *col_value = row->ithColumn(row_col_index);
|
||||
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
|
||||
new_row.setColumnValue(idx, ((ColIntegerValue*)col_value)->integerValue());
|
||||
new_row.setColumnValue(idx,
|
||||
((ColIntegerValue *) col_value)->integerValue());
|
||||
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
|
||||
new_row.setColumnValue(idx, col_value->floatValue());
|
||||
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
|
||||
|
|
@ -121,16 +143,15 @@ bool Executor::execute_select(SelectFromTableNode& node) {
|
|||
}
|
||||
|
||||
// add row to result
|
||||
result.m_rows.push_back(new_row);
|
||||
result->m_rows.push_back(new_row);
|
||||
}
|
||||
}
|
||||
|
||||
result.print();
|
||||
|
||||
return true;
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
bool Executor::execute_delete(DeleteFromTableNode& node) {
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_delete(DeleteFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -147,10 +168,11 @@ bool Executor::execute_delete(DeleteFromTableNode& node) {
|
|||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return create_stmt_result_table(0, "delete succeded");
|
||||
}
|
||||
|
||||
bool Executor::execute_update(UpdateTableNode &node) {
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_update(UpdateTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -160,18 +182,21 @@ bool Executor::execute_update(UpdateTableNode &node) {
|
|||
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
|
||||
// eval where for row
|
||||
if (evalWhere(node.where.get(), table, row)) {
|
||||
// TODO do update
|
||||
int i = 0;
|
||||
for (auto col : node.cols_names) {
|
||||
// TODO cache it like in select
|
||||
ColDefNode cdef = table->get_column_def(col.name);
|
||||
|
||||
std::unique_ptr<Node> new_val = evalArithmetic(static_cast<ArithmeticalOperatorNode &>(*node.values[i]), table, row);
|
||||
std::unique_ptr<ValueNode> new_val = evalArithmetic(cdef.type,
|
||||
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
|
||||
table, row);
|
||||
|
||||
if (cdef.type == ColumnType::integer_type) {
|
||||
row->setColumnValue(cdef.order, ((IntValueNode*)new_val.get())->value);
|
||||
row->setColumnValue(cdef.order, new_val->getIntValue());
|
||||
} else if (cdef.type == ColumnType::float_type) {
|
||||
row->setColumnValue(cdef.order, ((FloatValueNode*)new_val.get())->value);
|
||||
row->setColumnValue(cdef.order, new_val->getDoubleValue());
|
||||
} else if (cdef.type == ColumnType::varchar_type) {
|
||||
row->setColumnValue(cdef.order, new_val->getStringValue());
|
||||
} else {
|
||||
throw Exception("Implement me!");
|
||||
}
|
||||
|
|
@ -180,7 +205,49 @@ bool Executor::execute_update(UpdateTableNode &node) {
|
|||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return create_stmt_result_table(0, "delete succeeded");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> Executor::execute_load(LoadIntoTableNode &node) {
|
||||
// find source table
|
||||
Table *table_def = find_table(node.table_name);
|
||||
|
||||
// read data
|
||||
std::ifstream ifs(node.filename);
|
||||
std::string content((std::istreambuf_iterator<char>(ifs)),
|
||||
(std::istreambuf_iterator<char>()));
|
||||
|
||||
CsvReader csvparser{};
|
||||
auto csv = csvparser.parseCSV(content);
|
||||
|
||||
std::vector<ColDefNode> &colDefs = table_def->m_col_defs;
|
||||
|
||||
for (auto it = csv.begin() + 1; it != csv.end(); ++it) {
|
||||
std::vector<std::string> csv_line = *it;
|
||||
|
||||
// prepare empty new_row
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
|
||||
// copy values
|
||||
for (size_t i = 0; i < table_def->columns_count(); i++) {
|
||||
ColDefNode col_def = table_def->get_column_def(colDefs[i].name);
|
||||
|
||||
// TODO validate value
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stoi(csv_line[i]));
|
||||
} else if (col_def.type == ColumnType::float_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
|
||||
} else {
|
||||
new_row.setColumnValue(col_def.order, csv_line[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// append new_row
|
||||
table_def->addRow(new_row);
|
||||
}
|
||||
|
||||
return create_stmt_result_table(0, "load succeeded");
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -200,60 +267,28 @@ bool Executor::evalWhere(Node *where, Table *table,
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
std::unique_ptr<Node> left_value = evalNode(table, row, filter.left.get());
|
||||
std::unique_ptr<Node> right_value = evalNode(table, row, filter.right.get());
|
||||
|
||||
bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
std::unique_ptr<ValueNode> left_value = evalNode(table, row, filter.left.get());
|
||||
std::unique_ptr<ValueNode> right_value = evalNode(table, row, filter.right.get());
|
||||
|
||||
double comparator;
|
||||
|
||||
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) {
|
||||
auto lvalue = static_cast<IntValueNode *>(left_value.get());
|
||||
auto rvalue = static_cast<IntValueNode *>(right_value.get());
|
||||
comparator = lvalue->value - rvalue->value;
|
||||
}
|
||||
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) {
|
||||
auto *lvalue = static_cast<IntValueNode *>(left_value.get());
|
||||
auto *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
||||
comparator = (double)lvalue->value - rvalue->value;
|
||||
}
|
||||
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::string_value) {
|
||||
auto *lvalue = static_cast<IntValueNode *>(left_value.get());
|
||||
auto *rvalue = static_cast<StringValueNode *>(right_value.get());
|
||||
comparator = std::to_string(lvalue->value).compare(rvalue->value);
|
||||
}
|
||||
|
||||
|
||||
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) {
|
||||
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
||||
auto *rvalue = static_cast<IntValueNode *>(right_value.get());
|
||||
comparator = lvalue->value - (double)rvalue->value;
|
||||
}
|
||||
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value) {
|
||||
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
||||
auto *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
||||
comparator = lvalue->value - rvalue->value;
|
||||
}
|
||||
if (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::string_value) {
|
||||
auto *lvalue = static_cast<FloatValueNode *>(left_value.get());
|
||||
auto *rvalue = static_cast<StringValueNode *>(right_value.get());
|
||||
comparator = std::to_string(lvalue->value).compare(rvalue->value);
|
||||
}
|
||||
|
||||
|
||||
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::int_value) {
|
||||
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
||||
IntValueNode *rvalue = static_cast<IntValueNode *>(right_value.get());
|
||||
comparator = lvalue->value.compare(std::to_string(rvalue->value));
|
||||
}
|
||||
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::float_value) {
|
||||
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
||||
FloatValueNode *rvalue = static_cast<FloatValueNode *>(right_value.get());
|
||||
comparator = lvalue->value.compare(std::to_string(rvalue->value));
|
||||
}
|
||||
if (left_value->node_type == NodeType::string_value && right_value->node_type == NodeType::string_value) {
|
||||
StringValueNode *lvalue = static_cast<StringValueNode *>(left_value.get());
|
||||
StringValueNode *rvalue = static_cast<StringValueNode *>(right_value.get());
|
||||
comparator = lvalue->value.compare(rvalue->value);
|
||||
comparator = left_value->getIntValue() - right_value->getIntValue();
|
||||
} else if ((left_value->node_type == NodeType::int_value &&
|
||||
right_value->node_type == NodeType::float_value) ||
|
||||
(left_value->node_type == NodeType::float_value &&
|
||||
right_value->node_type == NodeType::int_value) ||
|
||||
(left_value->node_type == NodeType::float_value &&
|
||||
right_value->node_type == NodeType::float_value)) {
|
||||
comparator = left_value->getDoubleValue() - right_value->getDoubleValue();
|
||||
} else if (left_value->node_type == NodeType::string_value ||
|
||||
right_value->node_type == NodeType::string_value) {
|
||||
comparator = left_value->getStringValue().compare(right_value->getStringValue());
|
||||
} else {
|
||||
// TODO throw exception
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -273,12 +308,16 @@ bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Tabl
|
|||
}
|
||||
|
||||
throw Exception("invalid relational operator");
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
||||
|
||||
std::unique_ptr<ValueNode>
|
||||
Executor::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
||||
if (node->node_type == NodeType::database_value) {
|
||||
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node);
|
||||
ColDefNode col_def = table->get_column_def(dvl->col_name); // TODO optimize it to just get this def once
|
||||
ColDefNode col_def = table->get_column_def(
|
||||
dvl->col_name); // TODO optimize it to just get this def once
|
||||
auto db_value = row->ithColumn(col_def.order);
|
||||
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
|
|
@ -307,24 +346,74 @@ std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::all
|
|||
throw Exception("invalid type");
|
||||
}
|
||||
|
||||
|
||||
bool Executor::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &iter) const {
|
||||
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
|
||||
|
||||
if ((node.op == LogicalOperatorType::and_operator && !left) || (node.op == LogicalOperatorType::or_operator && left))
|
||||
if ((node.op == LogicalOperatorType::and_operator && !left) ||
|
||||
(node.op == LogicalOperatorType::or_operator && left))
|
||||
return left;
|
||||
|
||||
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter);
|
||||
return right;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Executor::evalArithmetic(ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::copy_value:
|
||||
std::unique_ptr<ValueNode>
|
||||
Executor::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
if (node.op == ArithmeticalOperatorType::copy_value) {
|
||||
return evalNode(table, row, node.left.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> left = evalNode(table, row, node.left.get());
|
||||
std::unique_ptr<ValueNode> right = evalNode(table, row, node.right.get());
|
||||
|
||||
if (outType == ColumnType::float_type) {
|
||||
double l = ((ValueNode *) left.get())->getDoubleValue();
|
||||
double r = ((ValueNode *) right.get())->getDoubleValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<FloatValueNode>(l + r);
|
||||
case ArithmeticalOperatorType::minus_operator:
|
||||
return std::make_unique<FloatValueNode>(l - r);
|
||||
case ArithmeticalOperatorType::multiply_operator:
|
||||
return std::make_unique<FloatValueNode>(l * r);
|
||||
case ArithmeticalOperatorType::divide_operator:
|
||||
return std::make_unique<FloatValueNode>(l / r);
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
} else if (outType == ColumnType::integer_type) {
|
||||
int l = ((ValueNode *) left.get())->getIntValue();
|
||||
int r = ((ValueNode *) right.get())->getIntValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<IntValueNode>(l + r);
|
||||
case ArithmeticalOperatorType::minus_operator:
|
||||
return std::make_unique<IntValueNode>(l - r);
|
||||
case ArithmeticalOperatorType::multiply_operator:
|
||||
return std::make_unique<IntValueNode>(l * r);
|
||||
case ArithmeticalOperatorType::divide_operator:
|
||||
return std::make_unique<IntValueNode>(l / r);
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
|
||||
} else if (outType == ColumnType::varchar_type) {
|
||||
std::string l = ((ValueNode *) left.get())->getStringValue();
|
||||
std::string r = ((ValueNode *) right.get())->getStringValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<StringValueNode>(l + r);
|
||||
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
}
|
||||
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
|
||||
}
|
||||
30
executor.h
30
executor.h
|
|
@ -5,30 +5,40 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class Executor {
|
||||
private:
|
||||
|
||||
public:
|
||||
Executor();
|
||||
|
||||
bool execute(Node& node);
|
||||
std::unique_ptr<Table> execute(Node &node);
|
||||
|
||||
private:
|
||||
bool execute_create_table(CreateTableNode& node);
|
||||
bool execute_insert_into_table(InsertIntoTableNode& node);
|
||||
bool execute_select(SelectFromTableNode& node);
|
||||
bool execute_delete(DeleteFromTableNode& node);
|
||||
bool execute_update(UpdateTableNode& node);
|
||||
std::unique_ptr<Table> execute_create_table(CreateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_select(SelectFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_update(UpdateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
|
||||
|
||||
Table *find_table(const std::string name);
|
||||
|
||||
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
|
||||
|
||||
private:
|
||||
std::vector<Table> m_tables;
|
||||
|
||||
bool evalWhere(Node *where, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
|
||||
std::unique_ptr<Node>
|
||||
evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
|
||||
std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
|
||||
Node *node) const;
|
||||
|
||||
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
|
|
@ -37,6 +47,8 @@ private:
|
|||
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &iter) const;
|
||||
|
||||
std::unique_ptr<Node> evalArithmetic(ArithmeticalOperatorNode &node, Table *table,
|
||||
std::unique_ptr<ValueNode> evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
};
|
||||
|
||||
}
|
||||
64
lexer.cpp
64
lexer.cpp
|
|
@ -3,12 +3,24 @@
|
|||
|
||||
#include <algorithm>
|
||||
|
||||
namespace usql {
|
||||
|
||||
Token::Token(const std::string &token_str, TokenType typ) {
|
||||
token_string = token_str;
|
||||
type = typ;
|
||||
}
|
||||
|
||||
|
||||
Lexer::Lexer() {
|
||||
k_words_regex =
|
||||
"[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/"
|
||||
",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n";
|
||||
k_int_regex = "[0-9]+";
|
||||
k_int_underscored_regex = "[0-9][0-9_]+[0-9]";
|
||||
k_double_regex = "[0-9]+\\.[0-9]+";
|
||||
k_identifier_regex = "[A-Za-z]+[A-Za-z0-9_#]*";
|
||||
}
|
||||
|
||||
void Lexer::parse(const std::string &code) {
|
||||
// TODO handle empty code
|
||||
m_tokens.clear();
|
||||
|
|
@ -19,14 +31,10 @@ void Lexer::parse(const std::string &code) {
|
|||
}
|
||||
m_code_str = code;
|
||||
if (!m_code_str.empty() && m_code_str.back() != '\n') {
|
||||
m_code_str.append("\n"); // TODO tempo solution to prevent possible situation when last line is a comment
|
||||
m_code_str.append("\n"); // TODO temp solution to prevent possible situation when last line is a comment
|
||||
}
|
||||
|
||||
// TODO make it constant
|
||||
std::regex words_regex("[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/"
|
||||
",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n");
|
||||
|
||||
auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), words_regex);
|
||||
auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), k_words_regex);
|
||||
auto words_end = std::sregex_iterator();
|
||||
|
||||
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
|
||||
|
|
@ -72,7 +80,8 @@ void Lexer::skipToken(TokenType type) {
|
|||
if (tokenType() == type) {
|
||||
nextToken();
|
||||
} else {
|
||||
throw Exception("ERROR unexpected token " + consumeCurrentToken().token_string + ", instead of " + typeToString(type));
|
||||
throw Exception("ERROR unexpected token " + consumeCurrentToken().token_string + ", instead of " +
|
||||
typeToString(type));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -84,12 +93,15 @@ void Lexer::skipTokenOptional(TokenType type) {
|
|||
|
||||
TokenType Lexer::tokenType() { return m_index < m_tokens.size() ? currentToken().type : TokenType::eof; }
|
||||
|
||||
TokenType Lexer::nextTokenType() { return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof; }
|
||||
TokenType Lexer::nextTokenType() {
|
||||
return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof;
|
||||
}
|
||||
|
||||
TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; }
|
||||
|
||||
bool Lexer::isRelationalOperator(TokenType token_type) {
|
||||
return (token_type == TokenType::equal || token_type == TokenType::not_equal || token_type == TokenType::greater || token_type == TokenType::greater_equal ||
|
||||
return (token_type == TokenType::equal || token_type == TokenType::not_equal ||
|
||||
token_type == TokenType::greater || token_type == TokenType::greater_equal ||
|
||||
token_type == TokenType::lesser || token_type == TokenType::lesser_equal);
|
||||
}
|
||||
|
||||
|
|
@ -98,17 +110,13 @@ bool Lexer::isLogicalOperator(TokenType token_type) {
|
|||
}
|
||||
|
||||
bool Lexer::isArithmeticalOperator(TokenType token_type) {
|
||||
return (token_type == TokenType::plus || token_type == TokenType::minus || token_type == TokenType::multiply || token_type == TokenType::divide);
|
||||
return (token_type == TokenType::plus || token_type == TokenType::minus ||
|
||||
token_type == TokenType::multiply ||
|
||||
token_type == TokenType::divide);
|
||||
}
|
||||
|
||||
TokenType Lexer::type(const std::string &token) {
|
||||
// TODO move it to class level not to reinit it again and again
|
||||
std::regex int_regex("[0-9]+");
|
||||
std::regex int_underscored_regex("[0-9][0-9_]+[0-9]");
|
||||
std::regex double_regex("[0-9]+\\.[0-9]+");
|
||||
std::regex identifier_regex("[A-Za-z]+[A-Za-z0-9_#]*");
|
||||
|
||||
// TODO 'one is evaluated as identifier
|
||||
// TODO, FIXME 'one is evaluated as identifier
|
||||
if (token == ";")
|
||||
return TokenType::semicolon;
|
||||
|
||||
|
|
@ -184,6 +192,9 @@ TokenType Lexer::type(const std::string &token) {
|
|||
if (token == "update")
|
||||
return TokenType::keyword_update;
|
||||
|
||||
if (token == "load")
|
||||
return TokenType::keyword_load;
|
||||
|
||||
if (token == "not")
|
||||
return TokenType::keyword_not;
|
||||
|
||||
|
|
@ -211,7 +222,8 @@ TokenType Lexer::type(const std::string &token) {
|
|||
if (token == "\n" || token == "\r\n" || token == "\r")
|
||||
return TokenType::newline;
|
||||
|
||||
if (token.length() > 1 && token.at(0) == '%' && (token.at(token.length() - 1) == '\n' || token.at(token.length() - 1) == '\r'))
|
||||
if (token.length() > 1 && token.at(0) == '%' &&
|
||||
(token.at(token.length() - 1) == '\n' || token.at(token.length() - 1) == '\r'))
|
||||
return TokenType::comment;
|
||||
|
||||
// if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"')
|
||||
|
|
@ -220,16 +232,16 @@ TokenType Lexer::type(const std::string &token) {
|
|||
if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'')
|
||||
return TokenType::string_literal;
|
||||
|
||||
if (std::regex_match(token, int_regex))
|
||||
if (std::regex_match(token, k_int_regex))
|
||||
return TokenType::int_number;
|
||||
|
||||
if (std::regex_match(token, int_underscored_regex))
|
||||
if (std::regex_match(token, k_int_underscored_regex))
|
||||
return TokenType::int_number;
|
||||
|
||||
if (std::regex_match(token, double_regex))
|
||||
if (std::regex_match(token, k_double_regex))
|
||||
return TokenType::double_number;
|
||||
|
||||
if (std::regex_match(token, identifier_regex))
|
||||
if (std::regex_match(token, k_identifier_regex))
|
||||
return TokenType::identifier;
|
||||
|
||||
if (m_index + 1 >= m_tokens.size())
|
||||
|
|
@ -338,6 +350,12 @@ std::string Lexer::typeToString(TokenType token_type) {
|
|||
case TokenType::keyword_copy:
|
||||
txt = "copy";
|
||||
break;
|
||||
case TokenType::keyword_update:
|
||||
txt = "update";
|
||||
break;
|
||||
case TokenType::keyword_load:
|
||||
txt = "load";
|
||||
break;
|
||||
case TokenType::keyword_not:
|
||||
txt = "not";
|
||||
break;
|
||||
|
|
@ -395,3 +413,5 @@ std::string Lexer::typeToString(TokenType token_type) {
|
|||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
}
|
||||
22
lexer.h
22
lexer.h
|
|
@ -5,6 +5,8 @@
|
|||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
enum class TokenType {
|
||||
undef,
|
||||
identifier,
|
||||
|
|
@ -23,6 +25,7 @@ enum class TokenType {
|
|||
keyword_where,
|
||||
keyword_delete,
|
||||
keyword_update,
|
||||
keyword_load,
|
||||
keyword_from,
|
||||
keyword_insert,
|
||||
keyword_into,
|
||||
|
|
@ -53,36 +56,45 @@ enum class TokenType {
|
|||
struct Token {
|
||||
std::string token_string;
|
||||
TokenType type;
|
||||
|
||||
Token(const std::string &token_str, TokenType typ);
|
||||
};
|
||||
|
||||
class Lexer {
|
||||
public:
|
||||
Lexer() {};
|
||||
Lexer();
|
||||
|
||||
void parse(const std::string &code);
|
||||
|
||||
void debugTokens();
|
||||
|
||||
Token currentToken();
|
||||
|
||||
Token consumeCurrentToken();
|
||||
|
||||
void nextToken();
|
||||
|
||||
void skipToken(TokenType type);
|
||||
|
||||
void skipTokenOptional(TokenType type);
|
||||
|
||||
TokenType tokenType();
|
||||
|
||||
TokenType nextTokenType();
|
||||
|
||||
TokenType prevTokenType();
|
||||
|
||||
static bool isRelationalOperator(TokenType token_type);
|
||||
|
||||
static bool isLogicalOperator(TokenType token_type);
|
||||
|
||||
static bool isArithmeticalOperator(TokenType token_type);
|
||||
|
||||
private:
|
||||
TokenType type(const std::string &token);
|
||||
|
||||
std::string stringLiteral(std::string token);
|
||||
|
||||
static std::string typeToString(TokenType token_type);
|
||||
|
||||
|
||||
|
|
@ -90,4 +102,12 @@ private:
|
|||
std::string m_code_str;
|
||||
std::vector<Token> m_tokens;
|
||||
int m_index = 0;
|
||||
|
||||
std::regex k_words_regex;
|
||||
std::regex k_int_regex;
|
||||
std::regex k_int_underscored_regex;
|
||||
std::regex k_double_regex;
|
||||
std::regex k_identifier_regex;
|
||||
};
|
||||
|
||||
}
|
||||
19
main.cpp
19
main.cpp
|
|
@ -8,8 +8,8 @@
|
|||
// drop table
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
Parser parser{};
|
||||
Executor executor{};
|
||||
usql::Parser parser{};
|
||||
usql::Executor executor{};
|
||||
|
||||
std::vector<std::string> sql_commands{
|
||||
"create table a (i integer not null, s varchar(64), f float null)",
|
||||
|
|
@ -26,15 +26,22 @@ int main(int argc, char *argv[]) {
|
|||
"delete from a where i = 4",
|
||||
"select i, s from a where i > 0",
|
||||
"update a set f = 9.99 where i = 3",
|
||||
// "update a set s = 'three', f = 1.0 + 2.0 where i = 3",
|
||||
"select i, s, f from a where i = 3"
|
||||
// "select i, s from a where i > 0"
|
||||
"select i, s, f from a where i = 3",
|
||||
"update a set s = 'three', f = f + 0.01 where i = 3",
|
||||
"select i, s, f from a where i = 3",
|
||||
"create table data (ticker varchar(8), price float null)",
|
||||
"load data from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/data.csv')",
|
||||
"select ticker, price from data"
|
||||
};
|
||||
|
||||
|
||||
for (auto command : sql_commands) {
|
||||
std::cout << command << std::endl;
|
||||
auto node = parser.parse(command);
|
||||
executor.execute(*node);
|
||||
auto result = executor.execute(*node);
|
||||
|
||||
result->print();
|
||||
// std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
51
parser.cpp
51
parser.cpp
|
|
@ -1,6 +1,8 @@
|
|||
#include "parser.h"
|
||||
#include "exception.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
// TOOD handle premature eof
|
||||
|
||||
Parser::Parser() {
|
||||
|
|
@ -13,15 +15,22 @@ std::unique_ptr<Node> Parser::parse(const std::string &code) {
|
|||
|
||||
if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
|
||||
return parse_create_table();
|
||||
} if (lexer.tokenType() == TokenType::keyword_insert) {
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_insert) {
|
||||
return parse_insert_into_table();
|
||||
} if (lexer.tokenType() == TokenType::keyword_select) {
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_select) {
|
||||
return parse_select_from_table();
|
||||
} if (lexer.tokenType() == TokenType::keyword_delete) {
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_delete) {
|
||||
return parse_delete_from_table();
|
||||
} if (lexer.tokenType() == TokenType::keyword_update) {
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_update) {
|
||||
return parse_update_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_load) {
|
||||
return parse_load_table();
|
||||
}
|
||||
|
||||
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
|
||||
return std::make_unique<Node>(NodeType::error);
|
||||
|
|
@ -48,7 +57,7 @@ std::unique_ptr<Node> Parser::parse_create_table() {
|
|||
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
|
||||
column_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
// column type and optionaly len
|
||||
// column type and optionally len
|
||||
if (lexer.tokenType() == TokenType::keyword_int) {
|
||||
column_type = ColumnType::integer_type;
|
||||
lexer.nextToken();
|
||||
|
|
@ -73,7 +82,8 @@ std::unique_ptr<Node> Parser::parse_create_table() {
|
|||
lexer.nextToken();
|
||||
}
|
||||
|
||||
cols_def.push_back(ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
|
||||
cols_def.push_back(
|
||||
ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
|
||||
|
|
@ -174,10 +184,13 @@ std::unique_ptr<Node> Parser::parse_update_table() {
|
|||
ArithmeticalOperatorType op = parse_arithmetical_operator();
|
||||
std::unique_ptr<Node> right = Parser::parse_operand_node();
|
||||
|
||||
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left), std::move(right)));
|
||||
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left),
|
||||
std::move(right)));
|
||||
} else {
|
||||
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0);
|
||||
values.push_back(std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value, std::move(left), std::move(right)));
|
||||
values.push_back(
|
||||
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
|
||||
std::move(left), std::move(right)));
|
||||
}
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
|
||||
|
|
@ -188,6 +201,19 @@ std::unique_ptr<Node> Parser::parse_update_table() {
|
|||
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_load_table() {
|
||||
lexer.skipToken(TokenType::keyword_load);
|
||||
lexer.skipTokenOptional(TokenType::keyword_into);
|
||||
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
lexer.skipTokenOptional(TokenType::keyword_from);
|
||||
|
||||
std::string file_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_where_clause() {
|
||||
// TODO add support for multiple filters
|
||||
// TODO add support for parenthesis
|
||||
|
|
@ -256,6 +282,7 @@ RelationalOperatorType Parser::parse_relational_operator() {
|
|||
throw Exception("Unknown relational operator");
|
||||
}
|
||||
}
|
||||
|
||||
LogicalOperatorType Parser::parse_logical_operator() {
|
||||
auto op = lexer.consumeCurrentToken();
|
||||
switch (op.type) {
|
||||
|
|
@ -273,7 +300,15 @@ ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
|
|||
switch (op.type) {
|
||||
case TokenType::plus:
|
||||
return ArithmeticalOperatorType::plus_operator;
|
||||
case TokenType::minus:
|
||||
return ArithmeticalOperatorType::minus_operator;
|
||||
case TokenType::multiply:
|
||||
return ArithmeticalOperatorType::multiply_operator;
|
||||
case TokenType::divide:
|
||||
return ArithmeticalOperatorType::divide_operator;
|
||||
default:
|
||||
throw Exception("Unknown arithmetical operator");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
75
parser.h
75
parser.h
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
|
||||
enum class ColumnType {
|
||||
integer_type,
|
||||
|
|
@ -27,6 +29,7 @@ enum class NodeType {
|
|||
select_from,
|
||||
delete_from,
|
||||
update_table,
|
||||
load_table,
|
||||
column_name,
|
||||
column_value,
|
||||
column_def,
|
||||
|
|
@ -63,31 +66,61 @@ struct ColDefNode : Node {
|
|||
bool null;
|
||||
|
||||
ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) :
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len), null(nullable) {}
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
|
||||
null(nullable) {}
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct TrueNode : Node {
|
||||
TrueNode() : Node(NodeType::true_node) {}
|
||||
};
|
||||
|
||||
struct IntValueNode : Node {
|
||||
struct ValueNode : Node {
|
||||
ValueNode(NodeType type) : Node(type) {}
|
||||
|
||||
virtual int getIntValue() = 0;
|
||||
|
||||
virtual double getDoubleValue() = 0;
|
||||
|
||||
virtual std::string getStringValue() = 0;
|
||||
|
||||
virtual ~ValueNode() {};
|
||||
};
|
||||
|
||||
struct IntValueNode : ValueNode {
|
||||
int value;
|
||||
|
||||
IntValueNode(int value) : Node(NodeType::int_value), value(value) {}
|
||||
IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
|
||||
|
||||
int getIntValue() { return value; };
|
||||
|
||||
double getDoubleValue() { return (double) value; };
|
||||
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
struct FloatValueNode : Node {
|
||||
struct FloatValueNode : ValueNode {
|
||||
double value;
|
||||
|
||||
FloatValueNode(double value) : Node(NodeType::float_value), value(value) {}
|
||||
FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
|
||||
|
||||
int getIntValue() { return (int) value; };
|
||||
|
||||
double getDoubleValue() { return value; };
|
||||
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
struct StringValueNode : Node {
|
||||
struct StringValueNode : ValueNode {
|
||||
std::string value;
|
||||
|
||||
StringValueNode(std::string value) : Node(NodeType::string_value), value(value) {}
|
||||
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
|
||||
|
||||
int getIntValue() { return std::stoi(value); };
|
||||
|
||||
double getDoubleValue() { return std::stod(value); };
|
||||
|
||||
std::string getStringValue() { return value; };
|
||||
};
|
||||
|
||||
struct DatabaseValueNode : Node {
|
||||
|
|
@ -184,7 +217,17 @@ struct UpdateTableNode : Node {
|
|||
|
||||
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)), where(std::move(where_clause)) {}
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
|
||||
where(std::move(where_clause)) {}
|
||||
};
|
||||
|
||||
struct LoadIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::string filename;
|
||||
|
||||
LoadIntoTableNode(const std::string name, std::string file) :
|
||||
Node(NodeType::load_table), table_name(name), filename(file) {}
|
||||
|
||||
};
|
||||
|
||||
struct DeleteFromTableNode : Node {
|
||||
|
|
@ -197,8 +240,6 @@ struct DeleteFromTableNode : Node {
|
|||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
|
||||
|
|
@ -209,15 +250,25 @@ public:
|
|||
|
||||
private:
|
||||
std::unique_ptr<Node> parse_create_table();
|
||||
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_update_table();
|
||||
|
||||
std::unique_ptr<Node> parse_load_table();
|
||||
|
||||
std::unique_ptr<Node> parse_where_clause();
|
||||
|
||||
std::unique_ptr<Node> parse_operand_node();
|
||||
|
||||
RelationalOperatorType parse_relational_operator();
|
||||
|
||||
LogicalOperatorType parse_logical_operator();
|
||||
|
||||
ArithmeticalOperatorType parse_arithmetical_operator();
|
||||
|
||||
private:
|
||||
|
|
@ -226,3 +277,5 @@ private:
|
|||
std::unique_ptr<Node> parse_relational_expression();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
10
row.cpp
10
row.cpp
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
#include "row.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Row::Row(int cols_count) {
|
||||
m_columns.reserve(cols_count);
|
||||
|
|
@ -47,7 +48,12 @@ void Row::setColumnValue(int col_index, std::string value) {
|
|||
};
|
||||
|
||||
void Row::print() {
|
||||
for(int i=0; i<m_columns.size(); i++) {
|
||||
m_columns[i].get()->print();
|
||||
for (int ci = 0; ci < m_columns.size(); ci++) {
|
||||
if (ci > 0) std::cout << ",";
|
||||
auto v = m_columns[ci]->stringValue();
|
||||
std::cout << v;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
42
row.h
42
row.h
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class ColumnValue {
|
||||
|
||||
|
||||
|
|
@ -18,19 +20,21 @@ private:
|
|||
};
|
||||
|
||||
|
||||
|
||||
struct ColValue {
|
||||
|
||||
virtual bool isNull() { return false; };
|
||||
|
||||
virtual bool isInteger() { return false; };
|
||||
|
||||
virtual bool isFloat() { return false; };
|
||||
|
||||
virtual bool isString() { return false; };
|
||||
|
||||
virtual int integerValue() { throw Exception("Not supported"); };
|
||||
virtual double floatValue() { throw Exception("Not supported"); };
|
||||
virtual std::string stringValue() { throw Exception("Not supported"); };
|
||||
|
||||
virtual void print() {std::cout << "ColValue:" << std::endl; };
|
||||
virtual double floatValue() { throw Exception("Not supported"); };
|
||||
|
||||
virtual std::string stringValue() { throw Exception("Not supported"); };
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -38,22 +42,23 @@ struct ColNullValue : ColValue {
|
|||
|
||||
virtual bool isNull() { return true; };
|
||||
|
||||
virtual void print() {std::cout << "ColNullValue:" << std::endl; };
|
||||
virtual std::string stringValue() { return "null"; };
|
||||
};
|
||||
|
||||
|
||||
struct ColIntegerValue : ColValue {
|
||||
|
||||
ColIntegerValue(int value) : m_integer(value) {};
|
||||
|
||||
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}
|
||||
|
||||
virtual bool isInteger() { return true; };
|
||||
|
||||
virtual int integerValue() { return m_integer; };
|
||||
virtual double floatValue() { return (double) m_integer; };
|
||||
virtual std::string stringValue() { return std::to_string(m_integer); };
|
||||
|
||||
virtual void print() {std::cout << "ColIntegerValue: " << m_integer <<std::endl; };
|
||||
virtual double floatValue() { return (double) m_integer; };
|
||||
|
||||
virtual std::string stringValue() { return std::to_string(m_integer); };
|
||||
|
||||
int m_integer;
|
||||
};
|
||||
|
|
@ -62,15 +67,16 @@ struct ColIntegerValue : ColValue {
|
|||
struct ColFloatValue : ColValue {
|
||||
|
||||
ColFloatValue(double value) : m_float(value) {};
|
||||
|
||||
ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {}
|
||||
|
||||
virtual bool isFloat() { return true; }
|
||||
|
||||
virtual int integerValue() { return (int) m_float; };
|
||||
virtual double floatValue() { return m_float; };
|
||||
virtual std::string stringValue() { return std::to_string(m_float); };
|
||||
|
||||
virtual void print() {std::cout << "ColFloatValue: " << m_float <<std::endl; };
|
||||
virtual double floatValue() { return m_float; };
|
||||
|
||||
virtual std::string stringValue() { return std::to_string(m_float); };
|
||||
|
||||
double m_float;
|
||||
};
|
||||
|
|
@ -79,30 +85,34 @@ struct ColFloatValue : ColValue {
|
|||
struct ColStringValue : ColValue {
|
||||
|
||||
ColStringValue(const std::string value) : m_string(value) {};
|
||||
|
||||
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
|
||||
|
||||
virtual bool isString() { return true; }
|
||||
|
||||
virtual int integerValue() { return std::stoi(m_string); };
|
||||
virtual double floatValue() { return std::stod(m_string); };
|
||||
virtual std::string stringValue() { return m_string; };
|
||||
|
||||
virtual void print() {std::cout << "ColStringValue: " << m_string <<std::endl; };
|
||||
virtual double floatValue() { return std::stod(m_string); };
|
||||
|
||||
virtual std::string stringValue() { return m_string; };
|
||||
|
||||
std::string m_string;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Row {
|
||||
|
||||
public:
|
||||
Row(int cols_count);
|
||||
|
||||
Row(const Row &other);
|
||||
|
||||
Row &operator=(Row other);
|
||||
|
||||
void setColumnValue(int col_index, int value);
|
||||
|
||||
void setColumnValue(int col_index, double value);
|
||||
|
||||
void setColumnValue(int col_index, std::string value);
|
||||
|
||||
ColValue &operator[](int i) {
|
||||
|
|
@ -118,3 +128,5 @@ public:
|
|||
private:
|
||||
std::vector<std::unique_ptr<ColValue>> m_columns;
|
||||
};
|
||||
|
||||
}
|
||||
11
table.cpp
11
table.cpp
|
|
@ -1,6 +1,8 @@
|
|||
|
||||
#include "table.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Table::Table(const std::string name, const std::vector<ColDefNode> columns) {
|
||||
m_name = name;
|
||||
m_col_defs = columns;
|
||||
|
|
@ -26,11 +28,7 @@ Row Table::createEmptyRow() {
|
|||
void Table::print() {
|
||||
std::cout << "** " << m_name << " **" << std::endl;
|
||||
for (auto row : m_rows) {
|
||||
for(int ci = 0; ci < columns_count(); ci++) {
|
||||
auto v = row[ci].stringValue();
|
||||
std::cout << v << ",";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
row.print();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -41,6 +39,9 @@ Table::Table(const Table& other) {
|
|||
}
|
||||
|
||||
void Table::addRow(const Row &row) {
|
||||
// TODO validate for not null values
|
||||
// todo validate for length etc
|
||||
m_rows.push_back(row);
|
||||
}
|
||||
|
||||
}
|
||||
9
table.h
9
table.h
|
|
@ -5,15 +5,16 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
// TODO make it a class
|
||||
namespace usql {
|
||||
|
||||
struct Table {
|
||||
|
||||
// public:
|
||||
Table(const Table &other);
|
||||
|
||||
Table(const std::string name, const std::vector<ColDefNode> columns);
|
||||
|
||||
ColDefNode get_column_def(const std::string &col_name);
|
||||
|
||||
int columns_count() { return m_col_defs.size(); };
|
||||
|
||||
Row createEmptyRow(); // TODO this means unnecessary copying
|
||||
|
|
@ -21,9 +22,9 @@ struct Table {
|
|||
|
||||
void print();
|
||||
|
||||
|
||||
// private:
|
||||
std::string m_name;
|
||||
std::vector<ColDefNode> m_col_defs;
|
||||
std::vector<Row> m_rows;
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue