From 02c4294cdae545ee45743ee5fcdc7291dc2674f8 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 1 Nov 2021 16:57:22 +0100 Subject: [PATCH] Now QueryBilder, QueryResult, and TableBullder are returned wrapped into a Ref<> from the database subsystem by default. --- core/database/database.cpp | 12 ++++++------ core/database/database.h | 8 +++++--- core/database/query_builder.cpp | 4 ++-- core/database/query_builder.h | 2 +- core/database/table_builder.cpp | 4 ++-- core/database/table_builder.h | 2 +- database/mysql/mysql_database.cpp | 12 ++++++------ database/mysql/mysql_database.h | 6 +++--- database/mysql/mysql_query_builder.cpp | 2 +- database/mysql/mysql_query_builder.h | 2 +- database/sqlite/sqlite3_database.cpp | 6 +++--- database/sqlite/sqlite3_database.h | 6 +++--- database/sqlite/sqlite3_query_builder.cpp | 2 +- database/sqlite/sqlite3_query_builder.h | 2 +- database/sqlite/sqlite3_table_builder.cpp | 4 +++- database/sqlite/sqlite3_table_builder.h | 2 +- modules/message_page/message_page.cpp | 19 ++++++------------- modules/users/user_model.cpp | 5 +---- 18 files changed, 47 insertions(+), 53 deletions(-) diff --git a/core/database/database.cpp b/core/database/database.cpp index 34ecace..4c63281 100644 --- a/core/database/database.cpp +++ b/core/database/database.cpp @@ -7,18 +7,18 @@ void Database::connect(const std::string &connection_str) { } -QueryResult *Database::query(const std::string &query) { - return nullptr; +Ref Database::query(const std::string &query) { + return Ref(); } void Database::query_run(const std::string &query) { } -QueryBuilder *Database::get_query_builder() { - return new QueryBuilder(); +Ref Database::get_query_builder() { + return Ref(new QueryBuilder()); } -TableBuilder *Database::get_table_builder() { - return new TableBuilder(); +Ref Database::get_table_builder() { + return Ref(new TableBuilder()); } std::string Database::escape(const std::string str) { diff --git a/core/database/database.h b/core/database/database.h index 03146ac..120617f 100644 --- a/core/database/database.h +++ b/core/database/database.h @@ -4,6 +4,8 @@ #include #include +#include "core/reference.h" + enum QueryErrorCode { OK, ERROR @@ -29,11 +31,11 @@ public: //virtual void where(""); etc virtual void connect(const std::string &connection_str); - virtual QueryResult *query(const std::string &query); + virtual Ref query(const std::string &query); virtual void query_run(const std::string &query); - virtual QueryBuilder *get_query_builder(); - virtual TableBuilder *get_table_builder(); + virtual Ref get_query_builder(); + virtual Ref get_table_builder(); virtual std::string escape(const std::string str); virtual void escape(const std::string str, std::string *to); diff --git a/core/database/query_builder.cpp b/core/database/query_builder.cpp index 5d51b5d..7700ddb 100644 --- a/core/database/query_builder.cpp +++ b/core/database/query_builder.cpp @@ -219,8 +219,8 @@ QueryBuilder *QueryBuilder::reset() { return this; } -QueryResult *QueryBuilder::run() { - return nullptr; +Ref QueryBuilder::run() { + return Ref(); } void QueryBuilder::run_query() { diff --git a/core/database/query_builder.h b/core/database/query_builder.h index f6ac0e1..9349b92 100644 --- a/core/database/query_builder.h +++ b/core/database/query_builder.h @@ -96,7 +96,7 @@ public: virtual QueryBuilder *reset(); - virtual QueryResult *run(); + virtual Ref run(); virtual void run_query(); std::string get_result(); diff --git a/core/database/table_builder.cpp b/core/database/table_builder.cpp index 593fc4f..1649ae4 100644 --- a/core/database/table_builder.cpp +++ b/core/database/table_builder.cpp @@ -69,8 +69,8 @@ TableBuilder *TableBuilder::references(const std::string &table, const std::stri return this; } -QueryResult *TableBuilder::run() { - return nullptr; +Ref TableBuilder::run() { + return Ref(); } void TableBuilder::run_query() { diff --git a/core/database/table_builder.h b/core/database/table_builder.h index 4965037..a7149f9 100644 --- a/core/database/table_builder.h +++ b/core/database/table_builder.h @@ -30,7 +30,7 @@ public: virtual TableBuilder *foreign_key(const std::string &name); virtual TableBuilder *references(const std::string &table, const std::string &name); - virtual QueryResult *run(); + virtual Ref run(); virtual void run_query(); void print(); diff --git a/database/mysql/mysql_database.cpp b/database/mysql/mysql_database.cpp index feb4827..08ca475 100644 --- a/database/mysql/mysql_database.cpp +++ b/database/mysql/mysql_database.cpp @@ -25,7 +25,7 @@ void MysqlDatabase::connect(const std::string &connection_str) { } } -QueryResult *MysqlDatabase::query(const std::string &query) { +Ref MysqlDatabase::query(const std::string &query) { if (!mysql) return nullptr; @@ -48,7 +48,7 @@ QueryResult *MysqlDatabase::query(const std::string &query) { res->result = result; //res->next_row(); - return res; + return Ref(res); } void MysqlDatabase::query_run(const std::string &query) { @@ -87,15 +87,15 @@ void MysqlDatabase::query_run(const std::string &query) { */ } -QueryBuilder *MysqlDatabase::get_query_builder() { +Ref MysqlDatabase::get_query_builder() { MysqlQueryBuilder *b = new MysqlQueryBuilder(); b->_db = this; - return b; + return Ref(b); } -TableBuilder *MysqlDatabase::get_table_builder() { - return new MysqlTableBuilder(); +Ref MysqlDatabase::get_table_builder() { + return Ref(new MysqlTableBuilder()); } std::string MysqlDatabase::escape(const std::string str) { diff --git a/database/mysql/mysql_database.h b/database/mysql/mysql_database.h index 9dad340..70d0fe7 100644 --- a/database/mysql/mysql_database.h +++ b/database/mysql/mysql_database.h @@ -16,11 +16,11 @@ class MysqlDatabase : public Database { public: void connect(const std::string &connection_str); - QueryResult *query(const std::string &query); + Ref query(const std::string &query); void query_run(const std::string &query); - QueryBuilder *get_query_builder(); - TableBuilder *get_table_builder(); + Ref get_query_builder(); + Ref get_table_builder(); std::string escape(const std::string str); void escape(const std::string str, std::string *to); diff --git a/database/mysql/mysql_query_builder.cpp b/database/mysql/mysql_query_builder.cpp index 8891837..59d4c43 100644 --- a/database/mysql/mysql_query_builder.cpp +++ b/database/mysql/mysql_query_builder.cpp @@ -92,7 +92,7 @@ void MysqlQueryBuilder::end_command() { query_result += ";"; } -QueryResult *MysqlQueryBuilder::run() { +Ref MysqlQueryBuilder::run() { end_command(); if (!_db) { diff --git a/database/mysql/mysql_query_builder.h b/database/mysql/mysql_query_builder.h index f8a3d7c..9b7db05 100644 --- a/database/mysql/mysql_query_builder.h +++ b/database/mysql/mysql_query_builder.h @@ -36,7 +36,7 @@ public: void end_command(); - QueryResult *run(); + Ref run(); void run_query(); MysqlQueryBuilder(); diff --git a/database/sqlite/sqlite3_database.cpp b/database/sqlite/sqlite3_database.cpp index db9a6a6..9fc9c6c 100644 --- a/database/sqlite/sqlite3_database.cpp +++ b/database/sqlite/sqlite3_database.cpp @@ -18,14 +18,14 @@ void SQLite3Database::_unregister() { DatabaseManager::_unregister_db_creation_func("sqlite"); } -QueryBuilder *SQLite3Database::get_query_builder() { +Ref SQLite3Database::get_query_builder() { SQLite3QueryBuilder *b = new SQLite3QueryBuilder(); b->_db = this; return b; } -TableBuilder *SQLite3Database::get_table_builder() { +Ref SQLite3Database::get_table_builder() { SQLite3TableBuilder *b = new SQLite3TableBuilder(); b->_db = this; @@ -41,7 +41,7 @@ void SQLite3Database::connect(const std::string &connection_str) { ret = sqlite3_open(connection_str.c_str(), &conn); } -QueryResult *SQLite3Database::query(const std::string &query) { +Ref SQLite3Database::query(const std::string &query) { Sqlite3QueryResult *res = new Sqlite3QueryResult(); res->query(query, conn); diff --git a/database/sqlite/sqlite3_database.h b/database/sqlite/sqlite3_database.h index f1a0b2a..5a33147 100644 --- a/database/sqlite/sqlite3_database.h +++ b/database/sqlite/sqlite3_database.h @@ -19,11 +19,11 @@ public: static void _register(); static void _unregister(); - QueryBuilder *get_query_builder(); - TableBuilder *get_table_builder(); + Ref get_query_builder(); + Ref get_table_builder(); void connect(const std::string &connection_str); - QueryResult *query(const std::string &query); + Ref query(const std::string &query); void query_run(const std::string &query); std::string escape(const std::string str); diff --git a/database/sqlite/sqlite3_query_builder.cpp b/database/sqlite/sqlite3_query_builder.cpp index 3542bf4..9ac0be1 100644 --- a/database/sqlite/sqlite3_query_builder.cpp +++ b/database/sqlite/sqlite3_query_builder.cpp @@ -282,7 +282,7 @@ void SQLite3QueryBuilder::end_command() { query_result += ";"; } -QueryResult *SQLite3QueryBuilder::run() { +Ref SQLite3QueryBuilder::run() { if (!_db) { printf("SQLite3QueryBuilder::run !db!\n"); diff --git a/database/sqlite/sqlite3_query_builder.h b/database/sqlite/sqlite3_query_builder.h index 2739d62..adda796 100644 --- a/database/sqlite/sqlite3_query_builder.h +++ b/database/sqlite/sqlite3_query_builder.h @@ -74,7 +74,7 @@ public: void end_command(); - QueryResult *run(); + Ref run(); void run_query(); SQLite3QueryBuilder(); diff --git a/database/sqlite/sqlite3_table_builder.cpp b/database/sqlite/sqlite3_table_builder.cpp index cf29cde..a332dd4 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 "core/database/query_result.h" + #include "sqlite3_database.h" TableBuilder *SQLite3TableBuilder::create_table(const std::string &name) { @@ -105,7 +107,7 @@ TableBuilder *SQLite3TableBuilder::references(const std::string &table, const st return this; } -QueryResult *SQLite3TableBuilder::run() { +Ref SQLite3TableBuilder::run() { if (!_db) { printf("SQLite3TableBuilder::run !db!\n"); diff --git a/database/sqlite/sqlite3_table_builder.h b/database/sqlite/sqlite3_table_builder.h index 15601b7..b76583a 100644 --- a/database/sqlite/sqlite3_table_builder.h +++ b/database/sqlite/sqlite3_table_builder.h @@ -30,7 +30,7 @@ public: TableBuilder *foreign_key(const std::string &name); TableBuilder *references(const std::string &table, const std::string &name); - virtual QueryResult *run(); + virtual Ref run(); virtual void run_query(); SQLite3TableBuilder(); diff --git a/modules/message_page/message_page.cpp b/modules/message_page/message_page.cpp index bf34d1d..3c08712 100644 --- a/modules/message_page/message_page.cpp +++ b/modules/message_page/message_page.cpp @@ -7,23 +7,18 @@ #include "core/database/query_result.h" void MessagePage::index(Request *request) { - QueryBuilder *b = db->get_query_builder(); + Ref b = db->get_query_builder(); b->select("text")->from("message_page")->end_command(); - QueryResult *res = db->query(b->query_result); + Ref res = db->query(b->query_result); std::vector msgs; - if (res) { - while (res->next_row()) { - msgs.push_back(res->get_cell(0)); - } + while (res->next_row()) { + msgs.push_back(res->get_cell(0)); } - delete res; - delete b; - std::string r = ""; for (uint32_t i = 0; i < messages.size(); ++i) { @@ -42,7 +37,7 @@ void MessagePage::index(Request *request) { } void MessagePage::migrate() { - TableBuilder *t = db->get_table_builder(); + Ref t = db->get_table_builder(); t->drop_table("message_page"); db->query_run(t->result); @@ -57,7 +52,7 @@ void MessagePage::migrate() { db->query(t->result); - QueryBuilder *b = db->get_query_builder(); + Ref b = db->get_query_builder(); b->insert("message_page")->values("default, 'aaewdwd'"); @@ -71,8 +66,6 @@ void MessagePage::migrate() { printf("%s\n", b->query_result.c_str()); db->query_run(b->query_result); - - delete t; } MessagePage::MessagePage() : diff --git a/modules/users/user_model.cpp b/modules/users/user_model.cpp index 47c0b22..088d0e5 100644 --- a/modules/users/user_model.cpp +++ b/modules/users/user_model.cpp @@ -101,12 +101,9 @@ void UserModel::save_user(Ref &user) { b->end_command(); b->select_last_insert_id(); - QueryResult *r = b->run(); + Ref r = b->run(); user->id = r->get_last_insert_rowid(); - - delete r; - } else { b->udpate(_table_name); b->set();