system catalogue initial version

This commit is contained in:
vaclavt
2022-05-31 19:30:41 +02:00
parent 8f60b11c25
commit 1aa5fe3002
3 changed files with 62 additions and 7 deletions

View File

@@ -6,9 +6,36 @@
namespace usql { namespace usql {
USql::USql() {
// create catalogue tables first
std::vector<std::string> k_debug_sql_commands {
"create table usql_tables(name varchar(32) not null, modified boolean not null)",
"create table usql_columns(table_name varchar(32) not null, column_name varchar(32) not null, column_type varchar(16) not null, column_length integer not null, nullable boolean not null, column_order integer not null)"
};
// create cataloque tables
for (const auto &command : k_debug_sql_commands) {
std::unique_ptr<Node> create_table_node = m_parser.parse(command);
const CreateTableNode &node = static_cast<CreateTableNode &>(*create_table_node);
Table table{node.table_name, node.cols_defs};
m_tables.push_back(table);
}
// insert data into cataloque tables
for (const auto &command : k_debug_sql_commands) {
std::unique_ptr<Node> create_table_node = m_parser.parse(command);
const CreateTableNode &node = static_cast<CreateTableNode &>(*create_table_node);
execute_create_table_sys_catalogue(node);
}
}
std::unique_ptr<Table> USql::execute(const std::string &command) { std::unique_ptr<Table> USql::execute(const std::string &command) {
try { try {
std::unique_ptr<Node> node = m_parser.parse(command); std::unique_ptr<Node> node = m_parser.parse(command);
// node->dump();
return execute(*node); return execute(*node);
} catch (const std::exception &e) { } catch (const std::exception &e) {

View File

@@ -11,16 +11,16 @@
namespace usql { namespace usql {
class USql { class USql {
public: public:
USql() = default; USql();
std::unique_ptr<Table> execute(const std::string &command); std::unique_ptr<Table> execute(const std::string &command);
private: private:
std::unique_ptr<Table> execute(Node &node); std::unique_ptr<Table> execute(Node &node);
std::unique_ptr<Table> execute_create_table(const CreateTableNode &node); std::unique_ptr<Table> execute_create_table(const CreateTableNode &node);
bool execute_create_table_sys_catalogue(const CreateTableNode &node);
std::unique_ptr<Table> execute_create_index(const CreateIndexNode &node); std::unique_ptr<Table> execute_create_index(const CreateIndexNode &node);
std::unique_ptr<Table> execute_create_table_as_table(const CreateTableAsSelectNode &node); std::unique_ptr<Table> execute_create_table_as_table(const CreateTableAsSelectNode &node);
std::unique_ptr<Table> execute_load(const LoadIntoTableNode &node); std::unique_ptr<Table> execute_load(const LoadIntoTableNode &node);
@@ -59,9 +59,6 @@ private:
void check_index_not_exists(const std::string &index_name); void check_index_not_exists(const std::string &index_name);
private: private:
Parser m_parser;
std::list<Table> m_tables;
static void execute_distinct(SelectFromTableNode &node, Table *result); static void execute_distinct(SelectFromTableNode &node, Table *result);
static void execute_order_by(SelectFromTableNode &node, Table *result); static void execute_order_by(SelectFromTableNode &node, Table *result);
static void execute_offset_limit(OffsetLimitNode &node, Table *result); static void execute_offset_limit(OffsetLimitNode &node, Table *result);
@@ -97,6 +94,11 @@ private:
bool normalize_where(const Node *node) const; bool normalize_where(const Node *node) const;
Table::rows_scanner get_iterator(Table *table, const Node *where) const; Table::rows_scanner get_iterator(Table *table, const Node *where) const;
private:
Parser m_parser;
std::list<Table> m_tables;
}; };

View File

@@ -15,10 +15,35 @@ std::unique_ptr<Table> USql::execute_create_table(const CreateTableNode &node) {
Table table{node.table_name, node.cols_defs}; Table table{node.table_name, node.cols_defs};
m_tables.push_back(table); m_tables.push_back(table);
execute_create_table_sys_catalogue(node);
return create_stmt_result_table(0, "table created", 0); return create_stmt_result_table(0, "table created", 0);
} }
bool USql::execute_create_table_sys_catalogue(const CreateTableNode &node) {
// usql_tables
auto r = execute("insert into usql_tables(name, modified) values('" + node.table_name + "', false)");
// usql_columns
for(const ColDefNode & col_def : node.cols_defs) {
std::string i {"insert into usql_columns(table_name, column_name, column_type, column_length, nullable, column_order) values("};
i += "'" + node.table_name + "', ";
i += "'" + col_def.name + "', ";
i += "'" + column_type_name(col_def.type) + "', ";
i += std::to_string(col_def.length) + ", ";
i += (col_def.null ? "true, " : "false, ");
i += std::to_string(col_def.order);
i += ")";
auto r = execute(i);
// r->print();
}
return true;
}
std::unique_ptr<Table> USql::execute_create_index(const CreateIndexNode &node) { std::unique_ptr<Table> USql::execute_create_index(const CreateIndexNode &node) {
Table *table_def = find_table(node.table_name); // throws exception if not found Table *table_def = find_table(node.table_name); // throws exception if not found
ColDefNode col_def = table_def->get_column_def(node.column_name); // throws exception if not found ColDefNode col_def = table_def->get_column_def(node.column_name); // throws exception if not found
@@ -57,7 +82,6 @@ std::unique_ptr<Table> USql::execute_create_table_as_table(const CreateTableAsSe
} }
std::unique_ptr<Table> USql::execute_drop(const DropTableNode &node) { std::unique_ptr<Table> USql::execute_drop(const DropTableNode &node) {
auto name_cmp = [node](const Table& t) { return t.m_name == node.table_name; }; auto name_cmp = [node](const Table& t) { return t.m_name == node.table_name; };
@@ -70,11 +94,13 @@ std::unique_ptr<Table> USql::execute_drop(const DropTableNode &node) {
throw Exception("table not found (" + node.table_name + ")"); throw Exception("table not found (" + node.table_name + ")");
} }
std::unique_ptr<Table> USql::execute_set(const SetNode &node) { std::unique_ptr<Table> USql::execute_set(const SetNode &node) {
Settings::set_setting(node.name, node.value); Settings::set_setting(node.name, node.value);
return create_stmt_result_table(0, "set succeeded", 1); return create_stmt_result_table(0, "set succeeded", 1);
} }
std::unique_ptr<Table> USql::execute_show(const ShowNode &node) { std::unique_ptr<Table> USql::execute_show(const ShowNode &node) {
std::string value = Settings::get_setting(node.name); std::string value = Settings::get_setting(node.name);
return create_stmt_result_table(0, "show succeeded: " + value, 1); return create_stmt_result_table(0, "show succeeded: " + value, 1);