From b06323739939b7df06a87750fa1452017b998c11 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 1 Dec 2020 23:37:10 +0100 Subject: [PATCH] Initial TableBuilder implementation, and simple migrations. --- core/application.cpp | 4 ++ core/application.h | 2 + core/database.cpp | 6 ++- core/database.h | 4 +- core/object.cpp | 4 ++ core/object.h | 2 + core/table_builder.cpp | 50 ++++++++++++++++++ core/table_builder.h | 27 ++++++++++ database/mysql/mysql_database.cpp | 7 ++- database/mysql/mysql_database.h | 3 +- database/mysql/mysql_table_builder.cpp | 72 ++++++++++++++++++++++++++ database/mysql/mysql_table_builder.h | 27 ++++++++++ main.cpp | 18 ++++++- modules/message_page/message_page.cpp | 36 ++++++++----- modules/message_page/message_page.h | 2 + rdn_application.cpp | 4 ++ rdn_application.h | 2 + 17 files changed, 252 insertions(+), 18 deletions(-) create mode 100644 core/table_builder.cpp create mode 100644 core/table_builder.h create mode 100644 database/mysql/mysql_table_builder.cpp create mode 100644 database/mysql/mysql_table_builder.h diff --git a/core/application.cpp b/core/application.cpp index d785c33..e949917 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -119,6 +119,10 @@ void Application::send_file(const std::string &path, Request *request) { request->send(); } +void Application::migrate() { + +} + Application::Application() { _instance = this; } diff --git a/core/application.h b/core/application.h index 94780a8..5342d2f 100644 --- a/core/application.h +++ b/core/application.h @@ -28,6 +28,8 @@ public: static void default_routing_middleware(Object *instance, Request *request); + virtual void migrate(); + Application(); virtual ~Application(); diff --git a/core/database.cpp b/core/database.cpp index e0f1306..dca8eee 100644 --- a/core/database.cpp +++ b/core/database.cpp @@ -6,10 +6,14 @@ void Database::connect(const std::string &connection_str) { void Database::query(const std::string &query) { } -QueryBuilder *Database::get_builder() { +QueryBuilder *Database::get_query_builder() { return new QueryBuilder(); } +TableBuilder *Database::get_table_builder() { + return new TableBuilder(); +} + Database::Database() { } diff --git a/core/database.h b/core/database.h index c61d376..2f8aaff 100644 --- a/core/database.h +++ b/core/database.h @@ -5,6 +5,7 @@ #include #include "query_builder.h" +#include "table_builder.h" enum QueryErrorCode { OK, @@ -37,7 +38,8 @@ public: virtual void connect(const std::string &connection_str); virtual void query(const std::string &query); - virtual QueryBuilder *get_builder(); + virtual QueryBuilder *get_query_builder(); + virtual TableBuilder *get_table_builder(); Database(); ~Database(); diff --git a/core/object.cpp b/core/object.cpp index c14c7a8..2bfeb66 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -2,6 +2,10 @@ #include "database.h" +void Object::migrate() { + +} + Object::Object() { db = nullptr; } diff --git a/core/object.h b/core/object.h index b8fb2af..9ff6bb2 100644 --- a/core/object.h +++ b/core/object.h @@ -71,6 +71,8 @@ public: //setting object? //FileCache? -> set it to the global singleton by default? + virtual void migrate(); + Object(); virtual ~Object(); }; diff --git a/core/table_builder.cpp b/core/table_builder.cpp new file mode 100644 index 0000000..44f5243 --- /dev/null +++ b/core/table_builder.cpp @@ -0,0 +1,50 @@ +#include "table_builder.h" + +TableBuilder *TableBuilder::create_table(const std::string &name) { + return this; +} + +TableBuilder *TableBuilder::integer(const std::string &name) { + return this; +} + +TableBuilder *TableBuilder::date(const std::string &name) { + return this; +} + +TableBuilder *TableBuilder::varchar(const std::string &name, const int length) { + return this; +} + +TableBuilder *TableBuilder::not_null() { + return this; +} + +TableBuilder *TableBuilder::null() { + return this; +} + +TableBuilder *TableBuilder::auto_increment() { + return this; +} + +TableBuilder *TableBuilder::primary_key(const std::string &name) { + return this; +} + +TableBuilder *TableBuilder::primary_key() { + return this; +} + +TableBuilder *TableBuilder::next_row() { + return this; +} + +void TableBuilder::finalize() { +} + +TableBuilder::TableBuilder() { +} + +TableBuilder::~TableBuilder() { +} \ No newline at end of file diff --git a/core/table_builder.h b/core/table_builder.h new file mode 100644 index 0000000..6955e1f --- /dev/null +++ b/core/table_builder.h @@ -0,0 +1,27 @@ +#ifndef TABLE_BUILDER_H +#define TABLE_BUILDER_H + +#include + +class TableBuilder { +public: + virtual TableBuilder *create_table(const std::string &name); + virtual TableBuilder *integer(const std::string &name); + virtual TableBuilder *date(const std::string &name); + virtual TableBuilder *varchar(const std::string &name, const int length); + virtual TableBuilder *not_null(); + virtual TableBuilder *null(); + virtual TableBuilder *auto_increment(); + virtual TableBuilder *primary_key(const std::string &name); + virtual TableBuilder *primary_key(); + virtual TableBuilder *next_row(); + + virtual void finalize(); + + TableBuilder(); + virtual ~TableBuilder(); + + std::string result; +}; + +#endif \ No newline at end of file diff --git a/database/mysql/mysql_database.cpp b/database/mysql/mysql_database.cpp index e20c23e..770fcb5 100644 --- a/database/mysql/mysql_database.cpp +++ b/database/mysql/mysql_database.cpp @@ -5,6 +5,7 @@ #include "core/database_manager.h" #include "mysql_query_builder.h" +#include "mysql_table_builder.h" void MysqlDatabase::connect(const std::string &connection_str) { mysql = mysql_init(mysql); @@ -54,10 +55,14 @@ void MysqlDatabase::query(const std::string &query) { } -QueryBuilder *MysqlDatabase::get_builder() { +QueryBuilder *MysqlDatabase::get_query_builder() { return new MysqlQueryBuilder(); } +TableBuilder *MysqlDatabase::get_table_builder() { + return new MysqlTableBuilder(); +} + MysqlDatabase::MysqlDatabase() : Database() { diff --git a/database/mysql/mysql_database.h b/database/mysql/mysql_database.h index 85877a3..58ed15e 100644 --- a/database/mysql/mysql_database.h +++ b/database/mysql/mysql_database.h @@ -18,7 +18,8 @@ public: void connect(const std::string &connection_str); void query(const std::string &query); - QueryBuilder *get_builder(); + QueryBuilder *get_query_builder(); + TableBuilder *get_table_builder(); static Database *_creation_func(); static void _register(); diff --git a/database/mysql/mysql_table_builder.cpp b/database/mysql/mysql_table_builder.cpp new file mode 100644 index 0000000..6ff5c08 --- /dev/null +++ b/database/mysql/mysql_table_builder.cpp @@ -0,0 +1,72 @@ +#include "mysql_table_builder.h" + +TableBuilder *MysqlTableBuilder::create_table(const std::string &name) { + result += "CREATE TABLE " + name + " ( "; + + return this; +} + +TableBuilder *MysqlTableBuilder::integer(const std::string &name) { + result += name + " INTEGER "; + + return this; +} + +TableBuilder *MysqlTableBuilder::date(const std::string &name) { + result += name + " DATE "; + + return this; +} + +TableBuilder *MysqlTableBuilder::varchar(const std::string &name, const int length) { + result += name + " VARCHAR(" + std::to_string(length) + ")"; + + return this; +} + +TableBuilder *MysqlTableBuilder::not_null() { + result += "NOT NULL "; + + return this; +} + +TableBuilder *MysqlTableBuilder::null() { + result += "NULL "; + + return this; +} + +TableBuilder *MysqlTableBuilder::auto_increment() { + result += "AUTO_INCREMENT "; + + return this; +} + +TableBuilder *MysqlTableBuilder::primary_key(const std::string &name) { + result += "PRIMARY KEY (" + name + ") "; + + return this; +} + +TableBuilder *MysqlTableBuilder::primary_key() { + result += "PRIMARY KEY "; + + return this; +} + +TableBuilder *MysqlTableBuilder::next_row() { + result += ", "; + + return this; +} + +void MysqlTableBuilder::finalize() { + result += ");"; +} + + +MysqlTableBuilder::MysqlTableBuilder() { +} + +MysqlTableBuilder::~MysqlTableBuilder() { +} \ No newline at end of file diff --git a/database/mysql/mysql_table_builder.h b/database/mysql/mysql_table_builder.h new file mode 100644 index 0000000..ddd7260 --- /dev/null +++ b/database/mysql/mysql_table_builder.h @@ -0,0 +1,27 @@ +#ifndef MYSQL_TABLE_BUILDER_H +#define MYSQL_TABLE_BUILDER_H + +#include + +#include "core/table_builder.h" + +class MysqlTableBuilder : public TableBuilder { +public: + TableBuilder *create_table(const std::string &name); + TableBuilder *integer(const std::string &name); + TableBuilder *date(const std::string &name); + TableBuilder *varchar(const std::string &name, const int length); + TableBuilder *not_null(); + TableBuilder *null(); + TableBuilder *auto_increment(); + TableBuilder *primary_key(const std::string &name); + TableBuilder *primary_key(); + TableBuilder *next_row(); + + void finalize(); + + MysqlTableBuilder(); + virtual ~MysqlTableBuilder(); +}; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 33c820a..7b7dd47 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "core/application.h" #include "core/file_cache.h" @@ -14,6 +15,16 @@ #define MAIN_CLASS RDNApplication int main(int argc, char **argv) { + bool migrate = false; + + for (int i = 1; i < argc; ++i) { + const char *a = argv[i]; + + if (a[0] == 'm') { + migrate = true; + } + } + initialize_database_backends(); FileCache *file_cache = new FileCache(true); @@ -36,7 +47,12 @@ int main(int argc, char **argv) { server->port = 8080; server->initialize(); - server->main_loop(); + + if (!migrate) { + server->main_loop(); + } else { + app->migrate(); + } delete server; delete app; diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index bfe8ad2..1a3d01b 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -5,13 +5,13 @@ #include "core/query_builder.h" void MessagePage::index(Request *request) { - QueryBuilder *b = db->get_builder(); + QueryBuilder *b = db->get_query_builder(); - b->select("*")->from("tutorials_tbl")->finalize(); + b->select("*")->from("tutorials_tbl")->finalize(); - db->query(b->query_result); + db->query(b->query_result); - delete b; + delete b; /* db->query("show databases;"); @@ -19,23 +19,33 @@ void MessagePage::index(Request *request) { db->query("SELECT * FROM tutorials_tbl;"); */ - std::string r = ""; + std::string r = ""; - for (uint32_t i = 0; i < messages.size(); ++i) { - r += "

" + messages[i] + "


"; - } + for (uint32_t i = 0; i < messages.size(); ++i) { + r += "

" + messages[i] + "


"; + } - r += ""; + r += ""; request->response->setBody(r); request->send(); } -MessagePage::MessagePage() : Object() { - messages.push_back("T message 1"); - messages.push_back("T message 2"); +void MessagePage::migrate() { + TableBuilder *t = db->get_table_builder(); + + t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("dd", 30)->finalize(); + + db->query(t->result); + + delete t; +} + +MessagePage::MessagePage() : + Object() { + messages.push_back("T message 1"); + messages.push_back("T message 2"); } MessagePage::~MessagePage() { - } \ No newline at end of file diff --git a/modules/message_page/message_page.h b/modules/message_page/message_page.h index 5a4611b..7671eaf 100644 --- a/modules/message_page/message_page.h +++ b/modules/message_page/message_page.h @@ -15,6 +15,8 @@ class MessagePage : public Object { public: void index(Request *request); + void migrate(); + MessagePage(); ~MessagePage(); diff --git a/rdn_application.cpp b/rdn_application.cpp index f66e694..657f779 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -55,6 +55,10 @@ void RDNApplication::setup_middleware() { //middlewares.push_back(RDNApplication::session_middleware_func); } +void RDNApplication::migrate() { + message_page->migrate(); +} + RDNApplication::RDNApplication() : Application() { diff --git a/rdn_application.h b/rdn_application.h index d34e07a..787713c 100644 --- a/rdn_application.h +++ b/rdn_application.h @@ -18,6 +18,8 @@ public: virtual void setup_routes(); virtual void setup_middleware(); + virtual void migrate(); + RDNApplication(); ~RDNApplication();