usql update

usql is still very primitive..it just barely works
This commit is contained in:
VaclavT 2021-07-23 00:00:39 +02:00
parent 4f113ab2c5
commit 5005644d98
26 changed files with 877 additions and 472 deletions

View File

@ -1,6 +1,19 @@
(usql "create table data (ticker varchar(8), price float null)") (define create_tbl_sql "create table prices (datetime integer, symbol varchar(8), prev_close float, open float, price float, change float, change_prct varchar(16))")
(usql "load data from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/data.csv')") (define insert_sql "insert into prices (datetime, symbol, prev_close, open, price, change, change_prct) values (1626979443, 'MPC', 54.08, 53.82, 53.63, -0.832101, '-0.83 %')")
(print (usql "select ticker, price from data")) (define select_sql "select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices")
(print (usql create_tbl_sql))
(print (usql insert_sql))
(print (usql select_sql))
(print (usql "select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct values from prices"))
;; (print (usql "create table data (ticker varchar(8), price float null)"))
;; (usql "load data from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/data.csv')")
;; (print (usql "select ticker, price from data"))
;; (read-url "https://api.nasdaq.com/api/calendar/dividends/") ;; (read-url "https://api.nasdaq.com/api/calendar/dividends/")

8
ml.cpp
View File

@ -1926,14 +1926,12 @@ MlValue throw_exception(std::vector<MlValue> args, MlEnvironment &env) {
} }
MlValue usql(std::vector<MlValue> args, MlEnvironment &env) { MlValue usql(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env);
if (args.size() != 1) if (args.size() != 1)
throw MlError(MlValue("usql", throw_exception), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("usql", throw_exception), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
try { return uSQL::instance().execute(args[0].as_string());
return uSQL::instance().execute(args[0].as_string());
} catch (std::exception &e) {
return MlValue::nil();
}
} }
} // namespace builtin } // namespace builtin

View File

@ -19,11 +19,11 @@ MlValue uSQL::ivaluize(const usql::Table *table) {
auto c = row.ithColumn(i); auto c = row.ithColumn(i);
auto type = table->m_col_defs[i].type; auto type = table->m_col_defs[i].type;
if (type == ColumnType::integer_type) { if (type == ColumnType::integer_type) {
columns.push_back(MlValue((long)c->integerValue())); columns.push_back(MlValue(c->getIntValue()));
} else if (type == ColumnType::float_type) { } else if (type == ColumnType::float_type) {
columns.push_back(MlValue((double)c->floatValue())); columns.push_back(MlValue(c->getDoubleValue()));
} else { } else {
columns.push_back(MlValue::string(c->stringValue())); columns.push_back(MlValue::string(c->getStringValue()));
} }
} }
rows.push_back(columns); rows.push_back(columns);

21
usql/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
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 ml_date.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} stdc++ m)
target_compile_options(usql PRIVATE -g)

View File

@ -1,11 +0,0 @@
### TODO
- unify using of float and double keywords to double
- use long data type for int
- add exceptions
- class members should have prefix m_
- add pipe | token
- add to_date a to_number functions
- add min and max functions
- add logging
- add const wherever should be

11
usql/Readme.md Normal file
View File

@ -0,0 +1,11 @@
### TODO
- save table command
- move csv generation from usql(save_table) to table class
- add exceptions
- class members should have prefix m_
- add pipe | token
- add to_date a to_string functions
- add min and max functions, eg aggregate functions
- add logging
- add const wherever should be

1
usql/clib/date.h Symbolic link
View File

@ -0,0 +1 @@
../../clib/date.h

3
usql/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

