int is long, select column can be function, some fixes..

just to get it work.. needs improvement
This commit is contained in:
VaclavT 2021-07-19 19:44:46 +02:00
parent 9afbe6435e
commit dec99b823a
14 changed files with 8697 additions and 196 deletions

View File

@ -12,7 +12,7 @@ project(usql)
set(PROJECT_NAME usql)
set(SOURCE
exception.cpp lexer.cpp parser.cpp usql.cpp main.cpp table.cpp table.h row.cpp row.h csvreader.cpp csvreader.h)
exception.cpp lexer.cpp parser.cpp usql.cpp main.cpp table.cpp table.h row.cpp row.h csvreader.cpp csvreader.h ml_date.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})

8319
clib/date.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -198,6 +198,9 @@ namespace usql {
if (token == "load")
return TokenType::keyword_load;
if (token == "save")
return TokenType::keyword_save;
if (token == "not")
return TokenType::keyword_not;
@ -362,6 +365,9 @@ namespace usql {
case TokenType::keyword_load:
txt = "load";
break;
case TokenType::keyword_save:
txt = "save";
break;
case TokenType::keyword_not:
txt = "not";
break;

View File

@ -27,6 +27,7 @@ namespace usql {
keyword_delete,
keyword_update,
keyword_load,
keyword_save,
keyword_from,
keyword_insert,
keyword_into,

View File

@ -3,7 +3,7 @@
// https://dev.to/joaoh82/what-would-sqlite-look-like-if-written-in-rust-part-1-2np4
// parser should get lexer as param and table executor to be able translate * or get types or so
// parser should get m_lexer as param and table executor to be able translate * or get types or so
// podporovat create as select
// drop table
@ -15,6 +15,8 @@ int main(int argc, char *argv[]) {
"insert into a (i, s) values(3, 'two')",
"insert into a (i, s) values(4, lower('FOUR'))",
"insert into a (i, s) values(5, 'five')",
"insert into a (i, s) values(to_date('20.12.1973', '%d.%m.%Y'), 'six')",
"save table a into '/tmp/a.csv'",
// "select i, s from a where i > 2",
// "select i, s from a where i = 1",
// "select i, s from a where s = 'two'",
@ -29,9 +31,10 @@ int main(int argc, char *argv[]) {
// "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",
"select i, s, f from a where i < 300",
// "select i, s, f from a where i < 300",
"create table x as select i, s, f from a where i < 300",
"select i, s, f from x where i < 300"
"select i, s, f from x where i < 300",
"select i, to_string(i, '%d.%m.%Y'), s, f from a where i > 300"
};

59
ml_date.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "ml_date.h"
long now() {
// get-universal-time
time_t t = std::time(0);
long int now = static_cast<long int>(t);
return now;
}
std::string date_to_string(const long datetime, const std::string format) {
// std::locale::global(std::locale("en-US.UTF8"));
time_t timestamp = datetime;
char mbstr[128];
if (std::strftime(mbstr, sizeof(mbstr), format.c_str(), std::localtime(&timestamp))) {
std::string result = {mbstr};
return result;
}
// TODO exception here
return "invalid argument";
}
long string_to_date(const std::string &datestr, const std::string &format) {
// format for example "%d.%m.%Y";
std::istringstream in{datestr.c_str()};
date::sys_seconds tp;
in >> date::parse(format, tp);
return tp.time_since_epoch().count();
}
long add_to_date(const long datetime, const long quantity, const std::string &part) {
// part is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'
// very basic implementation, just for now - no timezones DST etc
time_t base = datetime;
struct tm *tm = localtime(&base);
if (part == "year") {
tm->tm_year += quantity;
} else if (part == "month") {
tm->tm_mon += quantity;
} else if (part == "day") {
tm->tm_mday += quantity;
} else if (part == "hour") {
tm->tm_hour += quantity;
} else if (part == "minute") {
tm->tm_min += quantity;
} else if (part == "second") {
tm->tm_sec += quantity;
} else {
// TODO exception here
}
return mktime(tm);
}

17
ml_date.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include "clib/date.h"
#include <string>
#include <vector>
long now();
std::string date_to_string(const long datetime, const std::string format);
long string_to_date(const std::string &datestr, const std::string &format);
long add_to_date(const long datetime, const long quantity, const std::string &part);

View File

@ -6,54 +6,57 @@ namespace usql {
// TOOD handle premature eof
Parser::Parser() {
lexer = Lexer{};
m_lexer = Lexer{};
}
std::unique_ptr<Node> Parser::parse(const std::string &code) {
lexer.parse(code);
// lexer.debugTokens();
m_lexer.parse(code);
// m_lexer.debugTokens();
if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
if (m_lexer.tokenType() == TokenType::keyword_create && m_lexer.nextTokenType() == TokenType::keyword_table) {
return parse_create_table();
}
if (lexer.tokenType() == TokenType::keyword_insert) {
if (m_lexer.tokenType() == TokenType::keyword_insert) {
return parse_insert_into_table();
}
if (lexer.tokenType() == TokenType::keyword_select) {
if (m_lexer.tokenType() == TokenType::keyword_select) {
return parse_select_from_table();
}
if (lexer.tokenType() == TokenType::keyword_delete) {
if (m_lexer.tokenType() == TokenType::keyword_delete) {
return parse_delete_from_table();
}
if (lexer.tokenType() == TokenType::keyword_update) {
if (m_lexer.tokenType() == TokenType::keyword_update) {
return parse_update_table();
}
if (lexer.tokenType() == TokenType::keyword_load) {
if (m_lexer.tokenType() == TokenType::keyword_load) {
return parse_load_table();
}
if (m_lexer.tokenType() == TokenType::keyword_save) {
return parse_save_table();
}
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
std::cout << "ERROR, token:" << m_lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error);
}
std::unique_ptr<Node> Parser::parse_create_table() {
std::vector<ColDefNode> cols_def{};
lexer.skipToken(TokenType::keyword_create);
lexer.skipToken(TokenType::keyword_table);
m_lexer.skipToken(TokenType::keyword_create);
m_lexer.skipToken(TokenType::keyword_table);
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = m_lexer.consumeCurrentToken().token_string;
// create as select
if (lexer.tokenType() == TokenType::keyword_as) {
lexer.skipToken(TokenType::keyword_as);
if (m_lexer.tokenType() == TokenType::keyword_as) {
m_lexer.skipToken(TokenType::keyword_as);
std::unique_ptr<Node> select = parse_select_from_table();
return std::make_unique<CreateTableAsSelectNode>(table_name, std::move(select));
} else {
lexer.skipToken(TokenType::open_paren);
m_lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
@ -62,40 +65,40 @@ namespace usql {
bool column_nullable{true};
// column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = m_lexer.consumeCurrentToken().token_string;
// column type and optionally len
if (lexer.tokenType() == TokenType::keyword_int) {
if (m_lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_float) {
m_lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_varchar) {
m_lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
m_lexer.nextToken();
m_lexer.skipToken(TokenType::open_paren);
if (m_lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(m_lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
m_lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
if (lexer.tokenType() == TokenType::keyword_not) {
lexer.nextToken();
lexer.skipToken(TokenType::keyword_null);
if (m_lexer.tokenType() == TokenType::keyword_not) {
m_lexer.nextToken();
m_lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (lexer.tokenType() == TokenType::keyword_null) {
lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_null) {
m_lexer.nextToken();
}
cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
lexer.skipTokenOptional(TokenType::comma);
m_lexer.skipTokenOptional(TokenType::comma);
// TODO in future constraints
} while (lexer.tokenType() != TokenType::close_paren);
} while (m_lexer.tokenType() != TokenType::close_paren);
return std::make_unique<CreateTableNode>(table_name, cols_def);
}
@ -107,90 +110,107 @@ namespace usql {
std::vector<ColNameNode> cols_names{};
std::vector<std::unique_ptr<Node>> cols_values{};
lexer.skipToken(TokenType::keyword_insert);
lexer.skipToken(TokenType::keyword_into);
m_lexer.skipToken(TokenType::keyword_insert);
m_lexer.skipToken(TokenType::keyword_into);
// table name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = m_lexer.consumeCurrentToken().token_string;
// column names
lexer.skipToken(TokenType::open_paren);
m_lexer.skipToken(TokenType::open_paren);
do {
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
cols_names.push_back(lexer.consumeCurrentToken().token_string);
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
cols_names.push_back(m_lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
m_lexer.skipToken(TokenType::close_paren);
lexer.skipToken(TokenType::keyword_values);
m_lexer.skipToken(TokenType::keyword_values);
// column values
lexer.skipToken(TokenType::open_paren);
m_lexer.skipToken(TokenType::open_paren);
do {
// cols_values.push_back(lexer.consumeCurrentToken().token_string);
// cols_values.push_back(m_lexer.consumeCurrentToken().token_string);
auto col_value = parse_value();
cols_values.push_back(std::move(col_value));
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren);
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, std::move(cols_values));
}
std::unique_ptr<Node> Parser::parse_value() {
if (lexer.tokenType() == TokenType::int_number) {
return std::make_unique<IntValueNode>(std::stoi(lexer.consumeCurrentToken().token_string));
if (m_lexer.tokenType() == TokenType::int_number) {
return std::make_unique<IntValueNode>(std::stoi(m_lexer.consumeCurrentToken().token_string));
}
if (lexer.tokenType() == TokenType::double_number) {
return std::make_unique<FloatValueNode>(std::stof(lexer.consumeCurrentToken().token_string));
if (m_lexer.tokenType() == TokenType::double_number) {
return std::make_unique<FloatValueNode>(std::stof(m_lexer.consumeCurrentToken().token_string));
}
if (lexer.tokenType() == TokenType::string_literal) {
return std::make_unique<StringValueNode>(lexer.consumeCurrentToken().token_string);
if (m_lexer.tokenType() == TokenType::string_literal) {
return std::make_unique<StringValueNode>(m_lexer.consumeCurrentToken().token_string);
}
if (lexer.tokenType() == TokenType::identifier) {
std::string func_name = lexer.consumeCurrentToken().token_string;
std::vector<std::unique_ptr<Node>> pars;
if (m_lexer.tokenType() == TokenType::identifier) {
std::string name = m_lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::open_paren);
while (lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
pars.push_back(parse_value());
lexer.skipTokenOptional(TokenType::comma);
// function
if (m_lexer.tokenType() == TokenType::open_paren) {
std::vector<std::unique_ptr<Node>> pars;
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(name, std::move(pars));
} else {
return std::make_unique<ColNameNode>(name);
}
lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(func_name, std::move(pars));
}
throw Exception("Syntax error");
}
std::unique_ptr<Node> Parser::parse_select_from_table() {
std::vector<ColNameNode> cols_names{};
std::unique_ptr<Node> Parser::parse_select_from_table() {
auto cols = std::make_unique<std::vector<SelectColNode>>();
lexer.skipToken(TokenType::keyword_select);
while (lexer.tokenType() != TokenType::keyword_from) {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::keyword_select);
int i = 1;
while (m_lexer.tokenType() != TokenType::keyword_from) {
auto col_value = parse_value();
std::string alias;
if (col_value->node_type == NodeType::column_name) {
alias = ((ColNameNode*) col_value.get())->name;
} else {
alias = "c" + std::to_string(i);
i++;
}
lexer.skipToken(TokenType::keyword_from);
std::string table_name = lexer.consumeCurrentToken().token_string;
cols->push_back(SelectColNode{std::move(col_value), alias});
std::unique_ptr<Node> where_node = parse_where_clause();
m_lexer.skipTokenOptional(TokenType::comma);
}
// if (lexer.tokenType() == TokenType::keyword_order_by) {}
// if (lexer.tokenType() == TokenType::keyword_offset) {}
// if (lexer.tokenType() == TokenType::keyword_limit) {}
m_lexer.skipToken(TokenType::keyword_from);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node));
}
std::unique_ptr<Node> where_node = parse_where_clause();
// if (m_lexer.tokenType() == TokenType::keyword_order_by) {}
// if (m_lexer.tokenType() == TokenType::keyword_offset) {}
// if (m_lexer.tokenType() == TokenType::keyword_limit) {}
return std::make_unique<SelectFromTableNode>(table_name, std::move(cols), std::move(where_node));
}
std::unique_ptr<Node> Parser::parse_delete_from_table() {
lexer.skipToken(TokenType::keyword_delete);
lexer.skipToken(TokenType::keyword_from);
m_lexer.skipToken(TokenType::keyword_delete);
m_lexer.skipToken(TokenType::keyword_from);
std::string table_name = lexer.consumeCurrentToken().token_string;
std::string table_name = m_lexer.consumeCurrentToken().token_string;
std::unique_ptr<Node> where_node = parse_where_clause();
@ -198,22 +218,22 @@ std::unique_ptr<Node> Parser::parse_value() {
}
std::unique_ptr<Node> Parser::parse_update_table() {
lexer.skipToken(TokenType::keyword_update);
lexer.skipTokenOptional(TokenType::keyword_table);
m_lexer.skipToken(TokenType::keyword_update);
m_lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = lexer.consumeCurrentToken().token_string;
std::string table_name = m_lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::keyword_set);
m_lexer.skipToken(TokenType::keyword_set);
std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
do {
cols_names.push_back(lexer.consumeCurrentToken().token_string);
lexer.skipToken(TokenType::equal);
cols_names.push_back(m_lexer.consumeCurrentToken().token_string);
m_lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node();
if (Lexer::isArithmeticalOperator(lexer.tokenType())) {
if (Lexer::isArithmeticalOperator(m_lexer.tokenType())) {
ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node();
@ -225,9 +245,9 @@ std::unique_ptr<Node> Parser::parse_value() {
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
std::move(left), std::move(right)));
}
lexer.skipTokenOptional(TokenType::comma);
m_lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::keyword_where && lexer.tokenType() != TokenType::eof);
} while (m_lexer.tokenType() != TokenType::keyword_where && m_lexer.tokenType() != TokenType::eof);
std::unique_ptr<Node> where_node = parse_where_clause();
@ -235,37 +255,51 @@ std::unique_ptr<Node> Parser::parse_value() {
}
std::unique_ptr<Node> Parser::parse_load_table() {
lexer.skipToken(TokenType::keyword_load);
lexer.skipTokenOptional(TokenType::keyword_into);
m_lexer.skipToken(TokenType::keyword_load);
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string table_name = lexer.consumeCurrentToken().token_string;
std::string table_name = m_lexer.consumeCurrentToken().token_string;
lexer.skipTokenOptional(TokenType::keyword_from);
m_lexer.skipTokenOptional(TokenType::keyword_from);
std::string file_name = lexer.consumeCurrentToken().token_string;
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_save_table() {
m_lexer.skipToken(TokenType::keyword_save);
m_lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<SaveTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_where_clause() {
// TODO add support for multiple filters
// TODO add support for parenthesis
if (lexer.tokenType() != TokenType::keyword_where) {
if (m_lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>();
}
std::unique_ptr<Node> node;
lexer.skipToken(TokenType::keyword_where);
m_lexer.skipToken(TokenType::keyword_where);
do {
node = parse_relational_expression();
if (Lexer::isLogicalOperator(lexer.tokenType())) {
if (Lexer::isLogicalOperator(m_lexer.tokenType())) {
auto operation = parse_logical_operator();
std::unique_ptr<Node> node2 = parse_relational_expression();
node = std::make_unique<LogicalOperatorNode>(operation, std::move(node), std::move(node2));
}
} while (lexer.tokenType() != TokenType::eof); // until whole where clause parsed
} while (m_lexer.tokenType() != TokenType::eof); // until whole where clause parsed
return node;
}
@ -280,8 +314,8 @@ std::unique_ptr<Node> Parser::parse_value() {
std::unique_ptr<Node> Parser::parse_operand_node() {
// while not end or order or limit
auto token_type = lexer.tokenType();
std::string tokenString = lexer.consumeCurrentToken().token_string;
auto token_type = m_lexer.tokenType();
std::string tokenString = m_lexer.consumeCurrentToken().token_string;
switch (token_type) {
case TokenType::int_number:
return std::make_unique<IntValueNode>(std::stoi(tokenString));
@ -297,7 +331,7 @@ std::unique_ptr<Node> Parser::parse_value() {
}
RelationalOperatorType Parser::parse_relational_operator() {
auto op = lexer.consumeCurrentToken();
auto op = m_lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::equal:
return RelationalOperatorType::equal;
@ -317,7 +351,7 @@ std::unique_ptr<Node> Parser::parse_value() {
}
LogicalOperatorType Parser::parse_logical_operator() {
auto op = lexer.consumeCurrentToken();
auto op = m_lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::logical_and:
return LogicalOperatorType::and_operator;
@ -329,7 +363,7 @@ std::unique_ptr<Node> Parser::parse_value() {
}
ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
auto op = lexer.consumeCurrentToken();
auto op = m_lexer.consumeCurrentToken();
switch (op.type) {
case TokenType::plus:
return ArithmeticalOperatorType::plus_operator;

View File

@ -8,7 +8,6 @@
namespace usql {
enum class ColumnType {
integer_type,
float_type,
@ -31,6 +30,7 @@ namespace usql {
delete_from,
update_table,
load_table,
save_table,
column_name,
column_value,
function,
@ -47,11 +47,17 @@ namespace usql {
struct ColNameNode : Node {
std::string name;
ColNameNode(const std::string col_name) :
Node(NodeType::column_name), name(col_name) {}
ColNameNode(const std::string col_name) : Node(NodeType::column_name), name(col_name) {}
};
struct SelectColNode : Node {
std::unique_ptr<Node> value;
std::string name;
SelectColNode(std::unique_ptr<Node> column, const std::string alias) :
Node(NodeType::column_name), value(std::move(column)), name(alias) {}
};
// TODO add order in row
struct ColDefNode : Node {
std::string name;
ColumnType type;
@ -79,24 +85,20 @@ namespace usql {
struct ValueNode : Node {
ValueNode(NodeType type) : Node(type) {}
virtual int getIntValue() = 0;
virtual long getIntValue() = 0;
virtual double getDoubleValue() = 0;
virtual std::string getStringValue() = 0;
virtual ~ValueNode() {};
};
struct IntValueNode : ValueNode {
int value;
long value;
IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
int getIntValue() { return value; };
IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
long getIntValue() { return value; };
double getDoubleValue() { return (double) value; };
std::string getStringValue() { return std::to_string(value); }
};
@ -105,10 +107,8 @@ namespace usql {
FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
int getIntValue() { return (int) value; };
long getIntValue() { return (int) value; };
double getDoubleValue() { return value; };
std::string getStringValue() { return std::to_string(value); }
};
@ -117,10 +117,8 @@ namespace usql {
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
int getIntValue() { return std::stoi(value); };
long getIntValue() { return std::stoi(value); };
double getDoubleValue() { return std::stod(value); };
std::string getStringValue() { return value; };
};
@ -183,7 +181,6 @@ namespace usql {
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
};
struct CreateTableNode : Node {
std::string table_name;
std::vector<ColDefNode> cols_defs;
@ -192,11 +189,10 @@ namespace usql {
Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
};
struct InsertIntoTableNode : Node {
std::string table_name;
std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
std::string table_name;
std::vector<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> values) :
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(std::move(values)) {}
@ -204,14 +200,13 @@ namespace usql {
struct SelectFromTableNode : Node {
std::string table_name;
std::vector<ColNameNode> cols_names;
std::unique_ptr<std::vector<SelectColNode>> cols_names;
std::unique_ptr<Node> where;
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)) {}
SelectFromTableNode(std::string name, std::unique_ptr<std::vector<SelectColNode>> names, std::unique_ptr<Node> where_clause) :
Node(NodeType::select_from), table_name(name), cols_names(std::move(names)), where(std::move(where_clause)) {}
};
struct CreateTableAsSelectNode : Node {
std::string table_name;
std::unique_ptr<Node> select_table;
@ -220,7 +215,6 @@ namespace usql {
Node(NodeType::create_table_as_select), table_name(name), select_table(std::move(table)) {}
};
struct UpdateTableNode : Node {
std::string table_name;
std::vector<ColNameNode> cols_names;
@ -239,7 +233,14 @@ namespace usql {
LoadIntoTableNode(const std::string name, std::string file) :
Node(NodeType::load_table), table_name(name), filename(file) {}
};
struct SaveTableNode : Node {
std::string table_name;
std::string filename;
SaveTableNode(const std::string name, std::string file) :
Node(NodeType::save_table), table_name(name), filename(file) {}
};
struct DeleteFromTableNode : Node {
@ -248,7 +249,6 @@ namespace usql {
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
};
@ -262,34 +262,23 @@ namespace usql {
private:
std::unique_ptr<Node> parse_create_table();
std::unique_ptr<Node> parse_insert_into_table();
std::unique_ptr<Node> parse_value();
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_save_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:
Lexer lexer;
Lexer m_lexer;
std::unique_ptr<Node> parse_relational_expression();
};
}

View File

@ -39,7 +39,7 @@ namespace usql {
m_columns[col_index] = std::make_unique<ColNullValue>();
}
void Row::setColumnValue(int col_index, int value) {
void Row::setColumnValue(int col_index, long value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}

25
row.h
View File

@ -11,11 +11,8 @@ namespace usql {
struct ColValue {
virtual bool isNull() { return false; };
virtual int integerValue() { throw Exception("Not supported"); };
virtual long integerValue() { throw Exception("Not supported"); };
virtual double floatValue() { throw Exception("Not supported"); };
virtual std::string stringValue() { throw Exception("Not supported"); };
};
@ -23,7 +20,6 @@ namespace usql {
struct ColNullValue : ColValue {
virtual bool isNull() { return true; };
virtual std::string stringValue() { return "null"; };
};
@ -31,13 +27,10 @@ namespace usql {
struct ColIntegerValue : ColValue {
ColIntegerValue(int value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
virtual int integerValue() { return m_integer; };
virtual long integerValue() { return m_integer; };
virtual double floatValue() { return (double) m_integer; };
virtual std::string stringValue() { return std::to_string(m_integer); };
int m_integer;
@ -47,13 +40,10 @@ namespace usql {
struct ColFloatValue : ColValue {
ColFloatValue(double value) : m_float(value) {};
ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {}
virtual int integerValue() { return (int) m_float; };
virtual long integerValue() { return (int) m_float; };
virtual double floatValue() { return m_float; };
virtual std::string stringValue() { return std::to_string(m_float); };
double m_float;
@ -63,13 +53,10 @@ namespace usql {
struct ColStringValue : ColValue {
ColStringValue(const std::string value) : m_string(value) {};
ColStringValue(const ColStringValue &other) : m_string(other.m_string) {};
virtual int integerValue() { return std::stoi(m_string); };
virtual long integerValue() { return std::stoi(m_string); };
virtual double floatValue() { return std::stod(m_string); };
virtual std::string stringValue() { return m_string; };
std::string m_string;
@ -85,7 +72,7 @@ namespace usql {
Row &operator=(Row other);
void setColumnNull(int col_index);
void setColumnValue(int col_index, int value);
void setColumnValue(int col_index, long value);
void setColumnValue(int col_index, double value);
void setColumnValue(int col_index, const std::string &value);
@ -103,4 +90,4 @@ namespace usql {
std::vector<std::unique_ptr<ColValue>> m_columns;
};
}
} // namespace

View File

@ -35,7 +35,9 @@ void Table::print() {
Table::Table(const Table &other) {
m_name = other.m_name;
m_col_defs = other.m_col_defs;
m_rows.clear(); // row not copied now
for(const Row& orig_row : other.m_rows) {
addCopyOfRow(orig_row);
}
}
void Table::addRow(const Row &row) {

119
usql.cpp
View File

@ -1,12 +1,18 @@
#include "usql.h"
#include "exception.h"
#include "csvreader.h"
#include "ml_date.h"
#include <algorithm>
#include <fstream>
namespace usql {
USql::USql(){
m_tables.reserve(16); // some initial size, to prevent immediate reallocation
}
std::unique_ptr<Table> USql::execute(const std::string &command) {
auto node = m_parser.parse(command);
@ -30,6 +36,8 @@ std::unique_ptr<Table> USql::execute(Node &node) {
return execute_update(static_cast<UpdateTableNode &>(node));
case NodeType::load_table:
return execute_load(static_cast<LoadIntoTableNode &>(node));
case NodeType::save_table:
return execute_save(static_cast<SaveTableNode &>(node));
default:
return create_stmt_result_table(-1, "unknown statement");
}
@ -108,15 +116,26 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// create result table
std::vector<ColDefNode> result_tbl_col_defs{};
std::vector<int> source_table_col_index{};
int i = 0; // new column order
for (auto rc : node.cols_names) {
ColDefNode cdef = table->get_column_def(rc.name);
source_table_col_index.push_back(cdef.order);
auto col = ColDefNode(rc.name, cdef.type, i, cdef.length, cdef.null);
result_tbl_col_defs.push_back(col);
// new column order
for (int i = 0; i < node.cols_names->size(); i++) {
auto column = node.cols_names.get();
std::string new_col_name = column->operator[](i).name;
i++;
if (column->operator[](i).value->node_type == NodeType::column_name) {
ColDefNode cdef = table->get_column_def(new_col_name);
source_table_col_index.push_back(cdef.order);
auto col = ColDefNode(new_col_name, cdef.type, i, cdef.length, cdef.null);
result_tbl_col_defs.push_back(col);
} else {
// TODO replace this hardcoded values
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, i, 64, true};
source_table_col_index.push_back(-1);
auto col = ColDefNode(new_col_name, cdef.type, i, cdef.length, cdef.null);
result_tbl_col_defs.push_back(col);
}
}
auto result = std::make_unique<Table>("result", result_tbl_col_defs);
@ -130,14 +149,30 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// copy column values
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 (!col_value->isNull()) {
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
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)
new_row.setColumnValue(idx, col_value->stringValue());
if (row_col_index == -1) {
// todo its function TODO col_names zmenit na colValues
auto evaluated_value = evalValueNode(table, *row, node.cols_names.get()->operator[](idx).value.get());
ValueNode *col_value = evaluated_value.get();
if (true /* !col_value->isNull() */) { // TODO sjednotit nasledujici
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
new_row.setColumnValue(idx, col_value->getIntValue());
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
new_row.setColumnValue(idx, col_value->getDoubleValue());
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type)
new_row.setColumnValue(idx, col_value->getStringValue());
}
} else {
ColValue *col_value = row->ithColumn(row_col_index);
if (!col_value->isNull()) {
if (result_tbl_col_defs[idx].type == ColumnType::integer_type)
new_row.setColumnValue(idx, 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)
new_row.setColumnValue(idx, col_value->stringValue());
}
}
}
@ -234,7 +269,7 @@ std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
// TODO validate value
if (col_def.type == ColumnType::integer_type) {
new_row.setColumnValue(col_def.order, std::stoi(csv_line[i]));
new_row.setColumnValue(col_def.order, std::stol(csv_line[i]));
} else if (col_def.type == ColumnType::float_type) {
new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
} else {
@ -250,6 +285,41 @@ std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
}
std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
// find source table
Table *table_def = find_table(node.table_name);
// header
std::string out_string;
for(int i = 0; i < table_def->m_col_defs.size(); i++) {
if (i > 0) out_string += ",";
out_string += table_def->m_col_defs[i].name;
}
// rows
for (auto it = table_def->m_rows.begin() + 1; it != table_def->m_rows.end(); ++it) {
std::string csv_line = "";
for(int i = 0; i < table_def->m_col_defs.size(); i++) {
if (i > 0) csv_line += ",";
auto col = it->ithColumn(i);
if (!col->isNull()) {
csv_line += col->stringValue(); // TODO handle enclosing commas etc
}
}
out_string += "\n";
out_string += csv_line;
}
// save data
std::ofstream file(node.filename);
file << out_string;
file.close();
return create_stmt_result_table(0, "save succeeded");
}
bool USql::evalWhere(Node *where, Table *table, Row &row) const {
switch (where->node_type) { // no where clause
case NodeType::true_node:
@ -304,7 +374,7 @@ bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *t
std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) const {
if (node->node_type == NodeType::database_value) {
if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
return evalDatabaseValueNode(table, row, node);
} else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) {
@ -374,6 +444,19 @@ std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, N
return std::make_unique<StringValueNode>(str);
}
if (fnc->function == "to_date") {
std::string date = evaluatedPars[0]->getStringValue();
std::string format = evaluatedPars[1]->getStringValue();
long epoch_time = string_to_date(date, format);
return std::make_unique<IntValueNode>(epoch_time);
}
if (fnc->function == "to_string") {
long date = evaluatedPars[0]->getIntValue();
std::string format = evaluatedPars[1]->getStringValue();
std::string formated_date = date_to_string(date, format);
return std::make_unique<StringValueNode>(formated_date);
}
throw Exception("invalid function");
}
@ -444,7 +527,7 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
}
std::unique_ptr<Table> USql::create_stmt_result_table(int code, std::string text) {
std::unique_ptr<Table> USql::create_stmt_result_table(long 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));

5
usql.h
View File

@ -10,7 +10,7 @@ namespace usql {
class USql {
public:
USql() {};
USql();
std::unique_ptr<Table> execute(const std::string &command);
@ -24,6 +24,7 @@ private:
std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
std::unique_ptr<Table> execute_update(UpdateTableNode &node);
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
std::unique_ptr<Table> execute_save(SaveTableNode &node);
private:
@ -40,7 +41,7 @@ private:
std::unique_ptr<ValueNode> evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const;
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
std::unique_ptr<Table> create_stmt_result_table(long code, std::string text);
Table *find_table(const std::string name);