very basic version of usql
This commit is contained in:
parent
f594437b61
commit
577370caef
|
|
@ -31,11 +31,21 @@ set(SOURCE
|
|||
ml_string.cpp
|
||||
ml_util.cpp
|
||||
ml_profiler.cpp
|
||||
ml_usql.cpp
|
||||
clib/csvparser.cpp
|
||||
clib/sslclient.cpp
|
||||
clib/json11.cpp
|
||||
clib/printf.cpp
|
||||
clib/linenoise.c)
|
||||
clib/linenoise.c
|
||||
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)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCE})
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ utils/local_install.sh
|
|||
|
||||
|
||||
### KNOWNN BUGS
|
||||
|
||||
(read-url "https://api.nasdaq.com/api/calendar/dividends/") ; hangs in sslclient.cpp line 132
|
||||
|
||||
### TODO
|
||||
- add debug support, at least call stack
|
||||
|
|
|
|||
37
debug.lsp
37
debug.lsp
|
|
@ -1,20 +1,27 @@
|
|||
(define fc 5)
|
||||
(define sc 5)
|
||||
(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"))
|
||||
|
||||
(define first_tid (thread-create (while (> fc 0) (do (thread-under-lock "ilock" (do (set! fc (dec fc))(print 1))) (thread-sleep 500)))))
|
||||
(define second_tid (thread-create (while (> sc 0) (do (thread-under-lock "ilock" (do (set! sc (dec sc))(print 2))) (thread-sleep 750)))))
|
||||
;; (read-url "https://api.nasdaq.com/api/calendar/dividends/")
|
||||
|
||||
(print "first thread id:" first_tid)
|
||||
(print "second thread id:" second_tid)
|
||||
(threads-join)
|
||||
(print "ok")
|
||||
|
||||
(try
|
||||
(ls-dir "nonexisting/dir")
|
||||
(print "dir not exists"))
|
||||
;; (define fc 5)
|
||||
;; (define sc 5)
|
||||
|
||||
(try
|
||||
(throw "my-exception")
|
||||
(print "exception caught:" ml-exception))
|
||||
;; (define first_tid (thread-create (while (> fc 0) (do (thread-under-lock "ilock" (do (set! fc (dec fc))(print 1))) (thread-sleep 500)))))
|
||||
;; (define second_tid (thread-create (while (> sc 0) (do (thread-under-lock "ilock" (do (set! sc (dec sc))(print 2))) (thread-sleep 750)))))
|
||||
|
||||
(/ 1 0)
|
||||
;; (print "first thread id:" first_tid)
|
||||
;; (print "second thread id:" second_tid)
|
||||
;; (threads-join)
|
||||
;; (print "ok")
|
||||
|
||||
;; (try
|
||||
;; (ls-dir "nonexisting/dir")
|
||||
;; (print "dir not exists"))
|
||||
|
||||
;; (try
|
||||
;; (throw "my-exception")
|
||||
;; (print "exception caught:" ml-exception))
|
||||
|
||||
;; (/ 1 0)
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
|
||||
#include "ml_usql.h"
|
||||
|
||||
using namespace usql;
|
||||
|
||||
MlValue uSQL::execute(const std::string &command) {
|
||||
std::unique_ptr<usql::Table> sql = USql::execute(command);
|
||||
|
||||
return ivaluize(sql.get());
|
||||
}
|
||||
|
||||
MlValue uSQL::ivaluize(const usql::Table *table) {
|
||||
std::vector<MlValue> rows;
|
||||
std::vector<MlValue> columns;
|
||||
|
||||
for (auto row : table->m_rows) {
|
||||
columns.clear();
|
||||
for (int i = 0; i < table->columns_count(); i++) {
|
||||
auto c = row.ithColumn(i);
|
||||
auto type = table->m_col_defs[i].type;
|
||||
if (type == ColumnType::integer_type) {
|
||||
columns.push_back(MlValue((long)c->integerValue()));
|
||||
} else if (type == ColumnType::float_type) {
|
||||
columns.push_back(MlValue((double)c->floatValue()));
|
||||
} else {
|
||||
columns.push_back(MlValue::string(c->stringValue()));
|
||||
}
|
||||
}
|
||||
rows.push_back(columns);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include "ml.h"
|
||||
#include "usql/usql.h"
|
||||
|
||||
|
||||
class uSQL : public usql::USql {
|
||||
|
||||
private:
|
||||
uSQL() {};
|
||||
|
||||
public:
|
||||
static uSQL& instance(){
|
||||
static uSQL instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public:
|
||||
MlValue execute(const std::string &command);
|
||||
|
||||
private:
|
||||
MlValue ivaluize(const usql::Table *table);
|
||||
|
||||
};
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
### 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
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
|
||||
#include "csvreader.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
CsvReader::CsvReader(bool skip_hdr, char field_sep, char quote_ch, char line_sep, char line_sep2) {
|
||||
skip_header = skip_hdr;
|
||||
field_separator = field_sep;
|
||||
quote_character = quote_ch;
|
||||
line_separator = line_sep;
|
||||
line_separator2 = line_sep2;
|
||||
|
||||
header_skiped = false;
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> CsvReader::parseCSV(const std::string &csvSource) {
|
||||
int linesRead = 0;
|
||||
bool inQuote(false);
|
||||
bool newLine(false);
|
||||
std::string field;
|
||||
|
||||
std::vector<std::vector<std::string>> parsed_data;
|
||||
parsed_data.reserve(128);
|
||||
|
||||
std::vector<std::string> line;
|
||||
line.reserve(32);
|
||||
|
||||
std::string::const_iterator aChar = csvSource.begin();
|
||||
while (aChar != csvSource.end()) {
|
||||
if (*aChar == quote_character) {
|
||||
newLine = false;
|
||||
inQuote = !inQuote;
|
||||
} else if (*aChar == field_separator) {
|
||||
newLine = false;
|
||||
if (inQuote == true) {
|
||||
field += *aChar;
|
||||
} else {
|
||||
line.push_back(field);
|
||||
field.clear();
|
||||
}
|
||||
} else if (*aChar == line_separator || *aChar == line_separator2) {
|
||||
if (inQuote == true) {
|
||||
field += *aChar;
|
||||
} else {
|
||||
if (newLine == false) {
|
||||
line.push_back(field);
|
||||
add_line(line, parsed_data);
|
||||
field.clear();
|
||||
line.clear();
|
||||
linesRead++;
|
||||
if (linesRead == 16) {
|
||||
int linesEstimation =
|
||||
csvSource.size() /
|
||||
(std::distance(csvSource.begin(), aChar) / linesRead);
|
||||
if (linesEstimation > parsed_data.capacity())
|
||||
parsed_data.reserve(linesEstimation);
|
||||
}
|
||||
newLine = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newLine = false;
|
||||
field.push_back(*aChar);
|
||||
}
|
||||
|
||||
aChar++;
|
||||
}
|
||||
|
||||
if (field.size())
|
||||
line.push_back(field);
|
||||
|
||||
add_line(line, parsed_data);
|
||||
|
||||
return parsed_data;
|
||||
}
|
||||
|
||||
|
||||
void CsvReader::add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines) {
|
||||
if (skip_header && !header_skiped) {
|
||||
header_skiped = true;
|
||||
} else {
|
||||
if (line.size())
|
||||
lines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class CsvReader {
|
||||
|
||||
private:
|
||||
char field_separator;
|
||||
char line_separator;
|
||||
char line_separator2;
|
||||
char quote_character;
|
||||
|
||||
bool skip_header;
|
||||
bool header_skiped;
|
||||
|
||||
public:
|
||||
CsvReader(bool skip_hdr = false, char field_sep = ',', char quote_ch = '"', char line_sep = '\r',
|
||||
char line_sep2 = '\n');
|
||||
|
||||
std::vector<std::vector<std::string>> parseCSV(const std::string &csvSource);
|
||||
|
||||
private:
|
||||
void add_line(const std::vector<std::string> &line, std::vector<std::vector<std::string>> &lines);
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#include "exception.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Exception::Exception(const std::string &msg) {
|
||||
cause = msg;
|
||||
}
|
||||
|
||||
|
||||
const char *Exception::what() const noexcept { return cause.c_str(); }
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include "lexer.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class Exception : public std::exception {
|
||||
private:
|
||||
std::string cause;
|
||||
|
||||
public:
|
||||
Exception(const std::string &msg);
|
||||
|
||||
const char *what() const noexcept;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,417 @@
|
|||
#include "lexer.h"
|
||||
#include "exception.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace usql {
|
||||
|
||||
Token::Token(const std::string &token_str, TokenType typ) {
|
||||
token_string = token_str;
|
||||
type = typ;
|
||||
}
|
||||
|
||||
|
||||
Lexer::Lexer() {
|
||||
k_words_regex =
|
||||
"[0-9]+\\.[0-9]+|[0-9][0-9_]+[0-9]|[0-9]+|[A-Za-z]+[A-Za-z0-9_#]*|[\\(\\)\\[\\]\\{\\}]|[-\\+\\*/"
|
||||
",;:\?]|==|>=|<=|~=|>|<|=|;|~|\\||or|and|\n|\r|\r\n|'([^']|'')*'|\".*?\"|%.*?\n";
|
||||
k_int_regex = "[0-9]+";
|
||||
k_int_underscored_regex = "[0-9][0-9_]+[0-9]";
|
||||
k_double_regex = "[0-9]+\\.[0-9]+";
|
||||
k_identifier_regex = "[A-Za-z]+[A-Za-z0-9_#]*";
|
||||
}
|
||||
|
||||
void Lexer::parse(const std::string &code) {
|
||||
// TODO handle empty code
|
||||
m_tokens.clear();
|
||||
|
||||
// PERF something like this to preallocate ??
|
||||
if (code.size() > 100) {
|
||||
m_tokens.reserve(code.size() / 10);
|
||||
}
|
||||
m_code_str = code;
|
||||
if (!m_code_str.empty() && m_code_str.back() != '\n') {
|
||||
m_code_str.append("\n"); // TODO temp solution to prevent possible situation when last line is a comment
|
||||
}
|
||||
|
||||
auto words_begin = std::sregex_iterator(m_code_str.begin(), m_code_str.end(), k_words_regex);
|
||||
auto words_end = std::sregex_iterator();
|
||||
|
||||
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
|
||||
std::smatch match = *i;
|
||||
std::string match_str = match.str();
|
||||
TokenType token_type = type(match_str);
|
||||
if (token_type == TokenType::string_literal)
|
||||
match_str = stringLiteral(match_str);
|
||||
|
||||
if (token_type != TokenType::newline)
|
||||
m_tokens.push_back(Token{match_str, token_type});
|
||||
}
|
||||
|
||||
// DEBUG IT
|
||||
// debugTokens();
|
||||
|
||||
m_index = 0;
|
||||
}
|
||||
|
||||
void Lexer::debugTokens() {
|
||||
int i = 0;
|
||||
for (std::vector<Token>::iterator it = m_tokens.begin(); it != m_tokens.end(); ++it) {
|
||||
std::cerr << i << "\t" << it->token_string << std::endl;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
Token Lexer::currentToken() { return m_tokens[m_index]; }
|
||||
|
||||
Token Lexer::consumeCurrentToken() {
|
||||
int i = m_index;
|
||||
nextToken();
|
||||
return m_tokens[i];
|
||||
}
|
||||
|
||||
void Lexer::nextToken() {
|
||||
if (m_index < m_tokens.size()) {
|
||||
m_index++;
|
||||
}
|
||||
}
|
||||
|
||||
void Lexer::skipToken(TokenType type) {
|
||||
if (tokenType() == type) {
|
||||
nextToken();
|
||||
} else {
|
||||
throw Exception("ERROR unexpected token " + consumeCurrentToken().token_string + ", instead of " +
|
||||
typeToString(type));
|
||||
}
|
||||
}
|
||||
|
||||
void Lexer::skipTokenOptional(TokenType type) {
|
||||
if (tokenType() == type) {
|
||||
nextToken();
|
||||
}
|
||||
}
|
||||
|
||||
TokenType Lexer::tokenType() { return m_index < m_tokens.size() ? currentToken().type : TokenType::eof; }
|
||||
|
||||
TokenType Lexer::nextTokenType() {
|
||||
return m_index < m_tokens.size() - 1 ? m_tokens[m_index + 1].type : TokenType::eof;
|
||||
}
|
||||
|
||||
TokenType Lexer::prevTokenType() { return m_index > 0 ? m_tokens[m_index - 1].type : TokenType::undef; }
|
||||
|
||||
bool Lexer::isRelationalOperator(TokenType token_type) {
|
||||
return (token_type == TokenType::equal || token_type == TokenType::not_equal ||
|
||||
token_type == TokenType::greater || token_type == TokenType::greater_equal ||
|
||||
token_type == TokenType::lesser || token_type == TokenType::lesser_equal);
|
||||
}
|
||||
|
||||
bool Lexer::isLogicalOperator(TokenType token_type) {
|
||||
return (token_type == TokenType::logical_and || token_type == TokenType::logical_or);
|
||||
}
|
||||
|
||||
bool Lexer::isArithmeticalOperator(TokenType token_type) {
|
||||
return (token_type == TokenType::plus || token_type == TokenType::minus ||
|
||||
token_type == TokenType::multiply ||
|
||||
token_type == TokenType::divide);
|
||||
}
|
||||
|
||||
TokenType Lexer::type(const std::string &token) {
|
||||
// TODO, FIXME 'one is evaluated as identifier
|
||||
if (token == ";")
|
||||
return TokenType::semicolon;
|
||||
|
||||
if (token == "+")
|
||||
return TokenType::plus;
|
||||
|
||||
if (token == "-")
|
||||
return TokenType::minus;
|
||||
|
||||
if (token == "*")
|
||||
return TokenType::multiply;
|
||||
|
||||
if (token == "/")
|
||||
return TokenType::divide;
|
||||
|
||||
if (token == "(")
|
||||
return TokenType::open_paren;
|
||||
|
||||
if (token == ")")
|
||||
return TokenType::close_paren;
|
||||
|
||||
if (token == "=")
|
||||
return TokenType::equal;
|
||||
|
||||
if (token == "!=")
|
||||
return TokenType::not_equal;
|
||||
|
||||
if (token == ">")
|
||||
return TokenType::greater;
|
||||
|
||||
if (token == ">=")
|
||||
return TokenType::greater_equal;
|
||||
|
||||
if (token == "<")
|
||||
return TokenType::lesser;
|
||||
|
||||
if (token == "<=")
|
||||
return TokenType::lesser_equal;
|
||||
|
||||
if (token == "create")
|
||||
return TokenType::keyword_create;
|
||||
|
||||
if (token == "where")
|
||||
return TokenType::keyword_where;
|
||||
|
||||
if (token == "from")
|
||||
return TokenType::keyword_from;
|
||||
|
||||
if (token == "delete")
|
||||
return TokenType::keyword_delete;
|
||||
|
||||
if (token == "table")
|
||||
return TokenType::keyword_table;
|
||||
|
||||
if (token == "insert")
|
||||
return TokenType::keyword_insert;
|
||||
|
||||
if (token == "into")
|
||||
return TokenType::keyword_into;
|
||||
|
||||
if (token == "values")
|
||||
return TokenType::keyword_values;
|
||||
|
||||
if (token == "select")
|
||||
return TokenType::keyword_select;
|
||||
|
||||
if (token == "set")
|
||||
return TokenType::keyword_set;
|
||||
|
||||
if (token == "copy")
|
||||
return TokenType::keyword_copy;
|
||||
|
||||
if (token == "update")
|
||||
return TokenType::keyword_update;
|
||||
|
||||
if (token == "load")
|
||||
return TokenType::keyword_load;
|
||||
|
||||
if (token == "not")
|
||||
return TokenType::keyword_not;
|
||||
|
||||
if (token == "null")
|
||||
return TokenType::keyword_null;
|
||||
|
||||
if (token == "integer")
|
||||
return TokenType::keyword_int;
|
||||
|
||||
if (token == "float")
|
||||
return TokenType::keyword_float;
|
||||
|
||||
if (token == "varchar")
|
||||
return TokenType::keyword_varchar;
|
||||
|
||||
if (token == "or")
|
||||
return TokenType::logical_or;
|
||||
|
||||
if (token == "and")
|
||||
return TokenType::logical_and;
|
||||
|
||||
if (token == ",")
|
||||
return TokenType::comma;
|
||||
|
||||
if (token == "\n" || token == "\r\n" || token == "\r")
|
||||
return TokenType::newline;
|
||||
|
||||
if (token.length() > 1 && token.at(0) == '%' &&
|
||||
(token.at(token.length() - 1) == '\n' || token.at(token.length() - 1) == '\r'))
|
||||
return TokenType::comment;
|
||||
|
||||
// if (token.length() >= 2 && token.at(0) == '"' && token.at(token.length() - 1) == '"')
|
||||
// return TokenType::string_literal;
|
||||
|
||||
if (token.length() >= 2 && token.at(0) == '\'' && token.at(token.length() - 1) == '\'')
|
||||
return TokenType::string_literal;
|
||||
|
||||
if (std::regex_match(token, k_int_regex))
|
||||
return TokenType::int_number;
|
||||
|
||||
if (std::regex_match(token, k_int_underscored_regex))
|
||||
return TokenType::int_number;
|
||||
|
||||
if (std::regex_match(token, k_double_regex))
|
||||
return TokenType::double_number;
|
||||
|
||||
if (std::regex_match(token, k_identifier_regex))
|
||||
return TokenType::identifier;
|
||||
|
||||
if (m_index + 1 >= m_tokens.size())
|
||||
return TokenType::eof;
|
||||
|
||||
return TokenType::undef;
|
||||
}
|
||||
|
||||
std::string Lexer::stringLiteral(std::string token) {
|
||||
// remove ' or " from the literal ends
|
||||
bool replace = token[0] == '\'' && token[token.size() - 1] == '\'';
|
||||
|
||||
std::string str = token.substr(1, token.size() - 2);
|
||||
if (!replace) {
|
||||
return str;
|
||||
}
|
||||
std::string out = "";
|
||||
out.reserve(str.size());
|
||||
|
||||
|
||||
for (std::string::size_type i = 0; i < str.size(); ++i) {
|
||||
if (str[i] == '\'' && i < str.size() - 1) {
|
||||
if (str[i + 1] == '\'') {
|
||||
out.append(1, '\'');
|
||||
i++;
|
||||
} else {
|
||||
out.append(1, str[i]);
|
||||
}
|
||||
} else if (str[i] == '\\' && i < str.size() - 1) {
|
||||
if (str[i + 1] == 'n') {
|
||||
out.append(1, '\n');
|
||||
i++;
|
||||
} else if (str[i + 1] == 't') {
|
||||
out.append(1, '\t');
|
||||
i++;
|
||||
} else {
|
||||
out.append(1, str[i]);
|
||||
}
|
||||
} else {
|
||||
out.append(1, str[i]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string Lexer::typeToString(TokenType token_type) {
|
||||
std::string txt;
|
||||
switch (token_type) {
|
||||
case TokenType::undef:
|
||||
txt = "undef";
|
||||
break;
|
||||
case TokenType::identifier:
|
||||
txt = "identifier";
|
||||
break;
|
||||
case TokenType::plus:
|
||||
txt = "+";
|
||||
break;
|
||||
case TokenType::minus:
|
||||
txt = "-";
|
||||
break;
|
||||
case TokenType::multiply:
|
||||
txt = "*";
|
||||
break;
|
||||
case TokenType::divide:
|
||||
txt = "/";
|
||||
break;
|
||||
case TokenType::equal:
|
||||
txt = "==";
|
||||
break;
|
||||
case TokenType::not_equal:
|
||||
txt = "!=";
|
||||
break;
|
||||
case TokenType::greater:
|
||||
txt = ">";
|
||||
break;
|
||||
case TokenType::greater_equal:
|
||||
txt = ">=";
|
||||
break;
|
||||
case TokenType::lesser:
|
||||
txt = "<";
|
||||
break;
|
||||
case TokenType::lesser_equal:
|
||||
txt = "<=";
|
||||
break;
|
||||
case TokenType::keyword_create:
|
||||
txt = "create";
|
||||
break;
|
||||
case TokenType::keyword_where:
|
||||
txt = "where";
|
||||
break;
|
||||
case TokenType::keyword_table:
|
||||
txt = "table";
|
||||
break;
|
||||
case TokenType::keyword_into:
|
||||
txt = "into";
|
||||
break;
|
||||
case TokenType::keyword_values:
|
||||
txt = "values";
|
||||
break;
|
||||
case TokenType::keyword_select:
|
||||
txt = "select";
|
||||
break;
|
||||
case TokenType::keyword_set:
|
||||
txt = "set";
|
||||
break;
|
||||
case TokenType::keyword_copy:
|
||||
txt = "copy";
|
||||
break;
|
||||
case TokenType::keyword_update:
|
||||
txt = "update";
|
||||
break;
|
||||
case TokenType::keyword_load:
|
||||
txt = "load";
|
||||
break;
|
||||
case TokenType::keyword_not:
|
||||
txt = "not";
|
||||
break;
|
||||
case TokenType::keyword_null:
|
||||
txt = "null";
|
||||
break;
|
||||
case TokenType::keyword_int:
|
||||
txt = "integer";
|
||||
break;
|
||||
case TokenType::keyword_float:
|
||||
txt = "float";
|
||||
break;
|
||||
case TokenType::keyword_varchar:
|
||||
txt = "varchar";
|
||||
break;
|
||||
case TokenType::int_number:
|
||||
txt = "int number";
|
||||
break;
|
||||
case TokenType::double_number:
|
||||
txt = "double number";
|
||||
break;
|
||||
case TokenType::string_literal:
|
||||
txt = "string literal";
|
||||
break;
|
||||
case TokenType::open_paren:
|
||||
txt = "(";
|
||||
break;
|
||||
case TokenType::close_paren:
|
||||
txt = ")";
|
||||
break;
|
||||
case TokenType::logical_and:
|
||||
txt = "and";
|
||||
break;
|
||||
case TokenType::logical_or:
|
||||
txt = "or";
|
||||
break;
|
||||
case TokenType::semicolon:
|
||||
txt = ";";
|
||||
break;
|
||||
case TokenType::comma:
|
||||
txt = ",";
|
||||
break;
|
||||
case TokenType::newline:
|
||||
txt = "newline";
|
||||
break;
|
||||
case TokenType::comment:
|
||||
txt = "comment";
|
||||
break;
|
||||
case TokenType::eof:
|
||||
txt = "eof";
|
||||
break;
|
||||
default:
|
||||
txt = "FIXME, unknown token type";
|
||||
break;
|
||||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
enum class TokenType {
|
||||
undef,
|
||||
identifier,
|
||||
plus,
|
||||
minus,
|
||||
multiply,
|
||||
divide,
|
||||
equal,
|
||||
not_equal,
|
||||
greater,
|
||||
greater_equal,
|
||||
lesser,
|
||||
lesser_equal,
|
||||
keyword_create,
|
||||
keyword_table,
|
||||
keyword_where,
|
||||
keyword_delete,
|
||||
keyword_update,
|
||||
keyword_load,
|
||||
keyword_from,
|
||||
keyword_insert,
|
||||
keyword_into,
|
||||
keyword_values,
|
||||
keyword_select,
|
||||
keyword_set,
|
||||
keyword_copy,
|
||||
keyword_not,
|
||||
keyword_null,
|
||||
keyword_int,
|
||||
keyword_float,
|
||||
keyword_varchar,
|
||||
int_number,
|
||||
double_number,
|
||||
string_literal,
|
||||
open_paren,
|
||||
close_paren,
|
||||
logical_and,
|
||||
logical_or,
|
||||
pipe,
|
||||
semicolon,
|
||||
comma,
|
||||
newline,
|
||||
comment,
|
||||
eof
|
||||
};
|
||||
|
||||
struct Token {
|
||||
std::string token_string;
|
||||
TokenType type;
|
||||
|
||||
Token(const std::string &token_str, TokenType typ);
|
||||
};
|
||||
|
||||
class Lexer {
|
||||
public:
|
||||
Lexer();
|
||||
|
||||
void parse(const std::string &code);
|
||||
|
||||
void debugTokens();
|
||||
|
||||
Token currentToken();
|
||||
|
||||
Token consumeCurrentToken();
|
||||
|
||||
void nextToken();
|
||||
|
||||
void skipToken(TokenType type);
|
||||
|
||||
void skipTokenOptional(TokenType type);
|
||||
|
||||
TokenType tokenType();
|
||||
|
||||
TokenType nextTokenType();
|
||||
|
||||
TokenType prevTokenType();
|
||||
|
||||
static bool isRelationalOperator(TokenType token_type);
|
||||
|
||||
static bool isLogicalOperator(TokenType token_type);
|
||||
|
||||
static bool isArithmeticalOperator(TokenType token_type);
|
||||
|
||||
private:
|
||||
TokenType type(const std::string &token);
|
||||
|
||||
std::string stringLiteral(std::string token);
|
||||
|
||||
static std::string typeToString(TokenType token_type);
|
||||
|
||||
|
||||
private:
|
||||
std::string m_code_str;
|
||||
std::vector<Token> m_tokens;
|
||||
int m_index = 0;
|
||||
|
||||
std::regex k_words_regex;
|
||||
std::regex k_int_regex;
|
||||
std::regex k_int_underscored_regex;
|
||||
std::regex k_double_regex;
|
||||
std::regex k_identifier_regex;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,314 @@
|
|||
#include "parser.h"
|
||||
#include "exception.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
// TOOD handle premature eof
|
||||
|
||||
Parser::Parser() {
|
||||
lexer = Lexer{};
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse(const std::string &code) {
|
||||
lexer.parse(code);
|
||||
// lexer.debugTokens();
|
||||
|
||||
if (lexer.tokenType() == TokenType::keyword_create && lexer.nextTokenType() == TokenType::keyword_table) {
|
||||
return parse_create_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_insert) {
|
||||
return parse_insert_into_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_select) {
|
||||
return parse_select_from_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_delete) {
|
||||
return parse_delete_from_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_update) {
|
||||
return parse_update_table();
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::keyword_load) {
|
||||
return parse_load_table();
|
||||
}
|
||||
|
||||
std::cout << "ERROR, token:" << lexer.currentToken().token_string << std::endl;
|
||||
return std::make_unique<Node>(NodeType::error);
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_create_table() {
|
||||
std::vector<ColDefNode> cols_def{};
|
||||
|
||||
lexer.skipToken(TokenType::keyword_create);
|
||||
lexer.skipToken(TokenType::keyword_table);
|
||||
|
||||
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
lexer.skipToken(TokenType::open_paren);
|
||||
int column_order = 0;
|
||||
do {
|
||||
std::string column_name;
|
||||
ColumnType column_type;
|
||||
int column_len{1};
|
||||
bool column_nullable{true};
|
||||
|
||||
// column name
|
||||
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
|
||||
column_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
// column type and optionally len
|
||||
if (lexer.tokenType() == TokenType::keyword_int) {
|
||||
column_type = ColumnType::integer_type;
|
||||
lexer.nextToken();
|
||||
} else if (lexer.tokenType() == TokenType::keyword_float) {
|
||||
column_type = ColumnType::float_type;
|
||||
lexer.nextToken();
|
||||
} else if (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);
|
||||
} else { /* TODO handle error */ }
|
||||
lexer.skipToken(TokenType::close_paren);
|
||||
} else { /* TODO handle error */ }
|
||||
|
||||
if (lexer.tokenType() == TokenType::keyword_not) {
|
||||
lexer.nextToken();
|
||||
lexer.skipToken(TokenType::keyword_null);
|
||||
column_nullable = false;
|
||||
} else if (lexer.tokenType() == TokenType::keyword_null) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
cols_def.push_back(
|
||||
ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
|
||||
// TODO in future constraints
|
||||
|
||||
} while (lexer.tokenType() != TokenType::close_paren);
|
||||
|
||||
|
||||
return std::make_unique<CreateTableNode>(table_name, cols_def);
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_insert_into_table() {
|
||||
std::vector<Node> exec_code{};
|
||||
std::vector<ColNameNode> cols_names{};
|
||||
std::vector<ColValueNode> cols_values{};
|
||||
|
||||
lexer.skipToken(TokenType::keyword_insert);
|
||||
lexer.skipToken(TokenType::keyword_into);
|
||||
|
||||
// table name
|
||||
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
// column names
|
||||
lexer.skipToken(TokenType::open_paren);
|
||||
do {
|
||||
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
|
||||
cols_names.push_back(lexer.consumeCurrentToken().token_string);
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
} while (lexer.tokenType() != TokenType::close_paren);
|
||||
lexer.skipToken(TokenType::close_paren);
|
||||
|
||||
lexer.skipToken(TokenType::keyword_values);
|
||||
|
||||
// column values
|
||||
lexer.skipToken(TokenType::open_paren);
|
||||
do {
|
||||
cols_values.push_back(lexer.consumeCurrentToken().token_string);
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
} while (lexer.tokenType() != TokenType::close_paren);
|
||||
lexer.skipToken(TokenType::close_paren);
|
||||
|
||||
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values);
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_select_from_table() {
|
||||
std::vector<ColNameNode> cols_names{};
|
||||
|
||||
lexer.skipToken(TokenType::keyword_select);
|
||||
while (lexer.tokenType() != TokenType::keyword_from) {
|
||||
cols_names.push_back(lexer.consumeCurrentToken().token_string);
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
}
|
||||
|
||||
lexer.skipToken(TokenType::keyword_from);
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
std::unique_ptr<Node> where_node = parse_where_clause();
|
||||
|
||||
// if (lexer.tokenType() == TokenType::keyword_order_by) {}
|
||||
// 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));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_delete_from_table() {
|
||||
lexer.skipToken(TokenType::keyword_delete);
|
||||
lexer.skipToken(TokenType::keyword_from);
|
||||
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
std::unique_ptr<Node> where_node = parse_where_clause();
|
||||
|
||||
return std::make_unique<DeleteFromTableNode>(table_name, std::move(where_node));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_update_table() {
|
||||
lexer.skipToken(TokenType::keyword_update);
|
||||
lexer.skipTokenOptional(TokenType::keyword_table);
|
||||
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
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);
|
||||
|
||||
std::unique_ptr<Node> left = Parser::parse_operand_node();
|
||||
if (Lexer::isArithmeticalOperator(lexer.tokenType())) {
|
||||
ArithmeticalOperatorType op = parse_arithmetical_operator();
|
||||
std::unique_ptr<Node> right = Parser::parse_operand_node();
|
||||
|
||||
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left),
|
||||
std::move(right)));
|
||||
} else {
|
||||
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0);
|
||||
values.push_back(
|
||||
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
|
||||
std::move(left), std::move(right)));
|
||||
}
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
|
||||
} while (lexer.tokenType() != TokenType::keyword_where && lexer.tokenType() != TokenType::eof);
|
||||
|
||||
std::unique_ptr<Node> where_node = parse_where_clause();
|
||||
|
||||
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_load_table() {
|
||||
lexer.skipToken(TokenType::keyword_load);
|
||||
lexer.skipTokenOptional(TokenType::keyword_into);
|
||||
|
||||
std::string table_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
lexer.skipTokenOptional(TokenType::keyword_from);
|
||||
|
||||
std::string file_name = lexer.consumeCurrentToken().token_string;
|
||||
|
||||
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_where_clause() {
|
||||
// TODO add support for multiple filters
|
||||
// TODO add support for parenthesis
|
||||
|
||||
if (lexer.tokenType() != TokenType::keyword_where) {
|
||||
return std::make_unique<TrueNode>();
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> node;
|
||||
lexer.skipToken(TokenType::keyword_where);
|
||||
do {
|
||||
node = parse_relational_expression();
|
||||
|
||||
if (Lexer::isLogicalOperator(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
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_relational_expression() {
|
||||
auto left = parse_operand_node();
|
||||
auto operation = parse_relational_operator();
|
||||
auto right = parse_operand_node();
|
||||
|
||||
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
|
||||
}
|
||||
|
||||
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;
|
||||
switch (token_type) {
|
||||
case TokenType::int_number:
|
||||
return std::make_unique<IntValueNode>(std::stoi(tokenString));
|
||||
case TokenType::double_number:
|
||||
return std::make_unique<FloatValueNode>(std::stod(tokenString));
|
||||
case TokenType::string_literal:
|
||||
return std::make_unique<StringValueNode>(tokenString);
|
||||
case TokenType::identifier:
|
||||
return std::make_unique<DatabaseValueNode>(tokenString);
|
||||
default:;
|
||||
throw Exception("Unknown operand node");
|
||||
}
|
||||
}
|
||||
|
||||
RelationalOperatorType Parser::parse_relational_operator() {
|
||||
auto op = lexer.consumeCurrentToken();
|
||||
switch (op.type) {
|
||||
case TokenType::equal:
|
||||
return RelationalOperatorType::equal;
|
||||
case TokenType::not_equal:
|
||||
return RelationalOperatorType::not_equal;
|
||||
case TokenType::greater:
|
||||
return RelationalOperatorType::greater;
|
||||
case TokenType::greater_equal:
|
||||
return RelationalOperatorType::greater_equal;
|
||||
case TokenType::lesser:
|
||||
return RelationalOperatorType::lesser;
|
||||
case TokenType::lesser_equal:
|
||||
return RelationalOperatorType::lesser_equal;
|
||||
default:
|
||||
throw Exception("Unknown relational operator");
|
||||
}
|
||||
}
|
||||
|
||||
LogicalOperatorType Parser::parse_logical_operator() {
|
||||
auto op = lexer.consumeCurrentToken();
|
||||
switch (op.type) {
|
||||
case TokenType::logical_and:
|
||||
return LogicalOperatorType::and_operator;
|
||||
case TokenType::logical_or:
|
||||
return LogicalOperatorType::or_operator;
|
||||
default:
|
||||
throw Exception("Unknown logical operator");
|
||||
}
|
||||
}
|
||||
|
||||
ArithmeticalOperatorType Parser::parse_arithmetical_operator() {
|
||||
auto op = lexer.consumeCurrentToken();
|
||||
switch (op.type) {
|
||||
case TokenType::plus:
|
||||
return ArithmeticalOperatorType::plus_operator;
|
||||
case TokenType::minus:
|
||||
return ArithmeticalOperatorType::minus_operator;
|
||||
case TokenType::multiply:
|
||||
return ArithmeticalOperatorType::multiply_operator;
|
||||
case TokenType::divide:
|
||||
return ArithmeticalOperatorType::divide_operator;
|
||||
default:
|
||||
throw Exception("Unknown arithmetical operator");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,280 @@
|
|||
#pragma once
|
||||
|
||||
#include "lexer.h"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
|
||||
enum class ColumnType {
|
||||
integer_type,
|
||||
float_type,
|
||||
varchar_type
|
||||
};
|
||||
|
||||
enum class NodeType {
|
||||
true_node,
|
||||
int_value,
|
||||
float_value,
|
||||
string_value,
|
||||
database_value,
|
||||
logical_operator,
|
||||
relational_operator,
|
||||
arithmetical_operator,
|
||||
create_table,
|
||||
insert_into,
|
||||
select_from,
|
||||
delete_from,
|
||||
update_table,
|
||||
load_table,
|
||||
column_name,
|
||||
column_value,
|
||||
column_def,
|
||||
error
|
||||
};
|
||||
|
||||
struct Node {
|
||||
NodeType node_type;
|
||||
|
||||
Node(const NodeType type) : node_type(type) {}
|
||||
};
|
||||
|
||||
struct ColNameNode : Node {
|
||||
std::string name;
|
||||
|
||||
ColNameNode(const std::string col_name) :
|
||||
Node(NodeType::column_name), name(col_name) {}
|
||||
};
|
||||
|
||||
struct ColValueNode : Node {
|
||||
std::string value;
|
||||
|
||||
ColValueNode(const std::string col_value) :
|
||||
Node(NodeType::column_value), value(col_value) {}
|
||||
};
|
||||
|
||||
// TODO add order in row
|
||||
struct ColDefNode : Node {
|
||||
std::string name;
|
||||
ColumnType type;
|
||||
int order;
|
||||
int length;
|
||||
bool null;
|
||||
|
||||
ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) :
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
|
||||
null(nullable) {}
|
||||
};
|
||||
|
||||
|
||||
struct TrueNode : Node {
|
||||
TrueNode() : Node(NodeType::true_node) {}
|
||||
};
|
||||
|
||||
struct ValueNode : Node {
|
||||
ValueNode(NodeType type) : Node(type) {}
|
||||
|
||||
virtual int getIntValue() = 0;
|
||||
|
||||
virtual double getDoubleValue() = 0;
|
||||
|
||||
virtual std::string getStringValue() = 0;
|
||||
|
||||
virtual ~ValueNode() {};
|
||||
};
|
||||
|
||||
struct IntValueNode : ValueNode {
|
||||
int value;
|
||||
|
||||
IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
|
||||
|
||||
int getIntValue() { return value; };
|
||||
|
||||
double getDoubleValue() { return (double) value; };
|
||||
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
struct FloatValueNode : ValueNode {
|
||||
double value;
|
||||
|
||||
FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
|
||||
|
||||
int getIntValue() { return (int) value; };
|
||||
|
||||
double getDoubleValue() { return value; };
|
||||
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
struct StringValueNode : ValueNode {
|
||||
std::string value;
|
||||
|
||||
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
|
||||
|
||||
int getIntValue() { return std::stoi(value); };
|
||||
|
||||
double getDoubleValue() { return std::stod(value); };
|
||||
|
||||
std::string getStringValue() { return value; };
|
||||
};
|
||||
|
||||
struct DatabaseValueNode : Node {
|
||||
std::string col_name;
|
||||
|
||||
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
|
||||
};
|
||||
|
||||
enum class LogicalOperatorType {
|
||||
and_operator,
|
||||
or_operator,
|
||||
not_operator
|
||||
};
|
||||
|
||||
struct LogicalOperatorNode : Node {
|
||||
LogicalOperatorType op;
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
enum class RelationalOperatorType {
|
||||
equal,
|
||||
greater,
|
||||
greater_equal,
|
||||
lesser,
|
||||
lesser_equal,
|
||||
not_equal
|
||||
// like
|
||||
};
|
||||
|
||||
struct RelationalOperatorNode : Node {
|
||||
RelationalOperatorType op;
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
enum class ArithmeticalOperatorType {
|
||||
copy_value, // just copy lef value and do nothing with it
|
||||
plus_operator,
|
||||
minus_operator,
|
||||
multiply_operator,
|
||||
divide_operator
|
||||
};
|
||||
|
||||
struct ArithmeticalOperatorNode : Node {
|
||||
ArithmeticalOperatorType op;
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
|
||||
struct CreateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColDefNode> cols_defs;
|
||||
|
||||
CreateTableNode(const std::string name, std::vector<ColDefNode> defs) :
|
||||
Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
|
||||
};
|
||||
|
||||
struct InsertIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<ColValueNode> cols_values;
|
||||
|
||||
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
|
||||
};
|
||||
|
||||
struct SelectFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> 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)) {}
|
||||
};
|
||||
|
||||
struct UpdateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> values;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
|
||||
where(std::move(where_clause)) {}
|
||||
};
|
||||
|
||||
struct LoadIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::string filename;
|
||||
|
||||
LoadIntoTableNode(const std::string name, std::string file) :
|
||||
Node(NodeType::load_table), table_name(name), filename(file) {}
|
||||
|
||||
};
|
||||
|
||||
struct DeleteFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
|
||||
public:
|
||||
Parser();
|
||||
|
||||
std::unique_ptr<Node> parse(const std::string &code);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> parse_create_table();
|
||||
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_update_table();
|
||||
|
||||
std::unique_ptr<Node> parse_load_table();
|
||||
|
||||
std::unique_ptr<Node> parse_where_clause();
|
||||
|
||||
std::unique_ptr<Node> parse_operand_node();
|
||||
|
||||
RelationalOperatorType parse_relational_operator();
|
||||
|
||||
LogicalOperatorType parse_logical_operator();
|
||||
|
||||
ArithmeticalOperatorType parse_arithmetical_operator();
|
||||
|
||||
private:
|
||||
Lexer lexer;
|
||||
|
||||
std::unique_ptr<Node> parse_relational_expression();
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
|
||||
#include "row.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Row::Row(int cols_count) {
|
||||
m_columns.reserve(cols_count);
|
||||
for (int i = 0; i < cols_count; i++) {
|
||||
m_columns.push_back(std::make_unique<ColValue>());
|
||||
}
|
||||
}
|
||||
|
||||
Row::Row(const Row &other) {
|
||||
m_columns.reserve(other.m_columns.size());
|
||||
// TODO fixme this is nonsense
|
||||
for (int i = 0; i < other.m_columns.size(); i++) {
|
||||
m_columns.push_back(std::make_unique<ColNullValue>());
|
||||
}
|
||||
|
||||
for (int i = 0; i < other.m_columns.size(); i++) {
|
||||
if (ColIntegerValue *other_v = dynamic_cast<ColIntegerValue *>(other.m_columns[i].get())) {
|
||||
setColumnValue(i, other_v->integerValue());
|
||||
}
|
||||
if (ColFloatValue *other_v = dynamic_cast<ColFloatValue *>(other.m_columns[i].get())) {
|
||||
setColumnValue(i, other_v->floatValue());
|
||||
}
|
||||
if (ColStringValue *other_v = dynamic_cast<ColStringValue *>(other.m_columns[i].get())) {
|
||||
setColumnValue(i, other_v->stringValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row &Row::operator=(Row other) {
|
||||
std::swap(m_columns, other.m_columns);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, int value) {
|
||||
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, double value) {
|
||||
m_columns[col_index] = std::make_unique<ColFloatValue>(value);
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, const std::string &value) {
|
||||
m_columns[col_index] = std::make_unique<ColStringValue>(value);
|
||||
};
|
||||
|
||||
void Row::print() {
|
||||
for (int ci = 0; ci < m_columns.size(); ci++) {
|
||||
if (ci > 0) std::cout << ",";
|
||||
auto v = m_columns[ci]->stringValue();
|
||||
std::cout << v;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
#pragma once
|
||||
|
||||
#include "exception.h"
|
||||
#include "parser.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
|
||||
struct ColValue {
|
||||
|
||||
virtual bool isNull() { return false; };;;;
|
||||
|
||||
virtual int integerValue() { throw Exception("Not supported"); };
|
||||
|
||||
virtual double floatValue() { throw Exception("Not supported"); };
|
||||
|
||||
virtual std::string stringValue() { throw Exception("Not supported"); };
|
||||
};
|
||||
|
||||
|
||||
struct ColNullValue : ColValue {
|
||||
|
||||
virtual bool isNull() { return true; };
|
||||
|
||||
virtual std::string stringValue() { return "null"; };
|
||||
};
|
||||
|
||||
|
||||
struct ColIntegerValue : ColValue {
|
||||
|
||||
ColIntegerValue(int value) : m_integer(value) {};
|
||||
|
||||
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
|
||||
|
||||
virtual int integerValue() { return m_integer; };
|
||||
|
||||
virtual double floatValue() { return (double) m_integer; };
|
||||
|
||||
virtual std::string stringValue() { return std::to_string(m_integer); };
|
||||
|
||||
int m_integer;
|
||||
};
|
||||
|
||||
|
||||
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 double floatValue() { return m_float; };
|
||||
|
||||
virtual std::string stringValue() { return std::to_string(m_float); };
|
||||
|
||||
double m_float;
|
||||
};
|
||||
|
||||
|
||||
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 double floatValue() { return std::stod(m_string); };
|
||||
|
||||
virtual std::string stringValue() { return m_string; };
|
||||
|
||||
std::string m_string;
|
||||
};
|
||||
|
||||
|
||||
class Row {
|
||||
|
||||
public:
|
||||
Row(int cols_count);
|
||||
|
||||
Row(const Row &other);
|
||||
|
||||
Row &operator=(Row other);
|
||||
|
||||
void setColumnValue(int col_index, int value);
|
||||
|
||||
void setColumnValue(int col_index, double value);
|
||||
|
||||
void setColumnValue(int col_index, const std::string &value);
|
||||
|
||||
ColValue &operator[](int i) {
|
||||
return *m_columns[i];
|
||||
}
|
||||
|
||||
ColValue *ithColumn(int i) {
|
||||
return m_columns[i].get();
|
||||
}
|
||||
|
||||
void print();
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<ColValue>> m_columns;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
|
||||
#include "table.h"
|
||||
|
||||
namespace usql {
|
||||
|
||||
Table::Table(const std::string name, const std::vector<ColDefNode> columns) {
|
||||
m_name = name;
|
||||
m_col_defs = columns;
|
||||
m_rows.clear();
|
||||
}
|
||||
|
||||
ColDefNode Table::get_column_def(const std::string &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);
|
||||
if (col_def != std::end(m_col_defs)) {
|
||||
return *col_def;
|
||||
} else {
|
||||
throw Exception("column not exists (" + col_name + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Row Table::createEmptyRow() {
|
||||
return Row(columns_count());
|
||||
}
|
||||
|
||||
|
||||
void Table::print() {
|
||||
std::cout << "** " << m_name << " **" << std::endl;
|
||||
for (auto row : m_rows) {
|
||||
row.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
|
||||
}
|
||||
|
||||
void Table::addRow(const Row &row) {
|
||||
// TODO validate for not null values
|
||||
// todo validate for length etc
|
||||
m_rows.push_back(row);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include "parser.h"
|
||||
#include "row.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
struct Table {
|
||||
|
||||
Table(const Table &other);
|
||||
|
||||
Table(const std::string name, const std::vector<ColDefNode> columns);
|
||||
|
||||
ColDefNode get_column_def(const std::string &col_name);
|
||||
|
||||
int columns_count() const { return m_col_defs.size(); };
|
||||
|
||||
Row createEmptyRow(); // TODO this means unnecessary copying
|
||||
void addRow(const Row &row);
|
||||
|
||||
void print();
|
||||
|
||||
std::string m_name;
|
||||
std::vector<ColDefNode> m_col_defs;
|
||||
std::vector<Row> m_rows;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,420 @@
|
|||
#include "usql.h"
|
||||
#include "exception.h"
|
||||
#include "csvreader.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
namespace usql {
|
||||
|
||||
std::unique_ptr<Table> USql::execute(const std::string &command) {
|
||||
auto node = m_parser.parse(command);
|
||||
|
||||
return execute(*node);
|
||||
}
|
||||
|
||||
std::unique_ptr<Table> USql::execute(Node &node) {
|
||||
// TODO optimize execution nodes here
|
||||
switch (node.node_type) {
|
||||
case NodeType::create_table:
|
||||
return execute_create_table(static_cast<CreateTableNode &>(node));
|
||||
case NodeType::insert_into:
|
||||
return execute_insert_into_table(static_cast<InsertIntoTableNode &>(node));
|
||||
case NodeType::select_from:
|
||||
return execute_select(static_cast<SelectFromTableNode &>(node));
|
||||
case NodeType::delete_from:
|
||||
return execute_delete(static_cast<DeleteFromTableNode &>(node));
|
||||
case NodeType::update_table:
|
||||
return execute_update(static_cast<UpdateTableNode &>(node));
|
||||
case NodeType::load_table:
|
||||
return execute_load(static_cast<LoadIntoTableNode &>(node));
|
||||
default:
|
||||
return create_stmt_result_table(-1, "unknown statement");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
|
||||
// TODO check table does not exists
|
||||
Table table{node.table_name, node.cols_defs};
|
||||
m_tables.push_back(table);
|
||||
|
||||
return create_stmt_result_table(0, "table created");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
|
||||
// TODO check column names.size = values.size
|
||||
|
||||
// find table
|
||||
Table *table_def = find_table(node.table_name);
|
||||
|
||||
// prepare empty new_row
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
|
||||
// copy values
|
||||
for (size_t i = 0; i < node.cols_names.size(); i++) {
|
||||
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
|
||||
|
||||
// TODO validate value
|
||||
|
||||
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
|
||||
table_def->addRow(new_row);
|
||||
|
||||
return create_stmt_result_table(0, "insert succeded");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
Table *table = find_table(node.table_name);
|
||||
|
||||
// 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);
|
||||
|
||||
i++;
|
||||
}
|
||||
auto result = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
// execute access plan
|
||||
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
|
||||
// eval where for row
|
||||
if (evalWhere(node.where.get(), table, row)) {
|
||||
// prepare empty row
|
||||
Row new_row = result->createEmptyRow();
|
||||
|
||||
// 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 (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());
|
||||
}
|
||||
|
||||
// add row to result
|
||||
result->m_rows.push_back(new_row);
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
Table *table = find_table(node.table_name);
|
||||
|
||||
// execute access plan
|
||||
auto it = table->m_rows.begin();
|
||||
for (; it != table->m_rows.end();) {
|
||||
if (evalWhere(node.where.get(), table, it)) {
|
||||
// TODO this can be really expensive operation
|
||||
it = table->m_rows.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
return create_stmt_result_table(0, "delete succeded");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
Table *table = find_table(node.table_name);
|
||||
|
||||
// execute access plan
|
||||
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
|
||||
// eval where for row
|
||||
if (evalWhere(node.where.get(), table, row)) {
|
||||
int i = 0;
|
||||
for (auto col : node.cols_names) {
|
||||
// TODO cache it like in select
|
||||
ColDefNode cdef = table->get_column_def(col.name);
|
||||
|
||||
std::unique_ptr<ValueNode> new_val = evalArithmetic(cdef.type,
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return create_stmt_result_table(0, "delete succeeded");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
|
||||
// find source table
|
||||
Table *table_def = find_table(node.table_name);
|
||||
|
||||
// read data
|
||||
std::ifstream ifs(node.filename);
|
||||
std::string content((std::istreambuf_iterator<char>(ifs)),
|
||||
(std::istreambuf_iterator<char>()));
|
||||
|
||||
CsvReader csvparser{};
|
||||
auto csv = csvparser.parseCSV(content);
|
||||
|
||||
std::vector<ColDefNode> &colDefs = table_def->m_col_defs;
|
||||
|
||||
for (auto it = csv.begin() + 1; it != csv.end(); ++it) {
|
||||
std::vector<std::string> csv_line = *it;
|
||||
|
||||
// prepare empty new_row
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
|
||||
// copy values
|
||||
for (size_t i = 0; i < table_def->columns_count(); i++) {
|
||||
ColDefNode col_def = table_def->get_column_def(colDefs[i].name);
|
||||
|
||||
// TODO validate value
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stoi(csv_line[i]));
|
||||
} else if (col_def.type == ColumnType::float_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
|
||||
} else {
|
||||
new_row.setColumnValue(col_def.order, csv_line[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// append new_row
|
||||
table_def->addRow(new_row);
|
||||
}
|
||||
|
||||
return create_stmt_result_table(0, "load succeeded");
|
||||
}
|
||||
|
||||
|
||||
bool USql::evalWhere(Node *where, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
switch (where->node_type) { // no where clause
|
||||
case NodeType::true_node:
|
||||
return true;
|
||||
case NodeType::relational_operator: // just one condition
|
||||
return evalRelationalOperator(*((RelationalOperatorNode *) where), table, row);
|
||||
case NodeType::logical_operator:
|
||||
return evalLogicalOperator(*((LogicalOperatorNode *) where), table, row);
|
||||
default:
|
||||
throw Exception("Wrong node type");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
std::unique_ptr<ValueNode> left_value = evalNode(table, row, filter.left.get());
|
||||
std::unique_ptr<ValueNode> right_value = evalNode(table, row, filter.right.get());
|
||||
|
||||
double comparator;
|
||||
|
||||
if (left_value->node_type == NodeType::int_value && right_value->node_type == NodeType::int_value) {
|
||||
comparator = left_value->getIntValue() - right_value->getIntValue();
|
||||
} else if ((left_value->node_type == NodeType::int_value &&
|
||||
right_value->node_type == NodeType::float_value) ||
|
||||
(left_value->node_type == NodeType::float_value &&
|
||||
right_value->node_type == NodeType::int_value) ||
|
||||
(left_value->node_type == NodeType::float_value &&
|
||||
right_value->node_type == NodeType::float_value)) {
|
||||
comparator = left_value->getDoubleValue() - right_value->getDoubleValue();
|
||||
} else if (left_value->node_type == NodeType::string_value ||
|
||||
right_value->node_type == NodeType::string_value) {
|
||||
comparator = left_value->getStringValue().compare(right_value->getStringValue());
|
||||
} else {
|
||||
// TODO throw exception
|
||||
}
|
||||
|
||||
|
||||
switch (filter.op) {
|
||||
case RelationalOperatorType::equal:
|
||||
return comparator == 0.0;
|
||||
case RelationalOperatorType::not_equal:
|
||||
return comparator != 0.0;
|
||||
case RelationalOperatorType::greater:
|
||||
return comparator > 0.0;
|
||||
case RelationalOperatorType::greater_equal:
|
||||
return comparator >= 0.0;
|
||||
case RelationalOperatorType::lesser:
|
||||
return comparator < 0.0;
|
||||
case RelationalOperatorType::lesser_equal:
|
||||
return comparator <= 0.0;
|
||||
}
|
||||
|
||||
throw Exception("invalid relational operator");
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<ValueNode>
|
||||
USql::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
||||
if (node->node_type == NodeType::database_value) {
|
||||
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node);
|
||||
ColDefNode col_def = table->get_column_def(
|
||||
dvl->col_name); // TODO optimize it to just get this def once
|
||||
auto db_value = row->ithColumn(col_def.order);
|
||||
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
return std::make_unique<IntValueNode>(db_value->integerValue());
|
||||
}
|
||||
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) {
|
||||
IntValueNode *ivl = static_cast<IntValueNode *>(node);
|
||||
return std::make_unique<IntValueNode>(ivl->value);
|
||||
|
||||
} else if (node->node_type == NodeType::float_value) {
|
||||
FloatValueNode *ivl = static_cast<FloatValueNode *>(node);
|
||||
return std::make_unique<FloatValueNode>(ivl->value);
|
||||
|
||||
} else if (node->node_type == NodeType::string_value) {
|
||||
StringValueNode *ivl = static_cast<StringValueNode *>(node);
|
||||
return std::make_unique<StringValueNode>(ivl->value);
|
||||
}
|
||||
|
||||
throw Exception("invalid type");
|
||||
}
|
||||
|
||||
|
||||
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &iter) const {
|
||||
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
|
||||
|
||||
if ((node.op == LogicalOperatorType::and_operator && !left) ||
|
||||
(node.op == LogicalOperatorType::or_operator && left))
|
||||
return left;
|
||||
|
||||
bool right = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, iter);
|
||||
return right;
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<ValueNode>
|
||||
USql::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
if (node.op == ArithmeticalOperatorType::copy_value) {
|
||||
return evalNode(table, row, node.left.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueNode> left = evalNode(table, row, node.left.get());
|
||||
std::unique_ptr<ValueNode> right = evalNode(table, row, node.right.get());
|
||||
|
||||
if (outType == ColumnType::float_type) {
|
||||
double l = ((ValueNode *) left.get())->getDoubleValue();
|
||||
double r = ((ValueNode *) right.get())->getDoubleValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<FloatValueNode>(l + r);
|
||||
case ArithmeticalOperatorType::minus_operator:
|
||||
return std::make_unique<FloatValueNode>(l - r);
|
||||
case ArithmeticalOperatorType::multiply_operator:
|
||||
return std::make_unique<FloatValueNode>(l * r);
|
||||
case ArithmeticalOperatorType::divide_operator:
|
||||
return std::make_unique<FloatValueNode>(l / r);
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
} else if (outType == ColumnType::integer_type) {
|
||||
int l = ((ValueNode *) left.get())->getIntValue();
|
||||
int r = ((ValueNode *) right.get())->getIntValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<IntValueNode>(l + r);
|
||||
case ArithmeticalOperatorType::minus_operator:
|
||||
return std::make_unique<IntValueNode>(l - r);
|
||||
case ArithmeticalOperatorType::multiply_operator:
|
||||
return std::make_unique<IntValueNode>(l * r);
|
||||
case ArithmeticalOperatorType::divide_operator:
|
||||
return std::make_unique<IntValueNode>(l / r);
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
|
||||
} else if (outType == ColumnType::varchar_type) {
|
||||
std::string l = ((ValueNode *) left.get())->getStringValue();
|
||||
std::string r = ((ValueNode *) right.get())->getStringValue();
|
||||
switch (node.op) {
|
||||
case ArithmeticalOperatorType::plus_operator:
|
||||
return std::make_unique<StringValueNode>(l + r);
|
||||
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
}
|
||||
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
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{};
|
||||
result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false));
|
||||
result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
|
||||
|
||||
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
new_row.setColumnValue(0, code);
|
||||
new_row.setColumnValue(1, text);
|
||||
table_def->addRow(new_row);
|
||||
|
||||
return std::move(table_def);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
|
||||
#include "parser.h"
|
||||
#include "table.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace usql {
|
||||
|
||||
class USql {
|
||||
|
||||
public:
|
||||
USql() {};
|
||||
|
||||
std::unique_ptr<Table> execute(const std::string &command);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Table> execute(Node &node);
|
||||
|
||||
std::unique_ptr<Table> execute_create_table(CreateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_select(SelectFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_update(UpdateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
|
||||
|
||||
Table *find_table(const std::string name);
|
||||
|
||||
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
|
||||
|
||||
|
||||
private:
|
||||
bool evalWhere(Node *where, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
|
||||
std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
|
||||
Node *node) const;
|
||||
|
||||
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
|
||||
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &iter) const;
|
||||
|
||||
std::unique_ptr<ValueNode> evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
|
||||
private:
|
||||
Parser m_parser;
|
||||
std::vector<Table> m_tables;
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue