From 266263976d1aaea2132ab9c50372403b4bf92e7b Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Apr 2024 18:37:56 +0200 Subject: [PATCH] Added virtual methods to QueryBuilder for adding prepared statement placeholders. --- modules/database/query_builder.cpp | 29 ++++++++++++++++++- modules/database/query_builder.h | 15 ++++++---- .../database_sqlite/sqlite3_query_builder.cpp | 23 ++++++++++++++- .../database_sqlite/sqlite3_query_builder.h | 5 ++++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/modules/database/query_builder.cpp b/modules/database/query_builder.cpp index ee516f5fa..3ab876a00 100644 --- a/modules/database/query_builder.cpp +++ b/modules/database/query_builder.cpp @@ -31,8 +31,8 @@ #include "query_builder.h" -#include "query_result.h" #include "prepared_statement.h" +#include "query_result.h" String QueryBuilder::get_result() { return query_result; @@ -274,6 +274,17 @@ QueryBuilder *QueryBuilder::wildcard() { return this; } +// Prepared statement placeholders +QueryBuilder *QueryBuilder::psph() { + return this; +} +QueryBuilder *QueryBuilder::psphi(const String &p_id) { + return this; +} +QueryBuilder *QueryBuilder::psphr(const String &p_raw_id) { + return this; +} + QueryBuilder *QueryBuilder::w(const String &str) { query_result += str; @@ -396,6 +407,11 @@ void QueryBuilder::_bind_methods() { ClassDB::bind_method(D_METHOD("wildcard"), &QueryBuilder::_wildcard_bind); + // Prepared statement placeholders + ClassDB::bind_method(D_METHOD("psph"), &QueryBuilder::_psph_bind); + ClassDB::bind_method(D_METHOD("psphi", "id"), &QueryBuilder::_psphi_bind); + ClassDB::bind_method(D_METHOD("psphr", "raw_id"), &QueryBuilder::_psphr_bind); + ClassDB::bind_method(D_METHOD("w", "str"), &QueryBuilder::_w_bind); ClassDB::bind_method(D_METHOD("ew", "str"), &QueryBuilder::_ew_bind); @@ -592,6 +608,17 @@ Ref QueryBuilder::_wildcard_bind() { return Ref(wildcard()); } +// Prepared statement placeholders +Ref QueryBuilder::_psph_bind() { + return Ref(psph()); +} +Ref QueryBuilder::_psphi_bind(const String &p_id) { + return Ref(psphi(p_id)); +} +Ref QueryBuilder::_psphr_bind(const String &p_raw_id) { + return Ref(psphr(p_raw_id)); +} + Ref QueryBuilder::_w_bind(const String &str) { return Ref(w(str)); } diff --git a/modules/database/query_builder.h b/modules/database/query_builder.h index 5c1e17b38..0a4a0876c 100644 --- a/modules/database/query_builder.h +++ b/modules/database/query_builder.h @@ -129,6 +129,11 @@ public: virtual QueryBuilder *wildcard(); + // Prepared statement placeholders + virtual QueryBuilder *psph(); + virtual QueryBuilder *psphi(const String &p_id); + virtual QueryBuilder *psphr(const String &p_raw_id); + virtual QueryBuilder *w(const String &str); virtual QueryBuilder *ew(const String &str); @@ -228,16 +233,16 @@ protected: Ref _wildcard_bind(); + // Prepared statement placeholders + Ref _psph_bind(); + Ref _psphi_bind(const String &p_id); + Ref _psphr_bind(const String &p_raw_id); + Ref _w_bind(const String &str); Ref _ew_bind(const String &str); Ref _select_last_insert_id_bind(); - Ref _prepare_bind(); - Ref _set_params_bind(const int index, const String &value); - Ref _set_parami_bind(const int index, const int value); - Ref _set_paramf_bind(const int index, const float value); - Ref _end_command_bind(); Ref _reset_bind(); diff --git a/modules/database_sqlite/sqlite3_query_builder.cpp b/modules/database_sqlite/sqlite3_query_builder.cpp index c33032d86..ef999e3b5 100644 --- a/modules/database_sqlite/sqlite3_query_builder.cpp +++ b/modules/database_sqlite/sqlite3_query_builder.cpp @@ -2,8 +2,8 @@ #include "sqlite3_connection.h" #include "sqlite3_database.h" -#include "sqlite3_query_result.h" #include "sqlite3_prepared_statement.h" +#include "sqlite3_query_result.h" QueryBuilder *SQLite3QueryBuilder::select() { query_result += "SELECT "; @@ -340,6 +340,27 @@ QueryBuilder *SQLite3QueryBuilder::wildcard() { return this; } +// Prepared statement placeholders +QueryBuilder *SQLite3QueryBuilder::psph() { + query_result += "?"; + + return this; +} +QueryBuilder *SQLite3QueryBuilder::psphi(const String &p_id) { + if (p_id.is_valid_unsigned_integer()) { + query_result += "?" + p_id; + } else { + query_result += ":" + p_id; + } + + return this; +} +QueryBuilder *SQLite3QueryBuilder::psphr(const String &p_raw_id) { + query_result += p_raw_id; + + return this; +} + String SQLite3QueryBuilder::escape(const String ¶ms) { ERR_FAIL_COND_V(!_connection.is_valid(), String()); diff --git a/modules/database_sqlite/sqlite3_query_builder.h b/modules/database_sqlite/sqlite3_query_builder.h index 86f04bff7..2da96d067 100644 --- a/modules/database_sqlite/sqlite3_query_builder.h +++ b/modules/database_sqlite/sqlite3_query_builder.h @@ -73,6 +73,11 @@ public: QueryBuilder *wildcard(); + // Prepared statement placeholders + virtual QueryBuilder *psph(); + virtual QueryBuilder *psphi(const String &p_id); + virtual QueryBuilder *psphr(const String &p_raw_id); + QueryBuilder *select_last_insert_id(); String escape(const String ¶ms);