diff --git a/usql/usql.cpp b/usql/usql.cpp index 4cb568f..b077639 100644 --- a/usql/usql.cpp +++ b/usql/usql.cpp @@ -6,9 +6,36 @@ namespace usql { +USql::USql() { + // create catalogue tables first + + std::vector 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 create_table_node = m_parser.parse(command); + const CreateTableNode &node = static_cast(*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 create_table_node = m_parser.parse(command); + const CreateTableNode &node = static_cast(*create_table_node); + + execute_create_table_sys_catalogue(node); + } +} + std::unique_ptr USql::execute(const std::string &command) { try { std::unique_ptr node = m_parser.parse(command); + // node->dump(); return execute(*node); } catch (const std::exception &e) { diff --git a/usql/usql.h b/usql/usql.h index 5cd8dc8..5caa4fe 100644 --- a/usql/usql.h +++ b/usql/usql.h @@ -11,16 +11,16 @@ namespace usql { class USql { - + public: - USql() = default; - + USql(); std::unique_ptr
execute(const std::string &command); private: std::unique_ptr
execute(Node &node); std::unique_ptr
execute_create_table(const CreateTableNode &node); + bool execute_create_table_sys_catalogue(const CreateTableNode &node); std::unique_ptr
execute_create_index(const CreateIndexNode &node); std::unique_ptr
execute_create_table_as_table(const CreateTableAsSelectNode &node); std::unique_ptr
execute_load(const LoadIntoTableNode &node); @@ -59,9 +59,6 @@ private: void check_index_not_exists(const std::string &index_name); private: - Parser m_parser; - std::list
m_tables; - static void execute_distinct(SelectFromTableNode &node, Table *result); static void execute_order_by(SelectFromTableNode &node, Table *result); static void execute_offset_limit(OffsetLimitNode &node, Table *result); @@ -97,6 +94,11 @@ private: bool normalize_where(const Node *node) const; Table::rows_scanner get_iterator(Table *table, const Node *where) const; + + +private: + Parser m_parser; + std::list
m_tables; }; diff --git a/usql/usql_ddl.cpp b/usql/usql_ddl.cpp index c6b1e18..4fcaa2c 100644 --- a/usql/usql_ddl.cpp +++ b/usql/usql_ddl.cpp @@ -15,10 +15,35 @@ std::unique_ptr
USql::execute_create_table(const CreateTableNode &node) { Table table{node.table_name, node.cols_defs}; m_tables.push_back(table); + execute_create_table_sys_catalogue(node); + 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
USql::execute_create_index(const CreateIndexNode &node) { 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 @@ -57,7 +82,6 @@ std::unique_ptr
USql::execute_create_table_as_table(const CreateTableAsSe } - std::unique_ptr
USql::execute_drop(const DropTableNode &node) { auto name_cmp = [node](const Table& t) { return t.m_name == node.table_name; }; @@ -70,11 +94,13 @@ std::unique_ptr
USql::execute_drop(const DropTableNode &node) { throw Exception("table not found (" + node.table_name + ")"); } + std::unique_ptr
USql::execute_set(const SetNode &node) { Settings::set_setting(node.name, node.value); return create_stmt_result_table(0, "set succeeded", 1); } + std::unique_ptr
USql::execute_show(const ShowNode &node) { std::string value = Settings::get_setting(node.name); return create_stmt_result_table(0, "show succeeded: " + value, 1);