2020-12-01 22:23:28 +01:00
|
|
|
#include "mysql_query_builder.h"
|
2020-12-01 21:48:13 +01:00
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
#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;
|
|
|
|
}
|
2020-12-01 22:23:28 +01:00
|
|
|
|
|
|
|
QueryBuilder *MysqlQueryBuilder::select(const std::string ¶ms) {
|
2021-08-19 14:12:02 +02:00
|
|
|
query_result += "SELECT " + params + " ";
|
2020-12-01 21:48:13 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *MysqlQueryBuilder::where(const std::string ¶ms) {
|
2021-08-19 14:12:02 +02:00
|
|
|
query_result += "WHERE " + params + " ";
|
2020-12-01 21:48:13 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *MysqlQueryBuilder::from(const std::string ¶ms) {
|
2021-08-19 14:12:02 +02:00
|
|
|
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 ¶ms_str) {
|
|
|
|
query_result += "VALUES(" + params_str + ") ";
|
2020-12-01 21:48:13 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
std::string MysqlQueryBuilder::escape(const std::string ¶ms) {
|
|
|
|
if (!_db) {
|
|
|
|
printf("MysqlQueryBuilder::escape !db!\n");
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db->escape(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *MysqlQueryBuilder::prepare() {
|
|
|
|
return this;
|
|
|
|
}
|
2020-12-02 00:43:46 +01:00
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
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) {
|
2020-12-01 21:48:13 +01:00
|
|
|
return this;
|
2020-12-01 22:23:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
void MysqlQueryBuilder::end_command() {
|
2020-12-01 22:23:28 +01:00
|
|
|
query_result += ";";
|
|
|
|
}
|
2020-12-01 21:48:13 +01:00
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
QueryResult *MysqlQueryBuilder::run() {
|
2021-08-19 22:03:22 +02:00
|
|
|
end_command();
|
|
|
|
|
|
|
|
if (!_db) {
|
|
|
|
printf("MysqlQueryBuilder::run !db!\n");
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:07:31 +02:00
|
|
|
return _db->query(query_result);
|
2021-08-19 14:12:02 +02:00
|
|
|
}
|
2020-12-01 21:48:13 +01:00
|
|
|
|
2021-08-19 22:03:22 +02:00
|
|
|
void MysqlQueryBuilder::run_query() {
|
|
|
|
end_command();
|
|
|
|
|
|
|
|
if (!_db) {
|
|
|
|
printf("MysqlQueryBuilder::run_query !db!\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:07:31 +02:00
|
|
|
_db->query_run(query_result);
|
2021-08-19 22:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *MysqlQueryBuilder::limit(const int num) {
|
2021-08-19 22:07:31 +02:00
|
|
|
//query_result += "LIMIT " + num + " ";
|
2021-08-19 22:03:22 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:07:31 +02:00
|
|
|
QueryBuilder *MysqlQueryBuilder::offset(const int num) {
|
|
|
|
//query_result += "OFFSET " + num + " ";
|
2021-08-19 22:03:22 +02:00
|
|
|
|
2021-08-19 14:12:02 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MysqlQueryBuilder::MysqlQueryBuilder() {
|
2020-12-01 21:48:13 +01:00
|
|
|
}
|
|
|
|
MysqlQueryBuilder::~MysqlQueryBuilder() {
|
2020-12-01 22:23:28 +01:00
|
|
|
}
|