changes here and there

This commit is contained in:
VaclavT 2021-07-12 18:45:51 +02:00
parent 5e69ce1047
commit 5e4480c767
18 changed files with 1692 additions and 1309 deletions

View File

@ -7,15 +7,15 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14") set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
project(msql) project(usql)
set(PROJECT_NAME msql) set(PROJECT_NAME usql)
set(SOURCE 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}) add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} stdc++ m) target_link_libraries(${PROJECT_NAME} stdc++ m)
target_compile_options(msql PRIVATE -g) target_compile_options(usql PRIVATE -g)

View File

@ -1,6 +1,5 @@
### TODO ### TODO
- rename it to usql
- rename Exception to UException, Table to UTable, Row to URow etc - rename Exception to UException, Table to UTable, Row to URow etc
- remove newlines from lexed string tokens - remove newlines from lexed string tokens
- unify using of float and double keywords - unify using of float and double keywords

88
csvreader.cpp Normal file
View File

@ -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);
}
}
}

31
csvreader.h Normal file
View File

@ -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);
};
}

3
data.csv Normal file
View File

@ -0,0 +1,3 @@
Ticker,Price
FDX,257.3
C,59.85
1 Ticker Price
2 FDX 257.3
3 C 59.85

View File

@ -1,9 +1,11 @@
#include "exception.h" #include "exception.h"
namespace usql {
Exception::Exception(const std::string &msg) { Exception::Exception(const std::string &msg) {
cause = msg; cause = msg;
}
const char *Exception::what() const noexcept { return cause.c_str(); }
} }
const char* Exception::what() const noexcept { return cause.c_str(); }

View File

@ -4,12 +4,16 @@
#include <string> #include <string>
class Exception : public std::exception { namespace usql {
private:
class Exception : public std::exception {
private:
std::string cause; std::string cause;
public: public:
Exception(const std::string &msg); Exception(const std::string &msg);
const char* what() const noexcept; const char *what() const noexcept;
}; };
}

View File

