From f660e2a15566c1c0e30ed36d0afd7c37bf2fb0c7 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 28 Jul 2016 01:11:21 +0300 Subject: [PATCH] Add a property to the MySQL result-set to retrieve all available field names as an array. --- modules/mysql/ResultSet.cpp | 25 +++++++++++++++++++++++++ modules/mysql/ResultSet.hpp | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/modules/mysql/ResultSet.cpp b/modules/mysql/ResultSet.cpp index c5e0ae1a..fa11b429 100644 --- a/modules/mysql/ResultSet.cpp +++ b/modules/mysql/ResultSet.cpp @@ -156,6 +156,30 @@ void ResultSet::ValidateField(Int32 idx) const } #endif // _DEBUG +// ------------------------------------------------------------------------------------------------ +Array ResultSet::FieldNames() const +{ + SQMOD_VALIDATE_CREATED(*this); + // Grab the number of available fields + const SQInteger fcount = ConvTo< SQInteger >::From(m_Handle->mFieldCount); + // Grab the array with field instances + const ResHnd::FieldType * fields = m_Handle->mFields; + // Is there even something to process? + if (!fcount || !fields) + { + return Array(DefaultVM::Get(), 0); + } + // Allocate an array with the same amount of elements as the number of fields + Array arr(DefaultVM::Get(), fcount); + // Iterate over all the available fields and insert them into the created array + for (SQInteger n = 0; n < fcount; ++n) + { + arr.SetValue(n, (fields[n].name == nullptr) ? ToStrF("", n) : fields[n].name); + } + // Return the resulted array + return arr; +} + // ================================================================================================ void Register_ResultSet(Table & sqlns) { @@ -170,6 +194,7 @@ void Register_ResultSet(Table & sqlns) .Func(_SC("_tostring"), &ResultSet::ToString) // Properties .Prop(_SC("IsValid"), &ResultSet::IsValid) + .Prop(_SC("FieldNames"), &ResultSet::FieldNames) .Prop(_SC("RowIndex"), &ResultSet::RowIndex) .Prop(_SC("RowCount"), &ResultSet::RowCount) // Member Methods diff --git a/modules/mysql/ResultSet.hpp b/modules/mysql/ResultSet.hpp index 060c23eb..98bd09a2 100644 --- a/modules/mysql/ResultSet.hpp +++ b/modules/mysql/ResultSet.hpp @@ -192,6 +192,11 @@ public: return m_Handle; } + /* -------------------------------------------------------------------------------------------- + * Returns an array with all the field names available in the managed result set. + */ + Array FieldNames() const; + /* -------------------------------------------------------------------------------------------- * Returns the current position of the row cursor for the last Next(). */