1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 11 * Licensed to the Apache Software Foundation (ASF) under one or more 12 * contributor license agreements. See the NOTICE file distributed 13 * with this work for additional information regarding copyright 14 * ownership. The ASF licenses this file to you under the Apache 15 * License, Version 2.0 (the "License"); you may not use this file 16 * except in compliance with the License. You may obtain a copy of 17 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 18 */ 19 20 21 #include <hsqldb/HConnection.hxx> 22 #include <hsqldb/HTools.hxx> 23 #include <bitmaps.hlst> 24 25 #include <connectivity/dbtools.hxx> 26 27 #include <com/sun/star/container/XNameAccess.hpp> 28 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp> 29 #include <com/sun/star/sdbc/XRow.hpp> 30 #include <com/sun/star/graphic/GraphicProvider.hpp> 31 #include <com/sun/star/graphic/XGraphicProvider.hpp> 32 #include <com/sun/star/graphic/GraphicColorMode.hpp> 33 #include <com/sun/star/beans/PropertyValue.hpp> 34 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp> 35 36 #include <cppuhelper/exc_hlp.hxx> 37 #include <rtl/ustrbuf.hxx> 38 #include <sal/log.hxx> 39 #include <tools/diagnose_ex.h> 40 41 #include <resource/sharedresources.hxx> 42 #include <strings.hrc> 43 44 using ::com::sun::star::util::XFlushListener; 45 using ::com::sun::star::lang::EventObject; 46 using ::com::sun::star::uno::Reference; 47 using ::com::sun::star::uno::Exception; 48 using ::com::sun::star::uno::RuntimeException; 49 using ::com::sun::star::uno::UNO_QUERY; 50 using ::com::sun::star::uno::UNO_QUERY_THROW; 51 using ::com::sun::star::uno::XComponentContext; 52 using ::com::sun::star::sdbc::XStatement; 53 using ::com::sun::star::sdbc::XConnection; 54 using ::com::sun::star::sdbcx::XDataDefinitionSupplier; 55 using ::com::sun::star::sdbcx::XTablesSupplier; 56 using ::com::sun::star::container::XNameAccess; 57 using ::com::sun::star::uno::Sequence; 58 using ::com::sun::star::lang::WrappedTargetException; 59 using ::com::sun::star::sdbc::XDriver; 60 using ::com::sun::star::graphic::XGraphic; 61 using ::com::sun::star::graphic::GraphicProvider; 62 using ::com::sun::star::graphic::XGraphicProvider; 63 using ::com::sun::star::uno::XInterface; 64 using ::com::sun::star::lang::IllegalArgumentException; 65 using ::com::sun::star::sdbc::XResultSet; 66 using ::com::sun::star::sdbc::XDatabaseMetaData; 67 using ::com::sun::star::sdbc::XDatabaseMetaData2; 68 using ::com::sun::star::sdbc::XRow; 69 using ::com::sun::star::sdb::application::XDatabaseDocumentUI; 70 using ::com::sun::star::beans::PropertyValue; 71 72 73 namespace connectivity { namespace hsqldb 74 { disposing()75 void SAL_CALL OHsqlConnection::disposing() 76 { 77 m_aFlushListeners.disposeAndClear( EventObject( *this ) ); 78 OHsqlConnection_BASE::disposing(); 79 OConnectionWrapper::disposing(); 80 } 81 OHsqlConnection(const Reference<XDriver> & _rxDriver,const Reference<XConnection> & _xConnection,const Reference<XComponentContext> & _rxContext)82 OHsqlConnection::OHsqlConnection( const Reference< XDriver >& _rxDriver, 83 const Reference< XConnection >& _xConnection ,const Reference< XComponentContext >& _rxContext ) 84 :OHsqlConnection_BASE( m_aMutex ) 85 ,m_aFlushListeners( m_aMutex ) 86 ,m_xDriver( _rxDriver ) 87 ,m_xContext( _rxContext ) 88 ,m_bIni(true) 89 ,m_bReadOnly(false) 90 { 91 setDelegation(_xConnection,_rxContext,m_refCount); 92 } 93 ~OHsqlConnection()94 OHsqlConnection::~OHsqlConnection() 95 { 96 if ( !OHsqlConnection_BASE::rBHelper.bDisposed ) 97 { 98 osl_atomic_increment( &m_refCount ); 99 dispose(); 100 } 101 } 102 IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper)103 IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper) 104 IMPLEMENT_SERVICE_INFO(OHsqlConnection, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection") 105 IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper) 106 107 108 ::osl::Mutex& OHsqlConnection::getMutex() const 109 { 110 return m_aMutex; 111 } 112 113 checkDisposed() const114 void OHsqlConnection::checkDisposed() const 115 { 116 ::connectivity::checkDisposed( rBHelper.bDisposed ); 117 } 118 119 // XFlushable 120 flush()121 void SAL_CALL OHsqlConnection::flush( ) 122 { 123 MethodGuard aGuard( *this ); 124 125 try 126 { 127 if ( m_xConnection.is() ) 128 { 129 if ( m_bIni ) 130 { 131 m_bIni = false; 132 Reference< XDatabaseMetaData2 > xMeta2(m_xConnection->getMetaData(),UNO_QUERY_THROW); 133 const Sequence< PropertyValue > aInfo = xMeta2->getConnectionInfo(); 134 const PropertyValue* pIter = aInfo.getConstArray(); 135 const PropertyValue* pEnd = pIter + aInfo.getLength(); 136 for(;pIter != pEnd;++pIter) 137 { 138 if ( pIter->Name == "readonly" ) 139 m_bReadOnly = true; 140 } 141 } 142 try 143 { 144 if ( !m_bReadOnly ) 145 { 146 Reference< XStatement > xStmt( m_xConnection->createStatement(), css::uno::UNO_SET_THROW ); 147 xStmt->execute( "CHECKPOINT DEFRAG" ); 148 } 149 } 150 catch(const Exception& ) 151 { 152 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb"); 153 } 154 } 155 156 EventObject aFlushedEvent( *this ); 157 m_aFlushListeners.notifyEach( &XFlushListener::flushed, aFlushedEvent ); 158 } 159 catch(const Exception& ) 160 { 161 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb"); 162 } 163 } 164 165 addFlushListener(const Reference<XFlushListener> & l)166 void SAL_CALL OHsqlConnection::addFlushListener( const Reference< XFlushListener >& l ) 167 { 168 MethodGuard aGuard( *this ); 169 m_aFlushListeners.addInterface( l ); 170 } 171 172 removeFlushListener(const Reference<XFlushListener> & l)173 void SAL_CALL OHsqlConnection::removeFlushListener( const Reference< XFlushListener >& l ) 174 { 175 MethodGuard aGuard( *this ); 176 m_aFlushListeners.removeInterface( l ); 177 } 178 179 getTableIcon(const OUString & TableName,::sal_Int32)180 Reference< XGraphic > SAL_CALL OHsqlConnection::getTableIcon( const OUString& TableName, ::sal_Int32 /*_ColorMode*/ ) 181 { 182 MethodGuard aGuard( *this ); 183 184 impl_checkExistingTable_throw( TableName ); 185 if ( !impl_isTextTable_nothrow( TableName ) ) 186 return nullptr; 187 188 return impl_getTextTableIcon_nothrow(); 189 } 190 191 getTableEditor(const Reference<XDatabaseDocumentUI> & DocumentUI,const OUString & TableName)192 Reference< XInterface > SAL_CALL OHsqlConnection::getTableEditor( const Reference< XDatabaseDocumentUI >& DocumentUI, const OUString& TableName ) 193 { 194 MethodGuard aGuard( *this ); 195 196 impl_checkExistingTable_throw( TableName ); 197 if ( !impl_isTextTable_nothrow( TableName ) ) 198 return nullptr; 199 200 if ( !DocumentUI.is() ) 201 { 202 ::connectivity::SharedResources aResources; 203 const OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI)); 204 throw IllegalArgumentException( 205 sError, 206 *this, 207 0 208 ); 209 } // if ( !_DocumentUI.is() ) 210 211 212 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName ); 213 // return xEditor.get(); 214 return nullptr; 215 // editor not yet implemented in this CWS 216 } 217 218 impl_getTableContainer_throw()219 Reference< XNameAccess > OHsqlConnection::impl_getTableContainer_throw() 220 { 221 Reference< XNameAccess > xTables; 222 try 223 { 224 Reference< XConnection > xMe( *this, UNO_QUERY ); 225 Reference< XDataDefinitionSupplier > xDefinitionsSupp( m_xDriver, UNO_QUERY_THROW ); 226 Reference< XTablesSupplier > xTablesSupp( xDefinitionsSupp->getDataDefinitionByConnection( xMe ), css::uno::UNO_SET_THROW ); 227 xTables.set( xTablesSupp->getTables(), css::uno::UNO_SET_THROW ); 228 } 229 catch( const RuntimeException& ) { throw; } 230 catch( const Exception& ) 231 { 232 css::uno::Any anyEx = cppu::getCaughtException(); 233 ::connectivity::SharedResources aResources; 234 const OUString sError( aResources.getResourceString(STR_NO_TABLE_CONTAINER)); 235 throw WrappedTargetException( sError ,*this, anyEx ); 236 } 237 238 SAL_WARN_IF( !xTables.is(), "connectivity.hsqldb", "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" ); 239 return xTables; 240 } 241 242 //TODO: resource 243 impl_checkExistingTable_throw(const OUString & _rTableName)244 void OHsqlConnection::impl_checkExistingTable_throw( const OUString& _rTableName ) 245 { 246 bool bDoesExist = false; 247 try 248 { 249 Reference< XNameAccess > xTables( impl_getTableContainer_throw(), css::uno::UNO_SET_THROW ); 250 bDoesExist = xTables->hasByName( _rTableName ); 251 } 252 catch( const Exception& ) 253 { 254 // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only 255 // allowed to throw an IllegalArgumentException ourself 256 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb"); 257 } 258 259 if ( !bDoesExist ) 260 { 261 ::connectivity::SharedResources aResources; 262 const OUString sError( aResources.getResourceStringWithSubstitution( 263 STR_NO_TABLENAME, 264 "$tablename$", _rTableName 265 )); 266 throw IllegalArgumentException( sError,*this, 0 ); 267 } // if ( !bDoesExist ) 268 } 269 270 impl_isTextTable_nothrow(const OUString & _rTableName)271 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString& _rTableName ) 272 { 273 bool bIsTextTable = false; 274 try 275 { 276 Reference< XConnection > xMe( *this, UNO_QUERY_THROW ); 277 278 // split the fully qualified name 279 Reference< XDatabaseMetaData > xMetaData( xMe->getMetaData(), css::uno::UNO_SET_THROW ); 280 OUString sCatalog, sSchema, sName; 281 ::dbtools::qualifiedNameComponents( xMetaData, _rTableName, sCatalog, sSchema, sName, ::dbtools::EComposeRule::Complete ); 282 283 // get the table information 284 OUStringBuffer sSQL; 285 sSQL.append( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" ); 286 HTools::appendTableFilterCrit( sSQL, sCatalog, sSchema, sName, true ); 287 sSQL.append( " AND TABLE_TYPE = 'TABLE'" ); 288 289 Reference< XStatement > xStatement( xMe->createStatement(), css::uno::UNO_SET_THROW ); 290 Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), css::uno::UNO_SET_THROW ); 291 292 if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs 293 { 294 Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW ); 295 OUString sTableType = xValueAccess->getString( 1 ); 296 bIsTextTable = sTableType == "TEXT"; 297 } 298 } 299 catch( const Exception& ) 300 { 301 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb"); 302 } 303 304 return bIsTextTable; 305 } 306 307 impl_getTextTableIcon_nothrow()308 Reference< XGraphic > OHsqlConnection::impl_getTextTableIcon_nothrow() 309 { 310 Reference< XGraphic > xGraphic; 311 try 312 { 313 // create a graphic provider 314 Reference< XGraphicProvider > xProvider; 315 if ( m_xContext.is() ) 316 xProvider.set( GraphicProvider::create(m_xContext) ); 317 318 // assemble the image URL 319 OUString sImageURL = 320 // load the graphic from the global graphic repository 321 "private:graphicrepository/" 322 // the relative path within the images.zip 323 LINKED_TEXT_TABLE_IMAGE_RESOURCE; 324 325 // ask the provider to obtain a graphic 326 Sequence< PropertyValue > aMediaProperties( 1 ); 327 aMediaProperties[0].Name = "URL"; 328 aMediaProperties[0].Value <<= sImageURL; 329 xGraphic = xProvider->queryGraphic( aMediaProperties ); 330 OSL_ENSURE( xGraphic.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" ); 331 } 332 catch( const Exception& ) 333 { 334 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb"); 335 } 336 return xGraphic; 337 } 338 339 } } // namespace connectivity::hsqldb 340 341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 342