Now QueryBilder, QueryResult, and TableBullder are returned wrapped into a Ref<> from the database subsystem by default.

This commit is contained in:
Relintai 2021-11-01 16:57:22 +01:00
parent 8e624d8b5a
commit 02c4294cda
18 changed files with 47 additions and 53 deletions

View File

@ -7,18 +7,18 @@
void Database::connect(const std::string &connection_str) {
}
QueryResult *Database::query(const std::string &query) {
return nullptr;
Ref<QueryResult> Database::query(const std::string &query) {
return Ref<QueryResult>();
}
void Database::query_run(const std::string &query) {
}
QueryBuilder *Database::get_query_builder() {
return new QueryBuilder();
Ref<QueryBuilder> Database::get_query_builder() {
return Ref<QueryBuilder>(new QueryBuilder());
}
TableBuilder *Database::get_table_builder() {
return new TableBuilder();
Ref<TableBuilder> Database::get_table_builder() {
return Ref<TableBuilder>(new TableBuilder());
}
std::string Database::escape(const std::string str) {

View File

@ -4,6 +4,8 @@
#include <string>
#include <memory>
#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<QueryResult> 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<QueryBuilder> get_query_builder();
virtual Ref<TableBuilder> get_table_builder();
virtual std::string escape(const std::string str);
virtual void escape(const std::string str, std::string *to);

View File

@ -219,8 +219,8 @@ QueryBuilder *QueryBuilder::reset() {
return this;
}
QueryResult *QueryBuilder::run() {
return nullptr;
Ref<QueryResult> QueryBuilder::run() {
return Ref<QueryResult>();
}
void QueryBuilder::run_query() {

View File

@ -96,7 +96,7 @@ public:
virtual QueryBuilder *reset();
virtual QueryResult *run();
virtual Ref<QueryResult> run();
virtual void run_query();
std::string get_result();

View File

@ -69,8 +69,8 @@ TableBuilder *TableBuilder::references(const std::string &table, const std::stri
return this;
}
QueryResult *TableBuilder::run() {
return nullptr;
Ref<QueryResult> TableBuilder::run() {
return Ref<QueryResult>();
}
void TableBuilder::run_query() {

View File

@ -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<QueryResult> run();
virtual void run_query();
void print();

View File

@ -25,7 +25,7 @@ void MysqlDatabase::connect(const std::string &connection_str) {
}
}
QueryResult *MysqlDatabase::query(const std::string &query) {
Ref<QueryResult> 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<QueryResult>(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<QueryBuilder> MysqlDatabase::get_query_builder() {
MysqlQueryBuilder *b = new MysqlQueryBuilder();
b->_db = this;
return b;
return Ref<QueryBuilder>(b);
}
TableBuilder *MysqlDatabase::get_table_builder() {
return new MysqlTableBuilder();
Ref<TableBuilder> MysqlDatabase::get_table_builder() {
return Ref<TableBuilder>(new MysqlTableBuilder());
}
std::string MysqlDatabase::escape(const std::string str) {

View File

@ -16,11 +16,11 @@
class MysqlDatabase : public Database {
public:
void connect(const std::string &connection_str);
QueryResult *query(const std::string &query);
Ref<QueryResult> query(const std::string &query);
void query_run(const std::string &query);
QueryBuilder *get_query_builder();
TableBuilder *get_table_builder();
Ref<QueryBuilder> get_query_builder();
Ref<TableBuilder> get_table_builder();
std::string escape(const std::string str);
void escape(const std::string str, std::string *to);

View File

@ -92,7 +92,7 @@ void MysqlQueryBuilder::end_command() {
query_result += ";";
}
QueryResult *MysqlQueryBuilder::run() {
Ref<QueryResult> MysqlQueryBuilder::run() {
end_command();
if (!_db) {

View File

@ -36,7 +36,7 @@ public:
void end_command();
QueryResult *run();
Ref<QueryResult> run();
void run_query();
MysqlQueryBuilder();

View File

@ -18,14 +18,14 @@ void SQLite3Database::_unregister() {
DatabaseManager::_unregister_db_creation_func("sqlite");
}
QueryBuilder *SQLite3Database::get_query_builder() {
Ref<QueryBuilder> SQLite3Database::get_query_builder() {
SQLite3QueryBuilder *b = new SQLite3QueryBuilder();
b->_db = this;
return b;
}
TableBuilder *SQLite3Database::get_table_builder() {
Ref<TableBuilder> 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<QueryResult> SQLite3Database::query(const std::string &query) {
Sqlite3QueryResult *res = new Sqlite3QueryResult();
res->query(query, conn);

View File

@ -19,11 +19,11 @@ public:
static void _register();
static void _unregister();
QueryBuilder *get_query_builder();
TableBuilder *get_table_builder();
Ref<QueryBuilder> get_query_builder();
Ref<TableBuilder> get_table_builder();
void connect(const std::string &connection_str);
QueryResult *query(const std::string &query);
Ref<QueryResult> query(const std::string &query);
void query_run(const std::string &query);
std::string escape(const std::string str);

View File

@ -282,7 +282,7 @@ void SQLite3QueryBuilder::end_command() {
query_result += ";";
}
QueryResult *SQLite3QueryBuilder::run() {
Ref<QueryResult> SQLite3QueryBuilder::run() {
if (!_db) {
printf("SQLite3QueryBuilder::run !db!\n");

View File

@ -74,7 +74,7 @@ public:
void end_command();
QueryResult *run();
Ref<QueryResult> run();
void run_query();
SQLite3QueryBuilder();

View File

@ -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<QueryResult> SQLite3TableBuilder::run() {
if (!_db) {
printf("SQLite3TableBuilder::run !db!\n");

View File

@ -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<QueryResult> run();
virtual void run_query();
SQLite3TableBuilder();

View File

@ -7,23 +7,18 @@
#include "core/database/query_result.h"
void MessagePage::index(Request *request) {
QueryBuilder *b = db->get_query_builder();
Ref<QueryBuilder> b = db->get_query_builder();
b->select("text")->from("message_page")->end_command();
QueryResult *res = db->query(b->query_result);
Ref<QueryResult> res = db->query(b->query_result);
std::vector<std::string> 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 = "<html><body>";
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<TableBuilder> 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<QueryBuilder> 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() :

View File

@ -101,12 +101,9 @@ void UserModel::save_user(Ref<User> &user) {
b->end_command();
b->select_last_insert_id();
QueryResult *r = b->run();
Ref<QueryResult> r = b->run();
user->id = r->get_last_insert_rowid();
delete r;
} else {
b->udpate(_table_name);
b->set();