Skip to content

Commit

Permalink
Added next_column*() helper methods to PreparedStatement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Dec 11, 2024
1 parent 9da9dbc commit fe7f931
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
71 changes: 71 additions & 0 deletions modules/database/prepared_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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() {
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions modules/database/prepared_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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;
Expand All @@ -118,6 +137,7 @@ class PreparedStatement : public Reference {
static void _bind_methods();

String _sql;
int _current_column_index;
};

VARIANT_ENUM_CAST(PreparedStatement::Type);
Expand Down
8 changes: 8 additions & 0 deletions modules/database_sqlite/sqlite3_prepared_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -463,6 +465,8 @@ Error SQLite3PreparedStatement::step() {
return ERR_UNCONFIGURED;
}

_current_column_index = 0;

int res = sqlite3_step(_prepared_statement);

if (res == SQLITE_ROW) {
Expand All @@ -487,6 +491,8 @@ Error SQLite3PreparedStatement::reset() {
return ERR_UNCONFIGURED;
}

_current_column_index = 0;

int res = sqlite3_reset(_prepared_statement);

if (res != SQLITE_OK) {
Expand All @@ -500,6 +506,8 @@ Error SQLite3PreparedStatement::finalize() {
return OK;
}

_current_column_index = 0;

sqlite3_finalize(_prepared_statement);

_prepared_statement = NULL;
Expand Down

0 comments on commit fe7f931

Please sign in to comment.