diff --git a/modules/mysql/Handle/ResultSet.cpp b/modules/mysql/Handle/ResultSet.cpp index 072691a1..8640b353 100644 --- a/modules/mysql/Handle/ResultSet.cpp +++ b/modules/mysql/Handle/ResultSet.cpp @@ -326,4 +326,81 @@ void ResHnd::Create(const StmtRef & stmt) } } +// ------------------------------------------------------------------------------------------------ +Uint64 ResHnd::RowIndex() const +{ + // Is this result-set even valid? + if (!mPtr) + { + STHROWF("Invalid MySQL result-set"); + } + // Did we come from a statement? + else if (mStatement) + { + return (Uint64)mysql_stmt_row_tell(mStatement->mPtr); + } + // Just retrieve it from the result set + return (Uint64)mysql_row_tell(mPtr); +} + +// ------------------------------------------------------------------------------------------------ +Uint64 ResHnd::RowCount() const +{ + // Is this result-set even valid? + if (!mPtr) + { + STHROWF("Invalid MySQL result-set"); + } + // Did we come from a statement? + else if (mStatement) + { + return mysql_stmt_num_rows(mStatement->mPtr); + } + // Just retrieve it from the result set + return mysql_num_rows(mPtr); +} + +// ------------------------------------------------------------------------------------------------ +bool ResHnd::Next() +{ + // Is this result-set even valid? + if (!mPtr) + { + STHROWF("Invalid MySQL result-set"); + } + // Did we come from a statement? + if (mStatement) + { + // Step the statement + return !mysql_stmt_fetch(mStatement->mPtr); + } + // Fetch another row from the result set + mRow = mysql_fetch_row(mPtr); + // Fetch the data lengths + mLengths = mysql_fetch_lengths(mPtr); + // Return whether the fetched row is valid + return (mRow != NULL); +} + +// ------------------------------------------------------------------------------------------------ +bool ResHnd::SetRowIndex(Uint64 index) +{ + // Is this result-set even valid? + if (!mPtr) + { + STHROWF("Invalid MySQL result-set"); + } + // Did we come from a statement? + else if (mStatement) + { + mysql_stmt_data_seek(mStatement->mPtr, index); + } + else + { + mysql_data_seek(mPtr, index); + } + // Step the result set + return Next(); +} + } // Namespace:: SqMod diff --git a/modules/mysql/Handle/ResultSet.hpp b/modules/mysql/Handle/ResultSet.hpp index b0a27ae7..ecb73e87 100644 --- a/modules/mysql/Handle/ResultSet.hpp +++ b/modules/mysql/Handle/ResultSet.hpp @@ -201,6 +201,27 @@ public: * Create the result-set from a Statement. */ void Create(const StmtRef & stmt); + + /* -------------------------------------------------------------------------------------------- + * Returns the current position of the row cursor for the last Next(). + */ + Uint64 RowIndex() const; + + /* -------------------------------------------------------------------------------------------- + * Returns the number of rows in the result set. + */ + Uint64 RowCount() const; + + /* -------------------------------------------------------------------------------------------- + * Retrieve the next row from the query. + */ + bool Next(); + + /* -------------------------------------------------------------------------------------------- + * Seeks to an arbitrary row in a query result set. + */ + bool SetRowIndex(Uint64 index); + }; diff --git a/modules/mysql/ResultSet.cpp b/modules/mysql/ResultSet.cpp index b208b80d..810a3723 100644 --- a/modules/mysql/ResultSet.cpp +++ b/modules/mysql/ResultSet.cpp @@ -1,6 +1,9 @@ // ------------------------------------------------------------------------------------------------ #include "ResultSet.hpp" +// ------------------------------------------------------------------------------------------------ +#include + // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -113,7 +116,7 @@ SQInteger ResultSet::GetInt8(Uint32 idx) const return ConvTo< Int8 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Int8 >::From(*reinterpret_cast< Int8 ** >(m_Handle->mRow)[idx]); + return ConvTo< Int8 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -126,7 +129,7 @@ SQInteger ResultSet::GetUint8(Uint32 idx) const return ConvTo< Uint8 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Uint8 >::From(*reinterpret_cast< Uint8 ** >(m_Handle->mRow)[idx]); + return ConvTo< Uint8 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -139,7 +142,7 @@ SQInteger ResultSet::GetInt16(Uint32 idx) const return ConvTo< Int16 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Int16 >::From(*reinterpret_cast< Int16 ** >(m_Handle->mRow)[idx]); + return ConvTo< Int16 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -152,7 +155,7 @@ SQInteger ResultSet::GetUint16(Uint32 idx) const return ConvTo< Uint16 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Uint16 >::From(*reinterpret_cast< Uint16 ** >(m_Handle->mRow)[idx]); + return ConvTo< Uint16 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -165,7 +168,7 @@ SQInteger ResultSet::GetInt32(Uint32 idx) const return ConvTo< Int32 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Int32 >::From(*reinterpret_cast< Int32 ** >(m_Handle->mRow)[idx]); + return ConvTo< Int32 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -178,7 +181,7 @@ SQInteger ResultSet::GetUint32(Uint32 idx) const return ConvTo< Uint32 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Uint32 >::From(*reinterpret_cast< Uint32 ** >(m_Handle->mRow)[idx]); + return ConvTo< Uint32 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10)); } // ------------------------------------------------------------------------------------------------ @@ -191,7 +194,7 @@ Int64 ResultSet::GetInt64(Uint32 idx) const return ConvTo< Int64 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Int64 >::From(*reinterpret_cast< Int64 ** >(m_Handle->mRow)[idx]); + return std::strtoll(m_Handle->mRow[idx], nullptr, 10); } // ------------------------------------------------------------------------------------------------ @@ -204,7 +207,7 @@ Uint64 ResultSet::GetUint64(Uint32 idx) const return ConvTo< Uint64 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Uint64 >::From(*reinterpret_cast< Uint64 ** >(m_Handle->mRow)[idx]); + return std::strtoull(m_Handle->mRow[idx], nullptr, 10); } // ------------------------------------------------------------------------------------------------ @@ -217,7 +220,7 @@ SQFloat ResultSet::GetFloat32(Uint32 idx) const return ConvTo< Float32 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Float32 >::From(*reinterpret_cast< Float32 ** >(m_Handle->mRow)[idx]); + return std::strtof(m_Handle->mRow[idx], nullptr); } // ------------------------------------------------------------------------------------------------ @@ -230,7 +233,7 @@ SQFloat ResultSet::GetFloat64(Uint32 idx) const return ConvTo< Float64 >::From(m_Handle->mBinds[idx].mInt64); } // Retrieve the value directly from the row - return ConvTo< Float64 >::From(*reinterpret_cast< Float64 ** >(m_Handle->mRow)[idx]); + return std::strtod(m_Handle->mRow[idx], nullptr); } // ------------------------------------------------------------------------------------------------ @@ -243,7 +246,7 @@ bool ResultSet::GetBoolean(Uint32 idx) const return ConvTo< bool >::From(m_Handle->mBinds[idx].mUint64); } // Retrieve the value directly from the row - return ConvTo< bool >::From(*reinterpret_cast< Uint8 ** >(m_Handle->mRow)[idx]); + return ConvTo< bool >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10)); } // ================================================================================================ @@ -260,7 +263,12 @@ void Register_ResultSet(Table & sqlns) .Func(_SC("_tostring"), &ResultSet::ToString) // Properties .Prop(_SC("IsValid"), &ResultSet::IsValid) + .Prop(_SC("RowIndex"), &ResultSet::RowIndex) + .Prop(_SC("RowCount"), &ResultSet::RowCount) // Member Methods + .Func(_SC("Next"), &ResultSet::Next) + .Func(_SC("SetRowIndex"), &ResultSet::SetRowIndex) + .Func(_SC("SetLongRowIndex"), &ResultSet::SetLongRowIndex) .Func(_SC("GetInt8"), &ResultSet::GetInt8) .Func(_SC("GetUint8"), &ResultSet::GetUint8) .Func(_SC("GetInt16"), &ResultSet::GetInt16) diff --git a/modules/mysql/ResultSet.hpp b/modules/mysql/ResultSet.hpp index fffc257c..63f5af7c 100644 --- a/modules/mysql/ResultSet.hpp +++ b/modules/mysql/ResultSet.hpp @@ -173,6 +173,46 @@ public: return m_Handle; } + /* -------------------------------------------------------------------------------------------- + * Returns the current position of the row cursor for the last Next(). + */ + Object RowIndex() const + { + return MakeULongObj(SQMOD_GET_CREATED(*this)->RowIndex()); + } + + /* -------------------------------------------------------------------------------------------- + * Returns the number of rows in the result set. + */ + Object RowCount() const + { + return MakeULongObj(SQMOD_GET_CREATED(*this)->RowCount()); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the next row from the query. + */ + bool Next() const + { + return SQMOD_GET_CREATED(*this)->Next(); + } + + /* -------------------------------------------------------------------------------------------- + * Seeks to an arbitrary row in a query result set. + */ + bool SetRowIndex(SQInteger index) const + { + return SQMOD_GET_CREATED(*this)->SetRowIndex(ConvTo< Uint64 >::From(index)); + } + + /* -------------------------------------------------------------------------------------------- + * Seeks to an arbitrary row in a query result set. + */ + bool SetLongRowIndex(Object & index) const + { + return SQMOD_GET_CREATED(*this)->SetRowIndex(FetchULongObjVal(index)); + } + /* -------------------------------------------------------------------------------------------- * Retrieve a signed 8 bit integer from a field. */