1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_connectivity.hxx" 26 27 #include "hsqldb/HConnection.hxx" 28 #include "hsqldb/HTools.hxx" 29 #include "hsqlui.hrc" 30 31 #include <connectivity/dbtools.hxx> 32 33 /** === begin UNO includes === **/ 34 #include <com/sun/star/beans/NamedValue.hpp> 35 #include <com/sun/star/container/XNameAccess.hpp> 36 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp> 37 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp> 38 #include <com/sun/star/sdbc/XRow.hpp> 39 #include <com/sun/star/graphic/XGraphicProvider.hpp> 40 #include <com/sun/star/graphic/GraphicColorMode.hpp> 41 #include <com/sun/star/beans/PropertyValue.hpp> 42 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp> 43 /** === end UNO includes === **/ 44 45 #include <comphelper/componentcontext.hxx> 46 #include <comphelper/listenernotification.hxx> 47 #include <comphelper/sequence.hxx> 48 #include <cppuhelper/exc_hlp.hxx> 49 #include <rtl/ustrbuf.hxx> 50 #include <tools/diagnose_ex.h> 51 52 #include "resource/sharedresources.hxx" 53 #include "resource/hsqldb_res.hrc" 54 55 /** === begin UNO using === **/ 56 using ::com::sun::star::util::XFlushListener; 57 using ::com::sun::star::lang::EventObject; 58 using ::com::sun::star::uno::Reference; 59 using ::com::sun::star::uno::Exception; 60 using ::com::sun::star::uno::RuntimeException; 61 using ::com::sun::star::uno::UNO_QUERY; 62 using ::com::sun::star::uno::UNO_QUERY_THROW; 63 using ::com::sun::star::sdbc::XStatement; 64 using ::com::sun::star::sdbc::XConnection; 65 using ::com::sun::star::sdbcx::XDataDefinitionSupplier; 66 using ::com::sun::star::sdbcx::XTablesSupplier; 67 using ::com::sun::star::container::XNameAccess; 68 using ::com::sun::star::uno::Sequence; 69 using ::com::sun::star::beans::NamedValue; 70 using ::com::sun::star::lang::WrappedTargetException; 71 using ::com::sun::star::lang::ServiceNotRegisteredException; 72 using ::com::sun::star::sdbc::XDriver; 73 using ::com::sun::star::lang::XMultiServiceFactory; 74 using ::com::sun::star::graphic::XGraphic; 75 using ::com::sun::star::graphic::XGraphicProvider; 76 using ::com::sun::star::uno::XInterface; 77 using ::com::sun::star::lang::IllegalArgumentException; 78 using ::com::sun::star::ui::dialogs::XExecutableDialog; 79 using ::com::sun::star::uno::Any; 80 using ::com::sun::star::uno::makeAny; 81 using ::com::sun::star::sdbc::XResultSet; 82 using ::com::sun::star::sdbc::XDatabaseMetaData; 83 using ::com::sun::star::sdbc::XDatabaseMetaData2; 84 using ::com::sun::star::sdbc::XRow; 85 using ::com::sun::star::sdb::application::XDatabaseDocumentUI; 86 using ::com::sun::star::beans::PropertyValue; 87 /** === end UNO using === **/ 88 namespace GraphicColorMode = ::com::sun::star::graphic::GraphicColorMode; 89 90 namespace connectivity { namespace hsqldb 91 { 92 // ============================================================================= 93 // = FlushListeners 94 // ============================================================================= 95 typedef ::comphelper::OListenerContainerBase< XFlushListener, EventObject > FlushListeners_Base; 96 class FlushListeners : public FlushListeners_Base 97 { 98 public: FlushListeners(::osl::Mutex & _rMutex)99 FlushListeners( ::osl::Mutex& _rMutex ) :FlushListeners_Base( _rMutex ) { } 100 101 protected: 102 virtual bool implTypedNotify( 103 const Reference< XFlushListener >& _rxListener, 104 const EventObject& _rEvent 105 ) SAL_THROW( ( Exception ) ); 106 }; 107 108 // ----------------------------------------------------------------------------- implTypedNotify(const Reference<XFlushListener> & _rxListener,const EventObject & _rEvent)109 bool FlushListeners::implTypedNotify( const Reference< XFlushListener >& _rxListener, const EventObject& _rEvent ) SAL_THROW( ( Exception ) ) 110 { 111 _rxListener->flushed( _rEvent ); 112 return true; // continue notifying the other listeners, if any 113 } 114 115 // ============================================================================= 116 // = OHsqlConnection 117 // ============================================================================= 118 // ----------------------------------------------------------------------------- disposing(void)119 void SAL_CALL OHsqlConnection::disposing(void) 120 { 121 m_aFlushListeners.disposeAndClear( EventObject( *this ) ); 122 OHsqlConnection_BASE::disposing(); 123 OConnectionWrapper::disposing(); 124 } 125 // ----------------------------------------------------------------------------- OHsqlConnection(const Reference<XDriver> _rxDriver,const Reference<XConnection> & _xConnection,const Reference<XMultiServiceFactory> & _xORB)126 OHsqlConnection::OHsqlConnection( const Reference< XDriver > _rxDriver, 127 const Reference< XConnection >& _xConnection ,const Reference< XMultiServiceFactory>& _xORB ) 128 :OHsqlConnection_BASE( m_aMutex ) 129 ,m_aFlushListeners( m_aMutex ) 130 ,m_xDriver( _rxDriver ) 131 ,m_xORB( _xORB ) 132 ,m_bIni(true) 133 ,m_bReadOnly(false) 134 { 135 setDelegation(_xConnection,_xORB,m_refCount); 136 } 137 // ----------------------------------------------------------------------------- ~OHsqlConnection()138 OHsqlConnection::~OHsqlConnection() 139 { 140 if ( !OHsqlConnection_BASE::rBHelper.bDisposed ) 141 { 142 osl_incrementInterlockedCount( &m_refCount ); 143 dispose(); 144 } 145 } 146 // ----------------------------------------------------------------------------- IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper)147 IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper) 148 IMPLEMENT_SERVICE_INFO(OHsqlConnection, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection") 149 IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper) 150 151 //-------------------------------------------------------------------- 152 ::osl::Mutex& OHsqlConnection::getMutex() const 153 { 154 return m_aMutex; 155 } 156 157 //-------------------------------------------------------------------- checkDisposed() const158 void OHsqlConnection::checkDisposed() const 159 { 160 ::connectivity::checkDisposed( rBHelper.bDisposed ); 161 } 162 163 // XFlushable 164 //-------------------------------------------------------------------- flush()165 void SAL_CALL OHsqlConnection::flush( ) throw (RuntimeException) 166 { 167 MethodGuard aGuard( *this ); 168 169 try 170 { 171 if ( m_xConnection.is() ) 172 { 173 if ( m_bIni ) 174 { 175 m_bIni = false; 176 Reference< XDatabaseMetaData2 > xMeta2(m_xConnection->getMetaData(),UNO_QUERY_THROW); 177 const Sequence< PropertyValue > aInfo = xMeta2->getConnectionInfo(); 178 const PropertyValue* pIter = aInfo.getConstArray(); 179 const PropertyValue* pEnd = pIter + aInfo.getLength(); 180 for(;pIter != pEnd;++pIter) 181 { 182 if ( pIter->Name.compareToAscii("readonly") == 0 ) 183 m_bReadOnly = true; 184 } 185 } 186 if ( !m_bReadOnly ) 187 { 188 Reference< XStatement > xStmt( m_xConnection->createStatement(), UNO_QUERY_THROW ); 189 xStmt->execute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CHECKPOINT" ) ) ); 190 } 191 } 192 193 EventObject aFlushedEvent( *this ); 194 m_aFlushListeners.notifyEach( &XFlushListener::flushed, aFlushedEvent ); 195 } 196 catch(const Exception& ) 197 { 198 DBG_UNHANDLED_EXCEPTION(); 199 } 200 } 201 202 //-------------------------------------------------------------------- addFlushListener(const Reference<XFlushListener> & l)203 void SAL_CALL OHsqlConnection::addFlushListener( const Reference< XFlushListener >& l ) throw (RuntimeException) 204 { 205 MethodGuard aGuard( *this ); 206 m_aFlushListeners.addInterface( l ); 207 } 208 209 //-------------------------------------------------------------------- removeFlushListener(const Reference<XFlushListener> & l)210 void SAL_CALL OHsqlConnection::removeFlushListener( const Reference< XFlushListener >& l ) throw (RuntimeException) 211 { 212 MethodGuard aGuard( *this ); 213 m_aFlushListeners.removeInterface( l ); 214 } 215 216 // ------------------------------------------------------------------- getTableIcon(const::rtl::OUString & _TableName,::sal_Int32 _ColorMode)217 Reference< XGraphic > SAL_CALL OHsqlConnection::getTableIcon( const ::rtl::OUString& _TableName, ::sal_Int32 _ColorMode ) throw (RuntimeException) 218 { 219 MethodGuard aGuard( *this ); 220 221 impl_checkExistingTable_throw( _TableName ); 222 if ( !impl_isTextTable_nothrow( _TableName ) ) 223 return NULL; 224 225 return impl_getTextTableIcon_nothrow( _ColorMode ); 226 } 227 228 // ------------------------------------------------------------------- getTableEditor(const Reference<XDatabaseDocumentUI> & _DocumentUI,const::rtl::OUString & _TableName)229 Reference< XInterface > SAL_CALL OHsqlConnection::getTableEditor( const Reference< XDatabaseDocumentUI >& _DocumentUI, const ::rtl::OUString& _TableName ) throw (IllegalArgumentException, WrappedTargetException, RuntimeException) 230 { 231 MethodGuard aGuard( *this ); 232 233 impl_checkExistingTable_throw( _TableName ); 234 if ( !impl_isTextTable_nothrow( _TableName ) ) 235 return NULL; 236 237 if ( !_DocumentUI.is() ) 238 { 239 ::connectivity::SharedResources aResources; 240 const ::rtl::OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI)); 241 throw IllegalArgumentException( 242 sError, 243 *this, 244 0 245 ); 246 } // if ( !_DocumentUI.is() ) 247 248 249 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName ); 250 // return xEditor.get(); 251 return NULL; 252 // editor not yet implemented in this CWS 253 } 254 255 // ------------------------------------------------------------------- impl_getTableContainer_throw()256 Reference< XNameAccess > OHsqlConnection::impl_getTableContainer_throw() 257 { 258 Reference< XNameAccess > xTables; 259 try 260 { 261 Reference< XConnection > xMe( *this, UNO_QUERY ); 262 Reference< XDataDefinitionSupplier > xDefinitionsSupp( m_xDriver, UNO_QUERY_THROW ); 263 Reference< XTablesSupplier > xTablesSupp( xDefinitionsSupp->getDataDefinitionByConnection( xMe ), UNO_QUERY_THROW ); 264 xTables.set( xTablesSupp->getTables(), UNO_QUERY_THROW ); 265 } 266 catch( const RuntimeException& ) { throw; } 267 catch( const Exception& ) 268 { 269 ::connectivity::SharedResources aResources; 270 const ::rtl::OUString sError( aResources.getResourceString(STR_NO_TABLE_CONTAINER)); 271 throw WrappedTargetException( sError ,*this, ::cppu::getCaughtException() ); 272 } 273 274 OSL_POSTCOND( xTables.is(), "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" ); 275 return xTables; 276 } 277 278 //TODO: resource 279 #if 0 280 // ------------------------------------------------------------------- 281 Reference< XExecutableDialog > OHsqlConnection::impl_createLinkedTableEditor_throw( const Reference< XDatabaseDocumentUI >& _rxDocumentUI, const ::rtl::OUString& _rTableName ) 282 { 283 OSL_PRECOND( _rxDocumentUI.is(), "OHsqlConnection::impl_createLinkedTableEditor_throw: illegal document UI!" ); 284 Reference< XExecutableDialog > xDialog; 285 try 286 { 287 ::comphelper::ComponentContext aContext( m_xORB ); 288 Sequence< Any > aArguments(3); 289 aArguments[0] <<= NamedValue( 290 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TableContainer" ) ), 291 makeAny( impl_getTableContainer_throw() ) 292 ); 293 aArguments[1] <<= NamedValue( 294 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TableName" ) ), 295 makeAny( _rTableName ) 296 ); 297 aArguments[2] <<= NamedValue( 298 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ParentWindow" ) ), 299 makeAny( _rxDocumentUI->getApplicationMainWindow() ) 300 ); 301 302 aContext.createComponentWithArguments( "com.sun.star.sdb.hsql.LinkedTableEditor", aArguments, xDialog ); 303 if ( !xDialog.is() ) 304 throw ServiceNotRegisteredException( ::rtl::OUString::createFromAscii( "com.sun.star.sdb.hsql.LinkedTableEditor" ), *this ); 305 } 306 catch( const RuntimeException& ) { throw; } 307 catch( const Exception& ) 308 { 309 ::connectivity::SharedResources aResources; 310 const ::rtl::OUString sError( aResources.getResourceString(STR_NO_TABLE_EDITOR_DIALOG)); 311 throw WrappedTargetException( sError ,*this, ::cppu::getCaughtException() ); 312 } 313 return xDialog; 314 } 315 #endif 316 317 // ------------------------------------------------------------------- impl_checkExistingTable_throw(const::rtl::OUString & _rTableName)318 void OHsqlConnection::impl_checkExistingTable_throw( const ::rtl::OUString& _rTableName ) 319 { 320 bool bDoesExist = false; 321 try 322 { 323 Reference< XNameAccess > xTables( impl_getTableContainer_throw(), UNO_QUERY_THROW ); 324 if ( xTables.is() ) 325 bDoesExist = xTables->hasByName( _rTableName ); 326 } 327 catch( const Exception& ) 328 { 329 // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only 330 // allowed to throw an IllegalArgumentException ourself 331 DBG_UNHANDLED_EXCEPTION(); 332 } 333 334 if ( !bDoesExist ) 335 { 336 ::connectivity::SharedResources aResources; 337 const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution( 338 STR_NO_TABLENAME, 339 "$tablename$", _rTableName 340 )); 341 throw IllegalArgumentException( sError,*this, 0 ); 342 } // if ( !bDoesExist ) 343 } 344 345 // ------------------------------------------------------------------- impl_isTextTable_nothrow(const::rtl::OUString & _rTableName)346 bool OHsqlConnection::impl_isTextTable_nothrow( const ::rtl::OUString& _rTableName ) 347 { 348 bool bIsTextTable = false; 349 try 350 { 351 Reference< XConnection > xMe( *this, UNO_QUERY_THROW ); 352 353 // split the fully qualified name 354 Reference< XDatabaseMetaData > xMetaData( xMe->getMetaData(), UNO_QUERY_THROW ); 355 ::rtl::OUString sCatalog, sSchema, sName; 356 ::dbtools::qualifiedNameComponents( xMetaData, _rTableName, sCatalog, sSchema, sName, ::dbtools::eComplete ); 357 358 // get the table information 359 ::rtl::OUStringBuffer sSQL; 360 sSQL.appendAscii( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" ); 361 HTools::appendTableFilterCrit( sSQL, sCatalog, sSchema, sName, true ); 362 sSQL.appendAscii( " AND TABLE_TYPE = 'TABLE'" ); 363 364 Reference< XStatement > xStatement( xMe->createStatement(), UNO_QUERY_THROW ); 365 Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), UNO_QUERY_THROW ); 366 367 if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs 368 { 369 Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW ); 370 ::rtl::OUString sTableType = xValueAccess->getString( 1 ); 371 bIsTextTable = sTableType.equalsAscii( "TEXT" ); 372 } 373 } 374 catch( const Exception& ) 375 { 376 DBG_UNHANDLED_EXCEPTION(); 377 } 378 379 return bIsTextTable; 380 } 381 382 // ------------------------------------------------------------------- impl_getTextTableIcon_nothrow(::sal_Int32 _ColorMode)383 Reference< XGraphic > OHsqlConnection::impl_getTextTableIcon_nothrow( ::sal_Int32 _ColorMode ) 384 { 385 Reference< XGraphic > xGraphic; 386 try 387 { 388 // create a graphic provider 389 Reference< XGraphicProvider > xProvider; 390 if ( m_xORB.is() ) 391 xProvider.set( m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ), UNO_QUERY_THROW ); 392 393 // assemble the image URL 394 ::rtl::OUStringBuffer aImageURL; 395 aImageURL.appendAscii( "private:graphicrepository/" ); // load the graphic from the global graphic repository 396 aImageURL.appendAscii( "database/" ); // the relative path within the images.zip 397 if ( _ColorMode == GraphicColorMode::NORMAL ) 398 aImageURL.appendAscii( LINKED_TEXT_TABLE_IMAGE_RESOURCE ); 399 else 400 aImageURL.appendAscii( LINKED_TEXT_TABLE_IMAGE_RESOURCE_HC ); 401 // the name of the graphic to use 402 ::rtl::OUString sImageURL( aImageURL.makeStringAndClear() ); 403 404 // ask the provider to obtain a graphic 405 Sequence< PropertyValue > aMediaProperties( 1 ); 406 aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ); 407 aMediaProperties[0].Value <<= sImageURL; 408 xGraphic = xProvider->queryGraphic( aMediaProperties ); 409 OSL_ENSURE( xGraphic.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" ); 410 } 411 catch( const Exception& ) 412 { 413 DBG_UNHANDLED_EXCEPTION(); 414 } 415 return xGraphic; 416 } 417 418 } } // namespace connectivity::hsqldb 419