Implemented a QueryResult object for mysql. messagepage now queries the db too.

This commit is contained in:
Relintai 2020-12-02 00:43:46 +01:00
parent b063237399
commit b99cbf0164
12 changed files with 163 additions and 27 deletions

View File

@ -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])

View File

@ -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() {

View File

@ -4,21 +4,14 @@
#include <string>
#include <memory>
#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();

15
core/query_result.cpp Normal file
View File

@ -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() {
}

13
core/query_result.h Normal file
View File

@ -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

View File

@ -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();
}

View File

@ -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();

View File

@ -24,6 +24,8 @@ QueryBuilder *MysqlQueryBuilder::limit(const int min, const int max) {
}
QueryBuilder *MysqlQueryBuilder::insert(const std::string &table_name, const std::string &params_str) {
query_result += " INSERT INTO " + table_name + " VALUES( " + params_str + " );";
return this;
}

View File

@ -0,0 +1,27 @@
#include "mysql_query_result.h"
#include <cstdio>
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);
}
}

View File

@ -0,0 +1,20 @@
#ifndef MYSQL_QUERY_RESULT_H
#define MYSQL_QUERY_RESULT_H
#include "core/query_result.h"
#include <mysql.h>
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

View File

@ -51,6 +51,7 @@ int main(int argc, char **argv) {
if (!migrate) {
server->main_loop();
} else {
printf("Running migrations.\n");
app->migrate();
}

View File

@ -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<std::string> 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 = "<html><body>";
for (uint32_t i = 0; i < messages.size(); ++i) {
r += "<p>" + messages[i] + "</p><br>";
}
for (uint32_t i = 0; i < msgs.size(); ++i) {
r += "<p>" + msgs[i] + "</p><br>";
}
r += "</html></body>";
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;
}