@ -2,7 +2,7 @@
namespace usql { namespace usql {
Exception::Exception(const std::string &msg) { Exception::Exception(const std::string &msg) : std::runtime_error(msg) {
cause = msg; cause = msg;
} }

View File

@ -6,7 +6,7 @@
namespace usql { namespace usql {
class Exception : public std::exception { class Exception : public std::runtime_error {
private: private:
std::string cause; std::string cause;

View File

@ -13,12 +13,12 @@ namespace usql {
Lexer::Lexer() { Lexer::Lexer() {
k_words_regex = k_words_regex =
"[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/" "[-+]?[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"; ",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n";
k_int_regex = "[0-9]+"; k_int_regex = "[-+]?[0-9]+";
k_int_underscored_regex = "[0-9][0-9_]+[0-9]"; k_int_underscored_regex = "[-+]?[0-9][0-9_]+[0-9]";
k_double_regex = "[0-9]+\\.[0-9]+"; k_double_regex = "[-+]?[0-9]+\\.[0-9]+";
k_identifier_regex = "[A-Za-z]+[A-Za-z0-9_#]*"; k_identifier_regex = "[A-Za-z]+[A-Za-z0-9_#]*";
} }
void Lexer::parse(const std::string &code) { void Lexer::parse(const std::string &code) {
@ -156,6 +156,9 @@ namespace usql {
if (token == "<=") if (token == "<=")
return TokenType::lesser_equal; return TokenType::lesser_equal;
if (token == "as")
return TokenType::keyword_as;
if (token == "create") if (token == "create")
return TokenType::keyword_create; return TokenType::keyword_create;
@ -195,6 +198,9 @@ namespace usql {
if (token == "load") if (token == "load")
return TokenType::keyword_load; return TokenType::keyword_load;
if (token == "save")
return TokenType::keyword_save;
if (token == "not") if (token == "not")
return TokenType::keyword_not; return TokenType::keyword_not;
@ -202,7 +208,7 @@ namespace usql {
return TokenType::keyword_null; return TokenType::keyword_null;
if (token == "integer") if (token == "integer")
return TokenType::keyword_int; return TokenType::keyword_integer;
if (token == "float") if (token == "float")
return TokenType::keyword_float; return TokenType::keyword_float;
@ -326,6 +332,9 @@ namespace usql {
case TokenType::lesser_equal: case TokenType::lesser_equal:
txt = "<="; txt = "<=";
break; break;
case TokenType::keyword_as:
txt = "as";
break;
case TokenType::keyword_create: case TokenType::keyword_create:
txt = "create"; txt = "create";
break; break;
@ -356,13 +365,16 @@ namespace usql {
case TokenType::keyword_load: case TokenType::keyword_load:
txt = "load"; txt = "load";
break; break;
case TokenType::keyword_save:
txt = "save";
break;
case TokenType::keyword_not: case TokenType::keyword_not:
txt = "not"; txt = "not";
break; break;
case TokenType::keyword_null: case TokenType::keyword_null:
txt = "null"; txt = "null";
break; break;
case TokenType::keyword_int: case TokenType::keyword_integer:
txt = "integer"; txt = "integer";
break; break;
case TokenType::keyword_float: case TokenType::keyword_float:

View File

@ -20,12 +20,14 @@ namespace usql {
greater_equal, greater_equal,
lesser, lesser,
lesser_equal, lesser_equal,
keyword_as,
keyword_create, keyword_create,
keyword_table, keyword_table,
keyword_where, keyword_where,
keyword_delete, keyword_delete,
keyword_update, keyword_update,
keyword_load, keyword_load,
keyword_save,
keyword_from, keyword_from,
keyword_insert, keyword_insert,
keyword_into, keyword_into,
@ -35,7 +37,7 @@ namespace usql {
keyword_copy, keyword_copy,
keyword_not, keyword_not,
keyword_null, keyword_null,
keyword_int, keyword_integer,
keyword_float, keyword_float,
keyword_varchar, keyword_varchar,
int_number, int_number,

52
usql/main.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "parser.h"
#include "usql.h"
// https://dev.to/joaoh82/what-would-sqlite-look-like-if-written-in-rust-part-1-2np4
// 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
int main(int argc, char *argv[]) {
std::vector<std::string> sql_commands{
"create table a (i integer not null, s varchar(64), f float null)",
"insert into a (i, s) values(1, upper('one'))",
"insert into a (i, s) values(2, 'two')",
"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'",
"select i, s from a where i <= 3 and s = 'one'",
"select i, s from a where i > 0",
"delete from a where i = 4",
"select i, s from a where i > 0",
"update a set f = 9.99 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, 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",
"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 a where i > 300",
"select i, to_string(i, '%d.%m.%Y'), s, f from a where i > 300"
};
usql::USql uSql{};
for (auto command : sql_commands) {
std::cout << command << std::endl;
auto result = uSql.execute(command);
result->print();
}
return 0;
}

59
usql/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
usql/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,158 +6,218 @@ namespace usql {
// TOOD handle premature eof // TOOD handle premature eof
Parser::Parser() { Parser::Parser() {
lexer = Lexer{}; m_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); m_lexer.parse(code);
// lexer.debugTokens(); // 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(); return parse_create_table();
} }
if (lexer.tokenType() == TokenType::keyword_insert) { if (m_lexer.tokenType() == TokenType::keyword_insert) {
return parse_insert_into_table(); return parse_insert_into_table();
} }
if (lexer.tokenType() == TokenType::keyword_select) { if (m_lexer.tokenType() == TokenType::keyword_select) {
return parse_select_from_table(); return parse_select_from_table();
} }
if (lexer.tokenType() == TokenType::keyword_delete) { if (m_lexer.tokenType() == TokenType::keyword_delete) {
return parse_delete_from_table(); return parse_delete_from_table();
} }
if (lexer.tokenType() == TokenType::keyword_update) { if (m_lexer.tokenType() == TokenType::keyword_update) {
return parse_update_table(); return parse_update_table();
} }
if (lexer.tokenType() == TokenType::keyword_load) { if (m_lexer.tokenType() == TokenType::keyword_load) {
return parse_load_table(); 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); 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); m_lexer.skipToken(TokenType::keyword_create);
lexer.skipToken(TokenType::keyword_table); m_lexer.skipToken(TokenType::keyword_table);
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ } if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string; std::string table_name = m_lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::open_paren); // create as select
int column_order = 0; if (m_lexer.tokenType() == TokenType::keyword_as) {
do { m_lexer.skipToken(TokenType::keyword_as);
std::string column_name;
ColumnType column_type;
int column_len{1};
bool column_nullable{true};
// column name std::unique_ptr<Node> select = parse_select_from_table();
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
// column type and optionally len return std::make_unique<CreateTableAsSelectNode>(table_name, std::move(select));
if (lexer.tokenType() == TokenType::keyword_int) { } else {
column_type = ColumnType::integer_type; m_lexer.skipToken(TokenType::open_paren);
lexer.nextToken(); int column_order = 0;
} else if (lexer.tokenType() == TokenType::keyword_float) { do {
column_type = ColumnType::float_type; std::string column_name;
lexer.nextToken(); ColumnType column_type;
} else if (lexer.tokenType() == TokenType::keyword_varchar) { int column_len{1};
column_type = ColumnType::varchar_type; bool column_nullable{true};
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
if (lexer.tokenType() == TokenType::keyword_not) { // column name
lexer.nextToken(); if (m_lexer.tokenType() != TokenType::identifier) {
lexer.skipToken(TokenType::keyword_null); throw Exception("syntax error, expected identifier");
column_nullable = false; }
} else if (lexer.tokenType() == TokenType::keyword_null) { column_name = m_lexer.consumeCurrentToken().token_string;
lexer.nextToken();
}
cols_def.push_back( // column type and optionally len
ColDefNode(column_name, column_type, column_order++, column_len, column_nullable)); if (m_lexer.tokenType() == TokenType::keyword_integer) {
column_type = ColumnType::integer_type;
m_lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
m_lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
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 {
throw Exception("syntax error, expected int number");
}
m_lexer.skipToken(TokenType::close_paren);
} else {
throw Exception("syntax error, column type expected");
}
lexer.skipTokenOptional(TokenType::comma); if (m_lexer.tokenType() == TokenType::keyword_not) {
m_lexer.nextToken();
m_lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (m_lexer.tokenType() == TokenType::keyword_null) {
m_lexer.nextToken();
}
// TODO in future constraints cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
} while (lexer.tokenType() != TokenType::close_paren); m_lexer.skipTokenOptional(TokenType::comma);
// TODO in future constraints
} while (m_lexer.tokenType() != TokenType::close_paren);
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<ColNameNode> column_names{};
std::vector<ColNameNode> cols_names{}; std::vector<std::unique_ptr<Node>> column_values{};
std::vector<ColValueNode> cols_values{};
lexer.skipToken(TokenType::keyword_insert); m_lexer.skipToken(TokenType::keyword_insert);
lexer.skipToken(TokenType::keyword_into); m_lexer.skipToken(TokenType::keyword_into);
// table name // table name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ } if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string; std::string table_name = m_lexer.consumeCurrentToken().token_string;
// column names // column names
lexer.skipToken(TokenType::open_paren); m_lexer.skipToken(TokenType::open_paren);
do { do {
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ } if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
cols_names.push_back(lexer.consumeCurrentToken().token_string); column_names.push_back(m_lexer.consumeCurrentToken().token_string);
lexer.skipTokenOptional(TokenType::comma); m_lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren); } while (m_lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren); m_lexer.skipToken(TokenType::close_paren);
lexer.skipToken(TokenType::keyword_values); m_lexer.skipToken(TokenType::keyword_values);
// column values // column values
lexer.skipToken(TokenType::open_paren); m_lexer.skipToken(TokenType::open_paren);
do { do {
cols_values.push_back(lexer.consumeCurrentToken().token_string); auto col_value = parse_value();
column_values.push_back(std::move(col_value));
lexer.skipTokenOptional(TokenType::comma); m_lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren); } while (m_lexer.tokenType() != TokenType::close_paren);
lexer.skipToken(TokenType::close_paren); m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values); return std::make_unique<InsertIntoTableNode>(table_name, column_names, std::move(column_values));
} }
std::unique_ptr<Node> Parser::parse_select_from_table() { std::unique_ptr<Node> Parser::parse_value() {
std::vector<ColNameNode> cols_names{}; if (m_lexer.tokenType() == TokenType::int_number) {
return std::make_unique<IntValueNode>(std::stoi(m_lexer.consumeCurrentToken().token_string));
}
if (m_lexer.tokenType() == TokenType::double_number) {
return std::make_unique<DoubleValueNode>(std::stof(m_lexer.consumeCurrentToken().token_string));
}
if (m_lexer.tokenType() == TokenType::string_literal) {
return std::make_unique<StringValueNode>(m_lexer.consumeCurrentToken().token_string);
}
if (m_lexer.tokenType() == TokenType::identifier && m_lexer.nextTokenType() == TokenType::open_paren) {
// function
std::string function_name = m_lexer.consumeCurrentToken().token_string;
std::vector<std::unique_ptr<Node>> pars;
lexer.skipToken(TokenType::keyword_select); m_lexer.skipToken(TokenType::open_paren);
while (lexer.tokenType() != TokenType::keyword_from) { while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
cols_names.push_back(lexer.consumeCurrentToken().token_string); pars.push_back(parse_value());
lexer.skipTokenOptional(TokenType::comma); m_lexer.skipTokenOptional(TokenType::comma);
} }
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(function_name, std::move(pars));
}
if (m_lexer.tokenType() == TokenType::identifier) {
std::string name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<ColNameNode>(name);
}
lexer.skipToken(TokenType::keyword_from); throw Exception("Syntax error, current token: " + m_lexer.currentToken().token_string);
std::string table_name = lexer.consumeCurrentToken().token_string; }
std::unique_ptr<Node> where_node = parse_where_clause(); std::unique_ptr<Node> Parser::parse_select_from_table() {
auto cols = std::make_unique<std::vector<SelectColNode>>();
// if (lexer.tokenType() == TokenType::keyword_order_by) {} m_lexer.skipToken(TokenType::keyword_select);
// if (lexer.tokenType() == TokenType::keyword_offset) {}
// if (lexer.tokenType() == TokenType::keyword_limit) {}
return std::make_unique<SelectFromTableNode>(table_name, cols_names, std::move(where_node)); int i = 1;
} while (m_lexer.tokenType() != TokenType::keyword_from) {
auto column_value = parse_value();
std::string column_alias;
if (column_value->node_type == NodeType::column_name) {
column_alias = ((ColNameNode*) column_value.get())->name;
} else {
column_alias = "c" + std::to_string(i);
i++;
}
cols->push_back(SelectColNode{std::move(column_value), column_alias});
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::keyword_from);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
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() { std::unique_ptr<Node> Parser::parse_delete_from_table() {
lexer.skipToken(TokenType::keyword_delete); m_lexer.skipToken(TokenType::keyword_delete);
lexer.skipToken(TokenType::keyword_from); 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(); std::unique_ptr<Node> where_node = parse_where_clause();
@ -165,22 +225,22 @@ namespace usql {
} }
std::unique_ptr<Node> Parser::parse_update_table() { std::unique_ptr<Node> Parser::parse_update_table() {
lexer.skipToken(TokenType::keyword_update); m_lexer.skipToken(TokenType::keyword_update);
lexer.skipTokenOptional(TokenType::keyword_table); 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<ColNameNode> cols_names;
std::vector<std::unique_ptr<Node>> values; std::vector<std::unique_ptr<Node>> values;
do { do {
cols_names.push_back(lexer.consumeCurrentToken().token_string); cols_names.push_back(m_lexer.consumeCurrentToken().token_string);
lexer.skipToken(TokenType::equal); m_lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node(); 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(); ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node(); std::unique_ptr<Node> right = Parser::parse_operand_node();
@ -192,9 +252,9 @@ namespace usql {
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value, std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
std::move(left), std::move(right))); 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(); std::unique_ptr<Node> where_node = parse_where_clause();
@ -202,37 +262,51 @@ namespace usql {
} }
std::unique_ptr<Node> Parser::parse_load_table() { std::unique_ptr<Node> Parser::parse_load_table() {
lexer.skipToken(TokenType::keyword_load); m_lexer.skipToken(TokenType::keyword_load);
lexer.skipTokenOptional(TokenType::keyword_into); 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); 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() { 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
if (lexer.tokenType() != TokenType::keyword_where) { if (m_lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>(); return std::make_unique<TrueNode>();
} }
std::unique_ptr<Node> node; std::unique_ptr<Node> node;
lexer.skipToken(TokenType::keyword_where); m_lexer.skipToken(TokenType::keyword_where);
do { do {
node = parse_relational_expression(); node = parse_relational_expression();
if (Lexer::isLogicalOperator(lexer.tokenType())) { if (Lexer::isLogicalOperator(m_lexer.tokenType())) {
auto operation = parse_logical_operator(); auto operation = parse_logical_operator();
std::unique_ptr<Node> node2 = parse_relational_expression(); std::unique_ptr<Node> node2 = parse_relational_expression();
node = std::make_unique<LogicalOperatorNode>(operation, std::move(node), std::move(node2)); 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; return node;
} }
@ -247,13 +321,13 @@ namespace usql {
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 = m_lexer.tokenType();
std::string tokenString = lexer.consumeCurrentToken().token_string; std::string tokenString = m_lexer.consumeCurrentToken().token_string;
switch (token_type) { switch (token_type) {
case TokenType::int_number: case TokenType::int_number:
return std::make_unique<IntValueNode>(std::stoi(tokenString)); return std::make_unique<IntValueNode>(std::stoi(tokenString));
case TokenType::double_number: case TokenType::double_number:
return std::make_unique<FloatValueNode>(std::stod(tokenString)); return std::make_unique<DoubleValueNode>(std::stod(tokenString));
case TokenType::string_literal: case TokenType::string_literal:
return std::make_unique<StringValueNode>(tokenString); return std::make_unique<StringValueNode>(tokenString);
case TokenType::identifier: case TokenType::identifier:
@ -264,7 +338,7 @@ namespace usql {
} }
RelationalOperatorType Parser::parse_relational_operator() { RelationalOperatorType Parser::parse_relational_operator() {
auto op = lexer.consumeCurrentToken(); auto op = m_lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::equal: case TokenType::equal:
return RelationalOperatorType::equal; return RelationalOperatorType::equal;
@ -284,7 +358,7 @@ namespace usql {
} }
LogicalOperatorType Parser::parse_logical_operator() { LogicalOperatorType Parser::parse_logical_operator() {
auto op = lexer.consumeCurrentToken(); auto op = m_lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::logical_and: case TokenType::logical_and:
return LogicalOperatorType::and_operator; return LogicalOperatorType::and_operator;
@ -296,7 +370,7 @@ namespace usql {
} }
ArithmeticalOperatorType Parser::parse_arithmetical_operator() { ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
auto op = lexer.consumeCurrentToken(); auto op = m_lexer.consumeCurrentToken();
switch (op.type) { switch (op.type) {
case TokenType::plus: case TokenType::plus:
return ArithmeticalOperatorType::plus_operator; return ArithmeticalOperatorType::plus_operator;

View File

@ -8,7 +8,6 @@
namespace usql { namespace usql {
enum class ColumnType { enum class ColumnType {
integer_type, integer_type,
float_type, float_type,
@ -25,13 +24,16 @@ namespace usql {
relational_operator, relational_operator,
arithmetical_operator, arithmetical_operator,
create_table, create_table,
create_table_as_select,
insert_into, insert_into,
select_from, select_from,
delete_from, delete_from,
update_table, update_table,
load_table, load_table,
save_table,
column_name, column_name,
column_value, column_value,
function,
column_def, column_def,
error error
}; };
@ -45,18 +47,17 @@ namespace usql {
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 SelectColNode : Node {
std::string value; std::unique_ptr<Node> value;
std::string name;
ColValueNode(const std::string col_value) : SelectColNode(std::unique_ptr<Node> column, const std::string& alias) :
Node(NodeType::column_value), value(col_value) {} Node(NodeType::column_name), value(std::move(column)), name(alias) {}
}; };
// TODO add order in row
struct ColDefNode : Node { struct ColDefNode : Node {
std::string name; std::string name;
ColumnType type; ColumnType type;
@ -64,11 +65,18 @@ namespace usql {
int length; int length;
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, 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), Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
null(nullable) {} null(nullable) {}
}; };
struct FunctionNode : Node {
std::string function;
std::vector<std::unique_ptr<Node>> params;
FunctionNode(const std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
Node(NodeType::function), function(func_name), params(std::move(pars)) {}
};
struct TrueNode : Node { struct TrueNode : Node {
TrueNode() : Node(NodeType::true_node) {} TrueNode() : Node(NodeType::true_node) {}
@ -77,37 +85,32 @@ namespace usql {
struct ValueNode : Node { struct ValueNode : Node {
ValueNode(NodeType type) : Node(type) {} ValueNode(NodeType type) : Node(type) {}
virtual int getIntValue() = 0; virtual bool isNull() { return false; }
virtual long getIntValue() = 0;
virtual double getDoubleValue() = 0; virtual double getDoubleValue() = 0;
virtual std::string getStringValue() = 0; virtual std::string getStringValue() = 0;
virtual ~ValueNode() {}; virtual ~ValueNode() {};
}; };
struct IntValueNode : ValueNode { struct IntValueNode : ValueNode {
int value; long value;
IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {} IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
int getIntValue() { return value; }; long getIntValue() override { return value; };
double getDoubleValue() override { return (double) value; };
double getDoubleValue() { return (double) value; }; std::string getStringValue() override { return std::to_string(value); }
std::string getStringValue() { return std::to_string(value); }
}; };
struct FloatValueNode : ValueNode { struct DoubleValueNode : ValueNode {
double value; double value;
FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {} DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
int getIntValue() { return (int) value; }; long getIntValue() override { return (long) value; };
double getDoubleValue() override { return value; };
double getDoubleValue() { return value; }; std::string getStringValue() override { return std::to_string(value); }
std::string getStringValue() { return std::to_string(value); }
}; };
struct StringValueNode : ValueNode { struct StringValueNode : ValueNode {
@ -115,11 +118,9 @@ namespace usql {
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {} StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
int getIntValue() { return std::stoi(value); }; long getIntValue() override { return std::stoi(value); };
double getDoubleValue() override { return std::stod(value); };
double getDoubleValue() { return std::stod(value); }; std::string getStringValue() override { return value; };
std::string getStringValue() { return value; };
}; };
struct DatabaseValueNode : Node { struct DatabaseValueNode : Node {
@ -181,31 +182,38 @@ namespace usql {
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<std::unique_ptr<Node>> 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<std::unique_ptr<Node>> 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(std::move(values)) {}
}; };
struct SelectFromTableNode : Node { struct SelectFromTableNode : Node {
std::string table_name; std::string table_name;
std::vector<ColNameNode> cols_names; std::unique_ptr<std::vector<SelectColNode>> 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::unique_ptr<std::vector<SelectColNode>> 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(std::move(names)), where(std::move(where_clause)) {}
};
struct CreateTableAsSelectNode : Node {
std::string table_name;
std::unique_ptr<Node> select_table;
CreateTableAsSelectNode(const std::string name, std::unique_ptr<Node> table) :
Node(NodeType::create_table_as_select), table_name(name), select_table(std::move(table)) {}
}; };
struct UpdateTableNode : Node { struct UpdateTableNode : Node {
@ -226,16 +234,22 @@ namespace usql {
LoadIntoTableNode(const std::string name, std::string file) : LoadIntoTableNode(const std::string name, std::string file) :
Node(NodeType::load_table), table_name(name), filename(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 { 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)) {}
}; };
@ -249,32 +263,23 @@ namespace usql {
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_value();
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_load_table();
std::unique_ptr<Node> parse_save_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 m_lexer;
std::unique_ptr<Node> parse_relational_expression(); std::unique_ptr<Node> parse_relational_expression();
}; };
} }

View File

@ -19,13 +19,13 @@ namespace usql {
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->getIntValue());
} }
if (ColFloatValue *other_v = dynamic_cast<ColFloatValue *>(other.m_columns[i].get())) { if (ColDoubleValue *other_v = dynamic_cast<ColDoubleValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->floatValue()); setColumnValue(i, other_v->getDoubleValue());
} }
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->getStringValue());
} }
} }
} }
@ -35,22 +35,52 @@ namespace usql {
return *this; return *this;
} }
void Row::setColumnValue(int col_index, int value) { void Row::setColumnNull(int col_index) {
m_columns[col_index] = std::make_unique<ColNullValue>();
}
void Row::setColumnValue(int col_index, long value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value); m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
} }
void Row::setColumnValue(int col_index, double value) { void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColFloatValue>(value); m_columns[col_index] = std::make_unique<ColDoubleValue>(value);
} }
void Row::setColumnValue(int col_index, const std::string &value) { void Row::setColumnValue(int col_index, const std::string &value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value); m_columns[col_index] = std::make_unique<ColStringValue>(value);
}; };
void Row::setColumnValue(ColDefNode *col_def, ColValue *col_value) {
if (!col_value->isNull()) {
if (col_def->type == ColumnType::integer_type)
setColumnValue(col_def->order, col_value->getIntValue());
else if (col_def->type == ColumnType::float_type)
setColumnValue(col_def->order, col_value->getDoubleValue());
else if (col_def->type == ColumnType::varchar_type)
setColumnValue(col_def->order, col_value->getStringValue());
} else {
setColumnNull(col_def->order);
}
}
void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) {
if (!col_value->isNull()) {
if (col_def->type == ColumnType::integer_type)
setColumnValue(col_def->order, col_value->getIntValue());
else if (col_def->type == ColumnType::float_type)
setColumnValue(col_def->order, col_value->getDoubleValue());
else if (col_def->type == ColumnType::varchar_type)
setColumnValue(col_def->order, col_value->getStringValue());
} else {
setColumnNull(col_def->order);
}
}
void Row::print() { void Row::print() {
for (int ci = 0; ci < m_columns.size(); ci++) { for (int ci = 0; ci < m_columns.size(); ci++) {
if (ci > 0) std::cout << ","; if (ci > 0) std::cout << ",";
auto v = m_columns[ci]->stringValue(); auto v = m_columns[ci]->getStringValue();
std::cout << v; std::cout << v;
} }
std::cout << std::endl; std::cout << std::endl;

View File

@ -10,67 +10,54 @@ namespace usql {
struct ColValue { struct ColValue {
virtual bool isNull() { return false; };;;; virtual bool isNull() { return false; };
virtual long getIntValue() { throw Exception("Not supported"); };
virtual int integerValue() { throw Exception("Not supported"); }; virtual double getDoubleValue() { throw Exception("Not supported"); };
virtual std::string getStringValue() { throw Exception("Not supported"); };
virtual double floatValue() { throw Exception("Not supported"); };
virtual std::string stringValue() { throw Exception("Not supported"); };
}; };
struct ColNullValue : ColValue { struct ColNullValue : ColValue {
virtual bool isNull() { return true; }; virtual bool isNull() { return true; };
virtual std::string getStringValue() { return "null"; };
virtual std::string stringValue() { return "null"; };
}; };
struct ColIntegerValue : ColValue { struct ColIntegerValue : ColValue {
ColIntegerValue(int value) : m_integer(value) {}; ColIntegerValue(long value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {}; ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
virtual int integerValue() { return m_integer; }; virtual long getIntValue() { return m_integer; };
virtual double getDoubleValue() { return (double) m_integer; };
virtual double floatValue() { return (double) m_integer; }; virtual std::string getStringValue() { return std::to_string(m_integer); };
virtual std::string stringValue() { return std::to_string(m_integer); };
int m_integer; int m_integer;
}; };
struct ColFloatValue : ColValue { struct ColDoubleValue : ColValue {
ColFloatValue(double value) : m_float(value) {}; ColDoubleValue(double value) : m_double(value) {};
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
ColFloatValue(const ColFloatValue &other) : m_float(other.m_float) {} virtual long getIntValue() { return (long) m_double; };
virtual double getDoubleValue() { return m_double; };
virtual std::string getStringValue() { return std::to_string(m_double); };
virtual int integerValue() { return (int) m_float; }; double m_double;
virtual double floatValue() { return m_float; };
virtual std::string stringValue() { return std::to_string(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 int integerValue() { return std::stoi(m_string); }; virtual long getIntValue() { return std::stoi(m_string); };
virtual double getDoubleValue() { return std::stod(m_string); };
virtual double floatValue() { return std::stod(m_string); }; virtual std::string getStringValue() { return m_string; };
virtual std::string stringValue() { return m_string; };
std::string m_string; std::string m_string;
}; };
@ -80,22 +67,22 @@ namespace usql {
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 setColumnNull(int col_index);
void setColumnValue(int col_index, long value);
void setColumnValue(int col_index, double value); void setColumnValue(int col_index, double value);
void setColumnValue(int col_index, const std::string &value); void setColumnValue(int col_index, const std::string &value);
void setColumnValue(ColDefNode *col_def, ColValue *col_value);
void setColumnValue(ColDefNode *col_def, ValueNode *col_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) const {
return m_columns[i].get(); return m_columns[i].get();
} }
@ -105,4 +92,4 @@ namespace usql {
std::vector<std::unique_ptr<ColValue>> m_columns; std::vector<std::unique_ptr<ColValue>> m_columns;
}; };
} } // namespace

View File

@ -3,45 +3,71 @@
namespace usql { namespace usql {
Table::Table(const std::string name, const std::vector<ColDefNode> columns) { 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) {
row.print(); row.print();
} }
} }
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 for(const Row& orig_row : other.m_rows) {
} addCopyOfRow(orig_row);
}
}
void Table::addRow(const Row &row) { void Table::addRow(const Row &row) {
// TODO validate for not null values // TODO validate for not null values
// todo validate for length etc // todo validate for length etc
m_rows.push_back(row); m_rows.push_back(row);
} }
} void Table::addCopyOfRow(const Row &row) {
// TODO validate for not null values
// todo validate for length etc
Row new_row = createEmptyRow();
for(int i = 0; i < m_col_defs.size(); i++) {
ColValue *ct = row.ithColumn(i);
if (ct->isNull()) {
new_row.setColumnNull(i);
} else {
if (m_col_defs[i].type == ColumnType::integer_type) {
new_row.setColumnValue(i, row.ithColumn(i)->getIntValue());
} else if (m_col_defs[i].type == ColumnType::float_type) {
new_row.setColumnValue(i, row.ithColumn(i)->getDoubleValue());
} else if (m_col_defs[i].type == ColumnType::varchar_type) {
new_row.setColumnValue(i, row.ithColumn(i)->getStringValue());
}
}
}
m_rows.push_back(row);
}
} // namespace

View File

@ -10,7 +10,6 @@ namespace usql {
struct Table { 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);
@ -19,6 +18,7 @@ namespace usql {
Row createEmptyRow(); // TODO this means unnecessary copying Row createEmptyRow(); // TODO this means unnecessary copying
void addRow(const Row &row); void addRow(const Row &row);
void addCopyOfRow(const Row &row);
void print(); void print();

BIN
usql/table.h.gch Normal file

Binary file not shown.

View File

@ -1,6 +1,7 @@
#include "usql.h" #include "usql.h"
#include "exception.h" #include "exception.h"
#include "csvreader.h" #include "csvreader.h"
#include "ml_date.h"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -8,9 +9,14 @@
namespace usql { namespace usql {
std::unique_ptr<Table> USql::execute(const std::string &command) { std::unique_ptr<Table> USql::execute(const std::string &command) {
auto node = m_parser.parse(command); try {
std::unique_ptr<Node> node = m_parser.parse(command);
return execute(*node);
} catch (std::exception &e) {
return create_stmt_result_table(-1, e.what());
}
return execute(*node);
} }
std::unique_ptr<Table> USql::execute(Node &node) { std::unique_ptr<Table> USql::execute(Node &node) {
@ -18,18 +24,22 @@ std::unique_ptr<Table> USql::execute(Node &node) {
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));
case NodeType::insert_into: case NodeType::create_table_as_select:
return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node)); return execute_create_table_as_table(static_cast<CreateTableAsSelectNode &>(node));
case NodeType::select_from: case NodeType::insert_into:
return execute_select(static_cast<SelectFromTableNode &>(node)); return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node));
case NodeType::delete_from: case NodeType::select_from:
return execute_delete(static_cast<DeleteFromTableNode &>(node)); return execute_select(static_cast<SelectFromTableNode &>(node));
case NodeType::update_table: case NodeType::delete_from:
return execute_update(static_cast<UpdateTableNode &>(node)); return execute_delete(static_cast<DeleteFromTableNode &>(node));
case NodeType::load_table: case NodeType::update_table:
return execute_load(static_cast<LoadIntoTableNode &>(node)); return execute_update(static_cast<UpdateTableNode &>(node));
default: case NodeType::load_table:
return create_stmt_result_table(-1, "unknown statement"); 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");
} }
} }
@ -43,6 +53,28 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
} }
std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNode &node) {
// TODO check table does not exists
auto select = execute_select((SelectFromTableNode &) *node.select_table);
// create table
Table new_table{node.table_name, select->m_col_defs};
m_tables.push_back(new_table);
// copy rows
// must be here, if rows are put into new_table, they are lost during m_tables.push_table
Table *table = find_table(node.table_name);
for( Row& orig_row : select->m_rows) {
table->addCopyOfRow(orig_row);
}
select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode
return create_stmt_result_table(0, "table created");
}
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) { std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
// TODO check column names.size = values.size // TODO check column names.size = values.size
@ -55,64 +87,56 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// 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++) {
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name); ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
auto col_value = evalValueNode(table_def, new_row, node.cols_values[i].get());
// TODO validate value new_row.setColumnValue(&col_def, col_value.get());
if (col_def.type == ColumnType::integer_type) {
new_row.setColumnValue(col_def.order, std::stoi(node.cols_values[i].value));
} else if (col_def.type == ColumnType::float_type) {
new_row.setColumnValue(col_def.order, std::stof(node.cols_values[i].value));
} else {
new_row.setColumnValue(col_def.order, node.cols_values[i].value);
}
} }
// append new_row // append new_row
table_def->addRow(new_row); table_def->addRow(new_row);
return create_stmt_result_table(0, "insert succeded"); return create_stmt_result_table(0, "insert succeeded");
} }
std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) { std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// 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
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); for (int i = 0; i < node.cols_names->size(); i++) {
result_tbl_col_defs.push_back(col); auto [ src_tbl_col_index, rst_tbl_col_def ] = getColumnDefinition(table, &node.cols_names->operator[](i), i);
i++; source_table_col_index.push_back(src_tbl_col_index);
result_tbl_col_defs.push_back(rst_tbl_col_def);
} }
auto result = std::make_unique<Table>("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);
if (result_tbl_col_defs[idx].type == ColumnType::integer_type) if (row_col_index == -1) { // TODO introduce constant here
new_row.setColumnValue(idx, auto evaluated_value = evalValueNode(table, *row, node.cols_names->operator[](idx).value.get());
((ColIntegerValue *) col_value)->integerValue()); ValueNode *col_value = evaluated_value.get();
if (result_tbl_col_defs[idx].type == ColumnType::float_type)
new_row.setColumnValue(idx, col_value->floatValue()); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
if (result_tbl_col_defs[idx].type == ColumnType::varchar_type) } else {
new_row.setColumnValue(idx, col_value->stringValue()); ColValue *col_value = row->ithColumn(row_col_index);
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
}
} }
// add row to result // add row to result
@ -123,17 +147,38 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
return std::move(result); return std::move(result);
} }
std::tuple<int, ColDefNode> USql::getColumnDefinition(Table *table, SelectColNode *select_col_node, int col_order ) {
std::string new_col_name = select_col_node->name;
if (select_col_node->value->node_type == NodeType::column_name) {
ColDefNode src_cdef = table->get_column_def(new_col_name);
ColDefNode cdef = ColDefNode{new_col_name, src_cdef.type, col_order, src_cdef.length, src_cdef.null};
return std::make_tuple(src_cdef.order, cdef);
} else if (select_col_node->value->node_type == NodeType::function) {
auto node = static_cast<FunctionNode *>(select_col_node->value.get());
if (node->function == "to_string") {
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::varchar_type, col_order, 64, true};
return std::make_tuple(-1, cdef);
} else if (node->function == "to_date") {
ColDefNode cdef = ColDefNode{new_col_name, ColumnType::integer_type, col_order, 1, true};
return std::make_tuple(-1, cdef);
}
throw Exception("Unsupported function");
}
throw Exception("Unsupported node type");
}
std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) { std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
// 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);
} else { } else {
@ -141,38 +186,25 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
} }
} }
return create_stmt_result_table(0, "delete succeded"); return create_stmt_result_table(0, "delete succeeded");
} }
std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) { std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
// 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)) {
int i = 0; int i = 0;
for (auto col : node.cols_names) { for (const auto& col : node.cols_names) {
// TODO cache it like in select ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select
ColDefNode cdef = table->get_column_def(col.name); std::unique_ptr<ValueNode> new_val = evalArithmeticOperator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]), table, *row);
std::unique_ptr<ValueNode> new_val = evalArithmetic(cdef.type, row->setColumnValue(&col_def, new_val.get());
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, row);
if (cdef.type == ColumnType::integer_type) {
row->setColumnValue(cdef.order, new_val->getIntValue());
} else if (cdef.type == ColumnType::float_type) {
row->setColumnValue(cdef.order, new_val->getDoubleValue());
} else if (cdef.type == ColumnType::varchar_type) {
row->setColumnValue(cdef.order, new_val->getStringValue());
} else {
throw Exception("Implement me!");
}
i++; i++;
} }
} }
@ -208,7 +240,7 @@ std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
// TODO validate value // TODO validate value
if (col_def.type == ColumnType::integer_type) { 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) { } else if (col_def.type == ColumnType::float_type) {
new_row.setColumnValue(col_def.order, std::stof(csv_line[i])); new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
} else { } else {
@ -224,154 +256,230 @@ std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
} }
bool USql::evalWhere(Node *where, Table *table, std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
std::vector<Row, std::allocator<Row>>::iterator &row) const { // 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->getStringValue(); // 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 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 USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const {
std::vector<Row, std::allocator<Row>>::iterator &row) const { std::unique_ptr<ValueNode> left_value = evalValueNode(table, row, filter.left.get());
std::unique_ptr<ValueNode> left_value = evalNode(table, row, filter.left.get()); std::unique_ptr<ValueNode> right_value = evalValueNode(table, row, filter.right.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) {
comparator = left_value->getIntValue() - right_value->getIntValue(); comparator = left_value->getIntValue() - right_value->getIntValue();
} else if ((left_value->node_type == NodeType::int_value && } else if ((left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::float_value) ||
right_value->node_type == NodeType::float_value) || (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::int_value) ||
(left_value->node_type == NodeType::float_value && (left_value->node_type == NodeType::float_value && right_value->node_type == NodeType::float_value)) {
right_value->node_type == NodeType::int_value) ||
(left_value->node_type == NodeType::float_value &&
right_value->node_type == NodeType::float_value)) {
comparator = left_value->getDoubleValue() - right_value->getDoubleValue(); comparator = left_value->getDoubleValue() - right_value->getDoubleValue();
} else if (left_value->node_type == NodeType::string_value || } else if (left_value->node_type == NodeType::string_value || right_value->node_type == NodeType::string_value) {
right_value->node_type == NodeType::string_value) {
comparator = left_value->getStringValue().compare(right_value->getStringValue()); comparator = left_value->getStringValue().compare(right_value->getStringValue());
} else { } else {
// TODO throw exception // TODO throw exception
} }
switch (filter.op) { switch (filter.op) {
case RelationalOperatorType::equal: case RelationalOperatorType::equal:
return comparator == 0.0; return comparator == 0.0;
case RelationalOperatorType::not_equal: case RelationalOperatorType::not_equal:
return comparator != 0.0; return comparator != 0.0;
case RelationalOperatorType::greater: case RelationalOperatorType::greater:
return comparator > 0.0; return comparator > 0.0;
case RelationalOperatorType::greater_equal: case RelationalOperatorType::greater_equal:
return comparator >= 0.0; return comparator >= 0.0;
case RelationalOperatorType::lesser: case RelationalOperatorType::lesser:
return comparator < 0.0; return comparator < 0.0;
case RelationalOperatorType::lesser_equal: case RelationalOperatorType::lesser_equal:
return comparator <= 0.0; return comparator <= 0.0;
} }
throw Exception("invalid relational operator"); throw Exception("invalid relational operator");
} }
std::unique_ptr<ValueNode> std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) {
USql::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const { if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
if (node->node_type == NodeType::database_value) { return evalDatabaseValueNode(table, row, 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
auto db_value = row->ithColumn(col_def.order);
if (col_def.type == ColumnType::integer_type) { } else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) {
return std::make_unique<IntValueNode>(db_value->integerValue()); return evalLiteralValueNode(table, row, node);
}
if (col_def.type == ColumnType::float_type) {
return std::make_unique<FloatValueNode>(db_value->floatValue());
}
if (col_def.type == ColumnType::varchar_type) {
return std::make_unique<StringValueNode>(db_value->stringValue());
}
} else if (node->node_type == NodeType::int_value) { } else if (node->node_type == NodeType::function) {
IntValueNode *ivl = static_cast<IntValueNode *>(node); return evalFunctionValueNode(table, row, node);
}
throw Exception("unsupported node type");
}
std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, Node *node) {
auto *dvl = static_cast<DatabaseValueNode *>(node);
ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once
auto db_value = row.ithColumn(col_def.order);
if (col_def.type == ColumnType::integer_type) {
return std::make_unique<IntValueNode>(db_value->getIntValue());
}
if (col_def.type == ColumnType::float_type) {
return std::make_unique<DoubleValueNode>(db_value->getDoubleValue());
}
if (col_def.type == ColumnType::varchar_type) {
return std::make_unique<StringValueNode>(db_value->getStringValue());
}
throw Exception("unknown database value type");
}
std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::int_value) {
auto *ivl = static_cast<IntValueNode *>(node);
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); auto *ivl = static_cast<DoubleValueNode *>(node);
return std::make_unique<FloatValueNode>(ivl->value); return std::make_unique<DoubleValueNode>(ivl->value);
} else if (node->node_type == NodeType::string_value) { } else if (node->node_type == NodeType::string_value) {
StringValueNode *ivl = static_cast<StringValueNode *>(node); auto *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 USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, Node *node) {
std::vector<Row, std::allocator<Row>>::iterator &iter) const { auto *fnc = static_cast<FunctionNode *>(node);
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
if ((node.op == LogicalOperatorType::and_operator && !left) || std::vector<std::unique_ptr<ValueNode>> evaluatedPars;
(node.op == LogicalOperatorType::or_operator && left)) for(auto & param : fnc->params) {
evaluatedPars.push_back(evalValueNode(table, row, param.get()));
}
// TODO use some enum
if (fnc->function == "lower") {
std::string str = evaluatedPars[0]->getStringValue();
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return std::tolower(c); });
return std::make_unique<StringValueNode>(str);
}
if (fnc->function == "upper") {
std::string str = evaluatedPars[0]->getStringValue();
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) -> unsigned char { return std::toupper(c); });
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");
}
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
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, row);
return right; return right;
} }
std::unique_ptr<ValueNode> std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const {
USql::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const {
if (node.op == ArithmeticalOperatorType::copy_value) { if (node.op == ArithmeticalOperatorType::copy_value) {
return evalNode(table, row, node.left.get()); return evalValueNode(table, row, node.left.get());
} }
std::unique_ptr<ValueNode> left = evalNode(table, row, node.left.get()); std::unique_ptr<ValueNode> left = evalValueNode(table, row, node.left.get());
std::unique_ptr<ValueNode> right = evalNode(table, row, node.right.get()); std::unique_ptr<ValueNode> right = evalValueNode(table, row, node.right.get());
if (outType == ColumnType::float_type) { if (outType == ColumnType::float_type) {
double l = ((ValueNode *) left.get())->getDoubleValue(); double l = ((ValueNode *) left.get())->getDoubleValue();
double r = ((ValueNode *) right.get())->getDoubleValue(); double r = ((ValueNode *) right.get())->getDoubleValue();
switch (node.op) { switch (node.op) {
case ArithmeticalOperatorType::plus_operator: case ArithmeticalOperatorType::plus_operator:
return std::make_unique<FloatValueNode>(l + r); return std::make_unique<DoubleValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator: case ArithmeticalOperatorType::minus_operator:
return std::make_unique<FloatValueNode>(l - r); return std::make_unique<DoubleValueNode>(l - r);
case ArithmeticalOperatorType::multiply_operator: case ArithmeticalOperatorType::multiply_operator:
return std::make_unique<FloatValueNode>(l * r); return std::make_unique<DoubleValueNode>(l * r);
case ArithmeticalOperatorType::divide_operator: case ArithmeticalOperatorType::divide_operator:
return std::make_unique<FloatValueNode>(l / r); return std::make_unique<DoubleValueNode>(l / r);
default: default:
throw Exception("implement me!!"); throw Exception("implement me!!");
} }
} else if (outType == ColumnType::integer_type) { } else if (outType == ColumnType::integer_type) {
int l = ((ValueNode *) left.get())->getIntValue(); long l = ((ValueNode *) left.get())->getIntValue();
int r = ((ValueNode *) right.get())->getIntValue(); long r = ((ValueNode *) right.get())->getIntValue();
switch (node.op) { switch (node.op) {
case ArithmeticalOperatorType::plus_operator: case ArithmeticalOperatorType::plus_operator:
return std::make_unique<IntValueNode>(l + r); return std::make_unique<IntValueNode>(l + r);
case ArithmeticalOperatorType::minus_operator: case ArithmeticalOperatorType::minus_operator:
return std::make_unique<IntValueNode>(l - r); return std::make_unique<IntValueNode>(l - r);
case ArithmeticalOperatorType::multiply_operator: case ArithmeticalOperatorType::multiply_operator:
return std::make_unique<IntValueNode>(l * r); return std::make_unique<IntValueNode>(l * r);
case ArithmeticalOperatorType::divide_operator: case ArithmeticalOperatorType::divide_operator:
return std::make_unique<IntValueNode>(l / r); return std::make_unique<IntValueNode>(l / r);
default: default:
throw Exception("implement me!!"); throw Exception("implement me!!");
} }
} else if (outType == ColumnType::varchar_type) { } else if (outType == ColumnType::varchar_type) {
@ -380,9 +488,8 @@ USql::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *
switch (node.op) { switch (node.op) {
case ArithmeticalOperatorType::plus_operator: case ArithmeticalOperatorType::plus_operator:
return std::make_unique<StringValueNode>(l + r); return std::make_unique<StringValueNode>(l + r);
default:
default: throw Exception("implement me!!");
throw Exception("implement me!!");
} }
} }
@ -390,19 +497,7 @@ USql::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *
} }
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string& text) {
Table *USql::find_table(const std::string name) {
auto name_cmp = [name](const 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)) {
return table_def.operator->();
} else {
throw Exception("table not found (" + name + ")");
}
}
std::unique_ptr<Table> USql::create_stmt_result_table(int code, std::string text) {
std::vector<ColDefNode> result_tbl_col_defs{}; 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("code", ColumnType::integer_type, 0, 1, false));
result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false)); result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
@ -417,4 +512,16 @@ std::unique_ptr<Table> USql::create_stmt_result_table(int code, std::string text
return std::move(table_def); return std::move(table_def);
} }
}
Table *USql::find_table(const std::string &name) {
auto name_cmp = [name](const 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)) {
return table_def.operator->();
} else {
throw Exception("table not found (" + name + ")");
}
}
} // namespace

