drop table added

This commit is contained in:
VaclavT 2021-07-27 19:29:04 +02:00
parent 6d4fe43a3b
commit 7d91319f0b
11 changed files with 3282 additions and 161 deletions

View File

@ -1,11 +1,10 @@
### TODO
- drop table command
- move csv generation from usql(save_table) to table class
- add exceptions and rename it to UsqlException
- class members should have prefix m_
- maybe to create iterator on table
- support for *
- support for order by, offset, limit
- add pipe | token
- add count min and max functions, eg aggregate functions
- add logging
- maybe to create iterator on table
- add exceptions and rename it to UsqlException
- class members should have prefix m_
- add const wherever should be

View File

@ -162,6 +162,9 @@ namespace usql {
if (token == "create")
return TokenType::keyword_create;
if (token == "drop")
return TokenType::keyword_drop;
if (token == "where")
return TokenType::keyword_where;
@ -338,6 +341,9 @@ namespace usql {
case TokenType::keyword_create:
txt = "create";
break;
case TokenType::keyword_drop:
txt = "drop";
break;
case TokenType::keyword_where:
txt = "where";
break;

View File

@ -22,6 +22,7 @@ namespace usql {
lesser_equal,
keyword_as,
keyword_create,
keyword_drop,
keyword_table,
keyword_where,
keyword_delete,

View File

@ -3,15 +3,13 @@
// 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
using namespace std::chrono;
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'))",
"update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
"update table a set s = 'null string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
// "update table a set i = null",
"insert into a (i, s) values(2, 'two')",
"insert into a (i, s) values(3, 'two')",
@ -36,19 +34,25 @@ int main(int argc, char *argv[]) {
"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",
"drop table x",
"select i, s, f from a where i > 300",
"select i, to_string(i, '%d.%m.%Y'), s, f from a where i > 300",
"create table prices (datetime integer, symbol varchar(8), prev_close float, open float, price float, change float, change_prct varchar(16))",
"load prices from '/Users/vaclavt/Library/Mobile Documents/com~apple~CloudDocs/Development/usql/prices.csv'",
"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 %')",
"select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices"
"select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices where symbol = 'SYF'"
};
usql::USql uSql{};
for (auto command : sql_commands) {
std::cout << command << std::endl;
time_point<high_resolution_clock> start_time = high_resolution_clock::now();
auto result = uSql.execute(command);
time_point<high_resolution_clock> end_time = high_resolution_clock::now();
std::cout << command << std::endl;
std::cout << "run time: " << duration_cast<milliseconds>(end_time - start_time).count() << " ms " << std::endl;
result->print();
}

View File

@ -13,27 +13,22 @@ namespace usql {
m_lexer.parse(code);
// m_lexer.debugTokens();
if (m_lexer.tokenType() == TokenType::keyword_create && m_lexer.nextTokenType() == TokenType::keyword_table) {
if (m_lexer.tokenType() == TokenType::keyword_create && m_lexer.nextTokenType() == TokenType::keyword_table)
return parse_create_table();
}
if (m_lexer.tokenType() == TokenType::keyword_insert) {
if (m_lexer.tokenType() == TokenType::keyword_insert)
return parse_insert_into_table();
}
if (m_lexer.tokenType() == TokenType::keyword_select) {
if (m_lexer.tokenType() == TokenType::keyword_select)
return parse_select_from_table();
}
if (m_lexer.tokenType() == TokenType::keyword_delete) {
if (m_lexer.tokenType() == TokenType::keyword_delete)
return parse_delete_from_table();
}
if (m_lexer.tokenType() == TokenType::keyword_update) {
if (m_lexer.tokenType() == TokenType::keyword_update)
return parse_update_table();
}
if (m_lexer.tokenType() == TokenType::keyword_load) {
if (m_lexer.tokenType() == TokenType::keyword_load)
return parse_load_table();
}
if (m_lexer.tokenType() == TokenType::keyword_save) {
if (m_lexer.tokenType() == TokenType::keyword_save)
return parse_save_table();
}
if (m_lexer.tokenType() == TokenType::keyword_drop)
return parse_drop_table();
std::cout << "ERROR, token:" << m_lexer.currentToken().token_string << std::endl;
return std::make_unique<Node>(NodeType::error);
@ -110,7 +105,41 @@ namespace usql {
}
}
std::unique_ptr<Node> Parser::parse_load_table() {
m_lexer.skipToken(TokenType::keyword_load);
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::keyword_from);
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_save_table() {
m_lexer.skipToken(TokenType::keyword_save);
m_lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<SaveTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_drop_table() {
m_lexer.skipToken(TokenType::keyword_drop);
m_lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<DropTableNode>(table_name);
}
std::unique_ptr<Node> Parser::parse_insert_into_table() {
std::vector<ColNameNode> column_names{};
std::vector<std::unique_ptr<Node>> column_values{};
@ -261,33 +290,6 @@ 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::unique_ptr<Node> Parser::parse_load_table() {
m_lexer.skipToken(TokenType::keyword_load);
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::keyword_from);
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<LoadIntoTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_save_table() {
m_lexer.skipToken(TokenType::keyword_save);
m_lexer.skipTokenOptional(TokenType::keyword_table);
std::string table_name = m_lexer.consumeCurrentToken().token_string;
m_lexer.skipTokenOptional(TokenType::keyword_into);
std::string file_name = m_lexer.consumeCurrentToken().token_string;
return std::make_unique<SaveTableNode>(table_name, file_name);
}
std::unique_ptr<Node> Parser::parse_where_clause() {
// TODO add support for multiple filters
// TODO add support for parenthesis

View File

@ -32,6 +32,7 @@ namespace usql {
update_table,
load_table,
save_table,
drop_table,
column_name,
column_value,
function,
@ -256,6 +257,12 @@ namespace usql {
Node(NodeType::save_table), table_name(name), filename(file) {}
};
struct DropTableNode : Node {
std::string table_name;
DropTableNode(const std::string& name) : Node(NodeType::drop_table), table_name(name) {}
};
struct DeleteFromTableNode : Node {
std::string table_name;
std::unique_ptr<Node> where;
@ -275,15 +282,18 @@ namespace usql {
private:
std::unique_ptr<Node> parse_create_table();
std::unique_ptr<Node> parse_load_table();
std::unique_ptr<Node> parse_save_table();
std::unique_ptr<Node> parse_drop_table();
std::unique_ptr<Node> parse_insert_into_table();
std::unique_ptr<Node> parse_value();
std::unique_ptr<Node> parse_select_from_table();
std::unique_ptr<Node> parse_delete_from_table();
std::unique_ptr<Node> parse_update_table();
std::unique_ptr<Node> parse_load_table();
std::unique_ptr<Node> parse_save_table();
std::unique_ptr<Node> parse_where_clause();
std::unique_ptr<Node> parse_operand_node();
std::unique_ptr<Node> parse_value();
RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator();

3061
prices.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
#include "table.h"
#include "csvreader.h"
namespace usql {
@ -49,6 +51,43 @@ std::string Table::csv_string() {
return out_string;
}
int Table::load_csv_string(const std::string &content) {
int row_cnt = 0;
CsvReader csvparser{};
auto csv = csvparser.parseCSV(content);
std::vector<ColDefNode> &colDefs = 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 = create_empty_row();
// copy values
for (size_t i = 0; i < columns_count(); i++) {
ColDefNode col_def = get_column_def(colDefs[i].name);
// TODO validate value
if (col_def.type == ColumnType::integer_type) {
new_row.setColumnValue(col_def.order, std::stol(csv_line[i]));
} else if (col_def.type == ColumnType::float_type) {
new_row.setColumnValue(col_def.order, std::stof(csv_line[i]));
} else {
new_row.setColumnValue(col_def.order, csv_line[i]);
}
}
// append new_row
add_row(new_row);
row_cnt++;
}
return row_cnt;
}
void Table::print() {
std::cout << "** " << m_name << " **" << std::endl;
for (auto row : m_rows) {

View File

@ -26,6 +26,7 @@ namespace usql {
void validate_row(const Row &row);
std::string csv_string();
int load_csv_string(const std::string &content);
void print();

184
usql.cpp
View File

@ -1,6 +1,5 @@
#include "usql.h"
#include "exception.h"
#include "csvreader.h"
#include "ml_date.h"
#include <algorithm>
@ -14,7 +13,7 @@ std::unique_ptr<Table> USql::execute(const std::string &command) {
return execute(*node);
} catch (std::exception &e) {
return create_stmt_result_table(-1, e.what());
return create_stmt_result_table(-1, e.what(), 0);
}
}
@ -38,8 +37,10 @@ std::unique_ptr<Table> USql::execute(Node &node) {
return execute_load(static_cast<LoadIntoTableNode &>(node));
case NodeType::save_table:
return execute_save(static_cast<SaveTableNode &>(node));
case NodeType::drop_table:
return execute_drop(static_cast<DropTableNode &>(node));
default:
return create_stmt_result_table(-1, "unknown statement");
return create_stmt_result_table(-1, "unknown statement", 0);
}
}
@ -50,7 +51,7 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
Table table{node.table_name, node.cols_defs};
m_tables.push_back(table);
return create_stmt_result_table(0, "table created");
return create_stmt_result_table(0, "table created", 0);
}
@ -72,10 +73,54 @@ std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNo
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");
return create_stmt_result_table(0, "table created", table->m_rows.size());
}
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>()));
// load rows
auto rows_cnt = table_def->load_csv_string(content);
return create_stmt_result_table(0, "load succeeded", rows_cnt);
}
std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
// find source table
Table *table_def = find_table(node.table_name);
// make csv string
std::string csv_string = table_def->csv_string();
// save data
std::ofstream file(node.filename);
file << csv_string;
file.close();
return create_stmt_result_table(0, "save succeeded", 0);
}
std::unique_ptr<Table> USql::execute_drop(DropTableNode &node) {
auto name_cmp = [node](const Table& t) { return t.m_name == node.table_name; };
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
if (table_def != std::end(m_tables)) {
m_tables.erase(table_def);
return create_stmt_result_table(0, "drop succeeded", 0);
}
throw Exception("table not found (" + node.table_name + ")");
}
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
// TODO check column names.size = values.size
@ -88,7 +133,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// 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);
auto col_value = evalValueNode(table_def, new_row, node.cols_values[i].get());
auto col_value = eval_value_node(table_def, new_row, node.cols_values[i].get());
new_row.setColumnValue(&col_def, col_value.get());
}
@ -96,7 +141,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
// append new_row
table_def->add_row(new_row);
return create_stmt_result_table(0, "insert succeeded");
return create_stmt_result_table(0, "insert succeeded", 1);
}
@ -122,7 +167,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
// 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)) {
if (eval_where(node.where.get(), table, *row)) {
// prepare empty row
Row new_row = result->create_empty_row();
@ -131,7 +176,8 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
auto row_col_index = source_table_col_index[idx];
if (row_col_index == -1) { // TODO introduce constant here
auto evaluated_value = evalValueNode(table, *row, node.cols_names->operator[](idx).value.get());
auto evaluated_value = eval_value_node(table, *row, node.cols_names->operator[](
idx).value.get());
ValueNode *col_value = evaluated_value.get();
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
@ -178,16 +224,18 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
Table *table = find_table(node.table_name);
// execute access plan
int affected_rows = 0;
auto it = table->m_rows.begin();
for (; it != table->m_rows.end();) {
if (evalWhere(node.where.get(), table, *it)) {
if (eval_where(node.where.get(), table, *it)) {
it = table->m_rows.erase(it);
affected_rows++;
} else {
++it;
}
}
return create_stmt_result_table(0, "delete succeeded");
return create_stmt_result_table(0, "delete succeeded", affected_rows);
}
@ -196,92 +244,38 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
Table *table = find_table(node.table_name);
// execute access plan
int affected_rows = 0;
for (auto row = begin(table->m_rows); row != end(table->m_rows); ++row) {
// eval where for row
if (evalWhere(node.where.get(), table, *row)) {
if (eval_where(node.where.get(), table, *row)) {
int i = 0;
for (const auto& col : node.cols_names) {
ColDefNode col_def = table->get_column_def(col.name); // TODO cache it like in select
std::unique_ptr<ValueNode> new_val = evalArithmeticOperator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]), table, *row);
std::unique_ptr<ValueNode> new_val = eval_arithmetic_operator(col_def.type,
static_cast<ArithmeticalOperatorNode &>(*node.values[i]),
table, *row);
table->validate_column(&col_def, new_val.get());
row->setColumnValue(&col_def, new_val.get());
i++;
}
affected_rows++;
// TODO tady je problem, ze kdyz to zfajluje na jednom radku ostatni by se nemely provest
}
}
return create_stmt_result_table(0, "delete succeeded");
return create_stmt_result_table(0, "update succeeded", affected_rows);
}
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->create_empty_row();
// 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::stol(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->add_row(new_row);
}
return create_stmt_result_table(0, "load succeeded");
}
std::unique_ptr<Table> USql::execute_save(SaveTableNode &node) {
// find source table
Table *table_def = find_table(node.table_name);
std::string csv_string = table_def->csv_string();
// save data
std::ofstream file(node.filename);
file << csv_string;
file.close();
return create_stmt_result_table(0, "save succeeded");
}
bool USql::evalWhere(Node *where, Table *table, Row &row) const {
bool USql::eval_where(Node *where, Table *table, Row &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);
return eval_relational_operator(*((RelationalOperatorNode *) where), table, row);
case NodeType::logical_operator:
return evalLogicalOperator(*((LogicalOperatorNode *) where), table, row);
return eval_logical_operator(*((LogicalOperatorNode *) where), table, row);
default:
throw Exception("Wrong node type");
}
@ -290,9 +284,9 @@ bool USql::evalWhere(Node *where, Table *table, Row &row) const {
}
bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const {
std::unique_ptr<ValueNode> left_value = evalValueNode(table, row, filter.left.get());
std::unique_ptr<ValueNode> right_value = evalValueNode(table, row, filter.right.get());
bool USql::eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const {
std::unique_ptr<ValueNode> left_value = eval_value_node(table, row, filter.left.get());
std::unique_ptr<ValueNode> right_value = eval_value_node(table, row, filter.right.get());
double comparator;
@ -327,15 +321,15 @@ bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *t
}
std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *node) {
std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *node) {
if (node->node_type == NodeType::database_value || node->node_type == NodeType::column_name) { // TODO sjednotit
return evalDatabaseValueNode(table, row, node);
return eval_database_value_node(table, row, node);
} else if (node->node_type == NodeType::int_value || node->node_type == NodeType::float_value || node->node_type == NodeType::string_value) {
return evalLiteralValueNode(table, row, node);
return eval_literal_value_node(table, row, node);
} else if (node->node_type == NodeType::function) {
return evalFunctionValueNode(table, row, node);
return eval_function_value_node(table, row, node);
} else if (node->node_type == NodeType::null_value) {
return std::make_unique<NullValueNode>();
}
@ -343,7 +337,7 @@ std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *nod
}
std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, Node *node) {
std::unique_ptr<ValueNode> USql::eval_database_value_node(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.ith_column(col_def.order);
@ -361,7 +355,7 @@ std::unique_ptr<ValueNode> USql::evalDatabaseValueNode(Table *table, Row &row, N
}
std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, Node *node) {
std::unique_ptr<ValueNode> USql::eval_literal_value_node(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);
@ -380,12 +374,12 @@ std::unique_ptr<ValueNode> USql::evalLiteralValueNode(Table *table, Row &row, No
}
std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, Node *node) {
std::unique_ptr<ValueNode> USql::eval_function_value_node(Table *table, Row &row, Node *node) {
auto *fnc = static_cast<FunctionNode *>(node);
std::vector<std::unique_ptr<ValueNode>> evaluatedPars;
for(auto & param : fnc->params) {
evaluatedPars.push_back(evalValueNode(table, row, param.get()));
evaluatedPars.push_back(eval_value_node(table, row, param.get()));
}
// TODO use some enum
@ -417,24 +411,24 @@ std::unique_ptr<ValueNode> USql::evalFunctionValueNode(Table *table, Row &row, N
}
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
bool USql::eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const {
bool left = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, row);
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, row);
bool right = eval_relational_operator(static_cast<const RelationalOperatorNode &>(*node.right), pTable, row);
return right;
}
std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const {
std::unique_ptr<ValueNode> USql::eval_arithmetic_operator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const {
if (node.op == ArithmeticalOperatorType::copy_value) {
return evalValueNode(table, row, node.left.get());
return eval_value_node(table, row, node.left.get());
}
std::unique_ptr<ValueNode> left = evalValueNode(table, row, node.left.get());
std::unique_ptr<ValueNode> right = evalValueNode(table, row, node.right.get());
std::unique_ptr<ValueNode> left = eval_value_node(table, row, node.left.get());
std::unique_ptr<ValueNode> right = eval_value_node(table, row, node.right.get());
if (outType == ColumnType::float_type) {
double l = ((ValueNode *) left.get())->getDoubleValue();
@ -483,16 +477,18 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
}
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string& text) {
std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::string &text, long affected_rows) {
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));
result_tbl_col_defs.push_back(ColDefNode("affected_rows", ColumnType::integer_type, 0, 1, true));
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
Row new_row = table_def->create_empty_row();
new_row.setColumnValue(0, code);
new_row.setColumnValue(1, text);
new_row.setColumnValue(2, affected_rows);
table_def->add_row(new_row);
return std::move(table_def);

24
usql.h
View File

@ -21,29 +21,31 @@ private:
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_load(LoadIntoTableNode &node);
std::unique_ptr<Table> execute_save(SaveTableNode &node);
std::unique_ptr<Table> execute_drop(DropTableNode &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);
std::unique_ptr<Table> execute_save(SaveTableNode &node);
private:
bool evalWhere(Node *where, Table *table, Row &row) const;
bool eval_where(Node *where, Table *table, Row &row) const;
static std::unique_ptr<ValueNode> evalValueNode(Table *table, Row &row, Node *node);
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);
static std::unique_ptr<ValueNode> eval_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> eval_database_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> eval_literal_value_node(Table *table, Row &row, Node *node);
static std::unique_ptr<ValueNode> eval_function_value_node(Table *table, Row &row, Node *node);
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table, Row &row) const;
bool evalLogicalOperator(LogicalOperatorNode &node, Table *pTable, Row &row) const;
std::unique_ptr<ValueNode> evalArithmeticOperator(ColumnType outType, ArithmeticalOperatorNode &node, Table *table, Row &row) const;
bool eval_relational_operator(const RelationalOperatorNode &filter, Table *table, Row &row) const;
bool eval_logical_operator(LogicalOperatorNode &node, Table *pTable, Row &row) const;
std::unique_ptr<ValueNode> eval_arithmetic_operator(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::unique_ptr<Table> create_stmt_result_table(long code, const std::string &text, long affected_rows);
static std::tuple<int, ColDefNode> get_column_definition(Table *table, SelectColNode *select_col_node, int col_order) ;
Table *find_table(const std::string &name);