/*************************************************************************/ /* query_builder.cpp */ /*************************************************************************/ /* This file is part of: */ /* PANDEMONIUM ENGINE */ /* https://github.com/Relintai/pandemonium_engine */ /*************************************************************************/ /* Copyright (c) 2022-present Péter Magyar. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "query_builder.h" #include "prepared_statement.h" #include "query_result.h" String QueryBuilder::get_result() { return query_result; } void QueryBuilder::set_result(const String &val) { query_result.clear(); query_result.append(val); } QueryBuilder *QueryBuilder::select() { return this; } QueryBuilder *QueryBuilder::update() { return this; } QueryBuilder *QueryBuilder::del() { return this; } QueryBuilder *QueryBuilder::cvalues() { return this; } QueryBuilder *QueryBuilder::next_value() { return this; } QueryBuilder *QueryBuilder::begin_transaction() { return this; } QueryBuilder *QueryBuilder::commit() { return this; } QueryBuilder *QueryBuilder::nl() { query_result += "\n"; return this; } QueryBuilder *QueryBuilder::str() { return this; } QueryBuilder *QueryBuilder::cstr() { return this; } QueryBuilder *QueryBuilder::select(const String ¶ms) { return nselect(escape(params)); } QueryBuilder *QueryBuilder::update(const String ¶ms) { return nupdate(escape(params)); } QueryBuilder *QueryBuilder::del(const String ¶ms) { return ndel(escape(params)); } QueryBuilder *QueryBuilder::where(const String ¶ms) { return nwhere(escape(params)); } QueryBuilder *QueryBuilder::from(const String ¶ms) { return nfrom(escape(params)); } QueryBuilder *QueryBuilder::insert(const String &table_name, const String &columns) { return this; } QueryBuilder *QueryBuilder::values(const String ¶ms_str) { return nvalues(escape(params_str)); } QueryBuilder *QueryBuilder::val() { return this; } QueryBuilder *QueryBuilder::vals(const String ¶m) { return nval(escape(param)); } QueryBuilder *QueryBuilder::vals(const char *param) { return this; } QueryBuilder *QueryBuilder::vali(const int param) { return this; } QueryBuilder *QueryBuilder::valb(const bool param) { return this; } QueryBuilder *QueryBuilder::valf(const float param) { return this; } QueryBuilder *QueryBuilder::vald(const double param) { return this; } QueryBuilder *QueryBuilder::valph() { return this; } QueryBuilder *QueryBuilder::valphi(const String &p_id) { return this; } QueryBuilder *QueryBuilder::valphr(const String &p_raw_id) { return this; } QueryBuilder *QueryBuilder::like(const String &str) { return nlike(escape(str)); } QueryBuilder *QueryBuilder::sets() { return this; } QueryBuilder *QueryBuilder::cset() { return this; } QueryBuilder *QueryBuilder::setps(const String &col, const String ¶m) { return nsetp(col, escape(param)); } QueryBuilder *QueryBuilder::setps(const String &col, const char *param) { return this; } QueryBuilder *QueryBuilder::setpi(const String &col, const int param) { return this; } QueryBuilder *QueryBuilder::setpb(const String &col, const bool param) { return this; } QueryBuilder *QueryBuilder::setpf(const String &col, const float param) { return this; } QueryBuilder *QueryBuilder::setpd(const String &col, const double param) { return this; } QueryBuilder *QueryBuilder::setph(const String &col) { return this; } QueryBuilder *QueryBuilder::setphi(const String &col, const String &p_id) { return this; } QueryBuilder *QueryBuilder::setphr(const String &col, const String &p_raw_id) { return this; } QueryBuilder *QueryBuilder::wps(const String &col, const String ¶m) { return nwp(col, escape(param)); } QueryBuilder *QueryBuilder::wps(const String &col, const char *param) { return this; } QueryBuilder *QueryBuilder::wpi(const String &col, const int param) { return this; } QueryBuilder *QueryBuilder::wpb(const String &col, const bool param) { return this; } QueryBuilder *QueryBuilder::wph(const String &col) { return this; } QueryBuilder *QueryBuilder::wphi(const String &col, const String &p_id) { return this; } QueryBuilder *QueryBuilder::wphr(const String &col, const String &p_raw_id) { return this; } QueryBuilder *QueryBuilder::nselect(const String ¶ms) { return this; } QueryBuilder *QueryBuilder::nupdate(const String ¶ms) { return this; } QueryBuilder *QueryBuilder::ndel(const String ¶ms) { return this; } QueryBuilder *QueryBuilder::nwhere(const String ¶ms) { return this; } QueryBuilder *QueryBuilder::nfrom(const String ¶ms) { return this; } QueryBuilder *QueryBuilder::nlike(const String &str) { return this; } QueryBuilder *QueryBuilder::nvalues(const String ¶ms_str) { return this; } QueryBuilder *QueryBuilder::nval(const String ¶m) { return this; } QueryBuilder *QueryBuilder::nsetp(const String &col, const String &escape_param) { return this; } QueryBuilder *QueryBuilder::nwp(const String &col, const String &escape_param) { return this; } QueryBuilder *QueryBuilder::limit(const int num) { return this; } QueryBuilder *QueryBuilder::offset(const int num) { return this; } QueryBuilder *QueryBuilder::order_by_asc(const String &col) { query_result += "ORDER BY " + col + " ASC, "; return this; } QueryBuilder *QueryBuilder::order_by_desc(const String &col) { query_result += "ORDER BY " + col + " DESC, "; return this; } QueryBuilder *QueryBuilder::order_by(const String &col) { if (col.empty()) { query_result += "ORDER BY "; } else { query_result += "ORDER BY " + col + ", "; } return this; } QueryBuilder *QueryBuilder::corder_by() { ERR_FAIL_COND_V(query_result.length() <= 2, this); String qr = query_result.as_string(); qr[qr.length() - 2] = ' '; query_result.clear(); query_result.append(qr); return this; } QueryBuilder *QueryBuilder::order_by_add_col(const String &col) { query_result += col + ", "; return this; } QueryBuilder *QueryBuilder::asc(const String &col) { if (col.empty()) { query_result += "ASC, "; } else { query_result += col + " ASC, "; } return this; } QueryBuilder *QueryBuilder::desc(const String &col) { if (col.empty()) { query_result += "DESC, "; } else { query_result += col + " DESC, "; } return this; } QueryBuilder *QueryBuilder::land() { return this; } QueryBuilder *QueryBuilder::lor() { return this; } 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; return this; } QueryBuilder *QueryBuilder::ew(const String &str) { return w(escape(str)); } QueryBuilder *QueryBuilder::select_last_insert_id() { return this; } String QueryBuilder::escape(const String ¶ms) { return params; } QueryBuilder *QueryBuilder::end_command() { return this; } QueryBuilder *QueryBuilder::reset() { query_result.clear(); return this; } Ref QueryBuilder::run() { return Ref(); } void QueryBuilder::run_query() { } Ref QueryBuilder::create_prepared_statement() { return Ref(); } void QueryBuilder::print() { //printf("%s\n", query_result.get_data()); ERR_PRINT(query_result); } QueryBuilder::QueryBuilder() { } QueryBuilder::~QueryBuilder() { } void QueryBuilder::_bind_methods() { ClassDB::bind_method(D_METHOD("get_result"), &QueryBuilder::get_result); ClassDB::bind_method(D_METHOD("set_result", "value"), &QueryBuilder::set_result); ADD_PROPERTY(PropertyInfo(Variant::STRING, "result"), "set_result", "get_result"); ClassDB::bind_method(D_METHOD("cvalues"), &QueryBuilder::_cvalues_bind); ClassDB::bind_method(D_METHOD("next_value"), &QueryBuilder::_next_value_bind); ClassDB::bind_method(D_METHOD("begin_transaction"), &QueryBuilder::_begin_transaction_bind); ClassDB::bind_method(D_METHOD("commit"), &QueryBuilder::_commit_bind); ClassDB::bind_method(D_METHOD("nl"), &QueryBuilder::_nl_bind); ClassDB::bind_method(D_METHOD("str"), &QueryBuilder::_str_bind); ClassDB::bind_method(D_METHOD("cstr"), &QueryBuilder::_cstr_bind); ClassDB::bind_method(D_METHOD("select", "params"), &QueryBuilder::_select_bind, ""); ClassDB::bind_method(D_METHOD("update", "params"), &QueryBuilder::_update_bind, ""); ClassDB::bind_method(D_METHOD("del", "params"), &QueryBuilder::_del_bind, ""); ClassDB::bind_method(D_METHOD("where", "params"), &QueryBuilder::_where_bind, ""); ClassDB::bind_method(D_METHOD("from", "params"), &QueryBuilder::_from_bind, ""); ClassDB::bind_method(D_METHOD("insert", "table_name", "columns"), &QueryBuilder::_insert_bind, "", ""); ClassDB::bind_method(D_METHOD("values", "params_str"), &QueryBuilder::_values_bind, ""); ClassDB::bind_method(D_METHOD("val"), &QueryBuilder::_val_bind); ClassDB::bind_method(D_METHOD("vals", "param"), &QueryBuilder::_vals_bind); ClassDB::bind_method(D_METHOD("vali", "param"), &QueryBuilder::_vali_bind); ClassDB::bind_method(D_METHOD("valb", "param"), &QueryBuilder::_valb_bind); ClassDB::bind_method(D_METHOD("valf", "param"), &QueryBuilder::_valf_bind); ClassDB::bind_method(D_METHOD("vald", "param"), &QueryBuilder::_vald_bind); ClassDB::bind_method(D_METHOD("valph"), &QueryBuilder::_valph_bind); ClassDB::bind_method(D_METHOD("valphi", "id"), &QueryBuilder::_valphi_bind); ClassDB::bind_method(D_METHOD("valphr", "raw_id"), &QueryBuilder::_valphr_bind); ClassDB::bind_method(D_METHOD("like", "str"), &QueryBuilder::_like_bind, ""); ClassDB::bind_method(D_METHOD("sets"), &QueryBuilder::_sets_bind); ClassDB::bind_method(D_METHOD("cset"), &QueryBuilder::_cset_bind); ClassDB::bind_method(D_METHOD("setps", "col", "param"), &QueryBuilder::_setps_bind); ClassDB::bind_method(D_METHOD("setpi", "col", "param"), &QueryBuilder::_setpi_bind); ClassDB::bind_method(D_METHOD("setpb", "col", "param"), &QueryBuilder::_setpb_bind); ClassDB::bind_method(D_METHOD("setpf", "col", "param"), &QueryBuilder::_setpf_bind); ClassDB::bind_method(D_METHOD("setpd", "col", "param"), &QueryBuilder::_setpd_bind); ClassDB::bind_method(D_METHOD("setph", "col"), &QueryBuilder::_setph_bind); ClassDB::bind_method(D_METHOD("setphi", "col", "id"), &QueryBuilder::_setphi_bind); ClassDB::bind_method(D_METHOD("setphr", "col", "raw_id"), &QueryBuilder::_setphr_bind); ClassDB::bind_method(D_METHOD("wps", "col", "param"), &QueryBuilder::_wps_bind); ClassDB::bind_method(D_METHOD("wpi", "col", "param"), &QueryBuilder::_wpi_bind); ClassDB::bind_method(D_METHOD("wpb", "col", "param"), &QueryBuilder::_wpb_bind); ClassDB::bind_method(D_METHOD("wph", "col"), &QueryBuilder::_wph_bind); ClassDB::bind_method(D_METHOD("wphi", "col", "id"), &QueryBuilder::_wphi_bind); ClassDB::bind_method(D_METHOD("wphr", "col", "raw_id"), &QueryBuilder::_wphr_bind); ClassDB::bind_method(D_METHOD("nselect", "params"), &QueryBuilder::_nselect_bind); ClassDB::bind_method(D_METHOD("nupdate", "params"), &QueryBuilder::_nupdate_bind); ClassDB::bind_method(D_METHOD("ndel", "params"), &QueryBuilder::_ndel_bind); ClassDB::bind_method(D_METHOD("nwhere", "params"), &QueryBuilder::_nwhere_bind); ClassDB::bind_method(D_METHOD("nfrom", "params"), &QueryBuilder::_nfrom_bind); ClassDB::bind_method(D_METHOD("nlike", "str"), &QueryBuilder::_nlike_bind); ClassDB::bind_method(D_METHOD("nvalues", "params_str"), &QueryBuilder::_nvalues_bind); ClassDB::bind_method(D_METHOD("nval", "param"), &QueryBuilder::_nval_bind); ClassDB::bind_method(D_METHOD("nsetp", "col", "escape_param"), &QueryBuilder::_nsetp_bind); ClassDB::bind_method(D_METHOD("nwp", "col", "escape_param"), &QueryBuilder::_nwp_bind); ClassDB::bind_method(D_METHOD("limit", "num"), &QueryBuilder::_limit_bind); ClassDB::bind_method(D_METHOD("offset", "num"), &QueryBuilder::_offset_bind); ClassDB::bind_method(D_METHOD("order_by_asc", "col"), &QueryBuilder::_order_by_asc_bind); ClassDB::bind_method(D_METHOD("order_by_desc", "col"), &QueryBuilder::_order_by_desc_bind); ClassDB::bind_method(D_METHOD("order_by", "col"), &QueryBuilder::_order_by_bind); ClassDB::bind_method(D_METHOD("corder_by"), &QueryBuilder::_corder_by_bind); ClassDB::bind_method(D_METHOD("order_by_add_col", "col"), &QueryBuilder::_order_by_add_col_bind); ClassDB::bind_method(D_METHOD("asc", "col"), &QueryBuilder::_asc_bind, ""); ClassDB::bind_method(D_METHOD("desc", "col"), &QueryBuilder::_desc_bind, ""); ClassDB::bind_method(D_METHOD("land"), &QueryBuilder::_land_bind); ClassDB::bind_method(D_METHOD("lor"), &QueryBuilder::_lor_bind); 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); ClassDB::bind_method(D_METHOD("select_last_insert_id"), &QueryBuilder::_select_last_insert_id_bind); ClassDB::bind_method(D_METHOD("escape", "param"), &QueryBuilder::escape); ClassDB::bind_method(D_METHOD("end_command"), &QueryBuilder::_end_command_bind); ClassDB::bind_method(D_METHOD("reset"), &QueryBuilder::_reset_bind); ClassDB::bind_method(D_METHOD("run"), &QueryBuilder::run); ClassDB::bind_method(D_METHOD("run_query"), &QueryBuilder::run_query); ClassDB::bind_method(D_METHOD("create_prepared_statement"), &QueryBuilder::create_prepared_statement); } 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::_select_bind(const String ¶ms) { return Ref(select(params)); } Ref QueryBuilder::_update_bind(const String ¶ms) { return Ref(update(params)); } Ref QueryBuilder::_del_bind(const String ¶ms) { return Ref(del(params)); } Ref QueryBuilder::_where_bind(const String ¶ms) { return Ref(where(params)); } Ref QueryBuilder::_from_bind(const String ¶ms) { return Ref(from(params)); } Ref QueryBuilder::_insert_bind(const String &table_name, const String &columns) { return Ref(insert(table_name, columns)); } Ref QueryBuilder::_values_bind(const String ¶ms_str) { return Ref(values(params_str)); } 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::_valph_bind() { return Ref(valph()); } Ref QueryBuilder::_valphi_bind(const String &p_id) { return Ref(valphi(p_id)); } Ref QueryBuilder::_valphr_bind(const String &p_raw_id) { return Ref(valphr(p_raw_id)); } Ref QueryBuilder::_like_bind(const String &str) { return Ref(like(str)); } Ref QueryBuilder::_sets_bind() { return Ref(sets()); } 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::_setph_bind(const String &col) { return Ref(setph(col)); } Ref QueryBuilder::_setphi_bind(const String &col, const String &p_id) { return Ref(setphi(col, p_id)); } Ref QueryBuilder::_setphr_bind(const String &col, const String &p_raw_id) { return Ref(setphr(col, p_raw_id)); } 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::_wph_bind(const String &col) { return Ref(wph(col)); } Ref QueryBuilder::_wphi_bind(const String &col, const String &p_id) { return Ref(wphi(col, p_id)); } Ref QueryBuilder::_wphr_bind(const String &col, const String &p_raw_id) { return Ref(wphr(col, p_raw_id)); } 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::_corder_by_bind() { return Ref(corder_by()); } 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()); } // 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)); } 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::_end_command_bind() { return Ref(end_command()); } Ref QueryBuilder::_reset_bind() { return Ref(reset()); }