From fe7f931dc8ecea7c2856b42eeea8752aa58673fd Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 11 Dec 2024 16:59:27 +0100 Subject: [PATCH] Added next_column*() helper methods to PreparedStatement. --- modules/database/prepared_statement.cpp | 71 +++++++++++++++++++ modules/database/prepared_statement.h | 20 ++++++ .../sqlite3_prepared_statement.cpp | 8 +++ 3 files changed, 99 insertions(+) diff --git a/modules/database/prepared_statement.cpp b/modules/database/prepared_statement.cpp index 98318d6c4..a43956370 100644 --- a/modules/database/prepared_statement.cpp +++ b/modules/database/prepared_statement.cpp @@ -40,7 +40,57 @@ void PreparedStatement::set_sql(const String &p_sql) { _sql = p_sql; } +int PreparedStatement::get_current_column_index() const { + return _current_column_index; +} +void PreparedStatement::set_current_column_index(const int p_index) { + _current_column_index = p_index; +} + +String PreparedStatement::next_column_name() { + return column_name(_current_column_index++); +} +String PreparedStatement::next_column_decltype() { + return column_decltype(_current_column_index++); +} +PreparedStatement::Type PreparedStatement::next_column_type() { + return column_type(_current_column_index++); +} + +String PreparedStatement::next_column_database_name() { + return column_database_name(_current_column_index++); +} +String PreparedStatement::next_column_table_name() { + return column_table_name(_current_column_index++); +} +String PreparedStatement::next_column_origin_name() { + return column_origin_name(_current_column_index++); +} + +Vector PreparedStatement::next_column_blob() { + return column_blob(_current_column_index++); +} +float PreparedStatement::next_column_float() { + return column_float(_current_column_index++); +} +double PreparedStatement::next_column_double() { + return column_double(_current_column_index++); +} +int64_t PreparedStatement::next_column_int() { + return column_int(_current_column_index++); +} +int PreparedStatement::next_column_int64() { + return column_int64(_current_column_index++); +} +String PreparedStatement::next_column_text() { + return column_text(_current_column_index++); +} +Variant PreparedStatement::next_column_value() { + return column_value(_current_column_index++); +} + PreparedStatement::PreparedStatement() { + _current_column_index = 0; } PreparedStatement::~PreparedStatement() { @@ -95,6 +145,27 @@ void PreparedStatement::_bind_methods() { ClassDB::bind_method(D_METHOD("get_last_insert_rowid"), &PreparedStatement::get_last_insert_rowid); + + ClassDB::bind_method(D_METHOD("get_current_column_index"), &PreparedStatement::get_current_column_index); + ClassDB::bind_method(D_METHOD("set_current_column_index", "index"), &PreparedStatement::set_current_column_index); + ADD_PROPERTY(PropertyInfo(Variant::INT, "current_column_index"), "set_current_column_index", "get_current_column_index"); + + ClassDB::bind_method(D_METHOD("next_column_name"), &PreparedStatement::next_column_name); + ClassDB::bind_method(D_METHOD("next_column_decltype"), &PreparedStatement::next_column_decltype); + ClassDB::bind_method(D_METHOD("next_column_type"), &PreparedStatement::next_column_type); + + ClassDB::bind_method(D_METHOD("next_column_database_name"), &PreparedStatement::next_column_database_name); + ClassDB::bind_method(D_METHOD("next_column_table_name"), &PreparedStatement::next_column_table_name); + ClassDB::bind_method(D_METHOD("next_column_origin_name"), &PreparedStatement::next_column_origin_name); + + ClassDB::bind_method(D_METHOD("next_column_blob"), &PreparedStatement::next_column_blob); + ClassDB::bind_method(D_METHOD("next_column_float"), &PreparedStatement::next_column_float); + ClassDB::bind_method(D_METHOD("next_column_double"), &PreparedStatement::next_column_double); + ClassDB::bind_method(D_METHOD("next_column_int"), &PreparedStatement::next_column_int); + ClassDB::bind_method(D_METHOD("next_column_int64"), &PreparedStatement::next_column_int64); + ClassDB::bind_method(D_METHOD("next_column_text"), &PreparedStatement::next_column_text); + ClassDB::bind_method(D_METHOD("next_column_value"), &PreparedStatement::next_column_value); + // Control ClassDB::bind_method(D_METHOD("prepare"), &PreparedStatement::prepare); diff --git a/modules/database/prepared_statement.h b/modules/database/prepared_statement.h index d7b1ec81d..0d86aecb3 100644 --- a/modules/database/prepared_statement.h +++ b/modules/database/prepared_statement.h @@ -102,6 +102,25 @@ class PreparedStatement : public Reference { virtual int get_last_insert_rowid() = 0; + int get_current_column_index() const; + void set_current_column_index(const int p_index); + + String next_column_name(); + String next_column_decltype(); + Type next_column_type(); + + String next_column_database_name(); + String next_column_table_name(); + String next_column_origin_name(); + + Vector next_column_blob(); + float next_column_float(); + double next_column_double(); + int64_t next_column_int(); + int next_column_int64(); + String next_column_text(); + Variant next_column_value(); + // Control virtual Error prepare() = 0; virtual Error step() = 0; @@ -118,6 +137,7 @@ class PreparedStatement : public Reference { static void _bind_methods(); String _sql; + int _current_column_index; }; VARIANT_ENUM_CAST(PreparedStatement::Type); diff --git a/modules/database_sqlite/sqlite3_prepared_statement.cpp b/modules/database_sqlite/sqlite3_prepared_statement.cpp index 85b499993..5e36c8aa9 100644 --- a/modules/database_sqlite/sqlite3_prepared_statement.cpp +++ b/modules/database_sqlite/sqlite3_prepared_statement.cpp @@ -448,6 +448,8 @@ int SQLite3PreparedStatement::get_last_insert_rowid() { Error SQLite3PreparedStatement::prepare() { ERR_FAIL_COND_V(!_connection.is_valid(), FAILED); + _current_column_index = 0; + CharString cs = _sql.utf8(); int res = sqlite3_prepare_v2(_connection->conn, cs.get_data(), cs.size(), &_prepared_statement, NULL); @@ -463,6 +465,8 @@ Error SQLite3PreparedStatement::step() { return ERR_UNCONFIGURED; } + _current_column_index = 0; + int res = sqlite3_step(_prepared_statement); if (res == SQLITE_ROW) { @@ -487,6 +491,8 @@ Error SQLite3PreparedStatement::reset() { return ERR_UNCONFIGURED; } + _current_column_index = 0; + int res = sqlite3_reset(_prepared_statement); if (res != SQLITE_OK) { @@ -500,6 +506,8 @@ Error SQLite3PreparedStatement::finalize() { return OK; } + _current_column_index = 0; + sqlite3_finalize(_prepared_statement); _prepared_statement = NULL;