Added virtual methods to QueryBuilder for adding prepared statement placeholders.

This commit is contained in:
Relintai 2024-04-27 18:37:56 +02:00
parent a347ab75b4
commit 266263976d
4 changed files with 65 additions and 7 deletions

View File

@ -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> QueryBuilder::_wildcard_bind() {
return Ref<QueryBuilder>(wildcard());
}
// Prepared statement placeholders
Ref<QueryBuilder> QueryBuilder::_psph_bind() {
return Ref<QueryBuilder>(psph());
}
Ref<QueryBuilder> QueryBuilder::_psphi_bind(const String &p_id) {
return Ref<QueryBuilder>(psphi(p_id));
}
Ref<QueryBuilder> QueryBuilder::_psphr_bind(const String &p_raw_id) {
return Ref<QueryBuilder>(psphr(p_raw_id));
}
Ref<QueryBuilder> QueryBuilder::_w_bind(const String &str) {
return Ref<QueryBuilder>(w(str));
}

View File

@ -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<QueryBuilder> _wildcard_bind();
// Prepared statement placeholders
Ref<QueryBuilder> _psph_bind();
Ref<QueryBuilder> _psphi_bind(const String &p_id);
Ref<QueryBuilder> _psphr_bind(const String &p_raw_id);
Ref<QueryBuilder> _w_bind(const String &str);
Ref<QueryBuilder> _ew_bind(const String &str);
Ref<QueryBuilder> _select_last_insert_id_bind();
Ref<QueryBuilder> _prepare_bind();
Ref<QueryBuilder> _set_params_bind(const int index, const String &value);
Ref<QueryBuilder> _set_parami_bind(const int index, const int value);
Ref<QueryBuilder> _set_paramf_bind(const int index, const float value);
Ref<QueryBuilder> _end_command_bind();
Ref<QueryBuilder> _reset_bind();

View File

@ -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 &params) {
ERR_FAIL_COND_V(!_connection.is_valid(), String());

View File

@ -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 &params);