2021-08-19 22:18:11 +02:00
|
|
|
#include "sqlite3_query_builder.h"
|
|
|
|
|
|
|
|
#include "sqlite3_database.h"
|
|
|
|
#include "sqlite3_query_result.h"
|
|
|
|
|
2021-08-20 01:32:43 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2021-08-19 22:18:11 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::select() {
|
|
|
|
query_result += "SELECT ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:00:55 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::update() {
|
2021-08-20 12:11:27 +02:00
|
|
|
query_result += "UPDATE ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-08-20 12:49:57 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::del() {
|
|
|
|
query_result += "DELETE FROM ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:18:11 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::where() {
|
|
|
|
query_result += "WHERE ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::from() {
|
|
|
|
query_result += "FROM ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::insert() {
|
|
|
|
query_result += "INSERT INTO ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::values() {
|
|
|
|
query_result += "VALUES(";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::cvalues() {
|
2021-08-20 01:32:43 +02:00
|
|
|
query_result[query_result.size() - 2] = ' ';
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-14 19:22:49 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::next_value() {
|
|
|
|
query_result[query_result.size() - 2] = ' ';
|
|
|
|
query_result += "), (";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::begin_transaction() {
|
|
|
|
query_result += "BEGIN TRANSACTION;";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::commit() {
|
|
|
|
query_result += "COMMIT;";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-08-19 22:18:11 +02:00
|
|
|
|
2021-08-22 21:44:20 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::str() {
|
|
|
|
query_result += "'";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::cstr() {
|
|
|
|
query_result += "'";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::like() {
|
|
|
|
query_result += "LIKE ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nselect(const String ¶ms) {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += "SELECT " + params + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nupdate(const String ¶ms) {
|
2021-08-20 12:11:27 +02:00
|
|
|
query_result += "UPDATE " + params + " ";
|
2021-08-19 22:18:11 +02:00
|
|
|
|
2021-08-20 12:11:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::ndel(const String ¶ms) {
|
2021-08-20 12:49:57 +02:00
|
|
|
query_result += "DELETE FROM " + params + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nwhere(const String ¶ms) {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += "WHERE " + params + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nfrom(const String ¶ms) {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += "FROM " + params + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::insert(const String &table_name) {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += "INSERT INTO " + table_name + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::insert(const String &table_name, const String &columns) {
|
2021-08-20 01:32:43 +02:00
|
|
|
query_result += "INSERT INTO " + table_name + "(" + columns + ") ";
|
2021-08-19 22:18:11 +02:00
|
|
|
|
2021-08-20 01:32:43 +02:00
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nvalues(const String ¶ms_str) {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += "VALUES(" + params_str + ") ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:32:43 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::val() {
|
|
|
|
//query_result += "DEFAULT, ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nval(const String ¶m) {
|
2021-08-20 01:32:43 +02:00
|
|
|
query_result += "'" + param + "', ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::val(const char *param) {
|
2021-11-01 17:31:04 +01:00
|
|
|
query_result += "'" + String(param) + "', ";
|
2021-08-20 01:32:43 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::val(const int param) {
|
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::val(const bool param) {
|
|
|
|
if (param)
|
|
|
|
query_result += "1, ";
|
|
|
|
else
|
|
|
|
query_result += "0, ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-14 21:04:48 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::valf(const float param) {
|
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::vald(const double param) {
|
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nlike(const String &str) {
|
2021-08-22 21:44:20 +02:00
|
|
|
query_result += "LIKE '" + str + "' ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:11:27 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::set() {
|
|
|
|
query_result += "SET ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::cset() {
|
|
|
|
query_result[query_result.size() - 2] = ' ';
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nsetp(const String &col, const String ¶m) {
|
2021-08-20 12:11:27 +02:00
|
|
|
query_result += col + "='" + param + "', ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const char *param) {
|
|
|
|
query_result += col + "='" + String(param) + "', ";
|
2021-08-20 12:11:27 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const int param) {
|
2021-08-20 12:11:27 +02:00
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += col + "=" + ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::setp(const String &col, const bool param) {
|
2021-08-20 12:11:27 +02:00
|
|
|
if (param)
|
|
|
|
query_result += col + "=1, ";
|
|
|
|
else
|
|
|
|
query_result += col + "=0, ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-14 21:04:48 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::setpf(const String &col, const float param) {
|
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += col + "=" + ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::setpd(const String &col, const double param) {
|
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += col + "=" + ss.str() + ", ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-08-20 12:11:27 +02:00
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::nwp(const String &col, const String ¶m) {
|
2021-08-22 22:32:18 +02:00
|
|
|
query_result += col + "='" + param + "' ";
|
2021-08-20 12:49:57 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const char *param) {
|
|
|
|
query_result += col + "='" + String(param) + "' ";
|
2021-08-20 12:49:57 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const int param) {
|
2021-08-20 12:49:57 +02:00
|
|
|
//todo add a better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << param;
|
|
|
|
|
|
|
|
query_result += col + "=" + ss.str() + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::wp(const String &col, const bool param) {
|
2021-08-20 12:49:57 +02:00
|
|
|
if (param)
|
|
|
|
query_result += col + "=1 ";
|
|
|
|
else
|
|
|
|
query_result += col + "=0 ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:11:27 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::limit(const int num) {
|
2021-08-20 13:14:06 +02:00
|
|
|
//todo better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << num;
|
|
|
|
|
|
|
|
query_result += "LIMIT " + ss.str() + " ";
|
2021-08-20 12:11:27 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::offset(const int num) {
|
2021-08-20 13:14:06 +02:00
|
|
|
//todo better way
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << num;
|
|
|
|
|
|
|
|
query_result += "OFFSET " + ss.str() + " ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::land() {
|
|
|
|
query_result += "AND ";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::lor() {
|
|
|
|
query_result += "OR ";
|
2021-08-20 12:11:27 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:44:20 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::wildcard() {
|
|
|
|
query_result += "%";
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
String SQLite3QueryBuilder::escape(const String ¶ms) {
|
2021-08-19 22:18:11 +02:00
|
|
|
if (!_db) {
|
|
|
|
printf("SQLite3QueryBuilder::escape !db!\n");
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db->escape(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::prepare() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:04 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const String &value) {
|
2021-08-19 22:18:11 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const int value) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const float value) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:22:49 +01:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::end_command() {
|
2021-08-19 22:18:11 +02:00
|
|
|
query_result += ";";
|
2021-11-14 19:22:49 +01:00
|
|
|
|
|
|
|
return this;
|
2021-08-19 22:18:11 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:57:22 +01:00
|
|
|
Ref<QueryResult> SQLite3QueryBuilder::run() {
|
2021-08-19 22:18:11 +02:00
|
|
|
if (!_db) {
|
|
|
|
printf("SQLite3QueryBuilder::run !db!\n");
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _db->query(query_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SQLite3QueryBuilder::run_query() {
|
|
|
|
if (!_db) {
|
|
|
|
printf("SQLite3QueryBuilder::run_query !db!\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_db->query_run(query_result);
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:32:43 +02:00
|
|
|
QueryBuilder *SQLite3QueryBuilder::select_last_insert_id() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:18:11 +02:00
|
|
|
SQLite3QueryBuilder::SQLite3QueryBuilder() {
|
|
|
|
}
|
|
|
|
SQLite3QueryBuilder::~SQLite3QueryBuilder() {
|
|
|
|
}
|