More work on the query builders.

This commit is contained in:
Relintai 2021-08-19 22:03:22 +02:00
parent 8de082ffba
commit 8ebb648d37
5 changed files with 48 additions and 7 deletions

View File

@ -67,7 +67,11 @@ QueryBuilder *QueryBuilder::evalues(const std::string &params_str) {
return this;
}
QueryBuilder *QueryBuilder::limit(const int min, const int max) {
QueryBuilder *QueryBuilder::limit(const int num) {
return this;
}
QueryBuilder *QueryBuilder::offset(const int num) {
return this;
}
@ -95,6 +99,9 @@ QueryResult *QueryBuilder::run() {
return nullptr;
}
void QueryBuilder::run_query() {
}
std::string QueryBuilder::get_result() {
end_command();

View File

@ -28,7 +28,8 @@ public:
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 QueryBuilder *limit(const int num);
virtual QueryBuilder *offset(const int num);
virtual std::string escape(const std::string &params);
@ -40,6 +41,7 @@ public:
virtual void end_command();
virtual QueryResult *run();
virtual void run_query();
std::string get_result();

View File

@ -88,7 +88,10 @@ void MysqlDatabase::query_run(const std::string &query) {
}
QueryBuilder *MysqlDatabase::get_query_builder() {
return new MysqlQueryBuilder();
MysqlQueryBuilder *b = new MysqlQueryBuilder();
b->_db = this;
return b;
}
TableBuilder *MysqlDatabase::get_table_builder() {

View File

@ -98,10 +98,38 @@ void MysqlQueryBuilder::end_command() {
}
QueryResult *MysqlQueryBuilder::run() {
return nullptr;
end_command();
if (!_db) {
printf("MysqlQueryBuilder::run !db!\n");
return nullptr;
}
return _db->query(query_result)
}
QueryBuilder *MysqlQueryBuilder::limit(const int min, const int max) {
void MysqlQueryBuilder::run_query() {
end_command();
if (!_db) {
printf("MysqlQueryBuilder::run_query !db!\n");
return;
}
_db->query_run(query_result)
}
QueryBuilder *MysqlQueryBuilder::limit(const int num) {
query_result += "LIMIT " + num + " ";
return this;
}
virtual QueryBuilder *offset(const int num) {
query_result += "OFFSET " + num + " ";
return this;
}

View File

@ -24,7 +24,8 @@ public:
QueryBuilder *insert(const std::string &table_name);
QueryBuilder *values(const std::string &params_str);
QueryBuilder *limit(const int min, const int max);
QueryBuilder *limit(const int num);
QueryBuilder *offset(const int num);
std::string escape(const std::string &params);
@ -36,12 +37,12 @@ public:
void end_command();
QueryResult *run();
void run_query();
MysqlQueryBuilder();
~MysqlQueryBuilder();
MysqlDatabase *_db;
MysqlQueryResult *_result;
};
#endif