From 3f308cd524f167b458ab2e90d9e595649b9d4bfd Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 19 Aug 2021 22:18:11 +0200 Subject: [PATCH] Added a query builder for sqlite. --- database/sqlite/detect.py | 2 +- database/sqlite/sqlite3_database.cpp | 10 +- database/sqlite/sqlite3_database.h | 2 + database/sqlite/sqlite3_query_builder.cpp | 134 ++++++++++++++++++++++ database/sqlite/sqlite3_query_builder.h | 47 ++++++++ 5 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 database/sqlite/sqlite3_query_builder.cpp create mode 100644 database/sqlite/sqlite3_query_builder.h diff --git a/database/sqlite/detect.py b/database/sqlite/detect.py index b18ec98..fddd6c2 100644 --- a/database/sqlite/detect.py +++ b/database/sqlite/detect.py @@ -8,7 +8,7 @@ def is_active(): def get_name(): - return "pgsql" + return "sqlite3" def can_build(): diff --git a/database/sqlite/sqlite3_database.cpp b/database/sqlite/sqlite3_database.cpp index 99ce81a..1528abc 100644 --- a/database/sqlite/sqlite3_database.cpp +++ b/database/sqlite/sqlite3_database.cpp @@ -3,6 +3,7 @@ #include "core/database/database_manager.h" #include "sqlite3_query_result.h" +#include "sqlite3_query_builder.h" Database *SQLite3Database::_creation_func() { return new SQLite3Database(); @@ -16,6 +17,13 @@ void SQLite3Database::_unregister() { DatabaseManager::_unregister_db_creation_func("sqlite"); } +QueryBuilder *SQLite3Database::get_query_builder() { + SQLite3QueryBuilder *b = new SQLite3QueryBuilder(); + b->_db = this; + + return b; +} + void SQLite3Database::connect(const std::string &connection_str) { int ret = sqlite3_config(SQLITE_CONFIG_MULTITHREAD); if (ret != SQLITE_OK) { @@ -63,7 +71,7 @@ void SQLite3Database::escape(const std::string str, std::string *to) { ret = sqlite3_mprintf("%q", str.c_str()); if (ret) { - to->operator=(ret); + to->operator=(ret); sqlite3_free(ret); } diff --git a/database/sqlite/sqlite3_database.h b/database/sqlite/sqlite3_database.h index cf1998d..e1a17dd 100644 --- a/database/sqlite/sqlite3_database.h +++ b/database/sqlite/sqlite3_database.h @@ -19,6 +19,8 @@ public: static void _register(); static void _unregister(); + QueryBuilder *get_query_builder(); + void connect(const std::string &connection_str); QueryResult *query(const std::string &query); void query_run(const std::string &query); diff --git a/database/sqlite/sqlite3_query_builder.cpp b/database/sqlite/sqlite3_query_builder.cpp new file mode 100644 index 0000000..fbc59da --- /dev/null +++ b/database/sqlite/sqlite3_query_builder.cpp @@ -0,0 +1,134 @@ +#include "sqlite3_query_builder.h" + +#include "sqlite3_database.h" +#include "sqlite3_query_result.h" + +QueryBuilder *SQLite3QueryBuilder::select() { + query_result += "SELECT "; + + return this; +} +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() { + query_result += ") "; + + return this; +} + +QueryBuilder *SQLite3QueryBuilder::select(const std::string ¶ms) { + query_result += "SELECT " + params + " "; + + return this; +} + +QueryBuilder *SQLite3QueryBuilder::where(const std::string ¶ms) { + query_result += "WHERE " + params + " "; + + return this; +} + +QueryBuilder *SQLite3QueryBuilder::from(const std::string ¶ms) { + query_result += "FROM " + params + " "; + + return this; +} + +QueryBuilder *SQLite3QueryBuilder::insert(const std::string &table_name) { + query_result += "INSERT INTO " + table_name + " "; + + return this; +} + +QueryBuilder *SQLite3QueryBuilder::values(const std::string ¶ms_str) { + query_result += "VALUES(" + params_str + ") "; + + return this; +} + +std::string SQLite3QueryBuilder::escape(const std::string ¶ms) { + if (!_db) { + printf("SQLite3QueryBuilder::escape !db!\n"); + + return ""; + } + + return _db->escape(params); +} + +QueryBuilder *SQLite3QueryBuilder::prepare() { + return this; +} + +QueryBuilder *SQLite3QueryBuilder::set_param(const int index, const std::string &value) { + 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; +} + +void SQLite3QueryBuilder::end_command() { + query_result += ";"; +} + +QueryResult *SQLite3QueryBuilder::run() { + end_command(); + + if (!_db) { + printf("SQLite3QueryBuilder::run !db!\n"); + + return nullptr; + } + + return _db->query(query_result); +} + +void SQLite3QueryBuilder::run_query() { + end_command(); + + if (!_db) { + printf("SQLite3QueryBuilder::run_query !db!\n"); + + return; + } + + _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; +} + +SQLite3QueryBuilder::SQLite3QueryBuilder() { +} +SQLite3QueryBuilder::~SQLite3QueryBuilder() { +} \ No newline at end of file diff --git a/database/sqlite/sqlite3_query_builder.h b/database/sqlite/sqlite3_query_builder.h new file mode 100644 index 0000000..116bb7f --- /dev/null +++ b/database/sqlite/sqlite3_query_builder.h @@ -0,0 +1,47 @@ +#ifndef SQLITE3_QUERY_BUILDER_H +#define SQLITE3_QUERY_BUILDER_H + +#include +#include + +#include "core/database/query_builder.h" + +class SQLite3Database; + +class SQLite3QueryBuilder : public QueryBuilder { +public: + QueryBuilder *select(); + QueryBuilder *where(); + QueryBuilder *from(); + QueryBuilder *insert(); + QueryBuilder *values(); + QueryBuilder *cvalues(); + + QueryBuilder *select(const std::string ¶ms); + QueryBuilder *where(const std::string ¶ms); + QueryBuilder *from(const std::string ¶ms); + QueryBuilder *insert(const std::string &table_name); + QueryBuilder *values(const std::string ¶ms_str); + + QueryBuilder *limit(const int num); + QueryBuilder *offset(const int num); + + std::string escape(const std::string ¶ms); + + QueryBuilder *prepare(); + QueryBuilder *set_param(const int index, const std::string &value); + QueryBuilder *set_param(const int index, const int value); + QueryBuilder *set_param(const int index, const float value); + + void end_command(); + + QueryResult *run(); + void run_query(); + + SQLite3QueryBuilder(); + ~SQLite3QueryBuilder(); + + SQLite3Database *_db; +}; + +#endif \ No newline at end of file