@ -1,26 +1,46 @@
#include "executor.h" #include "executor.h"
#include "exception.h" #include "exception.h"
#include "csvreader.h"
#include <algorithm> #include <algorithm>
#include <fstream>
namespace usql {
Executor::Executor() { Executor::Executor() {
m_tables.clear(); m_tables.clear();
} }
Table* Executor::find_table(const std::string name) {
auto name_cmp = [name](Table t){ return t.m_name == name; }; Table *Executor::find_table(const std::string name) {
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp ); 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);
if (table_def != std::end(m_tables)) { if (table_def != std::end(m_tables)) {
return table_def.operator->(); return table_def.operator->();
} else { } else {
throw Exception("table not found (" + name + ")"); throw Exception("table not found (" + name + ")");
} }
} }
bool Executor::execute(Node& node) { std::unique_ptr<Table> Executor::create_stmt_result_table(int code, std::string text) {
// TODO optimize node here 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) { switch (node.node_type) {
case NodeType::create_table: case NodeType::create_table:
return execute_create_table(static_cast<CreateTableNode &>(node)); return execute_create_table(static_cast<CreateTableNode &>(node));
@ -31,35 +51,37 @@ bool Executor::execute(Node& node) {
case NodeType::delete_from: case NodeType::delete_from:
return execute_delete(static_cast<DeleteFromTableNode &>(node)); return execute_delete(static_cast<DeleteFromTableNode &>(node));
case NodeType::update_table: case NodeType::update_table:
return execute_update(static_cast<UpdateTableNode&>(node)); return execute_update(static_cast<UpdateTableNode &>(node));
case NodeType::load_table:
return execute_load(static_cast<LoadIntoTableNode &>(node));
default: default:
// TODO error message return create_stmt_result_table(-1, "unknown statement");
return false;
} }
} }
bool Executor::execute_create_table(CreateTableNode& node) {
std::unique_ptr<Table> Executor::execute_create_table(CreateTableNode &node) {
// TODO check table does not exists // TODO check table does not exists
Table table{node.table_name, node.cols_defs}; Table table{node.table_name, node.cols_defs};
m_tables.push_back(table); 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 // TODO check column names.size = values.size
// find table // find table
Table* table_def = find_table(node.table_name); Table *table_def = find_table(node.table_name);
// prepare empty new_row // prepare empty new_row
Row new_row = table_def->createEmptyRow(); Row new_row = table_def->createEmptyRow();
// copy values // copy values
for(size_t i=0; i<node.cols_names.size(); i++) { 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(node.cols_names[i].name);
ColDefNode col_def = table_def->get_column_def(colNameNode.name);
// TODO validate value // TODO validate value
@ -72,25 +94,24 @@ bool Executor::execute_insert_into_table(InsertIntoTableNode& node) {
} }
} }
// TODO check not null columns
// append new_row // append new_row
table_def->addRow(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 // TODO create plan for accessing rows
// find source table // find source table
Table* table = find_table(node.table_name); Table *table = find_table(node.table_name);
// create result table // create result table
std::vector<ColDefNode> result_tbl_col_defs{}; std::vector<ColDefNode> result_tbl_col_defs{};
std::vector<int> source_table_col_index{}; std::vector<int> source_table_col_index{};
int i = 0; // new column order int i = 0; // new column order
for(ColNameNode rc : node.cols_names) { for (ColNameNode rc : node.cols_names) {
ColDefNode cdef = table->get_column_def(rc.name); ColDefNode cdef = table->get_column_def(rc.name);
source_table_col_index.push_back(cdef.order); source_table_col_index.push_back(cdef.order);
@ -99,21 +120,22 @@ bool Executor::execute_select(SelectFromTableNode& node) {
i++; i++;
} }
Table result {"result", result_tbl_col_defs}; auto result = std::make_unique<Table>("result", result_tbl_col_defs);
// execute access plan // execute access plan
for (auto row = begin (table->m_rows); row != end (table->m_rows); ++row) { for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
// eval where for row // eval where for row
if (evalWhere(node.where.get(), table, row)) { if (evalWhere(node.where.get(), table, row)) {
// prepare empty row // prepare empty row
Row new_row = result.createEmptyRow(); Row new_row = result->createEmptyRow();
// copy column values // 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]; auto row_col_index = source_table_col_index[idx];
ColValue *col_value = row->ithColumn(row_col_index); ColValue *col_value = row->ithColumn(row_col_index);
if (result_tbl_col_defs[idx].type == ColumnType::integer_type) 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) if (result_tbl_col_defs[idx].type == ColumnType::float_type)
new_row.setColumnValue(idx, col_value->floatValue()); new_row.setColumnValue(idx, col_value->floatValue());
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type) if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
@ -121,24 +143,23 @@ bool Executor::execute_select(SelectFromTableNode& node) {
} }
// add row to result // add row to result
result.m_rows.push_back(new_row); result->m_rows.push_back(new_row);
} }
} }
result.print(); return std::move(result);
}
return true;
}
bool Executor::execute_delete(DeleteFromTableNode& node) { std::unique_ptr<Table> Executor::execute_delete(DeleteFromTableNode &node) {
// TODO create plan for accessing rows // TODO create plan for accessing rows
// find source table // find source table
Table* table = find_table(node.table_name); Table *table = find_table(node.table_name);
// execute access plan // execute access plan
auto it = table->m_rows.begin(); auto it = table->m_rows.begin();
for ( ; it != table->m_rows.end(); ) { for (; it != table->m_rows.end();) {
if (evalWhere(node.where.get(), table, it)) { if (evalWhere(node.where.get(), table, it)) {
// TODO this can be really expensive operation // TODO this can be really expensive operation
it = table->m_rows.erase(it); it = table->m_rows.erase(it);
@ -147,31 +168,35 @@ 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 // TODO create plan for accessing rows
// find source table // find source table
Table* table = find_table(node.table_name); Table *table = find_table(node.table_name);
// execute access plan // execute access plan
for (auto row = begin (table->m_rows); row != end (table->m_rows); ++row) { for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
// eval where for row // eval where for row
if (evalWhere(node.where.get(), table, row)) { if (evalWhere(node.where.get(), table, row)) {
// TODO do update
int i = 0; int i = 0;
for(auto col : node.cols_names) { for (auto col : node.cols_names) {
// TODO cache it like in select // TODO cache it like in select
ColDefNode cdef = table->get_column_def(col.name); 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) { 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) { } 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 { } else {
throw Exception("Implement me!"); throw Exception("Implement me!");
} }
@ -180,80 +205,90 @@ bool Executor::execute_update(UpdateTableNode &node) {
} }
} }
return true; return create_stmt_result_table(0, "delete succeeded");
} }
bool Executor::evalWhere(Node *where, Table *table, 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");
}
bool Executor::evalWhere(Node *where, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const { std::vector<Row, std::allocator<Row>>::iterator &row) const {
switch (where->node_type) { // no where clause switch (where->node_type) { // no where clause
case NodeType::true_node: case NodeType::true_node:
return true; return true;
case NodeType::relational_operator: // just one condition case NodeType::relational_operator: // just one condition
return evalRelationalOperator(*((RelationalOperatorNode *)where), table, row); return evalRelationalOperator(*((RelationalOperatorNode *) where), table, row);
case NodeType::logical_operator: case NodeType::logical_operator:
return evalLogicalOperator(*((LogicalOperatorNode *)where), table, row); return evalLogicalOperator(*((LogicalOperatorNode *) where), table, row);
default: default:
throw Exception("Wrong node type"); throw Exception("Wrong node type");
} }
return false; 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()); bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
std::unique_ptr<Node> right_value = evalNode(table, row, filter.right.get()); 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; double comparator;
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) { if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) {
auto lvalue = static_cast<IntValueNode *>(left_value.get()); comparator = left_value->getIntValue() - right_value->getIntValue();
auto rvalue = static_cast<IntValueNode *>(right_value.get()); } else if ((left_value->node_type == NodeType::int_value &&
comparator = lvalue->value - rvalue->value; right_value->node_type == NodeType::float_value) ||
} (left_value->node_type == NodeType::float_value &&
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) { right_value->node_type == NodeType::int_value) ||
auto *lvalue = static_cast<IntValueNode *>(left_value.get()); (left_value->node_type == NodeType::float_value &&
auto *rvalue = static_cast<FloatValueNode *>(right_value.get()); right_value->node_type == NodeType::float_value)) {
comparator = (double)lvalue->value - rvalue->value; comparator = left_value->getDoubleValue() - right_value->getDoubleValue();
} } else if (left_value->node_type == NodeType::string_value ||
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::string_value) { right_value->node_type == NodeType::string_value) {
auto *lvalue = static_cast<IntValueNode *>(left_value.get()); comparator = left_value->getStringValue().compare(right_value->getStringValue());
auto *rvalue = static_cast<StringValueNode *>(right_value.get()); } else {
comparator = std::to_string(lvalue->value).compare(rvalue->value); // TODO throw exception
}
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);
} }
@ -273,12 +308,16 @@ bool Executor::evalRelationalOperator(const RelationalOperatorNode &filter, Tabl
} }
throw Exception("invalid relational operator"); 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) { if (node->node_type == NodeType::database_value) {
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node); 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); auto db_value = row->ithColumn(col_def.order);
if (col_def.type == ColumnType::integer_type) { if (col_def.type == ColumnType::integer_type) {
@ -296,35 +335,85 @@ std::unique_ptr<Node> Executor::evalNode(Table *table, std::vector<Row, std::all
return std::make_unique<IntValueNode>(ivl->value); return std::make_unique<IntValueNode>(ivl->value);
} else if (node->node_type == NodeType::float_value) { } else if (node->node_type == NodeType::float_value) {
FloatValueNode *ivl = static_cast<FloatValueNode*>(node); FloatValueNode *ivl = static_cast<FloatValueNode *>(node);
return std::make_unique<FloatValueNode>(ivl->value); return std::make_unique<FloatValueNode>(ivl->value);
} else if (node->node_type == NodeType::string_value) { } else if (node->node_type == NodeType::string_value) {
StringValueNode *ivl = static_cast<StringValueNode*>(node); StringValueNode *ivl = static_cast<StringValueNode *>(node);
return std::make_unique<StringValueNode>(ivl->value); return std::make_unique<StringValueNode>(ivl->value);
} }
throw Exception("invalid type"); throw Exception("invalid type");
} }
bool Executor::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
bool Executor::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
std::vector<Row, std::allocator<Row>>::iterator &iter) const { std::vector<Row, std::allocator<Row>>::iterator &iter) const {
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter); 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; return left;
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter); bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter);
return right; return right;
} }
std::unique_ptr<Node> Executor::evalArithmetic(ArithmeticalOperatorNode &node, Table *table,
std::unique_ptr<ValueNode>
Executor::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const { std::vector<Row, std::allocator<Row>>::iterator &row) const {
if (node.op == ArithmeticalOperatorType::copy_value) {
switch (node.op) {
case ArithmeticalOperatorType::copy_value:
return evalNode(table, row, node.left.get()); 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: default:
throw Exception("implement me!!"); 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!!");
}
} }

View File

@ -5,30 +5,40 @@
#include <string> #include <string>
class Executor { namespace usql {
private:
public: class Executor {
private:
public:
Executor(); Executor();
bool execute(Node& node); std::unique_ptr<Table> execute(Node &node);
private: private:
bool execute_create_table(CreateTableNode& node); std::unique_ptr<Table> 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);
Table* find_table(const std::string name); std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
private:
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; std::vector<Table> m_tables;
bool evalWhere(Node *where, Table *table, bool evalWhere(Node *where, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const; std::vector<Row, std::allocator<Row>>::iterator &row) const;
std::unique_ptr<Node> std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
Node *node) const; Node *node) const;
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
@ -37,6 +47,8 @@ private:
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
std::vector<Row, std::allocator<Row>>::iterator &iter) const; 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; std::vector<Row, std::allocator<Row>>::iterator &row) const;
}; };
}

