Update sql statement for the query builder.

This commit is contained in:
Relintai 2021-08-20 12:11:27 +02:00
parent 3f02dc2f79
commit 5050f31ba0
5 changed files with 149 additions and 14 deletions

View File

@ -5,6 +5,9 @@
QueryBuilder *QueryBuilder::select() {
return this;
}
QueryBuilder *QueryBuilder::udpate() {
return this;
}
QueryBuilder *QueryBuilder::where() {
return this;
@ -25,7 +28,9 @@ QueryBuilder *QueryBuilder::cvalues() {
QueryBuilder *QueryBuilder::select(const std::string &params) {
return this;
}
QueryBuilder *QueryBuilder::udpate(const std::string &params) {
return this;
}
QueryBuilder *QueryBuilder::where(const std::string &params) {
return this;
}
@ -63,11 +68,35 @@ QueryBuilder *QueryBuilder::val(const bool param) {
return this;
}
QueryBuilder *QueryBuilder::set() {
return this;
}
QueryBuilder *QueryBuilder::cset() {
return this;
}
QueryBuilder *QueryBuilder::setp(const std::string &col, const std::string &param) {
return this;
}
QueryBuilder *QueryBuilder::setp(const std::string &col, const char *param) {
return this;
}
QueryBuilder *QueryBuilder::setp(const std::string &col, const int param) {
return this;
}
QueryBuilder *QueryBuilder::setp(const std::string &col, const bool param) {
return this;
}
QueryBuilder *QueryBuilder::eselect(const std::string &params) {
select(escape(params));
return this;
}
QueryBuilder *QueryBuilder::eudpate(const std::string &params) {
udpate(escape(params));
return this;
}
QueryBuilder *QueryBuilder::ewhere(const std::string &params) {
where(escape(params));
@ -94,7 +123,11 @@ QueryBuilder *QueryBuilder::eval(const std::string &param) {
return this;
}
QueryBuilder *QueryBuilder::esetp(const std::string &col, const std::string &escaped_param) {
setp(col, escape(escaped_param));
return this;
}
QueryBuilder *QueryBuilder::limit(const int num) {
return this;
}
@ -103,6 +136,13 @@ QueryBuilder *QueryBuilder::offset(const int num) {
return this;
}
QueryBuilder *QueryBuilder::w(const std::string &str) {
query_result += str + " ";
}
QueryBuilder *QueryBuilder::ew(const std::string &str) {
return w(escape(str));
}
QueryBuilder *QueryBuilder::select_last_insert_id() {
return this;
}

View File

@ -10,13 +10,16 @@ class QueryResult;
class QueryBuilder {
public:
virtual QueryBuilder *select();
virtual QueryBuilder *udpate();
virtual QueryBuilder *where();
virtual QueryBuilder *from();
virtual QueryBuilder *insert();
virtual QueryBuilder *values();
virtual QueryBuilder *cvalues();
virtual QueryBuilder *select(const std::string &params);
virtual QueryBuilder *udpate(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);
@ -28,16 +31,30 @@ public:
virtual QueryBuilder *val(const int param);
virtual QueryBuilder *val(const bool param);
virtual QueryBuilder *set();
virtual QueryBuilder *cset();
virtual QueryBuilder *setp(const std::string &col, const std::string &param);
virtual QueryBuilder *setp(const std::string &col, const char *param);
virtual QueryBuilder *setp(const std::string &col, const int param);
virtual QueryBuilder *setp(const std::string &col, const bool param);
virtual QueryBuilder *eselect(const std::string &params);
virtual QueryBuilder *eudpate(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 *limit(const int num);
virtual QueryBuilder *offset(const int num);
virtual QueryBuilder *w(const std::string &str);
virtual QueryBuilder *ew(const std::string &str);
virtual QueryBuilder *select_last_insert_id();
virtual std::string escape(const std::string &params);

View File

@ -10,6 +10,11 @@ QueryBuilder *SQLite3QueryBuilder::select() {
return this;
}
QueryBuilder *SQLite3QueryBuilder::udpate() {
query_result += "UPDATE ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::where() {
query_result += "WHERE ";
@ -42,7 +47,11 @@ QueryBuilder *SQLite3QueryBuilder::select(const std::string &params) {
return this;
}
QueryBuilder *SQLite3QueryBuilder::udpate(const std::string &params) {
query_result += "UPDATE " + params + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::where(const std::string &params) {
query_result += "WHERE " + params + " ";
@ -107,6 +116,56 @@ QueryBuilder *SQLite3QueryBuilder::val(const bool param) {
return this;
}
QueryBuilder *SQLite3QueryBuilder::set() {
query_result += "SET ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::cset() {
query_result[query_result.size() - 2] = ' ';
return this;
}
QueryBuilder *SQLite3QueryBuilder::setp(const std::string &col, const std::string &param) {
query_result += col + "='" + param + "', ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::setp(const std::string &col, const char *param) {
query_result += col + "='" + std::string(param) + "', ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::setp(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::setp(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 + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::offset(const int num) {
//query_result += "OFFSET " + num + " ";
return this;
}
std::string SQLite3QueryBuilder::escape(const std::string &params) {
if (!_db) {
printf("SQLite3QueryBuilder::escape !db!\n");
@ -155,18 +214,6 @@ void SQLite3QueryBuilder::run_query() {
_db->query_run(query_result);
}
QueryBuilder *SQLite3QueryBuilder::limit(const int num) {
//query_result += "LIMIT " + num + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::offset(const int num) {
//query_result += "OFFSET " + num + " ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::select_last_insert_id() {
return this;
}

View File

@ -11,13 +11,16 @@ class SQLite3Database;
class SQLite3QueryBuilder : public QueryBuilder {
public:
QueryBuilder *select();
QueryBuilder *udpate();
QueryBuilder *where();
QueryBuilder *from();
QueryBuilder *insert();
QueryBuilder *values();
QueryBuilder *cvalues();
QueryBuilder *select(const std::string &params);
QueryBuilder *udpate(const std::string &params);
QueryBuilder *where(const std::string &params);
QueryBuilder *from(const std::string &params);
QueryBuilder *insert(const std::string &table_name);
@ -29,6 +32,13 @@ public:
QueryBuilder *val(const int param);
QueryBuilder *val(const bool param);
QueryBuilder *set();
QueryBuilder *cset();
QueryBuilder *setp(const std::string &col, const std::string &param);
QueryBuilder *setp(const std::string &col, const char *param);
QueryBuilder *setp(const std::string &col, const int param);
QueryBuilder *setp(const std::string &col, const bool param);
QueryBuilder *limit(const int num);
QueryBuilder *offset(const int num);

View File

@ -33,7 +33,28 @@ void DBBasedUser::save() {
delete r;
} else {
//update
//todo better way
std::stringstream ss;
ss << id;
std::string uid = ss.str();
b->udpate(_table_name);
b->set();
b->esetp("username", name);
b->esetp("email", email);
b->setp("rank", rank);
b->setp("pre_salt", pre_salt);
b->setp("post_salt", post_salt);
b->setp("password_hash", password_hash);
b->setp("banned", banned);
b->setp("password_reset_token", password_reset_token);
b->setp("locked", locked);
b->cset();
b->where("id=" + uid);
//b->print();
b->run_query();
}
if (id == 0) {