connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx |   82 ++++++++--------
 connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx |   22 ++++
 2 files changed, 63 insertions(+), 41 deletions(-)

New commits:
commit 763d490ddef10d6c6c0968d1cfcb231e1acc4cfb
Author:     Tamas Bunth <tamas.bu...@collabora.co.uk>
AuthorDate: Tue Jan 15 17:53:51 2019 +0100
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Thu Jan 17 13:37:11 2019 +0100

    mysqlc: fetch data exactly when it's needed
    
    It fixes the issue that one could not call getXXX on a result set
    on which next() method was never called before.
    
    Change-Id: I972bb9d475d192a14ba1534dcbdac81c20f211d0
    Reviewed-on: https://gerrit.libreoffice.org/66431
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>
    Tested-by: Andras Timar <andras.ti...@collabora.com>

diff --git a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx 
b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx
index 4f1945d9a769..079fea40daf7 100644
--- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx
+++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.cxx
@@ -138,6 +138,13 @@ void OResultSet::ensureResultFetched()
 
 void OResultSet::ensureFieldInfoFetched()
 {
+    if (m_bResultFetched)
+        return; // already fetched
+
+    // it works only if result set is produced via mysql_store_result
+    // TODO ensure that
+    m_nRowCount = mysql_num_rows(m_pResult);
+
     if (!m_aFields.empty())
         return;
     unsigned nFieldCount = mysql_num_fields(m_pResult);
@@ -152,11 +159,6 @@ void OResultSet::fetchResult()
 {
     // Mysql C API does not allow simultaneously opened result sets, but sdbc 
does.
     // Because of that we need to fetch all of the data ASAP
-
-    // it works only if result set is produced via mysql_store_result
-    // TODO ensure that
-    m_nRowCount = mysql_num_rows(m_pResult);
-
     ensureFieldInfoFetched();
 
     // fetch all the data
@@ -230,8 +232,7 @@ uno::Reference<XInputStream> SAL_CALL 
OResultSet::getBinaryStream(sal_Int32 colu
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return nullptr;
 
@@ -244,8 +245,7 @@ uno::Reference<XInputStream> SAL_CALL 
OResultSet::getCharacterStream(sal_Int32 c
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     
mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getCharacterStream",
                                                             *this);
     return nullptr;
@@ -255,8 +255,7 @@ sal_Bool SAL_CALL OResultSet::getBoolean(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return false;
 
@@ -268,8 +267,7 @@ sal_Int8 SAL_CALL OResultSet::getByte(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return 0;
 
@@ -282,6 +280,7 @@ uno::Sequence<sal_Int8> SAL_CALL 
OResultSet::getBytes(sal_Int32 column)
 {
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
     MutexGuard aGuard(m_aMutex);
+    checkBordersAndEnsureFetched(column);
     OString sVal = m_aRows[m_nRowPosition][column - 1];
     if (checkNull(column))
         return uno::Sequence<sal_Int8>();
@@ -294,8 +293,7 @@ Date SAL_CALL OResultSet::getDate(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     Date d;
 
@@ -332,8 +330,7 @@ double SAL_CALL OResultSet::getDouble(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     if (checkNull(column))
         return 0.0;
@@ -346,8 +343,7 @@ float SAL_CALL OResultSet::getFloat(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return 0.0f;
 
@@ -359,6 +355,7 @@ sal_Int32 SAL_CALL OResultSet::getInt(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return 0;
 
@@ -378,8 +375,7 @@ sal_Int64 SAL_CALL OResultSet::getLong(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return 0LL;
 
@@ -398,8 +394,7 @@ uno::Reference<XArray> SAL_CALL 
OResultSet::getArray(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     
mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getArray", 
*this);
     return nullptr;
@@ -409,8 +404,7 @@ uno::Reference<XClob> SAL_CALL 
OResultSet::getClob(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     
mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getClob", 
*this);
     return nullptr;
@@ -420,8 +414,7 @@ uno::Reference<XBlob> SAL_CALL 
OResultSet::getBlob(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     
mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getBlob", 
*this);
     return nullptr;
@@ -431,8 +424,7 @@ uno::Reference<XRef> SAL_CALL OResultSet::getRef(sal_Int32 
column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     
mysqlc_sdbc_driver::throwFeatureNotImplementedException("OResultSet::getRef", 
*this);
     return nullptr;
@@ -443,8 +435,7 @@ Any SAL_CALL OResultSet::getObject(sal_Int32 column,
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     Any aRet = Any();
 
@@ -456,8 +447,7 @@ sal_Int16 SAL_CALL OResultSet::getShort(sal_Int32 column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return 0;
 
@@ -469,8 +459,7 @@ rtl::OUString SAL_CALL OResultSet::getString(sal_Int32 
column)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
     if (checkNull(column))
         return rtl::OUString{};
 
@@ -482,9 +471,8 @@ Time SAL_CALL OResultSet::getTime(sal_Int32 column)
 {
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
     MutexGuard aGuard(m_aMutex);
+    checkBordersAndEnsureFetched(column);
 
-    checkColumnIndex(column);
-    checkRowIndex();
     Time t;
     if (checkNull(column))
         return t;
@@ -520,8 +508,7 @@ DateTime SAL_CALL OResultSet::getTimestamp(sal_Int32 column)
 {
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
     MutexGuard aGuard(m_aMutex);
-    checkColumnIndex(column);
-    checkRowIndex();
+    checkBordersAndEnsureFetched(column);
 
     if (checkNull(column))
         return DateTime{};
@@ -558,6 +545,7 @@ sal_Bool SAL_CALL OResultSet::isAfterLast()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
 
     return m_nRowPosition >= m_nRowCount;
 }
@@ -566,6 +554,7 @@ sal_Bool SAL_CALL OResultSet::isFirst()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
 
     return m_nRowPosition == 0 && !isAfterLast();
 }
@@ -574,6 +563,7 @@ sal_Bool SAL_CALL OResultSet::isLast()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
 
     return m_nRowPosition == m_nRowCount - 1;
 }
@@ -589,6 +579,7 @@ void SAL_CALL OResultSet::afterLast()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
     m_nRowPosition = m_nRowCount;
 }
 
@@ -614,7 +605,7 @@ sal_Bool SAL_CALL OResultSet::last()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    ensureResultFetched();
+    ensureFieldInfoFetched();
     m_nRowPosition = m_nRowCount - 1;
 
     return true;
@@ -624,6 +615,7 @@ sal_Bool SAL_CALL OResultSet::absolute(sal_Int32 row)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
 
     sal_Int32 nToGo = row < 0 ? (m_nRowCount - 1) - row : row - 1;
 
@@ -641,6 +633,7 @@ sal_Bool SAL_CALL OResultSet::relative(sal_Int32 row)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
+    ensureFieldInfoFetched();
 
     if (row == 0)
         return true;
@@ -704,7 +697,7 @@ sal_Bool SAL_CALL OResultSet::next()
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
-    ensureResultFetched();
+    ensureFieldInfoFetched();
     if (m_nRowPosition + 1 > m_nRowCount) // afterlast
         return false;
     if (m_nRowPosition + 1 == m_nRowCount) // last
@@ -1113,4 +1106,11 @@ void OResultSet::checkColumnIndex(sal_Int32 index)
     }
 }
 
+void OResultSet::checkBordersAndEnsureFetched(sal_Int32 index)
+{
+    ensureResultFetched();
+    checkColumnIndex(index);
+    checkRowIndex();
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx 
b/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx
index 75311d3dd4d0..91202f5ff716 100644
--- a/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx
+++ b/connectivity/source/drivers/mysqlc/mysqlc_resultset.hxx
@@ -102,8 +102,30 @@ class OResultSet final : public OBase_Mutex,
 
     virtual ~OResultSet() override = default;
 
+    /**
+     * Ensures that the results of the query has already been fetched.
+     */
     void ensureResultFetched();
+
+    /**
+     * Ensures that meta data of the corresponding result set has been already
+     * queried. It should be called before freeing the result set, unless the
+     * information is lost.
+     */
     void ensureFieldInfoFetched();
+
+    /**
+     * Check the following things:
+     * - cursor is out of range. Throws expception if true.
+     * - column index is out of range. Throws exception if true.
+     * - result set is fetched. If no, then it fetches the result.
+     */
+    void checkBordersAndEnsureFetched(sal_Int32 index);
+
+    /**
+     * Fetches all the data from the MYSQL_RES object related to the class. It
+     * frees the MYSQL_RES object afterwards, so it cannot be used anymore.
+     */
     void fetchResult();
 
 public:
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to