More work on the query builder.

This commit is contained in:
Relintai 2021-08-20 12:49:57 +02:00
parent 5050f31ba0
commit af05bd037c
5 changed files with 131 additions and 44 deletions

View File

@ -8,6 +8,10 @@ QueryBuilder *QueryBuilder::select() {
QueryBuilder *QueryBuilder::udpate() {
return this;
}
QueryBuilder *QueryBuilder::del() {
return this;
}
QueryBuilder *QueryBuilder::where() {
return this;
@ -31,6 +35,9 @@ QueryBuilder *QueryBuilder::select(const std::string &params) {
QueryBuilder *QueryBuilder::udpate(const std::string &params) {
return this;
}
QueryBuilder *QueryBuilder::del(const std::string &params) {
return this;
}
QueryBuilder *QueryBuilder::where(const std::string &params) {
return this;
}
@ -87,47 +94,55 @@ QueryBuilder *QueryBuilder::setp(const std::string &col, const bool param) {
return this;
}
QueryBuilder *QueryBuilder::eselect(const std::string &params) {
select(escape(params));
QueryBuilder *QueryBuilder::wp(const std::string &col, const std::string &param) {
return this;
}
QueryBuilder *QueryBuilder::wp(const std::string &col, const char *param) {
return this;
}
QueryBuilder *QueryBuilder::wp(const std::string &col, const int param) {
return this;
}
QueryBuilder *QueryBuilder::wp(const std::string &col, const bool param) {
return this;
}
QueryBuilder *QueryBuilder::eselect(const std::string &params) {
return select(escape(params));
}
QueryBuilder *QueryBuilder::eudpate(const std::string &params) {
udpate(escape(params));
return this;
return udpate(escape(params));
}
QueryBuilder *QueryBuilder::edel(const std::string &params) {
return del(escape(params));
}
QueryBuilder *QueryBuilder::ewhere(const std::string &params) {
where(escape(params));
return this;
QueryBuilder *QueryBuilder::ewhere(const std::string &params) {
return where(escape(params));
}
QueryBuilder *QueryBuilder::efrom(const std::string &params) {
from(escape(params));
return this;
return from(escape(params));
}
QueryBuilder *QueryBuilder::einsert(const std::string &table_name) {
insert(escape(table_name));
return insert(escape(table_name));
return this;
}
QueryBuilder *QueryBuilder::evalues(const std::string &params_str) {
values(escape(params_str));
return this;
return values(escape(params_str));
}
QueryBuilder *QueryBuilder::eval(const std::string &param) {
val(escape(param));
return this;
return val(escape(param));
}
QueryBuilder *QueryBuilder::esetp(const std::string &col, const std::string &escaped_param) {
setp(col, escape(escaped_param));
return this;
QueryBuilder *QueryBuilder::esetp(const std::string &col, const std::string &escape_param) {
return setp(col, escape(escape_param));
}
QueryBuilder *QueryBuilder::ewp(const std::string &col, const std::string &escape_param) {
return wp(col, escape(escape_param));
}
QueryBuilder *QueryBuilder::limit(const int num) {
return this;
}
@ -138,6 +153,8 @@ QueryBuilder *QueryBuilder::offset(const int num) {
QueryBuilder *QueryBuilder::w(const std::string &str) {
query_result += str + " ";
return this;
}
QueryBuilder *QueryBuilder::ew(const std::string &str) {
return w(escape(str));
@ -167,6 +184,12 @@ QueryBuilder *QueryBuilder::set_param(const int index, const float value) {
void QueryBuilder::end_command() {
}
QueryBuilder *QueryBuilder::reset() {
query_result = "";
return this;
}
QueryResult *QueryBuilder::run() {
return nullptr;
}

View File

@ -11,8 +11,9 @@ class QueryBuilder {
public:
virtual QueryBuilder *select();
virtual QueryBuilder *udpate();
virtual QueryBuilder *where();
virtual QueryBuilder *del();
virtual QueryBuilder *where();
virtual QueryBuilder *from();
virtual QueryBuilder *insert();
virtual QueryBuilder *values();
@ -20,6 +21,8 @@ public:
virtual QueryBuilder *select(const std::string &params);
virtual QueryBuilder *udpate(const std::string &params);
virtual QueryBuilder *del(const std::string &params);
virtual QueryBuilder *where(const std::string &params);
virtual QueryBuilder *from(const std::string &params);
virtual QueryBuilder *insert(const std::string &table_name);
@ -39,15 +42,24 @@ public:
virtual QueryBuilder *setp(const std::string &col, const int param);
virtual QueryBuilder *setp(const std::string &col, const bool param);
virtual QueryBuilder *wp(const std::string &col, const std::string &param);
virtual QueryBuilder *wp(const std::string &col, const char *param);
virtual QueryBuilder *wp(const std::string &col, const int param);
virtual QueryBuilder *wp(const std::string &col, const bool param);
virtual QueryBuilder *eselect(const std::string &params);
virtual QueryBuilder *eudpate(const std::string &params);
virtual QueryBuilder *edel(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 *eval(const std::string &param);
//note col is NOT escaped
virtual QueryBuilder *esetp(const std::string &col, const std::string &escaped_param);
virtual QueryBuilder *esetp(const std::string &col, const std::string &escape_param);
//note col is NOT escaped
virtual QueryBuilder *ewp(const std::string &col, const std::string &escape_param);
virtual QueryBuilder *limit(const int num);
virtual QueryBuilder *offset(const int num);
@ -66,6 +78,8 @@ public:
virtual void end_command();
virtual QueryBuilder *reset();
virtual QueryResult *run();
virtual void run_query();

View File

@ -15,6 +15,12 @@ QueryBuilder *SQLite3QueryBuilder::udpate() {
return this;
}
QueryBuilder *SQLite3QueryBuilder::del() {
query_result += "DELETE FROM ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::where() {
query_result += "WHERE ";
@ -52,6 +58,12 @@ QueryBuilder *SQLite3QueryBuilder::udpate(const std::string &params) {
return this;
}
QueryBuilder *SQLite3QueryBuilder::del(const std::string &params) {
query_result += "DELETE FROM " + params + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::where(const std::string &params) {
query_result += "WHERE " + params + " ";
@ -154,6 +166,34 @@ QueryBuilder *SQLite3QueryBuilder::setp(const std::string &col, const bool param
return this;
}
QueryBuilder *SQLite3QueryBuilder::wp(const std::string &col, const std::string &param) {
query_result += col + "=" + param + "' ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::wp(const std::string &col, const char *param) {
query_result += col + "=" + std::string(param) + "' ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::wp(const std::string &col, const int param) {
//todo add a better way
std::stringstream ss;
ss << param;
query_result += col + "=" + ss.str() + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::wp(const std::string &col, const bool param) {
if (param)
query_result += col + "=1 ";
else
query_result += col + "=0 ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::limit(const int num) {
//query_result += "LIMIT " + num + " ";

View File

@ -12,8 +12,9 @@ class SQLite3QueryBuilder : public QueryBuilder {
public:
QueryBuilder *select();
QueryBuilder *udpate();
QueryBuilder *where();
QueryBuilder *del();
QueryBuilder *where();
QueryBuilder *from();
QueryBuilder *insert();
QueryBuilder *values();
@ -21,6 +22,8 @@ public:
QueryBuilder *select(const std::string &params);
QueryBuilder *udpate(const std::string &params);
QueryBuilder *del(const std::string &params);
QueryBuilder *where(const std::string &params);
QueryBuilder *from(const std::string &params);
QueryBuilder *insert(const std::string &table_name);
@ -39,6 +42,11 @@ public:
QueryBuilder *setp(const std::string &col, const int param);
QueryBuilder *setp(const std::string &col, const bool param);
QueryBuilder *wp(const std::string &col, const std::string &param);
QueryBuilder *wp(const std::string &col, const char *param);
QueryBuilder *wp(const std::string &col, const int param);
QueryBuilder *wp(const std::string &col, const bool param);
QueryBuilder *limit(const int num);
QueryBuilder *offset(const int num);

View File

@ -12,7 +12,9 @@ void DBBasedUser::save() {
QueryBuilder *b = DatabaseManager::get_singleton()->ddb->get_query_builder();
if (id == 0) {
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked")->values();
b->insert(_table_name, "username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->values();
b->eval(name);
b->eval(email);
b->val(rank);
@ -22,8 +24,9 @@ void DBBasedUser::save() {
b->val(banned);
b->val(password_reset_token);
b->val(locked);
b->cvalues()->end_command();
b->cvalues();
b->end_command();
b->select_last_insert_id();
QueryResult *r = b->run();
@ -50,7 +53,7 @@ void DBBasedUser::save() {
b->setp("password_reset_token", password_reset_token);
b->setp("locked", locked);
b->cset();
b->where("id=" + uid);
b->where()->wp("id", id);
//b->print();
@ -61,22 +64,22 @@ void DBBasedUser::save() {
return;
}
//todo better way
std::stringstream ss;
ss << id;
std::string uid = ss.str();
b->reset();
b->del(_table_name + "_sessions")->where()->wp("user_id", id)->end_command();
//b->print();
//todo
b->query_result = "DELETE FROM " + _table_name + "_sessions WHERE user_id=" + uid;
b->end_command();
b->run_query();
b->query_result = "";
b->reset();
for (int i = 0; i < sessions.size(); ++i) {
b->query_result += "INSERT INTO " + _table_name + "_sessions VALUES(" + uid + ", '" + sessions[i] + "');";
b->insert(_table_name + "_sessions")->values()->val(id)->val(sessions[i])->cvalues()->end_command();
}
//b->print();
b->run_query();
delete b;
@ -94,14 +97,13 @@ void DBBasedUser::load() {
b->select("username, email, rank, pre_salt, post_salt, password_hash, banned, password_reset_token, locked");
b->from(_table_name);
//todo better way
std::stringstream ss;
ss << id;
std::string uid = ss.str();
b->where("id=" + uid);
b->where()->wp("id", id);
b->end_command();
//todo get_cell with types
std::stringstream ss;
QueryResult *r = b->run();
if (r->next_row()) {
@ -132,7 +134,7 @@ void DBBasedUser::load() {
b->select("session_id");
b->from(_table_name + "_sessions");
b->where("user_id=" + uid);
b->where()->wp("user_id", id);
b->end_command();
r = b->run();