From 27df4fe282139119f4e87adaf28a7e964881ed3c Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 28 Jul 2016 01:15:39 +0300 Subject: [PATCH] Add a property to the MySQL result-set to retrieve field wrapper instances for all available fields as a table. --- modules/mysql/ResultSet.cpp | 31 +++++++++++++++++++++++++++++++ modules/mysql/ResultSet.hpp | 5 +++++ 2 files changed, 36 insertions(+) diff --git a/modules/mysql/ResultSet.cpp b/modules/mysql/ResultSet.cpp index c64557b2..87e27a34 100644 --- a/modules/mysql/ResultSet.cpp +++ b/modules/mysql/ResultSet.cpp @@ -207,6 +207,36 @@ Array ResultSet::GetFieldsArray() const return arr; } +// ------------------------------------------------------------------------------------------------ +Table ResultSet::GetFieldsTable() 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 Table(); + } + // Create a field instance to insert as copy + Field field(m_Handle); + // Allocate a table to be populated with field instances + Table tbl; + // Iterate over all the available fields and insert them into the created table + for (SQInteger n = 0; n < fcount; ++n) + { + // Update the field index + field.SetIndex(ConvTo< Int32 >::From(n)); + // Insert a copy of the field instance into the table + tbl.SetValue((fields[n].name == nullptr) ? ToStrF("", n) : fields[n].name, field); + } + // Return the resulted table + return tbl; +} + + // ================================================================================================ void Register_ResultSet(Table & sqlns) { @@ -223,6 +253,7 @@ void Register_ResultSet(Table & sqlns) .Prop(_SC("IsValid"), &ResultSet::IsValid) .Prop(_SC("FieldNames"), &ResultSet::FieldNames) .Prop(_SC("FieldsArray"), &ResultSet::GetFieldsArray) + .Prop(_SC("FieldsTable"), &ResultSet::GetFieldsTable) .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 41c04ecd..94ebd614 100644 --- a/modules/mysql/ResultSet.hpp +++ b/modules/mysql/ResultSet.hpp @@ -202,6 +202,11 @@ public: */ Array GetFieldsArray() const; + /* -------------------------------------------------------------------------------------------- + * Returns a table with wrapper instances for all the field available in the managed result set. + */ + Table GetFieldsTable() const; + /* -------------------------------------------------------------------------------------------- * Returns the current position of the row cursor for the last Next(). */