From f9bbf772ca8cb322c909d004825cf6889548cae7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 5 Jul 2022 20:34:12 +0200 Subject: [PATCH] Work on cleaning up QueryBuilder. --- modules/database/query_builder.cpp | 297 ++++++++++++++++++++++++++--- modules/database/query_builder.h | 146 ++++++++++++-- 2 files changed, 399 insertions(+), 44 deletions(-) diff --git a/modules/database/query_builder.cpp b/modules/database/query_builder.cpp index 662289dbf..f58a16551 100644 --- a/modules/database/query_builder.cpp +++ b/modules/database/query_builder.cpp @@ -13,7 +13,6 @@ QueryBuilder *QueryBuilder::del() { } QueryBuilder *QueryBuilder::where() { - return this; } QueryBuilder *QueryBuilder::from() { @@ -73,9 +72,6 @@ QueryBuilder *QueryBuilder::from(const String ¶ms) { return nfrom(escape(params)); } -QueryBuilder *QueryBuilder::insert(const String &table_name) { - return this; -} QueryBuilder *QueryBuilder::insert(const String &table_name, const String &columns) { return this; } @@ -87,18 +83,18 @@ QueryBuilder *QueryBuilder::val() { return this; } -QueryBuilder *QueryBuilder::val(const String ¶m) { +QueryBuilder *QueryBuilder::vals(const String ¶m) { return nval(escape(param)); } -QueryBuilder *QueryBuilder::val(const char *param) { +QueryBuilder *QueryBuilder::vals(const char *param) { return this; } -QueryBuilder *QueryBuilder::val(const int param) { +QueryBuilder *QueryBuilder::vali(const int param) { return this; } -QueryBuilder *QueryBuilder::val(const bool param) { +QueryBuilder *QueryBuilder::valb(const bool param) { return this; } @@ -119,16 +115,16 @@ QueryBuilder *QueryBuilder::set() { QueryBuilder *QueryBuilder::cset() { return this; } -QueryBuilder *QueryBuilder::setp(const String &col, const String ¶m) { +QueryBuilder *QueryBuilder::setps(const String &col, const String ¶m) { return nsetp(col, escape(param)); } -QueryBuilder *QueryBuilder::setp(const String &col, const char *param) { +QueryBuilder *QueryBuilder::setps(const String &col, const char *param) { return this; } -QueryBuilder *QueryBuilder::setp(const String &col, const int param) { +QueryBuilder *QueryBuilder::setpi(const String &col, const int param) { return this; } -QueryBuilder *QueryBuilder::setp(const String &col, const bool param) { +QueryBuilder *QueryBuilder::setpb(const String &col, const bool param) { return this; } QueryBuilder *QueryBuilder::setpf(const String &col, const float param) { @@ -138,16 +134,16 @@ QueryBuilder *QueryBuilder::setpd(const String &col, const double param) { return this; } -QueryBuilder *QueryBuilder::wp(const String &col, const String ¶m) { +QueryBuilder *QueryBuilder::wps(const String &col, const String ¶m) { return nwp(col, escape(param)); } -QueryBuilder *QueryBuilder::wp(const String &col, const char *param) { +QueryBuilder *QueryBuilder::wps(const String &col, const char *param) { return this; } -QueryBuilder *QueryBuilder::wp(const String &col, const int param) { +QueryBuilder *QueryBuilder::wpi(const String &col, const int param) { return this; } -QueryBuilder *QueryBuilder::wp(const String &col, const bool param) { +QueryBuilder *QueryBuilder::wpb(const String &col, const bool param) { return this; } @@ -175,10 +171,10 @@ QueryBuilder *QueryBuilder::nvalues(const String ¶ms_str) { } QueryBuilder *QueryBuilder::nval(const String ¶m) { - return val(escape(param)); + return vals(escape(param)); } QueryBuilder *QueryBuilder::nsetp(const String &col, const String &escape_param) { - return setp(col, escape(escape_param)); + return setps(col, escape(escape_param)); } QueryBuilder *QueryBuilder::nwp(const String &col, const String &escape_param) { @@ -276,13 +272,13 @@ String QueryBuilder::escape(const String ¶ms) { QueryBuilder *QueryBuilder::prepare() { return this; } -QueryBuilder *QueryBuilder::set_param(const int index, const String &value) { +QueryBuilder *QueryBuilder::set_params(const int index, const String &value) { return this; } -QueryBuilder *QueryBuilder::set_param(const int index, const int value) { +QueryBuilder *QueryBuilder::set_parami(const int index, const int value) { return this; } -QueryBuilder *QueryBuilder::set_param(const int index, const float value) { +QueryBuilder *QueryBuilder::set_paramf(const int index, const float value) { return this; } @@ -310,11 +306,266 @@ String QueryBuilder::get_result() { } void QueryBuilder::print() { - printf("%s\n", query_result.c_str()); + //printf("%s\n", query_result.c_str()); + ERR_PRINT(query_result); } QueryBuilder::QueryBuilder() { } QueryBuilder::~QueryBuilder() { -} \ No newline at end of file +} + +void QueryBuilder::_bind_methods() { + ClassDB::bind_method(D_METHOD("next_row"), &QueryResult::next_row); +} + +Ref QueryBuilder::_select_bind() { + return Ref(select()); +} +Ref QueryBuilder::_update_bind() { + return Ref(update()); +} +Ref QueryBuilder::_del_bind() { + return Ref(del()); +} + +Ref QueryBuilder::_where_bind() { + return Ref(where()); +} +Ref QueryBuilder::_from_bind() { + return Ref(from()); +} +Ref QueryBuilder::_insert_bind() { + return Ref(insert()); +} +Ref QueryBuilder::_values_bind() { + return Ref(values()); +} +Ref QueryBuilder::_cvalues_bind() { + return Ref(cvalues()); +} +Ref QueryBuilder::_next_value_bind() { + return Ref(next_value()); +} + +Ref QueryBuilder::_begin_transaction_bind() { + return Ref(begin_transaction()); +} +Ref QueryBuilder::_commit_bind() { + return Ref(commit()); +} + +Ref QueryBuilder::_nl_bind() { + return Ref(nl()); +} + +Ref QueryBuilder::_str_bind() { + return Ref(str()); +} +Ref QueryBuilder::_cstr_bind() { + return Ref(cstr()); +} + +Ref QueryBuilder::_like_bind() { + return Ref(like()); +} + +Ref QueryBuilder::_select_bind(const String ¶ms) { + return Ref(select()); +} +Ref QueryBuilder::_update_bind(const String ¶ms) { + return Ref(update()); +} +Ref QueryBuilder::_del_bind(const String ¶ms) { + return Ref(del()); +} + +Ref QueryBuilder::_where_bind(const String ¶ms) { + return Ref(where()); +} +Ref QueryBuilder::_from_bind(const String ¶ms) { + return Ref(from()); +} +Ref QueryBuilder::_insert_bind(const String &table_name, const String &columns) { + return Ref(insert()); +} +Ref QueryBuilder::_values_bind(const String ¶ms_str) { + return Ref(values()); +} +Ref QueryBuilder::_val_bind() { + return Ref(val()); +} +Ref QueryBuilder::_vals_bind(const String ¶m) { + return Ref(vals(param)); +} +Ref QueryBuilder::_vali_bind(const int param) { + return Ref(vali(param)); +} +Ref QueryBuilder::_valb_bind(const bool param) { + return Ref(valb(param)); +} +Ref QueryBuilder::_valf_bind(const float param) { + return Ref(valf(param)); +} +Ref QueryBuilder::_vald_bind(const double param) { + return Ref(vald(param)); +} + +Ref QueryBuilder::_like_bind(const String &str) { + return Ref(like()); +} + +Ref QueryBuilder::_set_bind() { + return Ref(set()); +} +Ref QueryBuilder::_cset_bind() { + return Ref(cset()); +} + +Ref QueryBuilder::_setps_bind(const String &col, const String ¶m) { + return Ref(setps(col, param)); +} +Ref QueryBuilder::_setpi_bind(const String &col, const int param) { + return Ref(setpi(col, param)); +} +Ref QueryBuilder::_setpb_bind(const String &col, const bool param) { + return Ref(setpb(col, param)); +} +Ref QueryBuilder::_setpf_bind(const String &col, const float param) { + return Ref(setpf(col, param)); +} +Ref QueryBuilder::_setpd_bind(const String &col, const double param) { + return Ref(setpd(col, param)); +} + +Ref QueryBuilder::_wps_bind(const String &col, const String ¶m) { + return Ref(wps(col, param)); +} +Ref QueryBuilder::_wpi_bind(const String &col, const int param) { + return Ref(wpi(col, param)); +} +Ref QueryBuilder::_wpb_bind(const String &col, const bool param) { + return Ref(wpb(col, param)); +} + +Ref QueryBuilder::_nselect_bind(const String ¶ms) { + return Ref(nselect(params)); +} +Ref QueryBuilder::_nupdate_bind(const String ¶ms) { + return Ref(nupdate(params)); +} +Ref QueryBuilder::_ndel_bind(const String ¶ms) { + return Ref(ndel(params)); +} + +Ref QueryBuilder::_nwhere_bind(const String ¶ms) { + return Ref(nwhere(params)); +} +Ref QueryBuilder::_nfrom_bind(const String ¶ms) { + return Ref(nfrom(params)); +} +Ref QueryBuilder::_nlike_bind(const String &str) { + return Ref(nlike(str)); +} +Ref QueryBuilder::_nvalues_bind(const String ¶ms_str) { + return Ref(nvalues(params_str)); +} +Ref QueryBuilder::_nval_bind(const String ¶m) { + return Ref(nval(param)); +} +//note col is NOT escaped +Ref QueryBuilder::_nsetp_bind(const String &col, const String &escape_param) { + return Ref(nsetp(col, escape_param)); +} +//note col is NOT escaped +Ref QueryBuilder::_nwp_bind(const String &col, const String &escape_param) { + return Ref(nwp(col, escape_param)); +} + +Ref QueryBuilder::_limit_bind(const int num) { + return Ref(limit(num)); +} +Ref QueryBuilder::_offset_bind(const int num) { + return Ref(offset(num)); +} + +Ref QueryBuilder::_order_by_asc_bind(const String &col) { + return Ref(order_by_asc(col)); +} +Ref QueryBuilder::_order_by_desc_bind(const String &col) { + return Ref(order_by_desc(col)); +} +Ref QueryBuilder::_order_by_bind(const String &col) { + return Ref(order_by(col)); +} + +Ref QueryBuilder::_order_by_bind() { + return Ref(order_by()); +} +Ref QueryBuilder::_corder_by_bind() { + return Ref(corder_by()); +} +Ref QueryBuilder::_asc_bind() { + return Ref(asc()); +} +Ref QueryBuilder::_desc_bind() { + return Ref(desc()); +} +Ref QueryBuilder::_order_by_add_col_bind(const String &col) { + return Ref(order_by_add_col(col)); +} +Ref QueryBuilder::_asc_bind(const String &col) { + return Ref(asc(col)); +} +Ref QueryBuilder::_desc_bind(const String &col) { + return Ref(desc(col)); +} + +//l=logical (and, or are operators) +Ref QueryBuilder::_land_bind() { + return Ref(land()); +} +Ref QueryBuilder::_lor_bind() { + return Ref(lor()); +} + +Ref QueryBuilder::_wildcard_bind() { + return Ref(wildcard()); +} + +Ref QueryBuilder::_w_bind(const String &str) { + return Ref(w(str)); +} +Ref QueryBuilder::_ew_bind(const String &str) { + return Ref(ew(str)); +} + +Ref QueryBuilder::_select_last_insert_id_bind() { + return Ref(select_last_insert_id()); +} + +Ref QueryBuilder::_escape_bind(const String ¶ms) { + return Ref(escape(params)); +} + +Ref QueryBuilder::_prepare_bind() { + return Ref(prepare()); +} +Ref QueryBuilder::_set_params_bind(const int index, const String &value) { + return Ref(set_params(index, value)); +} +Ref QueryBuilder::_set_parami_bind(const int index, const int value) { + return Ref(set_parami(index, value)); +} +Ref QueryBuilder::_set_paramf_bind(const int index, const float value) { + return Ref(set_paramf(index, value)); +} + +Ref QueryBuilder::_end_command_bind() { + return Ref(end_command()); +} + +Ref QueryBuilder::_reset_bind() { + return Ref(reset()); +} diff --git a/modules/database/query_builder.h b/modules/database/query_builder.h index d499e7e3c..4a3771913 100644 --- a/modules/database/query_builder.h +++ b/modules/database/query_builder.h @@ -1,7 +1,7 @@ #ifndef QUERY_BUILDER_H #define QUERY_BUILDER_H -#include "core/string.h" +#include "core/ustring.h" #include "core/reference.h" @@ -10,7 +10,7 @@ class QueryResult; //methods that start with an e escape their params. class QueryBuilder : public Reference { - RCPP_OBJECT(QueryBuilder, Reference); + GDCLASS(QueryBuilder, Reference); public: virtual QueryBuilder *select(); @@ -23,7 +23,7 @@ public: virtual QueryBuilder *values(); virtual QueryBuilder *cvalues(); virtual QueryBuilder *next_value(); - + virtual QueryBuilder *begin_transaction(); virtual QueryBuilder *commit(); @@ -40,14 +40,13 @@ public: virtual QueryBuilder *where(const String ¶ms); virtual QueryBuilder *from(const String ¶ms); - virtual QueryBuilder *insert(const String &table_name); - virtual QueryBuilder *insert(const String &table_name, const String &columns); + virtual QueryBuilder *insert(const String &table_name, const String &columns = ""); virtual QueryBuilder *values(const String ¶ms_str); virtual QueryBuilder *val(); - virtual QueryBuilder *val(const String ¶m); - virtual QueryBuilder *val(const char *param); - virtual QueryBuilder *val(const int param); - virtual QueryBuilder *val(const bool param); + virtual QueryBuilder *vals(const String ¶m); + virtual QueryBuilder *vals(const char *param); + virtual QueryBuilder *vali(const int param); + virtual QueryBuilder *valb(const bool param); virtual QueryBuilder *valf(const float param); virtual QueryBuilder *vald(const double param); @@ -56,17 +55,17 @@ public: virtual QueryBuilder *set(); virtual QueryBuilder *cset(); - virtual QueryBuilder *setp(const String &col, const String ¶m); - virtual QueryBuilder *setp(const String &col, const char *param); - virtual QueryBuilder *setp(const String &col, const int param); - virtual QueryBuilder *setp(const String &col, const bool param); + virtual QueryBuilder *setps(const String &col, const String ¶m); + virtual QueryBuilder *setps(const String &col, const char *param); + virtual QueryBuilder *setpi(const String &col, const int param); + virtual QueryBuilder *setpb(const String &col, const bool param); virtual QueryBuilder *setpf(const String &col, const float param); virtual QueryBuilder *setpd(const String &col, const double param); - virtual QueryBuilder *wp(const String &col, const String ¶m); - virtual QueryBuilder *wp(const String &col, const char *param); - virtual QueryBuilder *wp(const String &col, const int param); - virtual QueryBuilder *wp(const String &col, const bool param); + virtual QueryBuilder *wps(const String &col, const String ¶m); + virtual QueryBuilder *wps(const String &col, const char *param); + virtual QueryBuilder *wpi(const String &col, const int param); + virtual QueryBuilder *wpb(const String &col, const bool param); virtual QueryBuilder *nselect(const String ¶ms); virtual QueryBuilder *nupdate(const String ¶ms); @@ -111,9 +110,9 @@ public: virtual String escape(const String ¶ms); virtual QueryBuilder *prepare(); - virtual QueryBuilder *set_param(const int index, const String &value); - virtual QueryBuilder *set_param(const int index, const int value); - virtual QueryBuilder *set_param(const int index, const float value); + virtual QueryBuilder *set_params(const int index, const String &value); + virtual QueryBuilder *set_parami(const int index, const int value); + virtual QueryBuilder *set_paramf(const int index, const float value); virtual QueryBuilder *end_command(); @@ -130,6 +129,111 @@ public: virtual ~QueryBuilder(); String query_result; + +protected: + static void _bind_methods(); + + Ref _select_bind(); + Ref _update_bind(); + Ref _del_bind(); + + Ref _where_bind(); + Ref _from_bind(); + Ref _insert_bind(); + Ref _values_bind(); + Ref _cvalues_bind(); + Ref _next_value_bind(); + + Ref _begin_transaction_bind(); + Ref _commit_bind(); + + Ref _nl_bind(); + + Ref _str_bind(); + Ref _cstr_bind(); + + Ref _like_bind(); + + Ref _select_bind(const String ¶ms); + Ref _update_bind(const String ¶ms); + Ref _del_bind(const String ¶ms); + + Ref _where_bind(const String ¶ms); + Ref _from_bind(const String ¶ms); + Ref _insert_bind(const String &table_name, const String &columns = ""); + Ref _values_bind(const String ¶ms_str); + Ref _val_bind(); + Ref _vals_bind(const String ¶m); + Ref _vali_bind(const int param); + Ref _valb_bind(const bool param); + Ref _valf_bind(const float param); + Ref _vald_bind(const double param); + + Ref _like_bind(const String &str); + + Ref _set_bind(); + Ref _cset_bind(); + + Ref _setps_bind(const String &col, const String ¶m); + Ref _setpi_bind(const String &col, const int param); + Ref _setpb_bind(const String &col, const bool param); + Ref _setpf_bind(const String &col, const float param); + Ref _setpd_bind(const String &col, const double param); + + Ref _wps_bind(const String &col, const String ¶m); + Ref _wpi_bind(const String &col, const int param); + Ref _wpb_bind(const String &col, const bool param); + + Ref _nselect_bind(const String ¶ms); + Ref _nupdate_bind(const String ¶ms); + Ref _ndel_bind(const String ¶ms); + + Ref _nwhere_bind(const String ¶ms); + Ref _nfrom_bind(const String ¶ms); + Ref _nlike_bind(const String &str); + Ref _nvalues_bind(const String ¶ms_str); + Ref _nval_bind(const String ¶m); + //note col is NOT escaped + Ref _nsetp_bind(const String &col, const String &escape_param); + //note col is NOT escaped + Ref _nwp_bind(const String &col, const String &escape_param); + + Ref _limit_bind(const int num); + Ref _offset_bind(const int num); + + Ref _order_by_asc_bind(const String &col); + Ref _order_by_desc_bind(const String &col); + Ref _order_by_bind(const String &col); + + Ref _order_by_bind(); + Ref _corder_by_bind(); + Ref _asc_bind(); + Ref _desc_bind(); + Ref _order_by_add_col_bind(const String &col); + Ref _asc_bind(const String &col); + Ref _desc_bind(const String &col); + + //l=logical (and, or are operators) + Ref _land_bind(); + Ref _lor_bind(); + + Ref _wildcard_bind(); + + Ref _w_bind(const String &str); + Ref _ew_bind(const String &str); + + Ref _select_last_insert_id_bind(); + + Ref _escape_bind(const String ¶ms); + + 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(); }; -#endif \ No newline at end of file +#endif