Tag: cws_src680_dba201b User: fs Date: 05/07/21 05:00:35 Modified: /dba/connectivity/source/commontools/ dbtools.cxx
Log: #i52255# +ensureRowSetConnection File Changes: Directory: /dba/connectivity/source/commontools/ ================================================ File [changed]: dbtools.cxx Url: http://dba.openoffice.org/source/browse/dba/connectivity/source/commontools/dbtools.cxx?r1=1.55.54.1&r2=1.55.54.2 Delta lines: +126 -74 ---------------------- --- dbtools.cxx 11 Jul 2005 13:56:36 -0000 1.55.54.1 +++ dbtools.cxx 21 Jul 2005 12:00:33 -0000 1.55.54.2 @@ -2,9 +2,9 @@ * * $RCSfile: dbtools.cxx,v $ * - * $Revision: 1.55.54.1 $ + * $Revision: 1.55.54.2 $ * - * last change: $Author: fs $ $Date: 2005/07/11 13:56:36 $ + * last change: $Author: fs $ $Date: 2005/07/21 12:00:33 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -500,24 +500,46 @@ } //------------------------------------------------------------------------------ -Reference< XConnection> connectRowset(const Reference< XRowSet>& _rxRowSet, const Reference< XMultiServiceFactory>& _rxFactory, - sal_Bool _bSetAsActiveConnection ) SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) ) +// helper function which allows to implement both the connectRowset and the ensureRowSetConnection semantics +// if connectRowset (which is deprecated) is removed, this function and one of its parameters are +// not needed anymore, the whole implementation can be moved into ensureRowSetConnection then) +SharedConnection lcl_connectRowSet(const Reference< XRowSet>& _rxRowSet, const Reference< XMultiServiceFactory>& _rxFactory, + bool _bSetAsActiveConnection, bool _bAttachAutoDisposer ) + SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) ) { - Reference< XConnection> xReturn; - Reference< XPropertySet> xRowSetProps(_rxRowSet, UNO_QUERY); - if (xRowSetProps.is()) + SharedConnection xConnection; + + do { - Any aConn( xRowSetProps->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ActiveConnection"))) ); - aConn >>= xReturn; + Reference< XPropertySet> xRowSetProps(_rxRowSet, UNO_QUERY); + if ( !xRowSetProps.is() ) + break; - if (!xReturn.is()) + // 1. àlready connected? + Reference< XConnection > xExistingConn( + xRowSetProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ActiveConnection" ) ) ), + UNO_QUERY ); + + if ( xExistingConn.is() + // 2. embedded in a database? + || isEmbeddedInDatabase( _rxRowSet, xExistingConn ) + // 3. is there a connection in the parent hierarchy? + || ( xExistingConn = findConnection( _rxRowSet ) ).is() + ) { - // first look if there is a connection in the parent hierarchy - xReturn = findConnection(_rxRowSet); - if (!xReturn.is()) + if ( _bSetAsActiveConnection ) { - static const ::rtl::OUString s_sUserProp = ::rtl::OUString::createFromAscii("User"); - // the row set didn't supply a connection -> build one with it's current settings + xRowSetProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ActiveConnection" ) ), makeAny( xExistingConn ) ); + // no auto disposer needed, since we did not create the connection + } + + xConnection = SharedConnection( xExistingConn, false ); + break; + } + + // build a connection with it's current settings (4. data source name, or 5. URL) + + const ::rtl::OUString sUserProp = ::rtl::OUString::createFromAscii("User"); ::rtl::OUString sDataSourceName; xRowSetProps->getPropertyValue(::rtl::OUString::createFromAscii("DataSourceName")) >>= sDataSourceName; ::rtl::OUString sURL; @@ -527,11 +549,15 @@ // -> try to connect, get user and pwd setting for that ::rtl::OUString sUser, sPwd; - if (hasProperty(s_sUserProp, xRowSetProps)) - xRowSetProps->getPropertyValue(s_sUserProp) >>= sUser; + if (hasProperty(sUserProp, xRowSetProps)) + xRowSetProps->getPropertyValue(sUserProp) >>= sUser; if (hasProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), xRowSetProps)) xRowSetProps->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD)) >>= sPwd; - xReturn = getConnection_allowException(sDataSourceName, sUser, sPwd, _rxFactory); + + xConnection = SharedConnection( + getConnection_allowException( sDataSourceName, sUser, sPwd, _rxFactory ), + true /* take ownership */ + ); } else if (sURL.getLength()) { // the row set has no data source, but a connection url set @@ -541,8 +567,8 @@ if (xDriverManager.is()) { ::rtl::OUString sUser, sPwd; - if (hasProperty(s_sUserProp, xRowSetProps)) - xRowSetProps->getPropertyValue(s_sUserProp) >>= sUser; + if (hasProperty(sUserProp, xRowSetProps)) + xRowSetProps->getPropertyValue(sUserProp) >>= sUser; if (hasProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), xRowSetProps)) xRowSetProps->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD)) >>= sPwd; if (sUser.getLength()) @@ -552,32 +578,60 @@ aInfo.getArray()[0].Value <<= sUser; aInfo.getArray()[1].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("password")); aInfo.getArray()[1].Value <<= sPwd; - xReturn = xDriverManager->getConnectionWithInfo(sURL, aInfo); + xConnection = SharedConnection( + xDriverManager->getConnectionWithInfo( sURL, aInfo ), + !_bAttachAutoDisposer /* take ownership if and only if we're not going to auto-dispose the connection */ + ); } else // just use the url - xReturn = xDriverManager->getConnection(sURL); - } + xConnection = SharedConnection( + xDriverManager->getConnection( sURL ), + !_bAttachAutoDisposer /* take ownership if and only if we're not going to auto-dispose the connection */ + ); } } - // now if we got a connection, forward it to the row set - if (xReturn.is() && _bSetAsActiveConnection) + // now if we created a connection, forward it to the row set + if ( xConnection.is() && _bSetAsActiveConnection ) { try { - OAutoConnectionDisposer* pAutoDispose = new OAutoConnectionDisposer(_rxRowSet, xReturn); + if ( _bAttachAutoDisposer ) + { + OAutoConnectionDisposer* pAutoDispose = new OAutoConnectionDisposer( _rxRowSet, xConnection ); Reference< XPropertyChangeListener > xEnsureDelete(pAutoDispose); } + else + xRowSetProps->setPropertyValue( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ActiveConnection" ) ), + makeAny( xConnection.getTyped() ) + ); + } catch(Exception&) { OSL_ENSURE(0,"EXception when we set the new active connection!"); } } } - } + while ( false ); - return xReturn; + return xConnection; +} + +//------------------------------------------------------------------------------ +Reference< XConnection> connectRowset(const Reference< XRowSet>& _rxRowSet, const Reference< XMultiServiceFactory>& _rxFactory, + sal_Bool _bSetAsActiveConnection ) SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) ) +{ + SharedConnection xConnection = lcl_connectRowSet( _rxRowSet, _rxFactory, _bSetAsActiveConnection, true ); + return xConnection.getTyped(); +} + +//------------------------------------------------------------------------------ +SharedConnection ensureRowSetConnection(const Reference< XRowSet>& _rxRowSet, const Reference< XMultiServiceFactory>& _rxFactory, + bool _bUseAutoConnectionDisposer ) SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) ) +{ + return lcl_connectRowSet( _rxRowSet, _rxFactory, true, _bUseAutoConnectionDisposer ); } //------------------------------------------------------------------------------ @@ -1208,9 +1262,7 @@ ::rtl::OUString sStatement; try { - Reference< XConnection> xConn; - if ( !isEmbeddedInDatabase( _rxRowSet, xConn ) ) - xConn = connectRowset( Reference< XRowSet >( _rxRowSet, UNO_QUERY ), _rxFactory, sal_True ); + Reference< XConnection> xConn = connectRowset( Reference< XRowSet >( _rxRowSet, UNO_QUERY ), _rxFactory, sal_True ); if ( xConn.is() ) // implies _rxRowSet.is() { // build the statement the row set is based on (can't use the ActiveCommand property of the set --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
