[Libreoffice-commits] core.git: mysqlc/source

Noel Grandin noel.grandin at collabora.co.uk
Fri Oct 6 08:21:55 UTC 2017


 mysqlc/source/mysqlc_connection.cxx        |    6 +--
 mysqlc/source/mysqlc_connection.hxx        |    5 +--
 mysqlc/source/mysqlc_preparedstatement.cxx |   45 ++++++++++++++---------------
 mysqlc/source/mysqlc_statement.cxx         |   34 +++++++++------------
 mysqlc/source/mysqlc_statement.hxx         |    5 +--
 5 files changed, 45 insertions(+), 50 deletions(-)

New commits:
commit 7af521827e62aec9a695dce944d350fae7625b19
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Oct 6 09:31:05 2017 +0200

    use rtl::Reference in mysqlc
    
    instead of manual ref-counting
    
    Change-Id: I7b7c350613976463620a54757add6061cf383a4c
    Reviewed-on: https://gerrit.libreoffice.org/43183
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/mysqlc/source/mysqlc_connection.cxx b/mysqlc/source/mysqlc_connection.cxx
index 1618bdcd85da..f44fca89e205 100644
--- a/mysqlc/source/mysqlc_connection.cxx
+++ b/mysqlc/source/mysqlc_connection.cxx
@@ -65,10 +65,9 @@ OConnection::OConnection(MysqlCDriver& _rDriver, sql::Driver * _cppDriver)
     :OMetaConnection_BASE(m_aMutex)
     ,OSubComponent<OConnection, OConnection_BASE>(static_cast<cppu::OWeakObject*>(&_rDriver), this)
     ,m_xMetaData(nullptr)
-    ,m_rDriver(_rDriver)
+    ,m_xDriver(&_rDriver)
     ,cppDriver(_cppDriver)
 {
-    m_rDriver.acquire();
 }
 
 OConnection::~OConnection()
@@ -76,7 +75,6 @@ OConnection::~OConnection()
     if (!isClosed()) {
         close();
     }
-    m_rDriver.release();
 }
 
 void SAL_CALL OConnection::release()
@@ -536,7 +534,7 @@ rtl::OUString OConnection::transFormPreparedStatement(const rtl::OUString& _sSQL
             Reference< XConnection> xCon = this;
             aArgs[0] <<= NamedValue(rtl::OUString("ActiveConnection"), makeAny(xCon));
 
-            m_xParameterSubstitution.set(m_rDriver.getFactory()->createInstanceWithArguments("org.openoffice.comp.helper.ParameterSubstitution",aArgs),UNO_QUERY);
+            m_xParameterSubstitution.set(m_xDriver->getFactory()->createInstanceWithArguments("org.openoffice.comp.helper.ParameterSubstitution",aArgs),UNO_QUERY);
         } catch(const Exception&) {}
     }
     if ( m_xParameterSubstitution.is() ) {
diff --git a/mysqlc/source/mysqlc_connection.hxx b/mysqlc/source/mysqlc_connection.hxx
index 5e62eff048d0..4ef6bb044a45 100644
--- a/mysqlc/source/mysqlc_connection.hxx
+++ b/mysqlc/source/mysqlc_connection.hxx
@@ -41,6 +41,7 @@
 #include <cppuhelper/compbase3.hxx>
 #include <cppuhelper/weakref.hxx>
 #include <rtl/string.hxx>
+#include <rtl/ref.hxx>
 
 #include <map>
 
@@ -104,7 +105,7 @@ namespace connectivity
                                             // of all the Statement objects
                                             // for this Connection
 
-            MysqlCDriver&   m_rDriver;      // Pointer to the owning driver object
+            rtl::Reference<MysqlCDriver> m_xDriver; // Pointer to the owning driver object
             sql::Driver*    cppDriver;
 
         public:
@@ -185,7 +186,7 @@ namespace connectivity
             const ConnectionSettings& getConnectionSettings() const { return m_settings; }
             rtl::OUString transFormPreparedStatement(const rtl::OUString& _sSQL);
 
-            const MysqlCDriver&  getDriver()         const { return m_rDriver;}
+            const MysqlCDriver&  getDriver()         const { return *m_xDriver.get();}
 
         }; /* OConnection */
         // TODO: Not used.