130
lexer.cpp
View File

@ -3,13 +3,25 @@
#include <algorithm> #include <algorithm>
namespace usql {
Token::Token(const std::string &token_str, TokenType typ) { Token::Token(const std::string &token_str, TokenType typ) {
token_string = token_str; token_string = token_str;
type = typ; type = typ;
} }
void Lexer::parse(const std::string &code) {
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 // TODO handle empty code
m_tokens.clear(); m_tokens.clear();
@ -19,14 +31,10 @@ void Lexer::parse(const std::string &code) {
} }
m_code_str = code; m_code_str = code;
if (!m_code_str.empty() && m_code_str.back() != '\n') { 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 auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), k_words_regex);
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_end = std::sregex_iterator(); auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) { for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
@ -44,71 +52,71 @@ void Lexer::parse(const std::string &code) {
// debugTokens(); // debugTokens();
m_index = 0; m_index = 0;
} }
void Lexer::debugTokens() { void Lexer::debugTokens() {
int i = 0; int i = 0;
for (std::vector<Token>::iterator it = m_tokens.begin(); it != m_tokens.end(); ++it) { for (std::vector<Token>::iterator it = m_tokens.begin(); it != m_tokens.end(); ++it) {
std::cerr << i << "\t" << it->token_string << std::endl; std::cerr << i << "\t" << it->token_string << std::endl;
i++; i++;
} }
} }
Token Lexer::currentToken() { return m_tokens[m_index]; } Token Lexer::currentToken() { return m_tokens[m_index]; }
Token Lexer::consumeCurrentToken() { Token Lexer::consumeCurrentToken() {
int i = m_index; int i = m_index;
nextToken(); nextToken();
return m_tokens[i]; return m_tokens[i];
} }
void Lexer::nextToken() { void Lexer::nextToken() {
if (m_index < m_tokens.size()) { if (m_index < m_tokens.size()) {
m_index++; m_index++;
} }
} }
void Lexer::skipToken(TokenType type) { void Lexer::skipToken(TokenType type) {
if (tokenType() == type) { if (tokenType() == type) {
nextToken(); nextToken();
} else { } 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));
}
} }
}
void Lexer::skipTokenOptional(TokenType type) { void Lexer::skipTokenOptional(TokenType type) {
if (tokenType() == type) { if (tokenType() == type) {
nextToken(); nextToken();
} }
} }
TokenType Lexer::tokenType() { return m_index < m_tokens.size() ? currentToken().type : TokenType::eof; } 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; } TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; }
bool Lexer::isRelationalOperator(TokenType token_type) { 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); token_type == TokenType::lesser || token_type == TokenType::lesser_equal);
} }
bool Lexer::isLogicalOperator(TokenType token_type) { bool Lexer::isLogicalOperator(TokenType token_type) {
return (token_type == TokenType::logical_and || token_type == TokenType::logical_or); return (token_type == TokenType::logical_and || token_type == TokenType::logical_or);
} }
bool Lexer::isArithmeticalOperator(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) { TokenType Lexer::type(const std::string &token) {
// TODO move it to class level not to reinit it again and again // TODO, FIXME 'one is evaluated as identifier
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
if (token == ";") if (token == ";")
return TokenType::semicolon; return TokenType::semicolon;
@ -184,6 +192,9 @@ TokenType Lexer::type(const std::string &token) {
if (token == "update") if (token == "update")
return TokenType::keyword_update; return TokenType::keyword_update;
if (token == "load")
return TokenType::keyword_load;
if (token == "not") if (token == "not")
return TokenType::keyword_not; return TokenType::keyword_not;
@ -211,7 +222,8 @@ TokenType Lexer::type(const std::string &token) {
if (token == "\n" || token == "\r\n" || token == "\r") if (token == "\n" || token == "\r\n" || token == "\r")
return TokenType::newline; 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; return TokenType::comment;
// if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"') // if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"')
@ -220,27 +232,27 @@ TokenType Lexer::type(const std::string &token) {
if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'') if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'')
return TokenType::string_literal; return TokenType::string_literal;
if (std::regex_match(token, int_regex)) if (std::regex_match(token, k_int_regex))
return TokenType::int_number; 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; return TokenType::int_number;
if (std::regex_match(token, double_regex)) if (std::regex_match(token, k_double_regex))
return TokenType::double_number; return TokenType::double_number;
if (std::regex_match(token, identifier_regex)) if (std::regex_match(token, k_identifier_regex))
return TokenType::identifier; return TokenType::identifier;
if (m_index + 1 >= m_tokens.size()) if (m_index + 1 >= m_tokens.size())
return TokenType::eof; return TokenType::eof;
return TokenType::undef; return TokenType::undef;
} }
std::string Lexer::stringLiteral(std::string token) { std::string Lexer::stringLiteral(std::string token) {
// remove ' or " from the literal ends // remove ' or " from the literal ends
bool replace = token[0]=='\'' && token[token.size()-1]=='\''; bool replace = token[0] == '\'' && token[token.size() - 1] == '\'';
std::string str = token.substr(1, token.size() - 2); std::string str = token.substr(1, token.size() - 2);
if (!replace) { if (!replace) {
@ -250,19 +262,19 @@ std::string Lexer::stringLiteral(std::string token) {
out.reserve(str.size()); out.reserve(str.size());
for(std::string::size_type i = 0; i < str.size(); ++i) { for (std::string::size_type i = 0; i < str.size(); ++i) {
if (str[i] == '\'' && i < str.size() - 1) { if (str[i] == '\'' && i < str.size() - 1) {
if (str[i+1] == '\'') { if (str[i + 1] == '\'') {
out.append(1, '\''); out.append(1, '\'');
i++; i++;
} else { } else {
out.append(1, str[i]); out.append(1, str[i]);
} }
} else if (str[i] == '\\' && i < str.size() - 1) { } else if (str[i] == '\\' && i < str.size() - 1) {
if (str[i+1] == 'n') { if (str[i + 1] == 'n') {
out.append(1, '\n'); out.append(1, '\n');
i++; i++;
} else if (str[i+1] == 't') { } else if (str[i + 1] == 't') {
out.append(1, '\t'); out.append(1, '\t');
i++; i++;
} else { } else {
@ -273,9 +285,9 @@ std::string Lexer::stringLiteral(std::string token) {
} }
} }
return out; return out;
} }
std::string Lexer::typeToString(TokenType token_type) { std::string Lexer::typeToString(TokenType token_type) {
std::string txt; std::string txt;
switch (token_type) { switch (token_type) {
case TokenType::undef: case TokenType::undef:
@ -338,6 +350,12 @@ std::string Lexer::typeToString(TokenType token_type) {
case TokenType::keyword_copy: case TokenType::keyword_copy:
txt = "copy"; txt = "copy";
break; break;
case TokenType::keyword_update:
txt = "update";
break;
case TokenType::keyword_load:
txt = "load";
break;
case TokenType::keyword_not: case TokenType::keyword_not:
txt = "not"; txt = "not";
break; break;
@ -394,4 +412,6 @@ std::string Lexer::typeToString(TokenType token_type) {
break; break;
} }
return txt; return txt;
}
} }

42
lexer.h
View File

@ -5,7 +5,9 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
enum class TokenType { namespace usql {
enum class TokenType {
undef, undef,
identifier, identifier,
plus, plus,
@ -23,6 +25,7 @@ enum class TokenType {
keyword_where, keyword_where,
keyword_delete, keyword_delete,
keyword_update, keyword_update,
keyword_load,
keyword_from, keyword_from,
keyword_insert, keyword_insert,
keyword_into, keyword_into,
@ -48,46 +51,63 @@ enum class TokenType {
newline, newline,
comment, comment,
eof eof
}; };
struct Token { struct Token {
std::string token_string; std::string token_string;
TokenType type; TokenType type;
Token(const std::string &token_str, TokenType typ);
};
class Lexer { Token(const std::string &token_str, TokenType typ);
public: };
Lexer() {};
class Lexer {
public:
Lexer();
void parse(const std::string &code); void parse(const std::string &code);
void debugTokens(); void debugTokens();
Token currentToken(); Token currentToken();
Token consumeCurrentToken(); Token consumeCurrentToken();
void nextToken(); void nextToken();
void skipToken(TokenType type); void skipToken(TokenType type);
void skipTokenOptional(TokenType type); void skipTokenOptional(TokenType type);
TokenType tokenType(); TokenType tokenType();
TokenType nextTokenType(); TokenType nextTokenType();
TokenType prevTokenType(); TokenType prevTokenType();
static bool isRelationalOperator(TokenType token_type); static bool isRelationalOperator(TokenType token_type);
static bool isLogicalOperator(TokenType token_type); static bool isLogicalOperator(TokenType token_type);
static bool isArithmeticalOperator(TokenType token_type); static bool isArithmeticalOperator(TokenType token_type);
private: private:
TokenType type(const std::string &token); TokenType type(const std::string &token);
std::string stringLiteral(std::string token); std::string stringLiteral(std::string token);
static std::string typeToString(TokenType token_type); static std::string typeToString(TokenType token_type);
private: private:
std::string m_code_str; std::string m_code_str;
std::vector<Token> m_tokens; std::vector<Token> m_tokens;
int m_index = 0; 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;
};
}

View File

@ -8,10 +8,10 @@
// drop table // drop table
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Parser parser{}; usql::Parser parser{};
Executor executor{}; usql::Executor executor{};
std::vector<std::string> sql_commands { std::vector<std::string> sql_commands{
"create table a (i integer not null, s varchar(64), f float null)", "create table a (i integer not null, s varchar(64), f float null)",
"insert into a (i, s) values(1, 'one')", "insert into a (i, s) values(1, 'one')",
"insert into a (i, s) values(2, 'two')", "insert into a (i, s) values(2, 'two')",
@ -26,15 +26,22 @@ int main(int argc, char *argv[]) {
"delete from a where i = 4", "delete from a where i = 4",
"select i, s from a where i > 0", "select i, s from a where i > 0",
"update a set f = 9.99 where i = 3", "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, f from a where i = 3" "update a set s = 'three', f = f + 0.01 where i = 3",
// "select i, s from a where i > 0" "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) { for (auto command : sql_commands) {
std::cout << command << std::endl;
auto node = parser.parse(command); auto node = parser.parse(command);
executor.execute(*node); auto result = executor.execute(*node);
result->print();
// std::cout << std::endl;
} }
return 0; return 0;

View File

@ -1,34 +1,43 @@
#include "parser.h" #include "parser.h"
#include "exception.h" #include "exception.h"
namespace usql {
// TOOD handle premature eof // TOOD handle premature eof
Parser::Parser() { Parser::Parser() {
lexer = Lexer{}; lexer = Lexer{};
} }
std::unique_ptr<Node> Parser::parse(const std::string &code) { std::unique_ptr<Node> Parser::parse(const std::string &code) {
lexer.parse(code); lexer.parse(code);
// lexer.debugTokens(); // lexer.debugTokens();
if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) { if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
return parse_create_table(); return parse_create_table();
} if (lexer.tokenType() == TokenType::keyword_insert) { }
if (lexer.tokenType() == TokenType::keyword_insert) {
return parse_insert_into_table(); return parse_insert_into_table();
} if (lexer.tokenType() == TokenType::keyword_select) { }
if (lexer.tokenType() == TokenType::keyword_select) {
return parse_select_from_table(); return parse_select_from_table();
} if (lexer.tokenType() == TokenType::keyword_delete) { }
if (lexer.tokenType() == TokenType::keyword_delete) {
return parse_delete_from_table(); return parse_delete_from_table();
} if (lexer.tokenType() == TokenType::keyword_update) { }
if (lexer.tokenType() == TokenType::keyword_update) {
return parse_update_table(); return parse_update_table();
} }
if (lexer.tokenType() == TokenType::keyword_load) {
return parse_load_table();
}
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl; std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error); return std::make_unique<Node>(NodeType::error);
} }
std::unique_ptr<Node> Parser::parse_create_table() { std::unique_ptr<Node> Parser::parse_create_table() {
std::vector<ColDefNode> cols_def {}; std::vector<ColDefNode> cols_def{};
lexer.skipToken(TokenType::keyword_create); lexer.skipToken(TokenType::keyword_create);
lexer.skipToken(TokenType::keyword_table); lexer.skipToken(TokenType::keyword_table);
@ -41,14 +50,14 @@ std::unique_ptr<Node> Parser::parse_create_table() {
do { do {
std::string column_name; std::string column_name;
ColumnType column_type; ColumnType column_type;
int column_len {1}; int column_len{1};
bool column_nullable {true}; bool column_nullable{true};
// column name // column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ } if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string; column_name = lexer.consumeCurrentToken().token_string;
// column type and optionaly len // column type and optionally len
if (lexer.tokenType() == TokenType::keyword_int) { if (lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type; column_type = ColumnType::integer_type;
lexer.nextToken(); lexer.nextToken();
@ -73,7 +82,8 @@ std::unique_ptr<Node> Parser::parse_create_table() {
lexer.nextToken(); 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); lexer.skipTokenOptional(TokenType::comma);
@ -83,13 +93,13 @@ std::unique_ptr<Node> Parser::parse_create_table() {
return std::make_unique<CreateTableNode>(table_name, cols_def); return std::make_unique<CreateTableNode>(table_name, cols_def);
} }
std::unique_ptr<Node> Parser::parse_insert_into_table() { std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<Node> exec_code {}; std::vector<Node> exec_code{};
std::vector<ColNameNode> cols_names {}; std::vector<ColNameNode> cols_names{};
std::vector<ColValueNode> cols_values {}; std::vector<ColValueNode> cols_values{};
lexer.skipToken(TokenType::keyword_insert); lexer.skipToken(TokenType::keyword_insert);
lexer.skipToken(TokenType::keyword_into); lexer.skipToken(TokenType::keyword_into);
@ -120,10 +130,10 @@ std::unique_ptr<Node> Parser::parse_insert_into_table() {
lexer.skipToken(TokenType::close_paren); lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values); return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values);
} }
std::unique_ptr<Node> Parser::parse_select_from_table() { std::unique_ptr<Node> Parser::parse_select_from_table() {
std::vector<ColNameNode> cols_names {}; std::vector<ColNameNode> cols_names{};
lexer.skipToken(TokenType::keyword_select); lexer.skipToken(TokenType::keyword_select);
while (lexer.tokenType() != TokenType::keyword_from) { while (lexer.tokenType() != TokenType::keyword_from) {
@ -141,9 +151,9 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
// if (lexer.tokenType() == TokenType::keyword_limit) {} // if (lexer.tokenType() == TokenType::keyword_limit) {}
return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node)); return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node));
} }
std::unique_ptr<Node> Parser::parse_delete_from_table() { std::unique_ptr<Node> Parser::parse_delete_from_table() {
lexer.skipToken(TokenType::keyword_delete); lexer.skipToken(TokenType::keyword_delete);
lexer.skipToken(TokenType::keyword_from); lexer.skipToken(TokenType::keyword_from);
@ -152,9 +162,9 @@ std::unique_ptr<Node> Parser::parse_delete_from_table() {
std::unique_ptr<Node> where_node = parse_where_clause(); std::unique_ptr<Node> where_node = parse_where_clause();
return std::make_unique<DeleteFromTableNode>(table_name, std::move(where_node)); return std::make_unique<DeleteFromTableNode>(table_name, std::move(where_node));
} }
std::unique_ptr<Node> Parser::parse_update_table() { std::unique_ptr<Node> Parser::parse_update_table() {
lexer.skipToken(TokenType::keyword_update); lexer.skipToken(TokenType::keyword_update);
lexer.skipTokenOptional(TokenType::keyword_table); lexer.skipTokenOptional(TokenType::keyword_table);
@ -174,10 +184,13 @@ std::unique_ptr<Node> Parser::parse_update_table() {
ArithmeticalOperatorType op = parse_arithmetical_operator(); ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node(); 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 { } else {
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0); 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); lexer.skipTokenOptional(TokenType::comma);
@ -186,9 +199,22 @@ std::unique_ptr<Node> Parser::parse_update_table() {
std::unique_ptr<Node> where_node = parse_where_clause(); std::unique_ptr<Node> where_node = parse_where_clause();
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node)); return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
} }
std::unique_ptr<Node> Parser::parse_where_clause() { 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 multiple filters
// TODO add support for parenthesis // TODO add support for parenthesis
@ -209,17 +235,17 @@ std::unique_ptr<Node> Parser::parse_where_clause() {
} while (lexer.tokenType() != TokenType::eof); // until whole where clause parsed } while (lexer.tokenType() != TokenType::eof); // until whole where clause parsed
return node; return node;
} }
std::unique_ptr<Node> Parser::parse_relational_expression() { std::unique_ptr<Node> Parser::parse_relational_expression() {
auto left = parse_operand_node(); auto left = parse_operand_node();
auto operation = parse_relational_operator(); auto operation = parse_relational_operator();
auto right = parse_operand_node(); auto right = parse_operand_node();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right)); return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
} }
std::unique_ptr<Node> Parser::parse_operand_node() { std::unique_ptr<Node> Parser::parse_operand_node() {
// while not end or order or limit // while not end or order or limit
auto token_type = lexer.tokenType(); auto token_type = lexer.tokenType();
std::string tokenString = lexer.consumeCurrentToken().token_string; std::string tokenString = lexer.consumeCurrentToken().token_string;
@ -232,12 +258,12 @@ std::unique_ptr<Node> Parser::parse_operand_node() {
return std::make_unique<StringValueNode>(tokenString); return std::make_unique<StringValueNode>(tokenString);
case TokenType::identifier: case TokenType::identifier:
return std::make_unique<DatabaseValueNode>(tokenString); return std::make_unique<DatabaseValueNode>(tokenString);
default: ; default:;
throw Exception("Unknown operand node"); throw Exception("Unknown operand node");
} }
} }
RelationalOperatorType Parser::parse_relational_operator() { RelationalOperatorType Parser::parse_relational_operator() {
auto op = lexer.consumeCurrentToken(); auto op = lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::equal: case TokenType::equal:
@ -255,8 +281,9 @@ RelationalOperatorType Parser::parse_relational_operator() {
default: default:
throw Exception("Unknown relational operator"); throw Exception("Unknown relational operator");
} }
} }
LogicalOperatorType Parser::parse_logical_operator() {
LogicalOperatorType Parser::parse_logical_operator() {
auto op = lexer.consumeCurrentToken(); auto op = lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::logical_and: case TokenType::logical_and:
@ -266,14 +293,22 @@ LogicalOperatorType Parser::parse_logical_operator() {
default: default:
throw Exception("Unknown logical operator"); throw Exception("Unknown logical operator");
} }
} }
ArithmeticalOperatorType Parser::parse_arithmetical_operator() { ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
auto op = lexer.consumeCurrentToken(); auto op = lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::plus: case TokenType::plus:
return ArithmeticalOperatorType::plus_operator; 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: default:
throw Exception("Unknown arithmetical operator"); throw Exception("Unknown arithmetical operator");
} }
}
} }

171
parser.h
View File

@ -6,14 +6,16 @@
#include <vector> #include <vector>
namespace usql {
enum class ColumnType {
enum class ColumnType {
integer_type, integer_type,
float_type, float_type,
varchar_type varchar_type
}; };
enum class NodeType { enum class NodeType {
true_node, true_node,
int_value, int_value,
float_value, float_value,
@ -27,35 +29,36 @@ enum class NodeType {
select_from, select_from,
delete_from, delete_from,
update_table, update_table,
load_table,
column_name, column_name,
column_value, column_value,
column_def, column_def,
not_implemented_yet, not_implemented_yet,
error error
}; };
struct Node { struct Node {
NodeType node_type; NodeType node_type;
Node(const NodeType type) : node_type(type) {} Node(const NodeType type) : node_type(type) {}
}; };
struct ColNameNode : Node { struct ColNameNode : Node {
std::string name; std::string name;
ColNameNode(const std::string col_name) : ColNameNode(const std::string col_name) :
Node(NodeType::column_name), name(col_name) {} Node(NodeType::column_name), name(col_name) {}
}; };
struct ColValueNode : Node { struct ColValueNode : Node {
std::string value; std::string value;
ColValueNode(const std::string col_value) : ColValueNode(const std::string col_value) :
Node(NodeType::column_value), value(col_value) {} Node(NodeType::column_value), value(col_value) {}
}; };
// TODO add order in row // TODO add order in row
struct ColDefNode : Node { struct ColDefNode : Node {
std::string name; std::string name;
ColumnType type; ColumnType type;
int order; int order;
@ -63,55 +66,85 @@ struct ColDefNode : Node {
bool null; bool null;
ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) : 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 {
struct TrueNode : Node {
TrueNode() : Node(NodeType::true_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; int value;
IntValueNode(int value) : Node(NodeType::int_value), value(value) {} IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
};
struct FloatValueNode : Node { int getIntValue() { return value; };
double getDoubleValue() { return (double) value; };
std::string getStringValue() { return std::to_string(value); }
};
struct FloatValueNode : ValueNode {
double value; double value;
FloatValueNode(double value) : Node(NodeType::float_value), value(value) {} FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
};
struct StringValueNode : Node { int getIntValue() { return (int) value; };
double getDoubleValue() { return value; };
std::string getStringValue() { return std::to_string(value); }
};
struct StringValueNode : ValueNode {
std::string value; std::string value;
StringValueNode(std::string value) : Node(NodeType::string_value), value(value) {} StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
};
struct DatabaseValueNode : Node { int getIntValue() { return std::stoi(value); };
double getDoubleValue() { return std::stod(value); };
std::string getStringValue() { return value; };
};
struct DatabaseValueNode : Node {
std::string col_name; std::string col_name;
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {} DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
}; };
enum class LogicalOperatorType { enum class LogicalOperatorType {
and_operator, and_operator,
or_operator, or_operator,
not_operator not_operator
}; };
struct LogicalOperatorNode : Node { struct LogicalOperatorNode : Node {
LogicalOperatorType op; LogicalOperatorType op;
std::unique_ptr<Node> left; std::unique_ptr<Node> left;
std::unique_ptr<Node> right; std::unique_ptr<Node> right;
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {}; Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
}; };
enum class RelationalOperatorType { enum class RelationalOperatorType {
equal, equal,
greater, greater,
greater_equal, greater_equal,
@ -119,9 +152,9 @@ enum class RelationalOperatorType {
lesser_equal, lesser_equal,
not_equal not_equal
// like // like
}; };
struct RelationalOperatorNode : Node { struct RelationalOperatorNode : Node {
RelationalOperatorType op; RelationalOperatorType op;
std::unique_ptr<Node> left; std::unique_ptr<Node> left;
@ -129,17 +162,17 @@ struct RelationalOperatorNode : Node {
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {}; Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
}; };
enum class ArithmeticalOperatorType { enum class ArithmeticalOperatorType {
copy_value, // just copy lef value and do nothing with it copy_value, // just copy lef value and do nothing with it
plus_operator, plus_operator,
minus_operator, minus_operator,
multiply_operator, multiply_operator,
divide_operator divide_operator
}; };
struct ArithmeticalOperatorNode : Node { struct ArithmeticalOperatorNode : Node {
ArithmeticalOperatorType op; ArithmeticalOperatorType op;
std::unique_ptr<Node> left; std::unique_ptr<Node> left;
@ -147,36 +180,36 @@ struct ArithmeticalOperatorNode : Node {
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) : ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {}; Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
}; };
struct CreateTableNode : Node { struct CreateTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColDefNode> cols_defs; std::vector<ColDefNode> cols_defs;
CreateTableNode(const std::string name, std::vector<ColDefNode> defs) : CreateTableNode(const std::string name, std::vector<ColDefNode> defs) :
Node(NodeType::create_table), table_name(name), cols_defs(defs) {} Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
}; };
struct InsertIntoTableNode : Node { struct InsertIntoTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::vector<ColValueNode> cols_values; std::vector<ColValueNode> cols_values;
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) : InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {} Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
}; };
struct SelectFromTableNode : Node { struct SelectFromTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::unique_ptr<Node> where; std::unique_ptr<Node> where;
SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) : SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) :
Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {} Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {}
}; };
struct UpdateTableNode : Node { struct UpdateTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> values; std::vector<std::unique_ptr<Node>> values;
@ -184,45 +217,65 @@ struct UpdateTableNode : Node {
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals, UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
std::unique_ptr<Node> where_clause) : 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 DeleteFromTableNode : Node { 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 {
std::string table_name; std::string table_name;
std::unique_ptr<Node> where; std::unique_ptr<Node> where;
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) : DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {} Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
}; };
class Parser {
private:
public:
class Parser {
private:
public:
Parser(); Parser();
std::unique_ptr<Node> parse(const std::string &code); std::unique_ptr<Node> parse(const std::string &code);
private: private:
std::unique_ptr<Node> parse_create_table(); std::unique_ptr<Node> parse_create_table();
std::unique_ptr<Node> parse_insert_into_table(); std::unique_ptr<Node> parse_insert_into_table();
std::unique_ptr<Node> parse_select_from_table(); std::unique_ptr<Node> parse_select_from_table();
std::unique_ptr<Node> parse_delete_from_table(); std::unique_ptr<Node> parse_delete_from_table();
std::unique_ptr<Node> parse_update_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_where_clause();
std::unique_ptr<Node> parse_operand_node(); std::unique_ptr<Node> parse_operand_node();
RelationalOperatorType parse_relational_operator(); RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator(); LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator(); ArithmeticalOperatorType parse_arithmetical_operator();
private: private:
Lexer lexer; Lexer lexer;
std::unique_ptr<Node> parse_relational_expression(); std::unique_ptr<Node> parse_relational_expression();
}; };
}

56
row.cpp
View File

@ -1,15 +1,16 @@
#include "row.h" #include "row.h"
namespace usql {
Row::Row(int cols_count) { Row::Row(int cols_count) {
m_columns.reserve(cols_count); m_columns.reserve(cols_count);
for (int i = 0; i < cols_count; i++) { for (int i = 0; i < cols_count; i++) {
m_columns.push_back(std::make_unique<ColValue>()); m_columns.push_back(std::make_unique<ColValue>());
} }
} }
Row::Row(const Row &other) { Row::Row(const Row &other) {
m_columns.reserve(other.m_columns.size()); m_columns.reserve(other.m_columns.size());
// TODO fixme this is nonsense // TODO fixme this is nonsense
for (int i = 0; i < other.m_columns.size(); i++) { for (int i = 0; i < other.m_columns.size(); i++) {
@ -17,37 +18,42 @@ Row::Row(const Row &other) {
} }
for (int i = 0; i < other.m_columns.size(); i++) { for (int i = 0; i < other.m_columns.size(); i++) {
if (ColIntegerValue* other_v = dynamic_cast<ColIntegerValue*>(other.m_columns[i].get())) { if (ColIntegerValue *other_v = dynamic_cast<ColIntegerValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->integerValue()); setColumnValue(i, other_v->integerValue());
} }
if (ColFloatValue* other_v = dynamic_cast<ColFloatValue*>(other.m_columns[i].get())) { if (ColFloatValue *other_v = dynamic_cast<ColFloatValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->floatValue()); setColumnValue(i, other_v->floatValue());
} }
if (ColStringValue* other_v = dynamic_cast<ColStringValue*>(other.m_columns[i].get())) { if (ColStringValue *other_v = dynamic_cast<ColStringValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->stringValue()); setColumnValue(i, other_v->stringValue());
} }
} }
} }
Row& Row::operator=(Row other) { Row &Row::operator=(Row other) {
std::swap(m_columns, other.m_columns); std::swap(m_columns, other.m_columns);
return *this; return *this;
}
void Row::setColumnValue(int col_index, int value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}
void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColFloatValue>(value);
}
void Row::setColumnValue(int col_index, std::string value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value);
};
void Row::print() {
for(int i=0; i<m_columns.size(); i++) {
m_columns[i].get()->print();
} }
void Row::setColumnValue(int col_index, int value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}
void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColFloatValue>(value);
}
void Row::setColumnValue(int col_index, std::string value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value);
};
void Row::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;
}
} }

