usql update
This commit is contained in:
164
usql/usql.cpp
164
usql/usql.cpp
@@ -1,6 +1,5 @@
|
||||
#include "usql.h"
|
||||
#include "exception.h"
|
||||
#include "csvreader.h"
|
||||
#include "ml_date.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -38,6 +37,8 @@ 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");
|
||||
}
|
||||
@@ -45,7 +46,8 @@ std::unique_ptr<Table> USql::execute(Node &node) {
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
|
||||
// TODO check table does not exists
|
||||
check_table_not_exists(node.table_name);
|
||||
|
||||
Table table{node.table_name, node.cols_defs};
|
||||
m_tables.push_back(table);
|
||||
|
||||
@@ -54,7 +56,7 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNode &node) {
|
||||
// TODO check table does not exists
|
||||
check_table_not_exists(node.table_name);
|
||||
|
||||
auto select = execute_select((SelectFromTableNode &) *node.select_table);
|
||||
|
||||
@@ -66,7 +68,7 @@ std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNo
|
||||
// must be here, if rows are put into new_table, they are lost during m_tables.push_table
|
||||
Table *table = find_table(node.table_name);
|
||||
for( Row& orig_row : select->m_rows) {
|
||||
table->addCopyOfRow(orig_row);
|
||||
table->add_copy_of_row(orig_row);
|
||||
}
|
||||
|
||||
select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode
|
||||
@@ -75,6 +77,50 @@ std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNo
|
||||
}
|
||||
|
||||
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -82,7 +128,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
|
||||
Table *table_def = find_table(node.table_name);
|
||||
|
||||
// prepare empty new_row
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
Row new_row = table_def->create_empty_row();
|
||||
|
||||
// copy values
|
||||
for (size_t i = 0; i < node.cols_names.size(); i++) {
|
||||
@@ -93,7 +139,7 @@ std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node
|
||||
}
|
||||
|
||||
// append new_row
|
||||
table_def->addRow(new_row);
|
||||
table_def->add_row(new_row);
|
||||
|
||||
return create_stmt_result_table(0, "insert succeeded");
|
||||
}
|
||||
@@ -109,7 +155,8 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
std::vector<int> source_table_col_index{};
|
||||
|
||||
for (int i = 0; i < node.cols_names->size(); i++) {
|
||||
auto [ src_tbl_col_index, rst_tbl_col_def ] = getColumnDefinition(table, &node.cols_names->operator[](i), i);
|
||||
auto [ src_tbl_col_index, rst_tbl_col_def ] = get_column_definition(table,
|
||||
&node.cols_names->operator[](i), i);
|
||||
|
||||
source_table_col_index.push_back(src_tbl_col_index);
|
||||
result_tbl_col_defs.push_back(rst_tbl_col_def);
|
||||
@@ -122,7 +169,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
// eval where for row
|
||||
if (evalWhere(node.where.get(), table, *row)) {
|
||||
// prepare empty row
|
||||
Row new_row = result->createEmptyRow();
|
||||
Row new_row = result->create_empty_row();
|
||||
|
||||
// copy column values
|
||||
for (auto idx = 0; idx < result->columns_count(); idx++) {
|
||||
@@ -134,7 +181,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
|
||||
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
|
||||
} else {
|
||||
ColValue *col_value = row->ithColumn(row_col_index);
|
||||
ColValue *col_value = row->ith_column(row_col_index);
|
||||
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
|
||||
}
|
||||
}
|
||||
@@ -147,7 +194,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
std::tuple<int, ColDefNode> USql::getColumnDefinition(Table *table, SelectColNode *select_col_node, int col_order ) {
|
||||
std::tuple<int, ColDefNode> USql::get_column_definition(Table *table, SelectColNode *select_col_node, int col_order ) {
|
||||
std::string new_col_name = select_col_node->name;
|
||||
|
||||
if (select_col_node->value->node_type == NodeType::column_name) {
|
||||
@@ -179,7 +226,6 @@ std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
|
||||
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;
|
||||
@@ -204,9 +250,11 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
std::unique_ptr<ValueNode> new_val = evalArithmeticOperator(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++;
|
||||
}
|
||||
// TODO tady je problem, ze kdyz to zfajluje na jednom radku ostatni by se nemely provest
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,83 +262,6 @@ std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
}
|
||||
|
||||
|
||||
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::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->addRow(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);
|
||||
|
||||
// header
|
||||
std::string out_string;
|
||||
for(int i = 0; i < table_def->m_col_defs.size(); i++) {
|
||||
if (i > 0) out_string += ",";
|
||||
out_string += table_def->m_col_defs[i].name;
|
||||
}
|
||||
|
||||
// rows
|
||||
for (auto it = table_def->m_rows.begin() + 1; it != table_def->m_rows.end(); ++it) {
|
||||
std::string csv_line;
|
||||
for(int i = 0; i < table_def->m_col_defs.size(); i++) {
|
||||
if (i > 0) csv_line += ",";
|
||||
|
||||
auto col = it->ithColumn(i);
|
||||
if (!col->isNull()) {
|
||||
csv_line += col->getStringValue(); // TODO handle enclosing commas etc
|
||||
}
|
||||
}
|
||||
out_string += "\n";
|
||||
out_string += csv_line;
|
||||
}
|
||||
|
||||
// save data
|
||||
std::ofstream file(node.filename);
|
||||
file << out_string;
|
||||
file.close();
|
||||
|
||||
return create_stmt_result_table(0, "save succeeded");
|
||||
}
|
||||
|
||||
|
||||
bool USql::evalWhere(Node *where, Table *table, Row &row) const {
|
||||
switch (where->node_type) { // no where clause
|
||||
case NodeType::true_node:
|
||||
@@ -353,6 +324,8 @@ std::unique_ptr<ValueNode> USql::evalValueNode(Table *table, Row &row, Node *nod
|
||||
|
||||
} else if (node->node_type == NodeType::function) {
|
||||
return evalFunctionValueNode(table, row, node);
|
||||
} else if (node->node_type == NodeType::null_value) {
|
||||
return std::make_unique<NullValueNode>();
|
||||
}
|
||||
throw Exception("unsupported node type");
|
||||
}
|
||||
@@ -361,7 +334,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) {
|
||||
auto *dvl = static_cast<DatabaseValueNode *>(node);
|
||||
ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once
|
||||
auto db_value = row.ithColumn(col_def.order);
|
||||
auto db_value = row.ith_column(col_def.order);
|
||||
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
return std::make_unique<IntValueNode>(db_value->getIntValue());
|
||||
@@ -466,6 +439,7 @@ std::unique_ptr<ValueNode> USql::evalArithmeticOperator(ColumnType outType, Arit
|
||||
default:
|
||||
throw Exception("implement me!!");
|
||||
}
|
||||
|
||||
} else if (outType == ColumnType::integer_type) {
|
||||
long l = ((ValueNode *) left.get())->getIntValue();
|
||||
long r = ((ValueNode *) right.get())->getIntValue();
|
||||
@@ -504,10 +478,10 @@ std::unique_ptr<Table> USql::create_stmt_result_table(long code, const std::stri
|
||||
|
||||
auto table_def = std::make_unique<Table>("result", result_tbl_col_defs);
|
||||
|
||||
Row new_row = table_def->createEmptyRow();
|
||||
Row new_row = table_def->create_empty_row();
|
||||
new_row.setColumnValue(0, code);
|
||||
new_row.setColumnValue(1, text);
|
||||
table_def->addRow(new_row);
|
||||
table_def->add_row(new_row);
|
||||
|
||||
return std::move(table_def);
|
||||
}
|
||||
@@ -524,4 +498,12 @@ Table *USql::find_table(const std::string &name) {
|
||||
}
|
||||
}
|
||||
|
||||
void USql::check_table_not_exists(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)) {
|
||||
throw Exception("table already exists");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user