View File

@ -4,13 +4,15 @@
#include "table.h" #include "table.h"
#include <string> #include <string>
#include <list>
namespace usql { namespace usql {
class USql { class USql {
public: public:
USql() {}; USql() = default;
std::unique_ptr<Table> execute(const std::string &command); std::unique_ptr<Table> execute(const std::string &command);
@ -18,41 +20,37 @@ private:
std::unique_ptr<Table> execute(Node &node); std::unique_ptr<Table> execute(Node &node);
std::unique_ptr<Table> execute_create_table(CreateTableNode &node); std::unique_ptr<Table> execute_create_table(CreateTableNode &node);
std::unique_ptr<Table> execute_create_table_as_table(CreateTableAsSelectNode &node);
std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node); std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
std::unique_ptr<Table> execute_select(SelectFromTableNode &node); std::unique_ptr<Table> execute_select(SelectFromTableNode &node);
std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node); std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
std::unique_ptr<Table> execute_update(UpdateTableNode &node); std::unique_ptr<Table> execute_update(UpdateTableNode &node);
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node); std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
std::unique_ptr<Table> execute_save(SaveTableNode &node);
Table *find_table(const std::string name);
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
private: private:
bool evalWhere(Node *where, Table *table, bool evalWhere(Node *where, Table *table, Row &row) const;
std::vector<Row, std::allocator<Row>>::iterator &row) const;
std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, static std::unique_ptr<ValueNode> evalValueNode(Table *table, Row &row, Node *node);
Node *node) const; static std::unique_ptr<ValueNode> evalDatabaseValueNode(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> evalLiteralValueNode(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> evalFunctionValueNode(Table *table, Row &row, Node *node);
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const;
std::vector<Row, std::allocator<Row>>::iterator &iter) const; bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const;
std::unique_ptr<ValueNode> evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const;
static std::unique_ptr<Table> create_stmt_result_table(long code, const std::string& text);
static std::tuple<int, ColDefNode> getColumnDefinition(Table *table, SelectColNode *select_col_node, int col_order) ;
Table *find_table(const std::string &name);
std::unique_ptr<ValueNode> evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
std::vector<Row, std::allocator<Row>>::iterator &row) const;
private: private:
Parser m_parser; Parser m_parser;
std::vector<Table> m_tables; std::list<Table> m_tables;
}; };
} } // namespace

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c
c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp
c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o
cp stdlib/*.lsp /usr/local/var/mlisp/ cp stdlib/*.lsp /usr/local/var/mlisp/

View File

@ -24,7 +24,7 @@ fi
echo "Building ml" echo "Building ml"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c" ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp" ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -c -O2 -I/usr/local/opt/openssl/include -Iclib --std=c++17 ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o" ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o"