74
row.h
View File

@ -5,116 +5,128 @@
#include <vector> #include <vector>
class ColumnValue { namespace usql {
class ColumnValue {
private: private:
ColumnType m_type; ColumnType m_type;
union { union {
int int_value; int int_value;
double float_value; double float_value;
}; };
}; };
struct ColValue {
struct ColValue {
virtual bool isNull() { return false; }; virtual bool isNull() { return false; };
virtual bool isInteger() { return false; }; virtual bool isInteger() { return false; };
virtual bool isFloat() { return false; }; virtual bool isFloat() { return false; };
virtual bool isString() { return false; }; virtual bool isString() { return false; };
virtual int integerValue() { throw Exception("Not supported"); }; virtual int integerValue() { throw Exception("Not supported"); };
virtual double floatValue() { throw Exception("Not supported"); }; virtual double floatValue() { throw Exception("Not supported"); };
virtual std::string stringValue() { throw Exception("Not supported"); }; virtual std::string stringValue() { throw Exception("Not supported"); };
};
virtual void print() {std::cout << "ColValue:" << std::endl; };
};
struct ColNullValue : ColValue { struct ColNullValue : ColValue {
virtual bool isNull() { return true; }; virtual bool isNull() { return true; };
virtual void print() {std::cout << "ColNullValue:" << std::endl; }; virtual std::string stringValue() { return "null"; };
}; };
struct ColIntegerValue : ColValue { struct ColIntegerValue : ColValue {
ColIntegerValue(int value) : m_integer(value) {}; ColIntegerValue(int value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {} ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}
virtual bool isInteger() { return true; }; virtual bool isInteger() { return true; };
virtual int integerValue() { return m_integer; }; virtual int integerValue() { return m_integer; };
virtual double floatValue() { return (double) m_integer; }; virtual double floatValue() { return (double) m_integer; };
virtual std::string stringValue() { return std::to_string(m_integer); }; virtual std::string stringValue() { return std::to_string(m_integer); };
virtual void print() {std::cout << "ColIntegerValue: " << m_integer <<std::endl; };
int m_integer; int m_integer;
}; };
struct ColFloatValue : ColValue { struct ColFloatValue : ColValue {
ColFloatValue(double value) : m_float(value) {}; ColFloatValue(double value) : m_float(value) {};
ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {} ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {}
virtual bool isFloat() { return true; } virtual bool isFloat() { return true; }
virtual int integerValue() { return (int) m_float; }; virtual int integerValue() { return (int) m_float; };
virtual double floatValue() { return m_float; }; virtual double floatValue() { return m_float; };
virtual std::string stringValue() { return std::to_string(m_float); }; virtual std::string stringValue() { return std::to_string(m_float); };
virtual void print() {std::cout << "ColFloatValue: " << m_float <<std::endl; };
double m_float; double m_float;
}; };
struct ColStringValue : ColValue { struct ColStringValue : ColValue {
ColStringValue(const std::string value) : m_string(value) {}; ColStringValue(const std::string value) : m_string(value) {};
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {}; ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
virtual bool isString() { return true; } virtual bool isString() { return true; }
virtual int integerValue() { return std::stoi(m_string); }; virtual int integerValue() { return std::stoi(m_string); };
virtual double floatValue() { return std::stod(m_string); }; virtual double floatValue() { return std::stod(m_string); };
virtual std::string stringValue() { return m_string; }; virtual std::string stringValue() { return m_string; };
virtual void print() {std::cout << "ColStringValue: " << m_string <<std::endl; };
std::string m_string; std::string m_string;
}; };
class Row {
class Row { public:
public:
Row(int cols_count); Row(int cols_count);
Row(const Row &other); Row(const Row &other);
Row& operator=(Row other);
Row &operator=(Row other);
void setColumnValue(int col_index, int value); void setColumnValue(int col_index, int value);
void setColumnValue(int col_index, double value); void setColumnValue(int col_index, double value);
void setColumnValue(int col_index, std::string value); void setColumnValue(int col_index, std::string value);
ColValue& operator[](int i) { ColValue &operator[](int i) {
return *m_columns[i]; return *m_columns[i];
} }
ColValue* ithColumn(int i) { ColValue *ithColumn(int i) {
return m_columns[i].get(); return m_columns[i].get();
} }
void print(); void print();
private: private:
std::vector<std::unique_ptr<ColValue>> m_columns; std::vector<std::unique_ptr<ColValue>> m_columns;
}; };
}

