From 663ddb5bc92d46faa45b1381e8d1ca65f3248f0d Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 1 Dec 2020 18:29:41 +0100 Subject: [PATCH] Started work on the mysql database implementation. Mostly tests. --- core/database.cpp | 8 ++++ core/database.h | 3 ++ core/object.cpp | 4 +- core/object.h | 4 ++ database/mysql/mysql_database.cpp | 62 +++++++++++++++++++++++++++ database/mysql/mysql_database.h | 11 +++-- main.cpp | 5 ++- modules/message_page/message_page.cpp | 11 +++-- rdn_application.cpp | 3 ++ 9 files changed, 100 insertions(+), 11 deletions(-) diff --git a/core/database.cpp b/core/database.cpp index 7f00d7a..c651db9 100644 --- a/core/database.cpp +++ b/core/database.cpp @@ -1,5 +1,13 @@ #include "database.h" +void Database::connect(const std::string &connection_str) { + +} + +void Database::query(const std::string &query) { + +} + Database::Database() { } diff --git a/core/database.h b/core/database.h index 77fdb7f..4a3c0d6 100644 --- a/core/database.h +++ b/core/database.h @@ -31,6 +31,9 @@ public: //query interface (codeigniter 3 style) //virtual void where(""); etc + virtual void connect(const std::string &connection_str); + virtual void query(const std::string &query); + Database(); ~Database(); }; diff --git a/core/object.cpp b/core/object.cpp index a1b6372..c14c7a8 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1,7 +1,9 @@ #include "object.h" -Object::Object() { +#include "database.h" +Object::Object() { + db = nullptr; } Object::~Object() { diff --git a/core/object.h b/core/object.h index 6b36467..b8fb2af 100644 --- a/core/object.h +++ b/core/object.h @@ -5,6 +5,8 @@ #include #include +class Database; + //taken from GodotEngine's object.h #define RCPP_OBJECT(m_class, m_inherits) \ private: \ @@ -46,6 +48,8 @@ private: class Object { public: + Database *db; + virtual std::string get_class() const { return "Object"; } static void *get_class_ptr_static() { static int ptr; diff --git a/database/mysql/mysql_database.cpp b/database/mysql/mysql_database.cpp index 6b27f77..956fabb 100644 --- a/database/mysql/mysql_database.cpp +++ b/database/mysql/mysql_database.cpp @@ -1,7 +1,69 @@ #include "mysql_database.h" +#include + #include "core/database_manager.h" +void MysqlDatabase::connect(const std::string &connection_str) { + mysql = mysql_init(mysql); + mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0); + + std::string host = "127.0.0.1"; + std::string user = ""; + std::string password = ""; + std::string dbname = "testappdb"; + int port = 3306; + + mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0); + + if (mysql) { + printf("mysql connected\n"); + } +} + +void MysqlDatabase::query(const std::string &query) { + if (!mysql) + return; + + printf("%s\n", query.c_str()); + int error = mysql_real_query(mysql, query.c_str(), query.length()); + + if (error) { + const char *merr = mysql_error(mysql); + + printf("MySQL error: %s\n", merr); + return; + } + + printf("----------------\n"); + + MYSQL_RES *result = mysql_use_result(mysql); + //MYSQL_RES *result = mysql_store_result(mysql); + + MYSQL_ROW row; + while (row = mysql_fetch_row(result)) { + printf("%s\n", row[0]); + //printf("%s\n", row[1]); + } + + printf("----------------\n"); + + mysql_free_result(result); +} + +MysqlDatabase::MysqlDatabase() : + Database() { + + //mysql = new MYSQL(); + mysql = nullptr; +} + +MysqlDatabase::~MysqlDatabase() { + mysql_close(mysql); + + delete mysql; +} + Database *MysqlDatabase::_creation_func() { return new MysqlDatabase(); } diff --git a/database/mysql/mysql_database.h b/database/mysql/mysql_database.h index ba7c36a..d63f1e1 100644 --- a/database/mysql/mysql_database.h +++ b/database/mysql/mysql_database.h @@ -13,16 +13,15 @@ class MysqlDatabase : public Database { public: + void connect(const std::string &connection_str); + void query(const std::string &query); + static Database *_creation_func(); static void _register(); static void _unregister(); - MysqlDatabase() : Database() { - mysql = new MYSQL(); - } - ~MysqlDatabase() { - delete mysql; - } + MysqlDatabase(); + ~MysqlDatabase(); MYSQL *mysql; }; diff --git a/main.cpp b/main.cpp index d701b00..3aba5b4 100644 --- a/main.cpp +++ b/main.cpp @@ -21,7 +21,10 @@ int main(int argc, char **argv) { file_cache->wwwroot_refresh_cache(); DatabaseManager *dbm = new DatabaseManager(); - //dbm->create_database("mysql"); + uint32_t index = dbm->create_database("mysql"); + + Database *db = dbm->databases[0]; + db->connect(""); Application *app = new MAIN_CLASS(); diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index e4b80ea..0fbf552 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -1,6 +1,11 @@ #include "message_page.h" +#include "core/database.h" + void MessagePage::index(Request *request) { + db->query("show databases;"); + db->query("show tables;"); + db->query("SELECT * FROM tutorials_tbl;"); std::string r = ""; @@ -14,9 +19,9 @@ void MessagePage::index(Request *request) { request->send(); } -MessagePage::MessagePage() { - messages.push_back("t message 1"); - messages.push_back("t message 2"); +MessagePage::MessagePage() : Object() { + messages.push_back("T message 1"); + messages.push_back("T message 2"); } MessagePage::~MessagePage() { diff --git a/rdn_application.cpp b/rdn_application.cpp index 80bedaa..f66e694 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -8,6 +8,8 @@ #include "core/handler_instance.h" +#include "core/database_manager.h" + void RDNApplication::index(Object *instance, Request *request) { std::string body; @@ -68,6 +70,7 @@ RDNApplication::RDNApplication() : themes.push_back(t); message_page = new MessagePage(); + message_page->db = DatabaseManager::get_singleton()->databases[0]; } RDNApplication::~RDNApplication() {