Lost of work on the query/table builders.

This commit is contained in:
Relintai 2021-08-20 01:32:43 +02:00
parent e7c1cd5741
commit 2c5293f959
9 changed files with 145 additions and 27 deletions

View File

@ -37,10 +37,32 @@ QueryBuilder *QueryBuilder::from(const std::string &params) {
QueryBuilder *QueryBuilder::insert(const std::string &table_name) {
return this;
}
QueryBuilder *QueryBuilder::insert(const std::string &table_name, const std::string &columns) {
return this;
}
QueryBuilder *QueryBuilder::values(const std::string &params_str) {
return this;
}
QueryBuilder *QueryBuilder::val() {
return this;
}
QueryBuilder *QueryBuilder::val(const std::string &param) {
return this;
}
QueryBuilder *QueryBuilder::val(const char *param) {
return this;
}
QueryBuilder *QueryBuilder::val(const int param) {
return this;
}
QueryBuilder *QueryBuilder::val(const bool param) {
return this;
}
QueryBuilder *QueryBuilder::eselect(const std::string &params) {
select(escape(params));
@ -67,6 +89,12 @@ QueryBuilder *QueryBuilder::evalues(const std::string &params_str) {
return this;
}
QueryBuilder *QueryBuilder::eval(const std::string &param) {
val(escape(param));
return this;
}
QueryBuilder *QueryBuilder::limit(const int num) {
return this;
}
@ -75,6 +103,10 @@ QueryBuilder *QueryBuilder::offset(const int num) {
return this;
}
QueryBuilder *QueryBuilder::select_last_insert_id() {
return this;
}
std::string QueryBuilder::escape(const std::string &params) {
return params;
}
@ -108,6 +140,10 @@ std::string QueryBuilder::get_result() {
return query_result;
}
void QueryBuilder::print() {
printf("%s\n", query_result.c_str());
}
QueryBuilder::QueryBuilder() {
}

View File

@ -20,17 +20,26 @@ public:
virtual QueryBuilder *where(const std::string &params);
virtual QueryBuilder *from(const std::string &params);
virtual QueryBuilder *insert(const std::string &table_name);
virtual QueryBuilder *insert(const std::string &table_name, const std::string &columns);
virtual QueryBuilder *values(const std::string &params_str);
virtual QueryBuilder *val();
virtual QueryBuilder *val(const std::string &param);
virtual QueryBuilder *val(const char *param);
virtual QueryBuilder *val(const int param);
virtual QueryBuilder *val(const bool param);
virtual QueryBuilder *eselect(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);
virtual QueryBuilder *limit(const int num);
virtual QueryBuilder *offset(const int num);
virtual QueryBuilder *select_last_insert_id();
virtual std::string escape(const std::string &params);
virtual QueryBuilder *prepare();
@ -45,6 +54,8 @@ public:
std::string get_result();
void print();
QueryBuilder();
virtual ~QueryBuilder();

View File

@ -8,6 +8,10 @@ const char *QueryResult::get_cell(const int index) {
return "";
}
int QueryResult::get_last_insert_rowid() {
return 0;
}
QueryResult::QueryResult() {
}

View File

@ -5,6 +5,7 @@ class QueryResult {
public:
virtual bool next_row();
virtual const char*get_cell(const int index);
virtual int get_last_insert_rowid();
QueryResult();
virtual ~QueryResult();

View File

@ -3,6 +3,8 @@
#include "sqlite3_database.h"
#include "sqlite3_query_result.h"
#include <sstream>
QueryBuilder *SQLite3QueryBuilder::select() {
query_result += "SELECT ";
@ -29,6 +31,7 @@ QueryBuilder *SQLite3QueryBuilder::values() {
return this;
}
QueryBuilder *SQLite3QueryBuilder::cvalues() {
query_result[query_result.size() - 2] = ' ';
query_result += ") ";
return this;
@ -57,13 +60,53 @@ QueryBuilder *SQLite3QueryBuilder::insert(const std::string &table_name) {
return this;
}
QueryBuilder *SQLite3QueryBuilder::insert(const std::string &table_name, const std::string &columns) {
query_result += "INSERT INTO " + table_name + "(" + columns + ") ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::values(const std::string &params_str) {
query_result += "VALUES(" + params_str + ") ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::val() {
//query_result += "DEFAULT, ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::val(const std::string &param) {
query_result += "'" + param + "', ";
return this;
}
QueryBuilder *SQLite3QueryBuilder::val(const char *param) {
query_result += "'" + std::string(param) + "', ";
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;
}
std::string SQLite3QueryBuilder::escape(const std::string &params) {
if (!_db) {
printf("SQLite3QueryBuilder::escape !db!\n");
@ -93,8 +136,6 @@ void SQLite3QueryBuilder::end_command() {
}
QueryResult *SQLite3QueryBuilder::run() {
end_command();
if (!_db) {
printf("SQLite3QueryBuilder::run !db!\n");
@ -105,8 +146,6 @@ QueryResult *SQLite3QueryBuilder::run() {
}
void SQLite3QueryBuilder::run_query() {
end_command();
if (!_db) {
printf("SQLite3QueryBuilder::run_query !db!\n");
@ -128,6 +167,10 @@ QueryBuilder *SQLite3QueryBuilder::offset(const int num) {
return this;
}
QueryBuilder *SQLite3QueryBuilder::select_last_insert_id() {
return this;
}
SQLite3QueryBuilder::SQLite3QueryBuilder() {
}
SQLite3QueryBuilder::~SQLite3QueryBuilder() {

View File

@ -21,11 +21,19 @@ public:
QueryBuilder *where(const std::string &params);
QueryBuilder *from(const std::string &params);
QueryBuilder *insert(const std::string &table_name);
QueryBuilder *insert(const std::string &table_name, const std::string &columns);
QueryBuilder *values(const std::string &params_str);
QueryBuilder *val();
QueryBuilder *val(const std::string &param);
QueryBuilder *val(const char *param);
QueryBuilder *val(const int param);
QueryBuilder *val(const bool param);
QueryBuilder *limit(const int num);
QueryBuilder *offset(const int num);
QueryBuilder *select_last_insert_id();
std::string escape(const std::string &params);
QueryBuilder *prepare();

View File

@ -3,43 +3,55 @@
#include <cstdio>
bool Sqlite3QueryResult::next_row() {
return ++current_row < rows.size();
return ++current_row < rows.size();
}
const char* Sqlite3QueryResult::get_cell(const int index) {
return rows[current_row]->cells[index].c_str();
const char *Sqlite3QueryResult::get_cell(const int index) {
return rows[current_row]->cells[index].c_str();
}
int Sqlite3QueryResult::get_last_insert_rowid() {
return sqlite3_last_insert_rowid(_connection);
}
void Sqlite3QueryResult::query(const std::string &query, sqlite3 *conn) {
if (sqlite3_exec(conn, query.c_str(), Sqlite3QueryResult::run_query_finished, this, &err_msg) != SQLITE_OK) {
printf("SQLite3Database::query error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
sqlite3_free(err_msg);
}
_connection = conn;
if (sqlite3_exec(conn, query.c_str(), Sqlite3QueryResult::run_query_finished, this, &err_msg) != SQLITE_OK) {
printf("SQLite3Database::query error: \nQuery: %s \n Error:\n %s\n", query.c_str(), err_msg);
sqlite3_free(err_msg);
}
}
int Sqlite3QueryResult::run_query_finished(void *data, int argc, char **argv, char **col_names) {
Sqlite3QueryResult *res = reinterpret_cast<Sqlite3QueryResult*>(data);
Sqlite3QueryResult *res = reinterpret_cast<Sqlite3QueryResult *>(data);
//res->col_names = col_names;
//res->col_names = col_names;
Sqlite3QueryResultRow *r = new Sqlite3QueryResultRow();
Sqlite3QueryResultRow *r = new Sqlite3QueryResultRow();
for (int i = 0; i < argc; ++i) {
r->cells.push_back(argv[i]);
}
res->rows.push_back(r);
for (int i = 0; i < argc; ++i) {
if (argv[i]) {
r->cells.push_back(argv[i]);
} else {
//todo mark nulls in cells
r->cells.push_back("NULL");
}
}
return 0;
res->rows.push_back(r);
return 0;
}
Sqlite3QueryResult::Sqlite3QueryResult() : QueryResult() {
err_msg = nullptr;
current_row = -1;
Sqlite3QueryResult::Sqlite3QueryResult() :
QueryResult() {
err_msg = nullptr;
current_row = -1;
}
Sqlite3QueryResult::~Sqlite3QueryResult() {
for (int i = 0; i < rows.size(); ++i) {
delete rows[i];
}
for (int i = 0; i < rows.size(); ++i) {
delete rows[i];
}
}

View File

@ -12,6 +12,7 @@ class Sqlite3QueryResult : public QueryResult {
public:
bool next_row();
const char* get_cell(const int index);
int get_last_insert_rowid();
void query(const std::string &query, sqlite3 *conn);
@ -30,6 +31,8 @@ public:
char **col_names;
std::vector<Sqlite3QueryResultRow *> rows;
int current_row;
sqlite3 *_connection;
};
#endif

View File

@ -39,7 +39,7 @@ TableBuilder *SQLite3TableBuilder::null() {
}
TableBuilder *SQLite3TableBuilder::auto_increment() {
result += "AUTO_INCREMENT ";
//result += "AUTO_INCREMENT ";
return this;
}