create table as
This commit is contained in:
63
usql.cpp
63
usql.cpp
@@ -18,18 +18,20 @@ std::unique_ptr<Table> USql::execute(Node &node) {
|
||||
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");
|
||||
case NodeType::create_table_as_select:
|
||||
return execute_create_table_as_table(static_cast<CreateTableAsSelectNode &>(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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +45,28 @@ std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_create_table_as_table(CreateTableAsSelectNode &node) {
|
||||
// TODO check table does not exists
|
||||
|
||||
auto select = execute_select((SelectFromTableNode &) *node.select_table.get());
|
||||
|
||||
// create table
|
||||
Table new_table{node.table_name, select.get()->m_col_defs};
|
||||
m_tables.push_back(new_table);
|
||||
|
||||
// copy rows
|
||||
// must be here, if rows are put into new_table, they are lost during m_tables.push_table
|
||||
Table *table = find_table(node.table_name);
|
||||
for( Row& orig_row : select.get()->m_rows) {
|
||||
table->addCopyOfRow(orig_row);
|
||||
}
|
||||
|
||||
select.release(); // is it correct? hoping not to release select table here and then when releasing CreateTableAsSelectNode
|
||||
|
||||
return create_stmt_result_table(0, "table created");
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
|
||||
// TODO check column names.size = values.size
|
||||
|
||||
@@ -107,13 +131,14 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
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());
|
||||
if (!col_value->isNull()) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user