View File

@ -1,46 +1,47 @@
#include "table.h" #include "table.h"
Table::Table(const std::string name, const std::vector<ColDefNode> columns) { namespace usql {
Table::Table(const std::string name, const std::vector<ColDefNode> columns) {
m_name = name; m_name = name;
m_col_defs = columns; m_col_defs = columns;
m_rows.clear(); m_rows.clear();
} }
ColDefNode Table::get_column_def(const std::string& col_name) { ColDefNode Table::get_column_def(const std::string &col_name) {
auto name_cmp = [col_name](ColDefNode cd){ return cd.name == col_name; }; auto name_cmp = [col_name](ColDefNode cd) { return cd.name == col_name; };
auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp ); auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp);
if (col_def != std::end(m_col_defs)) { if (col_def != std::end(m_col_defs)) {
return *col_def; return *col_def;
} else { } else {
throw Exception("column not exists (" + col_name + ")"); throw Exception("column not exists (" + col_name + ")");
} }
} }
Row Table::createEmptyRow() { Row Table::createEmptyRow() {
return Row(columns_count()); return Row(columns_count());
} }
void Table::print() { void Table::print() {
std::cout << "** " << m_name << " **" << std::endl; std::cout << "** " << m_name << " **" << std::endl;
for(auto row : m_rows) { for (auto row : m_rows) {
for(int ci = 0; ci < columns_count(); ci++) { row.print();
auto v = row[ci].stringValue();
std::cout << v << ",";
} }
std::cout << std::endl;
} }
}
Table::Table(const Table& other) { Table::Table(const Table &other) {
m_name = other.m_name; m_name = other.m_name;
m_col_defs = other.m_col_defs; m_col_defs = other.m_col_defs;
m_rows.clear(); // row not copied now m_rows.clear(); // row not copied now
} }
void Table::addRow(const Row &row) { void Table::addRow(const Row &row) {
// TODO validate for not null values
// todo validate for length etc
m_rows.push_back(row); m_rows.push_back(row);
} }
}

17
table.h
View File

@ -5,15 +5,16 @@
#include <vector> #include <vector>
// TODO make it a class namespace usql {
struct Table {
// public: struct Table {
Table(const Table& other);
Table(const Table &other);
Table(const std::string name, const std::vector<ColDefNode> columns); Table(const std::string name, const std::vector<ColDefNode> columns);
ColDefNode get_column_def(const std::string& col_name); ColDefNode get_column_def(const std::string &col_name);
int columns_count() { return m_col_defs.size(); }; int columns_count() { return m_col_defs.size(); };
Row createEmptyRow(); // TODO this means unnecessary copying Row createEmptyRow(); // TODO this means unnecessary copying
@ -21,9 +22,9 @@ struct Table {
void print(); void print();
// private:
std::string m_name; std::string m_name;
std::vector<ColDefNode> m_col_defs; std::vector<ColDefNode> m_col_defs;
std::vector<Row> m_rows; std::vector<Row> m_rows;
}; };
}