diff --git a/mysqlc/source/mysqlc_preparedstatement.cxx b/mysqlc/source/mysqlc_preparedstatement.cxx
index 84d353ef5873..ec5b6f68bdb0 100644
--- a/mysqlc/source/mysqlc_preparedstatement.cxx
+++ b/mysqlc/source/mysqlc_preparedstatement.cxx
@@ -75,13 +75,12 @@ sal_Bool OPreparedStatement::supportsService(rtl::OUString const & ServiceName)
 OPreparedStatement::OPreparedStatement(OConnection* _pConnection, sql::PreparedStatement * _cppPrepStmt)
     :OCommonStatement(_pConnection, _cppPrepStmt)
 {
-    m_pConnection = _pConnection;
-    m_pConnection->acquire();
+    m_xConnection = _pConnection;
 
     try {
         m_paramCount = static_cast<sql::PreparedStatement *>(cppStatement)->getParameterMetaData()->getParameterCount();
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -130,7 +129,7 @@ Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData()
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::getMetaData", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return m_xMetaData;
 }
@@ -161,7 +160,7 @@ sal_Bool SAL_CALL OPreparedStatement::execute()
     try {
         success = static_cast<sql::PreparedStatement *>(cppStatement)->execute();
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return success;
 }
@@ -175,7 +174,7 @@ sal_Int32 SAL_CALL OPreparedStatement::executeUpdate()
     try {
         affectedRows = static_cast<sql::PreparedStatement *>(cppStatement)->executeUpdate();
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return affectedRows;
 }
@@ -187,12 +186,12 @@ void SAL_CALL OPreparedStatement::setString(sal_Int32 parameter, const rtl::OUSt
     checkParameterIndex(parameter);
 
     try {
-        std::string stringie(rtl::OUStringToOString(x, m_pConnection->getConnectionEncoding()).getStr());
+        std::string stringie(rtl::OUStringToOString(x, m_xConnection->getConnectionEncoding()).getStr());
         static_cast<sql::PreparedStatement *>(cppStatement)->setString(parameter, stringie);
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::clearParameters", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -201,7 +200,7 @@ Reference< XConnection > SAL_CALL OPreparedStatement::getConnection()
     MutexGuard aGuard(m_aMutex);
     checkDisposed(OPreparedStatement::rBHelper.bDisposed);
 
-    return m_pConnection;
+    return m_xConnection.get();
 }
 
 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery()
@@ -214,7 +213,7 @@ Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery()
         sql::ResultSet * res = static_cast<sql::PreparedStatement *>(cppStatement)->executeQuery();
         xResultSet = new OResultSet(this, res, getOwnConnection()->getConnectionEncoding());
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return xResultSet;
 }
@@ -230,7 +229,7 @@ void SAL_CALL OPreparedStatement::setBoolean(sal_Int32 parameter, sal_Bool x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBoolean", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -245,7 +244,7 @@ void SAL_CALL OPreparedStatement::setByte(sal_Int32 parameter, sal_Int8 x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setByte", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -268,7 +267,7 @@ void SAL_CALL OPreparedStatement::setDate(sal_Int32 parameter, const Date& aData
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setDate", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -291,7 +290,7 @@ void SAL_CALL OPreparedStatement::setTime(sal_Int32 parameter, const Time& aVal)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setTime", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -322,7 +321,7 @@ void SAL_CALL OPreparedStatement::setTimestamp(sal_Int32 parameter, const DateTi
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setTimestamp", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -337,7 +336,7 @@ void SAL_CALL OPreparedStatement::setDouble(sal_Int32 parameter, double x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setDouble", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -352,7 +351,7 @@ void SAL_CALL OPreparedStatement::setFloat(sal_Int32 parameter, float x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setFloat", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -367,7 +366,7 @@ void SAL_CALL OPreparedStatement::setInt(sal_Int32 parameter, sal_Int32 x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setInt", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -382,7 +381,7 @@ void SAL_CALL OPreparedStatement::setLong(sal_Int32 parameter, sal_Int64 aVal)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setLong", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -397,7 +396,7 @@ void SAL_CALL OPreparedStatement::setNull(sal_Int32 parameter, sal_Int32 sqlType
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setNull", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -635,7 +634,7 @@ void SAL_CALL OPreparedStatement::setShort(sal_Int32 parameter, sal_Int16 x)
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setShort", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -651,7 +650,7 @@ void SAL_CALL OPreparedStatement::setBytes(sal_Int32 parameter, const Sequence<
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBytes", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
@@ -687,7 +686,7 @@ void SAL_CALL OPreparedStatement::clearParameters()
     } catch (const sql::MethodNotImplementedException &) {
         mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::clearParameters", *this);
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
 }
 
diff --git a/mysqlc/source/mysqlc_statement.cxx b/mysqlc/source/mysqlc_statement.cxx
index 96ab88de79ee..0448ac5dcabc 100644
--- a/mysqlc/source/mysqlc_statement.cxx
+++ b/mysqlc/source/mysqlc_statement.cxx
@@ -56,10 +56,9 @@ OCommonStatement::OCommonStatement(OConnection* _pConnection, sql::Statement *_c
     :OCommonStatement_IBase(m_aMutex)
     ,OPropertySetHelper(OCommonStatement_IBase::rBHelper)
     ,OStatement_CBase( static_cast<cppu::OWeakObject*>(_pConnection), this )
-    ,m_pConnection(_pConnection)
+    ,m_xConnection(_pConnection)
     ,cppStatement(_cppStatement)
 {
-    m_pConnection->acquire();
 }
 
 OCommonStatement::~OCommonStatement()
@@ -79,10 +78,7 @@ void OCommonStatement::disposing()
 
     disposeResultSet();
 
-    if (m_pConnection) {
-        m_pConnection->release();
-        m_pConnection = nullptr;
-    }
+    m_xConnection.clear();
     delete cppStatement;
 
     dispose_ChildImpl();
@@ -136,13 +132,13 @@ sal_Bool SAL_CALL OCommonStatement::execute(const rtl::OUString& sql)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(rBHelper.bDisposed);
-    const rtl::OUString sSqlStatement = m_pConnection->transFormPreparedStatement( sql );
+    const rtl::OUString sSqlStatement = m_xConnection->transFormPreparedStatement( sql );
 
     bool success = false;
     try {
-        success = cppStatement->execute(rtl::OUStringToOString(sSqlStatement, m_pConnection->getConnectionSettings().encoding).getStr());
+        success = cppStatement->execute(rtl::OUStringToOString(sSqlStatement, m_xConnection->getConnectionSettings().encoding).getStr());
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return success;
 }
@@ -151,15 +147,15 @@ Reference< XResultSet > SAL_CALL OCommonStatement::executeQuery(const rtl::OUStr
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(rBHelper.bDisposed);
-    const rtl::OUString sSqlStatement = m_pConnection->transFormPreparedStatement(sql);
+    const rtl::OUString sSqlStatement = m_xConnection->transFormPreparedStatement(sql);
 
     Reference< XResultSet > xResultSet;
     try {
-        std::unique_ptr< sql::ResultSet > rset(cppStatement->executeQuery(rtl::OUStringToOString(sSqlStatement, m_pConnection->getConnectionEncoding()).getStr()));
-        xResultSet = new OResultSet(this, rset.get(), m_pConnection->getConnectionEncoding());
+        std::unique_ptr< sql::ResultSet > rset(cppStatement->executeQuery(rtl::OUStringToOString(sSqlStatement, m_xConnection->getConnectionEncoding()).getStr()));
+        xResultSet = new OResultSet(this, rset.get(), m_xConnection->getConnectionEncoding());
         rset.release();
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return xResultSet;
 }
@@ -170,7 +166,7 @@ Reference< XConnection > SAL_CALL OCommonStatement::getConnection()
     checkDisposed(rBHelper.bDisposed);
 
     // just return our connection here
-    return m_pConnection;
+    return m_xConnection.get();
 }
 
 sal_Int32 SAL_CALL OCommonStatement::getUpdateCount()
@@ -206,13 +202,13 @@ sal_Int32 SAL_CALL OCommonStatement::executeUpdate(const rtl::OUString& sql)
 {
     MutexGuard aGuard(m_aMutex);
     checkDisposed(rBHelper.bDisposed);
-    const rtl::OUString sSqlStatement = m_pConnection->transFormPreparedStatement(sql);
+    const rtl::OUString sSqlStatement = m_xConnection->transFormPreparedStatement(sql);
 
     sal_Int32 affectedRows = 0;
     try {
-        affectedRows = cppStatement->executeUpdate(rtl::OUStringToOString(sSqlStatement, m_pConnection->getConnectionEncoding()).getStr());
+        affectedRows = cppStatement->executeUpdate(rtl::OUStringToOString(sSqlStatement, m_xConnection->getConnectionEncoding()).getStr());
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return affectedRows;
 }
@@ -225,10 +221,10 @@ Reference< XResultSet > SAL_CALL OCommonStatement::getResultSet()
     Reference< XResultSet > xResultSet;
     try {
         std::unique_ptr< sql::ResultSet > rset(cppStatement->getResultSet());
-        xResultSet = new OResultSet(this, rset.get(), m_pConnection->getConnectionEncoding());
+        xResultSet = new OResultSet(this, rset.get(), m_xConnection->getConnectionEncoding());
         rset.release();
     } catch (const sql::SQLException &e) {
-        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding());
+        mysqlc_sdbc_driver::translateAndThrow(e, *this, m_xConnection->getConnectionEncoding());
     }
     return xResultSet;
 }
diff --git a/mysqlc/source/mysqlc_statement.hxx b/mysqlc/source/mysqlc_statement.hxx
index 61d69e824dd7..4a9e754ce175 100644
--- a/mysqlc/source/mysqlc_statement.hxx
+++ b/mysqlc/source/mysqlc_statement.hxx
@@ -34,6 +34,7 @@
 
 #include <cppconn/statement.h>
 #include <cppuhelper/compbase5.hxx>
+#include <rtl/ref.hxx>
 
 namespace connectivity
 {
@@ -70,7 +71,7 @@ namespace connectivity
             SQLWarning m_aLastWarning;
 
         protected:
-            OConnection*            m_pConnection;  // The owning Connection object
+            rtl::Reference<OConnection> m_xConnection;  // The owning Connection object
 
             sql::Statement          *cppStatement;
 
@@ -142,7 +143,7 @@ namespace connectivity
             sal_Bool SAL_CALL getMoreResults() SAL_OVERRIDE;
 
             // other methods
-            OConnection* getOwnConnection() const { return m_pConnection;}
+            OConnection* getOwnConnection() const { return m_xConnection.get();}
 
         private:
             using ::cppu::OPropertySetHelper::getFastPropertyValue;


More information about the Libreoffice-commits mailing list