usql update

This commit is contained in:
2021-08-23 18:14:05 +02:00
parent 8d220356f2
commit 0e90d6047c
28 changed files with 1623 additions and 3774 deletions

View File

@@ -15,6 +15,9 @@ namespace usql {
if (m_lexer.tokenType() == TokenType::keyword_create && m_lexer.nextTokenType() == TokenType::keyword_table)
return parse_create_table();
if (m_lexer.tokenType() == TokenType::keyword_drop)
return parse_drop_table();
if (m_lexer.tokenType() == TokenType::keyword_insert)
return parse_insert_into_table();
if (m_lexer.tokenType() == TokenType::keyword_select)
@@ -23,12 +26,16 @@ namespace usql {
return parse_delete_from_table();
if (m_lexer.tokenType() == TokenType::keyword_update)
return parse_update_table();
if (m_lexer.tokenType() == TokenType::keyword_load)
return parse_load_table();
if (m_lexer.tokenType() == TokenType::keyword_save)
return parse_save_table();
if (m_lexer.tokenType() == TokenType::keyword_drop)
return parse_drop_table();
if (m_lexer.tokenType() == TokenType::keyword_set)
return parse_set();
if (m_lexer.tokenType() == TokenType::keyword_show)
return parse_show();
std::cout << "ERROR, token:" << m_lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error);
@@ -40,7 +47,9 @@ namespace usql {
m_lexer.skipToken(TokenType::keyword_create);
m_lexer.skipToken(TokenType::keyword_table);
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
if (m_lexer.tokenType() != TokenType::identifier)
throw Exception("syntax error, expecting identifier but found " + m_lexer.currentToken().token_string);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
// create as select
@@ -54,16 +63,16 @@ namespace usql {
m_lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
std::string database_value;
ColumnType column_type;
int column_len{1};
bool column_nullable{true};
int column_len = 1;
bool column_nullable = true;
// column name
if (m_lexer.tokenType() != TokenType::identifier) {
throw Exception("syntax error, expected identifier");
}
column_name = m_lexer.consumeCurrentToken().token_string;
database_value = m_lexer.consumeCurrentToken().token_string;
// column type and optionally len
if (m_lexer.tokenType() == TokenType::keyword_integer) {
@@ -82,8 +91,14 @@ namespace usql {
throw Exception("syntax error, expected int number");
}
m_lexer.skipToken(TokenType::close_paren);
} else if (m_lexer.tokenType() == TokenType::keyword_date) {
column_type = ColumnType::date_type;
m_lexer.nextToken();
} else if (m_lexer.tokenType() == TokenType::keyword_bool) {
column_type = ColumnType::bool_type;
m_lexer.nextToken();
} else {
throw Exception("syntax error, column type expected");
throw Exception("syntax error, column type expected, found " + m_lexer.currentToken().token_string);
}
if (m_lexer.tokenType() == TokenType::keyword_not) {
@@ -94,11 +109,12 @@ namespace usql {
m_lexer.nextToken();
}
cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
cols_def.emplace_back(database_value, column_type, column_order++, column_len, column_nullable);
m_lexer.skipTokenOptional(TokenType::comma);
// TODO in future constraints
//constraints
//defaults
} while (m_lexer.tokenType() != TokenType::close_paren);
return std::make_unique<CreateTableNode>(table_name, cols_def);
@@ -139,23 +155,50 @@ namespace usql {
return std::make_unique<DropTableNode>(table_name);
}
std::unique_ptr<Node> Parser::parse_set() {
m_lexer.skipToken(TokenType::keyword_set);
if (m_lexer.currentToken().type!=TokenType::string_literal) throw Exception("Expecting literal in set name");
std::string name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::equal);
if (m_lexer.currentToken().type!=TokenType::string_literal) throw Exception("Expecting literal in set value");
std::string value = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<SetNode>(name, value);
}
std::unique_ptr<Node> Parser::parse_show() {
m_lexer.skipToken(TokenType::keyword_show);
if (m_lexer.currentToken().type!=TokenType::string_literal) throw Exception("Expecting literal on show parameter name");
std::string name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<ShowNode>(name);
}
std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<ColNameNode> column_names{};
std::vector<DatabaseValueNode> database_values{};
std::vector<std::unique_ptr<Node>> column_values{};
m_lexer.skipToken(TokenType::keyword_insert);
m_lexer.skipToken(TokenType::keyword_into);
// table name
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
if (m_lexer.tokenType() != TokenType::identifier)
throw Exception("syntax error, expecting identifier but found " + m_lexer.currentToken().token_string);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
// column names
m_lexer.skipToken(TokenType::open_paren);
do {
if (m_lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_names.push_back(m_lexer.consumeCurrentToken().token_string);
if (m_lexer.tokenType() != TokenType::identifier)
throw Exception("syntax error, expecting identifier but found " + m_lexer.currentToken().token_string);
database_values.emplace_back(m_lexer.consumeCurrentToken().token_string);
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
@@ -166,65 +209,53 @@ namespace usql {
// column values
m_lexer.skipToken(TokenType::open_paren);
do {
auto col_value = parse_value();
auto col_value = parse_expression();
column_values.push_back(std::move(col_value));
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::close_paren);
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<InsertIntoTableNode>(table_name, column_names, std::move(column_values));
return std::make_unique<InsertIntoTableNode>(table_name, database_values, std::move(column_values));
}
std::unique_ptr<Node> Parser::parse_value() {
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;
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(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);
}
throw Exception("Syntax error, current token: " + m_lexer.currentToken().token_string);
}
std::unique_ptr<Node> Parser::parse_select_from_table() {
std::unique_ptr<Node> Parser::parse_select_from_table() {
bool distinct = false;
auto cols = std::make_unique<std::vector<SelectColNode>>();
m_lexer.skipToken(TokenType::keyword_select);
if (m_lexer.tokenType() == TokenType::keyword_distinct) {
distinct = true;
m_lexer.skipToken(TokenType::keyword_distinct);
}
int i = 1;
while (m_lexer.tokenType() != TokenType::keyword_from) {
auto column_value = parse_value();
std::string column_alias;
if (m_lexer.tokenType()==TokenType::multiply) {
std::string name = m_lexer.consumeCurrentToken().token_string;
auto multiply_char = std::make_unique<DatabaseValueNode>(name);
if (column_value->node_type == NodeType::column_name) {
column_alias = ((ColNameNode*) column_value.get())->name;
cols->push_back(SelectColNode{std::move(multiply_char), "*"});
} else {
column_alias = "c" + std::to_string(i);
i++;
auto column_value = parse_expression();
std::string column_alias;
if (m_lexer.tokenType() == TokenType::keyword_as) {
m_lexer.skipToken(TokenType::keyword_as);
column_alias = m_lexer.consumeCurrentToken().token_string;
} else {
if (column_value->node_type == NodeType::database_value) {
column_alias = ((DatabaseValueNode*) column_value.get())->col_name;
} else {
column_alias = "c" + std::to_string(i);
i++;
}
}
cols->push_back(SelectColNode{std::move(column_value), column_alias});
}
cols->push_back(SelectColNode{std::move(column_value), column_alias});
m_lexer.skipTokenOptional(TokenType::comma);
}
@@ -235,12 +266,13 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
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) {}
std::vector<ColOrderNode> orderby_node = parse_order_by_clause();
return std::make_unique<SelectFromTableNode>(table_name, std::move(cols), std::move(where_node));
}
OffsetLimitNode offsetlimit_node = parse_offset_limit_clause();
return std::make_unique<SelectFromTableNode>(table_name, std::move(cols), std::move(where_node), orderby_node, offsetlimit_node, distinct);
}
std::unique_ptr<Node> Parser::parse_delete_from_table() {
m_lexer.skipToken(TokenType::keyword_delete);
@@ -261,24 +293,22 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
m_lexer.skipToken(TokenType::keyword_set);
std::vector<ColNameNode> cols_names;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
do {
cols_names.push_back(m_lexer.consumeCurrentToken().token_string);
cols_names.emplace_back(m_lexer.consumeCurrentToken().token_string);
m_lexer.skipToken(TokenType::equal);
std::unique_ptr<Node> left = Parser::parse_operand_node();
std::unique_ptr<Node> left = Parser::parse_value();
if (Lexer::isArithmeticalOperator(m_lexer.tokenType())) {
ArithmeticalOperatorType op = parse_arithmetical_operator();
std::unique_ptr<Node> right = Parser::parse_operand_node();
std::unique_ptr<Node> right = Parser::parse_value();
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left),
std::move(right)));
values.push_back(std::make_unique<ArithmeticalOperatorNode>(op, std::move(left), std::move(right)));
} else {
std::unique_ptr<Node> right = std::make_unique<IntValueNode>(0);
values.push_back(
std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
values.push_back(std::make_unique<ArithmeticalOperatorNode>(ArithmeticalOperatorType::copy_value,
std::move(left), std::move(right)));
}
m_lexer.skipTokenOptional(TokenType::comma);
@@ -290,55 +320,163 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
return std::make_unique<UpdateTableNode>(table_name, cols_names, std::move(values), std::move(where_node));
}
std::vector<ColOrderNode> Parser::parse_order_by_clause() {
std::vector<ColOrderNode> order_cols;
if (m_lexer.tokenType() == TokenType::keyword_order) {
m_lexer.skipToken(TokenType::keyword_order);
m_lexer.skipToken(TokenType::keyword_by);
do {
int col_index = FUNCTION_CALL;
bool asc = true;
auto token_type = m_lexer.tokenType();
std::string tokenString = m_lexer.consumeCurrentToken().token_string;
switch (token_type) {
case TokenType::int_number:
col_index = std::stoi(tokenString);
break;
default:
throw Exception("column index allowed in order by clause at this moment");
}
if (m_lexer.tokenType() == TokenType::keyword_asc) {
m_lexer.skipToken(TokenType::keyword_asc);
} else if (m_lexer.tokenType() == TokenType::keyword_desc) {
m_lexer.skipToken(TokenType::keyword_desc);
asc = false;
}
order_cols.emplace_back(col_index, asc);
m_lexer.skipTokenOptional(TokenType::comma);
} while (m_lexer.tokenType() != TokenType::eof && m_lexer.tokenType() != TokenType::keyword_offset && m_lexer.tokenType() != TokenType::keyword_limit);
}
return order_cols;
}
OffsetLimitNode Parser::parse_offset_limit_clause() {
int offset = 0;
int limit = 999999999;
if (m_lexer.tokenType() == TokenType::keyword_offset) {
m_lexer.skipToken(TokenType::keyword_offset);
if (m_lexer.tokenType() != TokenType::int_number)
throw Exception("expecting integer in offset clause");
offset = std::stoi(m_lexer.consumeCurrentToken().token_string);
}
if (m_lexer.tokenType() == TokenType::keyword_limit) {
m_lexer.skipToken(TokenType::keyword_limit);
if (m_lexer.tokenType() != TokenType::int_number)
throw Exception("expecting integer in limit clause");
limit = std::stoi(m_lexer.consumeCurrentToken().token_string);
}
return OffsetLimitNode{offset, limit};
}
std::unique_ptr<Node> Parser::parse_where_clause() {
// TODO add support for multiple filters
// TODO add support for parenthesis
if (m_lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>();
}
if (m_lexer.tokenType() != TokenType::keyword_where) {
return std::make_unique<TrueNode>();
m_lexer.skipToken(TokenType::keyword_where);
std::unique_ptr<Node> left = parse_expression();
do {
left = parse_expression(std::move(left));
} while (m_lexer.tokenType() != TokenType::eof && m_lexer.tokenType() != TokenType::keyword_order && m_lexer.tokenType() != TokenType::keyword_offset && m_lexer.tokenType() != TokenType::keyword_limit);
return left;
}
std::unique_ptr<Node> Parser::parse_expression() {
std::unique_ptr<Node> left = parse_value();
return parse_expression(std::move(left));
}
std::unique_ptr<Node> Parser::parse_expression(std::unique_ptr<Node> left) {
if (Lexer::isRelationalOperator(m_lexer.tokenType())) {
auto operation = parse_relational_operator();
auto right = parse_value();
return std::make_unique<RelationalOperatorNode>(operation, std::move(left), std::move(right));
} else if (Lexer::isLogicalOperator(m_lexer.tokenType())) {
auto operation = parse_logical_operator();
auto right = parse_expression();
return std::make_unique<LogicalOperatorNode>(operation, std::move(left), std::move(right));
} else if (Lexer::isArithmeticalOperator(m_lexer.tokenType())) {
auto operation = parse_arithmetical_operator();
auto right = parse_value();
return std::make_unique<ArithmeticalOperatorNode>(operation, std::move(left), std::move(right));
} else if (m_lexer.tokenType() == TokenType::int_number || m_lexer.tokenType() == TokenType::double_number ||m_lexer.tokenType() == TokenType::string_literal ||m_lexer.tokenType() == TokenType::identifier || m_lexer.tokenType() == TokenType::keyword_null || m_lexer.tokenType() == TokenType::open_paren) {
return parse_value();
}
std::unique_ptr<Node> node;
m_lexer.skipToken(TokenType::keyword_where);
do {
node = parse_relational_expression();
if (Lexer::isLogicalOperator(m_lexer.tokenType())) {
auto operation = parse_logical_operator();
std::unique_ptr<Node> node2 = parse_relational_expression();
node = std::make_unique<LogicalOperatorNode>(operation, std::move(node), std::move(node2));
}
} while (m_lexer.tokenType() != TokenType::eof); // until whole where clause parsed
return node;
return left;
}
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
std::unique_ptr<Node> Parser::parse_value() {
auto token_type = m_lexer.tokenType();
std::string tokenString = m_lexer.consumeCurrentToken().token_string;
switch (token_type) {
case TokenType::int_number:
return std::make_unique<IntValueNode>(std::stoi(tokenString));
case TokenType::double_number:
return std::make_unique<DoubleValueNode>(std::stod(tokenString));
case TokenType::string_literal:
return std::make_unique<StringValueNode>(tokenString);
case TokenType::identifier:
return std::make_unique<DatabaseValueNode>(tokenString);
case TokenType::keyword_null:
return std::make_unique<NullValueNode>();
default:;
throw Exception("Unknown operand node");
// parenthesised expression
if (token_type == TokenType::open_paren) {
m_lexer.skipToken(TokenType::open_paren);
auto left = parse_expression();
do {
left = parse_expression(std::move(left));
} while (m_lexer.tokenType() != TokenType::close_paren && m_lexer.tokenType() != TokenType::eof);
m_lexer.skipToken(TokenType::close_paren);
return left;
}
// function call
if (token_type == TokenType::identifier && m_lexer.nextTokenType() == TokenType::open_paren) {
std::string function_name = m_lexer.consumeCurrentToken().token_string;
std::vector<std::unique_ptr<Node>> pars;
m_lexer.skipToken(TokenType::open_paren);
while (m_lexer.tokenType() != TokenType::close_paren && m_lexer.tokenType() != TokenType::eof) {
pars.push_back(parse_value());
m_lexer.skipTokenOptional(TokenType::comma);
}
m_lexer.skipToken(TokenType::close_paren);
return std::make_unique<FunctionNode>(function_name, std::move(pars));
}
// numbers and strings
std::string tokenString = m_lexer.consumeCurrentToken().token_string;
if (token_type == TokenType::int_number)
return std::make_unique<IntValueNode>(std::stoi(tokenString));
if (token_type == TokenType::double_number)
return std::make_unique<DoubleValueNode>(std::stod(tokenString));
if (token_type == TokenType::string_literal)
return std::make_unique<StringValueNode>(tokenString);
// db column
if (token_type == TokenType::identifier)
return std::make_unique<DatabaseValueNode>(tokenString);
// null
if (token_type == TokenType::keyword_null)
return std::make_unique<NullValueNode>();
throw Exception("Unknown operand node " + tokenString);
}
RelationalOperatorType Parser::parse_relational_operator() {
@@ -357,7 +495,7 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
case TokenType::lesser_equal:
return RelationalOperatorType::lesser_equal;
default:
throw Exception("Unknown relational operator");
throw Exception("Unknown relational operator " + op.token_string);
}
}
@@ -389,4 +527,5 @@ std::unique_ptr<Node> Parser::parse_select_from_table() {
}
}
}
} // namespace