From d2a0f25369934a4e79a864a4dd19f2dcfadd6d31 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 19 Aug 2021 23:55:16 +0200 Subject: [PATCH] Lots of work on the table builder. --- core/database/table_builder.cpp | 39 +++++++++++++-- core/database/table_builder.h | 15 +++++- database/mysql/mysql_table_builder.cpp | 4 -- database/mysql/mysql_table_builder.h | 2 - database/sqlite/sqlite3_database.cpp | 7 ++- database/sqlite/sqlite3_table_builder.cpp | 61 +++++++++++++++++++++-- database/sqlite/sqlite3_table_builder.h | 15 +++++- modules/message_page/message_page.cpp | 2 +- 8 files changed, 128 insertions(+), 17 deletions(-) diff --git a/core/database/table_builder.cpp b/core/database/table_builder.cpp index b859b07..593fc4f 100644 --- a/core/database/table_builder.cpp +++ b/core/database/table_builder.cpp @@ -1,5 +1,7 @@ #include "table_builder.h" +#include "query_result.h" + TableBuilder *TableBuilder::create_table(const std::string &name) { return this; } @@ -40,11 +42,42 @@ TableBuilder *TableBuilder::next_row() { return this; } -TableBuilder *TableBuilder::drop_table(const std::string &name) { - +TableBuilder *TableBuilder::ccreate_table() { + return this; } -void TableBuilder::finalize() { +TableBuilder *TableBuilder::drop_table() { + return this; +} +TableBuilder *TableBuilder::drop_table_if_exists() { + return this; +} +TableBuilder *TableBuilder::drop_table(const std::string &name) { + return this; +} +TableBuilder *TableBuilder::drop_table_if_exists(const std::string &name) { + return this; +} +TableBuilder *TableBuilder::cdrop_table() { + return this; +} + +TableBuilder *TableBuilder::foreign_key(const std::string &name) { + return this; +} +TableBuilder *TableBuilder::references(const std::string &table, const std::string &name) { + return this; +} + +QueryResult *TableBuilder::run() { + return nullptr; +} + +void TableBuilder::run_query() { +} + +void TableBuilder::print() { + printf("%s\n", result.c_str()); } TableBuilder::TableBuilder() { diff --git a/core/database/table_builder.h b/core/database/table_builder.h index 5970816..98d690f 100644 --- a/core/database/table_builder.h +++ b/core/database/table_builder.h @@ -3,6 +3,8 @@ #include +class QueryResult; + class TableBuilder { public: virtual TableBuilder *create_table(const std::string &name); @@ -15,10 +17,21 @@ public: virtual TableBuilder *primary_key(const std::string &name); virtual TableBuilder *primary_key(); virtual TableBuilder *next_row(); + virtual TableBuilder *ccreate_table(); + virtual TableBuilder *drop_table(); + virtual TableBuilder *drop_table_if_exists(); virtual TableBuilder *drop_table(const std::string &name); + virtual TableBuilder *drop_table_if_exists(const std::string &name); + virtual TableBuilder *cdrop_table(); - virtual void finalize(); + virtual TableBuilder *foreign_key(const std::string &name); + virtual TableBuilder *references(const std::string &table, const std::string &name); + + virtual QueryResult *run(); + virtual void run_query(); + + void print(); TableBuilder(); virtual ~TableBuilder(); diff --git a/database/mysql/mysql_table_builder.cpp b/database/mysql/mysql_table_builder.cpp index dcf46d1..f6514a7 100644 --- a/database/mysql/mysql_table_builder.cpp +++ b/database/mysql/mysql_table_builder.cpp @@ -60,10 +60,6 @@ TableBuilder *MysqlTableBuilder::next_row() { return this; } -void MysqlTableBuilder::finalize() { - result += ");"; -} - TableBuilder *MysqlTableBuilder::drop_table(const std::string &name) { result += "DROP TABLE " + name + ";"; diff --git a/database/mysql/mysql_table_builder.h b/database/mysql/mysql_table_builder.h index 6cf4e54..80ee070 100644 --- a/database/mysql/mysql_table_builder.h +++ b/database/mysql/mysql_table_builder.h @@ -20,8 +20,6 @@ public: TableBuilder *drop_table(const std::string &name); - void finalize(); - MysqlTableBuilder(); virtual ~MysqlTableBuilder(); }; diff --git a/database/sqlite/sqlite3_database.cpp b/database/sqlite/sqlite3_database.cpp index 79c3250..db9a6a6 100644 --- a/database/sqlite/sqlite3_database.cpp +++ b/database/sqlite/sqlite3_database.cpp @@ -2,8 +2,8 @@ #include "core/database/database_manager.h" -#include "sqlite3_query_result.h" #include "sqlite3_query_builder.h" +#include "sqlite3_query_result.h" #include "sqlite3_table_builder.h" Database *SQLite3Database::_creation_func() { @@ -26,7 +26,10 @@ QueryBuilder *SQLite3Database::get_query_builder() { } TableBuilder *SQLite3Database::get_table_builder() { - return new SQLite3TableBuilder(); + SQLite3TableBuilder *b = new SQLite3TableBuilder(); + b->_db = this; + + return b; } void SQLite3Database::connect(const std::string &connection_str) { diff --git a/database/sqlite/sqlite3_table_builder.cpp b/database/sqlite/sqlite3_table_builder.cpp index 57467bc..8727243 100644 --- a/database/sqlite/sqlite3_table_builder.cpp +++ b/database/sqlite/sqlite3_table_builder.cpp @@ -1,5 +1,7 @@ #include "sqlite3_table_builder.h" +#include "sqlite3_database.h" + TableBuilder *SQLite3TableBuilder::create_table(const std::string &name) { result += "CREATE TABLE " + name + " ( "; @@ -19,13 +21,13 @@ TableBuilder *SQLite3TableBuilder::date(const std::string &name) { } TableBuilder *SQLite3TableBuilder::varchar(const std::string &name, const int length) { - result += name + " VARCHAR(" + std::to_string(length) + ")"; + result += name + " VARCHAR(" + std::to_string(length) + ") "; return this; } TableBuilder *SQLite3TableBuilder::not_null() { - result += "NOT NULL "; + //result += "NOT NULL "; return this; } @@ -60,15 +62,68 @@ TableBuilder *SQLite3TableBuilder::next_row() { return this; } -void SQLite3TableBuilder::finalize() { +TableBuilder *SQLite3TableBuilder::ccreate_table() { result += ");"; + + return this; } +TableBuilder *SQLite3TableBuilder::drop_table() { + result += "DROP TABLE "; + + return this; +} +TableBuilder *SQLite3TableBuilder::drop_table_if_exists() { + result += "DROP TABLE IF EXISTS "; + + return this; +} TableBuilder *SQLite3TableBuilder::drop_table(const std::string &name) { result += "DROP TABLE " + name + ";"; return this; } +TableBuilder *SQLite3TableBuilder::drop_table_if_exists(const std::string &name) { + result += "DROP TABLE IF EXISTS " + name + ";"; + + return this; +} +TableBuilder *SQLite3TableBuilder::cdrop_table() { + result += ";"; + + return this; +} + +TableBuilder *SQLite3TableBuilder::foreign_key(const std::string &name) { + result += "FOREIGN KEY (" + name + ") "; + + return this; +} +TableBuilder *SQLite3TableBuilder::references(const std::string &table, const std::string &name) { + result += "REFERENCES " + table + " (" + name + ") "; + + return this; +} + +QueryResult *SQLite3TableBuilder::run() { + if (!_db) { + printf("SQLite3TableBuilder::run !db!\n"); + + return nullptr; + } + + return _db->query(result); +} + +void SQLite3TableBuilder::run_query() { + if (!_db) { + printf("SQLite3TableBuilder::run_query !db!\n"); + + return; + } + + _db->query_run(result); +} SQLite3TableBuilder::SQLite3TableBuilder() { } diff --git a/database/sqlite/sqlite3_table_builder.h b/database/sqlite/sqlite3_table_builder.h index b4b9015..15601b7 100644 --- a/database/sqlite/sqlite3_table_builder.h +++ b/database/sqlite/sqlite3_table_builder.h @@ -5,6 +5,8 @@ #include "core/database/table_builder.h" +class SQLite3Database; + class SQLite3TableBuilder : public TableBuilder { public: TableBuilder *create_table(const std::string &name); @@ -17,13 +19,24 @@ public: TableBuilder *primary_key(const std::string &name); TableBuilder *primary_key(); TableBuilder *next_row(); + TableBuilder *ccreate_table(); + TableBuilder *drop_table(); + TableBuilder *drop_table_if_exists(); TableBuilder *drop_table(const std::string &name); + TableBuilder *drop_table_if_exists(const std::string &name); + TableBuilder *cdrop_table(); - void finalize(); + TableBuilder *foreign_key(const std::string &name); + TableBuilder *references(const std::string &table, const std::string &name); + + virtual QueryResult *run(); + virtual void run_query(); SQLite3TableBuilder(); virtual ~SQLite3TableBuilder(); + + SQLite3Database *_db; }; #endif \ No newline at end of file diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index d13adf8..bf34d1d 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -51,7 +51,7 @@ void MessagePage::migrate() { t->result.clear(); - t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("text", 30)->finalize(); + t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("text", 30)->ccreate_table(); printf("%s\n", t->result.c_str());