Started reworking the QueryBuilder. Now instead just creating a query string (when finished) it will actually be able to handle querying, escapes, and also prepared statements.

This commit is contained in:
Relintai 2021-08-19 14:12:02 +02:00
parent aea483052b
commit 8de082ffba
5 changed files with 231 additions and 28 deletions

View File

@ -1,5 +1,27 @@
#include "query_builder.h"
#include "query_result.h"
QueryBuilder *QueryBuilder::select() {
return this;
}
QueryBuilder *QueryBuilder::where() {
return this;
}
QueryBuilder *QueryBuilder::from() {
return this;
}
QueryBuilder *QueryBuilder::insert() {
return this;
}
QueryBuilder *QueryBuilder::values() {
return this;
}
QueryBuilder *QueryBuilder::cvalues() {
return this;
}
QueryBuilder *QueryBuilder::select(const std::string &params) {
return this;
}
@ -12,15 +34,71 @@ QueryBuilder *QueryBuilder::from(const std::string &params) {
return this;
}
QueryBuilder *QueryBuilder::insert(const std::string &table_name) {
return this;
}
QueryBuilder *QueryBuilder::values(const std::string &params_str) {
return this;
}
QueryBuilder *QueryBuilder::eselect(const std::string &params) {
select(escape(params));
return this;
}
QueryBuilder *QueryBuilder::ewhere(const std::string &params) {
where(escape(params));
return this;
}
QueryBuilder *QueryBuilder::efrom(const std::string &params) {
from(escape(params));
return this;
}
QueryBuilder *QueryBuilder::einsert(const std::string &table_name) {
insert(escape(table_name));
return this;
}
QueryBuilder *QueryBuilder::evalues(const std::string &params_str) {
values(escape(params_str));
return this;
}
QueryBuilder *QueryBuilder::limit(const int min, const int max) {
return this;
}
QueryBuilder *QueryBuilder::insert(const std::string &table_name, const std::string &params_str) {
std::string QueryBuilder::escape(const std::string &params) {
return params;
}
QueryBuilder *QueryBuilder::prepare() {
return this;
}
QueryBuilder *QueryBuilder::set_param(const int index, const std::string &value) {
return this;
}
QueryBuilder *QueryBuilder::set_param(const int index, const int value) {
return this;
}
QueryBuilder *QueryBuilder::set_param(const int index, const float value) {
return this;
}
void QueryBuilder::finalize() {
void QueryBuilder::end_command() {
}
QueryResult *QueryBuilder::run() {
return nullptr;
}
std::string QueryBuilder::get_result() {
end_command();
return query_result;
}
QueryBuilder::QueryBuilder() {

View File

@ -3,15 +3,45 @@
#include <string>
class QueryResult;
//methods that start with an e escape their params.
class QueryBuilder {
public:
virtual QueryBuilder *select();
virtual QueryBuilder *where();
virtual QueryBuilder *from();
virtual QueryBuilder *insert();
virtual QueryBuilder *values();
virtual QueryBuilder *cvalues();
virtual QueryBuilder *select(const std::string &params);
virtual QueryBuilder *where(const std::string &params);
virtual QueryBuilder *from(const std::string &params);
virtual QueryBuilder *limit(const int min, const int max);
virtual QueryBuilder *insert(const std::string &table_name, const std::string &params_str);
virtual QueryBuilder *insert(const std::string &table_name);
virtual QueryBuilder *values(const std::string &params_str);
virtual void finalize();
virtual QueryBuilder *eselect(const std::string &params);
virtual QueryBuilder *ewhere(const std::string &params);
virtual QueryBuilder *efrom(const std::string &params);
virtual QueryBuilder *einsert(const std::string &table_name);
virtual QueryBuilder *evalues(const std::string &params_str);
virtual QueryBuilder *limit(const int min, const int max);
virtual std::string escape(const std::string &params);
virtual QueryBuilder *prepare();
virtual QueryBuilder *set_param(const int index, const std::string &value);
virtual QueryBuilder *set_param(const int index, const int value);
virtual QueryBuilder *set_param(const int index, const float value);
virtual void end_command();
virtual QueryResult *run();
std::string get_result();
QueryBuilder();
virtual ~QueryBuilder();

View File

@ -1,41 +1,111 @@
#include "mysql_query_builder.h"
#include "mysql_database.h"
#include "mysql_query_result.h"
QueryBuilder *MysqlQueryBuilder::select() {
query_result += "SELECT ";
return this;
}
QueryBuilder *MysqlQueryBuilder::where() {
query_result += "WHERE ";
return this;
}
QueryBuilder *MysqlQueryBuilder::from() {
query_result += "FROM ";
return this;
}
QueryBuilder *MysqlQueryBuilder::insert() {
query_result += "INSERT INTO ";
return this;
}
QueryBuilder *MysqlQueryBuilder::values() {
query_result += "VALUES(";
return this;
}
QueryBuilder *MysqlQueryBuilder::cvalues() {
query_result += ") ";
return this;
}
QueryBuilder *MysqlQueryBuilder::select(const std::string &params) {
query_result += "SELECT " + params;
query_result += "SELECT " + params + " ";
return this;
}
QueryBuilder *MysqlQueryBuilder::where(const std::string &params) {
query_result += " WHERE " + params;
query_result += "WHERE " + params + " ";
return this;
}
QueryBuilder *MysqlQueryBuilder::from(const std::string &params) {
query_result += " FROM " + params;
query_result += "FROM " + params + " ";
return this;
}
QueryBuilder *MysqlQueryBuilder::insert(const std::string &table_name) {
query_result += "INSERT INTO " + table_name + " ";
return this;
}
QueryBuilder *MysqlQueryBuilder::values(const std::string &params_str) {
query_result += "VALUES(" + params_str + ") ";
return this;
}
QueryBuilder *MysqlQueryBuilder::limit(const int min, const int max) {
return this;
}
std::string MysqlQueryBuilder::escape(const std::string &params) {
if (!_db) {
printf("MysqlQueryBuilder::escape !db!\n");
return "";
}
return _db->escape(params);
}
QueryBuilder *MysqlQueryBuilder::prepare() {
return this;
}
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const std::string &value) {
return this;
}
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const int value) {
return this;
}
QueryBuilder *MysqlQueryBuilder::set_param(const int index, const float value) {
return this;
}
void MysqlQueryBuilder::end_command() {
query_result += ";";
}
QueryResult *MysqlQueryBuilder::run() {
return nullptr;
}
QueryBuilder *MysqlQueryBuilder::limit(const int min, const int max) {
return this;
}
QueryBuilder *MysqlQueryBuilder::insert(const std::string &table_name, const std::string &params_str) {
query_result += " INSERT INTO " + table_name + " VALUES( " + params_str + " );";
return this;
}
void MysqlQueryBuilder::finalize() {
query_result += ";";
}
MysqlQueryBuilder::MysqlQueryBuilder() {
}
MysqlQueryBuilder::~MysqlQueryBuilder() {
}

View File

@ -6,17 +6,42 @@
#include "core/database/query_builder.h"
class MysqlDatabase;
class MysqlQueryResult;
class MysqlQueryBuilder : public QueryBuilder {
public:
QueryBuilder *select();
QueryBuilder *where();
QueryBuilder *from();
QueryBuilder *insert();
QueryBuilder *values();
QueryBuilder *cvalues();
QueryBuilder *select(const std::string &params);
QueryBuilder *where(const std::string &params);
QueryBuilder *from(const std::string &params);
QueryBuilder *limit(const int min, const int max);
QueryBuilder *insert(const std::string &table_name, const std::string &params_str);
void finalize();
QueryBuilder *insert(const std::string &table_name);
QueryBuilder *values(const std::string &params_str);
MysqlQueryBuilder();
~MysqlQueryBuilder();
QueryBuilder *limit(const int min, const int max);
std::string escape(const std::string &params);
QueryBuilder *prepare();
QueryBuilder *set_param(const int index, const std::string &value);
QueryBuilder *set_param(const int index, const int value);
QueryBuilder *set_param(const int index, const float value);
void end_command();
QueryResult *run();
MysqlQueryBuilder();
~MysqlQueryBuilder();
MysqlDatabase *_db;
MysqlQueryResult *_result;
};
#endif

View File

@ -9,7 +9,7 @@
void MessagePage::index(Request *request) {
QueryBuilder *b = db->get_query_builder();
b->select("text")->from("message_page")->finalize();
b->select("text")->from("message_page")->end_command();
QueryResult *res = db->query(b->query_result);
@ -59,14 +59,14 @@ void MessagePage::migrate() {
QueryBuilder *b = db->get_query_builder();
b->insert("message_page", "default, 'aaewdwd'");
b->insert("message_page")->values("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'");
b->insert("message_page")->values("default, 'qqqqq'");
printf("%s\n", b->query_result.c_str());