From b99cbf0164266ed0636bdb0a92410af3ce11c95f Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 2 Dec 2020 00:43:46 +0100 Subject: [PATCH] Implemented a QueryResult object for mysql. messagepage now queries the db too. --- SConstruct | 3 +- core/database.cpp | 8 ++++- core/database.h | 16 +++------- core/query_result.cpp | 15 +++++++++ core/query_result.h | 13 ++++++++ database/mysql/mysql_database.cpp | 38 ++++++++++++++++++++-- database/mysql/mysql_database.h | 3 +- database/mysql/mysql_query_builder.cpp | 2 ++ database/mysql/mysql_query_result.cpp | 27 ++++++++++++++++ database/mysql/mysql_query_result.h | 20 ++++++++++++ main.cpp | 1 + modules/message_page/message_page.cpp | 44 ++++++++++++++++++++------ 12 files changed, 163 insertions(+), 27 deletions(-) create mode 100644 core/query_result.cpp create mode 100644 core/query_result.h create mode 100644 database/mysql/mysql_query_result.cpp create mode 100644 database/mysql/mysql_query_result.h diff --git a/SConstruct b/SConstruct index e67f97e..f23944f 100644 --- a/SConstruct +++ b/SConstruct @@ -134,6 +134,7 @@ env_base.Append(CXX=["-o3"]) env = env_base.Clone() Export("env") +SConscript("core/SCsub") for d in database_list: tmppath = "./database/" + d @@ -189,8 +190,6 @@ for m in module_list: sys.path.remove(tmppath) sys.modules.pop("detect") -SConscript("core/SCsub") - env.prg_sources = [ "rdn_application.cpp" ] libapp = env.add_library("application", env.prg_sources) env.Prepend(LIBS=[libapp]) diff --git a/core/database.cpp b/core/database.cpp index dca8eee..6fb3545 100644 --- a/core/database.cpp +++ b/core/database.cpp @@ -1,9 +1,15 @@ #include "database.h" +#include "query_builder.h" +#include "table_builder.h" +#include "query_result.h" + void Database::connect(const std::string &connection_str) { } -void Database::query(const std::string &query) { +QueryResult *Database::query(const std::string &query) { +} +void Database::query_run(const std::string &query) { } QueryBuilder *Database::get_query_builder() { diff --git a/core/database.h b/core/database.h index 2f8aaff..3db0cea 100644 --- a/core/database.h +++ b/core/database.h @@ -4,21 +4,14 @@ #include #include -#include "query_builder.h" -#include "table_builder.h" - enum QueryErrorCode { OK, ERROR }; -class QueryResult { -public: - //rows - //next_row() - //get_int(int index) - //get_string(int column) etc -}; +class QueryBuilder; +class TableBuilder; +class QueryResult; class Database { public: @@ -36,7 +29,8 @@ public: //virtual void where(""); etc virtual void connect(const std::string &connection_str); - virtual void query(const std::string &query); + virtual QueryResult *query(const std::string &query); + virtual void query_run(const std::string &query); virtual QueryBuilder *get_query_builder(); virtual TableBuilder *get_table_builder(); diff --git a/core/query_result.cpp b/core/query_result.cpp new file mode 100644 index 0000000..545fabd --- /dev/null +++ b/core/query_result.cpp @@ -0,0 +1,15 @@ +#include "query_result.h" + +bool QueryResult::next_row() { + return false; +} + +const char *QueryResult::get_cell(const int index) { + return ""; +} + +QueryResult::QueryResult() { +} + +QueryResult::~QueryResult() { +} diff --git a/core/query_result.h b/core/query_result.h new file mode 100644 index 0000000..ec1ef1b --- /dev/null +++ b/core/query_result.h @@ -0,0 +1,13 @@ +#ifndef QUERY_RESULT_H +#define QUERY_RESULT_H + +class QueryResult { +public: + virtual bool next_row(); + virtual const char*get_cell(const int index); + + QueryResult(); + virtual ~QueryResult(); +}; + +#endif \ No newline at end of file diff --git a/database/mysql/mysql_database.cpp b/database/mysql/mysql_database.cpp index 770fcb5..023f64c 100644 --- a/database/mysql/mysql_database.cpp +++ b/database/mysql/mysql_database.cpp @@ -6,6 +6,7 @@ #include "mysql_query_builder.h" #include "mysql_table_builder.h" +#include "mysql_query_result.h" void MysqlDatabase::connect(const std::string &connection_str) { mysql = mysql_init(mysql); @@ -24,11 +25,38 @@ void MysqlDatabase::connect(const std::string &connection_str) { } } -void MysqlDatabase::query(const std::string &query) { +QueryResult *MysqlDatabase::query(const std::string &query) { + if (!mysql) + return nullptr; + + //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 nullptr; + } + + MYSQL_RES *result = mysql_use_result(mysql); + + MysqlQueryResult *res = new MysqlQueryResult(); + + res->result = result; + //res->next_row(); + + return res; +} + +void MysqlDatabase::query_run(const std::string &query) { if (!mysql) return; - printf("%s\n", query.c_str()); + //printf("%s\n", query.c_str()); + int error = mysql_real_query(mysql, query.c_str(), query.length()); if (error) { @@ -38,6 +66,10 @@ void MysqlDatabase::query(const std::string &query) { return; } + //printf("query OK\n"); + //printf("----------------\n"); + +/* printf("----------------\n"); MYSQL_RES *result = mysql_use_result(mysql); @@ -52,9 +84,9 @@ void MysqlDatabase::query(const std::string &query) { printf("----------------\n"); mysql_free_result(result); + */ } - QueryBuilder *MysqlDatabase::get_query_builder() { return new MysqlQueryBuilder(); } diff --git a/database/mysql/mysql_database.h b/database/mysql/mysql_database.h index 58ed15e..b7bd7c4 100644 --- a/database/mysql/mysql_database.h +++ b/database/mysql/mysql_database.h @@ -16,7 +16,8 @@ class MysqlDatabase : public Database { public: void connect(const std::string &connection_str); - void query(const std::string &query); + QueryResult *query(const std::string &query); + void query_run(const std::string &query); QueryBuilder *get_query_builder(); TableBuilder *get_table_builder(); diff --git a/database/mysql/mysql_query_builder.cpp b/database/mysql/mysql_query_builder.cpp index 21f9676..dbfc29e 100644 --- a/database/mysql/mysql_query_builder.cpp +++ b/database/mysql/mysql_query_builder.cpp @@ -24,6 +24,8 @@ QueryBuilder *MysqlQueryBuilder::limit(const int min, const int max) { } QueryBuilder *MysqlQueryBuilder::insert(const std::string &table_name, const std::string ¶ms_str) { + query_result += " INSERT INTO " + table_name + " VALUES( " + params_str + " );"; + return this; } diff --git a/database/mysql/mysql_query_result.cpp b/database/mysql/mysql_query_result.cpp new file mode 100644 index 0000000..0ffc870 --- /dev/null +++ b/database/mysql/mysql_query_result.cpp @@ -0,0 +1,27 @@ +#include "mysql_query_result.h" + +#include + +bool MysqlQueryResult::next_row() { + current_row = mysql_fetch_row(result); + + //null if no result + return current_row; +} + +const char* MysqlQueryResult::get_cell(const int index) { + if (!current_row) + return ""; + + return current_row[index]; +} + +MysqlQueryResult::MysqlQueryResult() : QueryResult() { + result = nullptr; +} + +MysqlQueryResult::~MysqlQueryResult() { + if(result) { + mysql_free_result(result); + } +} \ No newline at end of file diff --git a/database/mysql/mysql_query_result.h b/database/mysql/mysql_query_result.h new file mode 100644 index 0000000..cfc447f --- /dev/null +++ b/database/mysql/mysql_query_result.h @@ -0,0 +1,20 @@ +#ifndef MYSQL_QUERY_RESULT_H +#define MYSQL_QUERY_RESULT_H + +#include "core/query_result.h" + +#include + +class MysqlQueryResult : public QueryResult { +public: + bool next_row(); + const char* get_cell(const int index); + + MysqlQueryResult(); + ~MysqlQueryResult(); + + MYSQL_ROW current_row; + MYSQL_RES *result; +}; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 7b7dd47..25c72e0 100644 --- a/main.cpp +++ b/main.cpp @@ -51,6 +51,7 @@ int main(int argc, char **argv) { if (!migrate) { server->main_loop(); } else { + printf("Running migrations.\n"); app->migrate(); } diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index 1a3d01b..a7d43d7 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -3,28 +3,37 @@ #include "core/database.h" #include "core/query_builder.h" +#include "core/table_builder.h" +#include "core/query_result.h" void MessagePage::index(Request *request) { QueryBuilder *b = db->get_query_builder(); - b->select("*")->from("tutorials_tbl")->finalize(); + b->select("text")->from("message_page")->finalize(); - db->query(b->query_result); + QueryResult *res = db->query(b->query_result); + std::vector msgs; + + if (res) { + while (res->next_row()) { + msgs.push_back(res->get_cell(0)); + } + } + + delete res; delete b; -/* - db->query("show databases;"); - db->query("show tables;"); - db->query("SELECT * FROM tutorials_tbl;"); -*/ - std::string r = ""; for (uint32_t i = 0; i < messages.size(); ++i) { r += "

" + messages[i] + "


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

" + msgs[i] + "


"; + } + r += ""; request->response->setBody(r); @@ -34,10 +43,27 @@ void MessagePage::index(Request *request) { 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(); + t->create_table("message_page")->integer("id")->auto_increment()->primary_key()->next_row()->varchar("text", 30)->finalize(); + + printf("%s\n", t->result.c_str()); db->query(t->result); + QueryBuilder *b = db->get_query_builder(); + + b->insert("message_page", "default, 'aaewdwd'"); + + printf("%s\n", b->query_result.c_str()); + + db->query_run(b->query_result); + + b->query_result.clear(); + b->insert("message_page", "default, 'qqqqq'"); + + printf("%s\n", b->query_result.c_str()); + + db->query_run(b->query_result); + delete t; }