1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "hsqldb/HConnection.hxx"
22 #include "hsqldb/HTools.hxx"
25 #include <connectivity/dbtools.hxx>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
30 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/graphic/GraphicProvider.hpp>
33 #include <com/sun/star/graphic/XGraphicProvider.hpp>
34 #include <com/sun/star/graphic/GraphicColorMode.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
38 #include <comphelper/listenernotification.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/sequence.hxx>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include <rtl/ustrbuf.hxx>
43 #include <tools/diagnose_ex.h>
45 #include "resource/sharedresources.hxx"
46 #include "resource/hsqldb_res.hrc"
48 using ::com::sun::star::util::XFlushListener
;
49 using ::com::sun::star::lang::EventObject
;
50 using ::com::sun::star::uno::Reference
;
51 using ::com::sun::star::uno::Exception
;
52 using ::com::sun::star::uno::RuntimeException
;
53 using ::com::sun::star::uno::UNO_QUERY
;
54 using ::com::sun::star::uno::UNO_QUERY_THROW
;
55 using ::com::sun::star::uno::XComponentContext
;
56 using ::com::sun::star::sdbc::XStatement
;
57 using ::com::sun::star::sdbc::XConnection
;
58 using ::com::sun::star::sdbcx::XDataDefinitionSupplier
;
59 using ::com::sun::star::sdbcx::XTablesSupplier
;
60 using ::com::sun::star::container::XNameAccess
;
61 using ::com::sun::star::uno::Sequence
;
62 using ::com::sun::star::beans::NamedValue
;
63 using ::com::sun::star::lang::WrappedTargetException
;
64 using ::com::sun::star::lang::ServiceNotRegisteredException
;
65 using ::com::sun::star::sdbc::XDriver
;
66 using ::com::sun::star::lang::XMultiServiceFactory
;
67 using ::com::sun::star::graphic::XGraphic
;
68 using ::com::sun::star::graphic::GraphicProvider
;
69 using ::com::sun::star::graphic::XGraphicProvider
;
70 using ::com::sun::star::uno::XInterface
;
71 using ::com::sun::star::lang::IllegalArgumentException
;
72 using ::com::sun::star::ui::dialogs::XExecutableDialog
;
73 using ::com::sun::star::uno::Any
;
74 using ::com::sun::star::uno::makeAny
;
75 using ::com::sun::star::sdbc::XResultSet
;
76 using ::com::sun::star::sdbc::XDatabaseMetaData
;
77 using ::com::sun::star::sdbc::XDatabaseMetaData2
;
78 using ::com::sun::star::sdbc::XRow
;
79 using ::com::sun::star::sdb::application::XDatabaseDocumentUI
;
80 using ::com::sun::star::beans::PropertyValue
;
82 namespace GraphicColorMode
= ::com::sun::star::graphic::GraphicColorMode
;
84 namespace connectivity
{ namespace hsqldb
86 void SAL_CALL
OHsqlConnection::disposing(void)
88 m_aFlushListeners
.disposeAndClear( EventObject( *this ) );
89 OHsqlConnection_BASE::disposing();
90 OConnectionWrapper::disposing();
93 OHsqlConnection::OHsqlConnection( const Reference
< XDriver
> _rxDriver
,
94 const Reference
< XConnection
>& _xConnection
,const Reference
< XComponentContext
>& _rxContext
)
95 :OHsqlConnection_BASE( m_aMutex
)
96 ,m_aFlushListeners( m_aMutex
)
97 ,m_xDriver( _rxDriver
)
98 ,m_xContext( _rxContext
)
102 setDelegation(_xConnection
,_rxContext
,m_refCount
);
105 OHsqlConnection::~OHsqlConnection()
107 if ( !OHsqlConnection_BASE::rBHelper
.bDisposed
)
109 osl_atomic_increment( &m_refCount
);
114 IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection
,OHsqlConnection_BASE
,OConnectionWrapper
)
115 IMPLEMENT_SERVICE_INFO(OHsqlConnection
, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection")
116 IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection
,OHsqlConnection_BASE
,OConnectionWrapper
)
119 ::osl::Mutex
& OHsqlConnection::getMutex() const
125 void OHsqlConnection::checkDisposed() const
127 ::connectivity::checkDisposed( rBHelper
.bDisposed
);
132 void SAL_CALL
OHsqlConnection::flush( ) throw (RuntimeException
, std::exception
)
134 MethodGuard
aGuard( *this );
138 if ( m_xConnection
.is() )
143 Reference
< XDatabaseMetaData2
> xMeta2(m_xConnection
->getMetaData(),UNO_QUERY_THROW
);
144 const Sequence
< PropertyValue
> aInfo
= xMeta2
->getConnectionInfo();
145 const PropertyValue
* pIter
= aInfo
.getConstArray();
146 const PropertyValue
* pEnd
= pIter
+ aInfo
.getLength();
147 for(;pIter
!= pEnd
;++pIter
)
149 if ( pIter
->Name
.equalsAscii("readonly") )
155 Reference
< XStatement
> xStmt( m_xConnection
->createStatement(), UNO_QUERY_THROW
);
156 xStmt
->execute( OUString( "CHECKPOINT DEFRAG" ) );
160 EventObject
aFlushedEvent( *this );
161 m_aFlushListeners
.notifyEach( &XFlushListener::flushed
, aFlushedEvent
);
163 catch(const Exception
& )
165 DBG_UNHANDLED_EXCEPTION();
170 void SAL_CALL
OHsqlConnection::addFlushListener( const Reference
< XFlushListener
>& l
) throw (RuntimeException
, std::exception
)
172 MethodGuard
aGuard( *this );
173 m_aFlushListeners
.addInterface( l
);
177 void SAL_CALL
OHsqlConnection::removeFlushListener( const Reference
< XFlushListener
>& l
) throw (RuntimeException
, std::exception
)
179 MethodGuard
aGuard( *this );
180 m_aFlushListeners
.removeInterface( l
);
184 Reference
< XGraphic
> SAL_CALL
OHsqlConnection::getTableIcon( const OUString
& _TableName
, ::sal_Int32
/*_ColorMode*/ ) throw (RuntimeException
, std::exception
)
186 MethodGuard
aGuard( *this );
188 impl_checkExistingTable_throw( _TableName
);
189 if ( !impl_isTextTable_nothrow( _TableName
) )
192 return impl_getTextTableIcon_nothrow();
196 Reference
< XInterface
> SAL_CALL
OHsqlConnection::getTableEditor( const Reference
< XDatabaseDocumentUI
>& _DocumentUI
, const OUString
& _TableName
) throw (IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
198 MethodGuard
aGuard( *this );
200 impl_checkExistingTable_throw( _TableName
);
201 if ( !impl_isTextTable_nothrow( _TableName
) )
204 if ( !_DocumentUI
.is() )
206 ::connectivity::SharedResources aResources
;
207 const OUString
sError( aResources
.getResourceString(STR_NO_DOCUMENTUI
));
208 throw IllegalArgumentException(
213 } // if ( !_DocumentUI.is() )
216 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
217 // return xEditor.get();
219 // editor not yet implemented in this CWS
223 Reference
< XNameAccess
> OHsqlConnection::impl_getTableContainer_throw()
225 Reference
< XNameAccess
> xTables
;
228 Reference
< XConnection
> xMe( *this, UNO_QUERY
);
229 Reference
< XDataDefinitionSupplier
> xDefinitionsSupp( m_xDriver
, UNO_QUERY_THROW
);
230 Reference
< XTablesSupplier
> xTablesSupp( xDefinitionsSupp
->getDataDefinitionByConnection( xMe
), UNO_QUERY_THROW
);
231 xTables
.set( xTablesSupp
->getTables(), UNO_QUERY_THROW
);
233 catch( const RuntimeException
& ) { throw; }
234 catch( const Exception
& )
236 ::connectivity::SharedResources aResources
;
237 const OUString
sError( aResources
.getResourceString(STR_NO_TABLE_CONTAINER
));
238 throw WrappedTargetException( sError
,*this, ::cppu::getCaughtException() );
241 SAL_WARN_IF( !xTables
.is(), "connectivity.hsqldb", "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
247 void OHsqlConnection::impl_checkExistingTable_throw( const OUString
& _rTableName
)
249 bool bDoesExist
= false;
252 Reference
< XNameAccess
> xTables( impl_getTableContainer_throw(), UNO_QUERY_THROW
);
254 bDoesExist
= xTables
->hasByName( _rTableName
);
256 catch( const Exception
& )
258 // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only
259 // allowed to throw an IllegalArgumentException ourself
260 DBG_UNHANDLED_EXCEPTION();
265 ::connectivity::SharedResources aResources
;
266 const OUString
sError( aResources
.getResourceStringWithSubstitution(
268 "$tablename$", _rTableName
270 throw IllegalArgumentException( sError
,*this, 0 );
271 } // if ( !bDoesExist )
275 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString
& _rTableName
)
277 bool bIsTextTable
= false;
280 Reference
< XConnection
> xMe( *this, UNO_QUERY_THROW
);
282 // split the fully qualified name
283 Reference
< XDatabaseMetaData
> xMetaData( xMe
->getMetaData(), UNO_QUERY_THROW
);
284 OUString sCatalog
, sSchema
, sName
;
285 ::dbtools::qualifiedNameComponents( xMetaData
, _rTableName
, sCatalog
, sSchema
, sName
, ::dbtools::eComplete
);
287 // get the table information
289 sSQL
.appendAscii( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
290 HTools::appendTableFilterCrit( sSQL
, sCatalog
, sSchema
, sName
, true );
291 sSQL
.appendAscii( " AND TABLE_TYPE = 'TABLE'" );
293 Reference
< XStatement
> xStatement( xMe
->createStatement(), UNO_QUERY_THROW
);
294 Reference
< XResultSet
> xTableHsqlType( xStatement
->executeQuery( sSQL
.makeStringAndClear() ), UNO_QUERY_THROW
);
296 if ( xTableHsqlType
->next() ) // might not succeed in case of VIEWs
298 Reference
< XRow
> xValueAccess( xTableHsqlType
, UNO_QUERY_THROW
);
299 OUString sTableType
= xValueAccess
->getString( 1 );
300 bIsTextTable
= sTableType
== "TEXT";
303 catch( const Exception
& )
305 DBG_UNHANDLED_EXCEPTION();
312 Reference
< XGraphic
> OHsqlConnection::impl_getTextTableIcon_nothrow()
314 Reference
< XGraphic
> xGraphic
;
317 // create a graphic provider
318 Reference
< XGraphicProvider
> xProvider
;
319 if ( m_xContext
.is() )
320 xProvider
.set( GraphicProvider::create(m_xContext
) );
322 // assemble the image URL
323 OUStringBuffer aImageURL
;
324 // load the graphic from the global graphic repository
325 aImageURL
.appendAscii( "private:graphicrepository/" );
326 // the relative path within the images.zip
327 aImageURL
.appendAscii( "database/" );
328 aImageURL
.appendAscii( LINKED_TEXT_TABLE_IMAGE_RESOURCE
);
329 // the name of the graphic to use
330 OUString
sImageURL( aImageURL
.makeStringAndClear() );
332 // ask the provider to obtain a graphic
333 Sequence
< PropertyValue
> aMediaProperties( 1 );
334 aMediaProperties
[0].Name
= "URL";
335 aMediaProperties
[0].Value
<<= sImageURL
;
336 xGraphic
= xProvider
->queryGraphic( aMediaProperties
);
337 OSL_ENSURE( xGraphic
.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
339 catch( const Exception
& )
341 DBG_UNHANDLED_EXCEPTION();
346 } } // namespace connectivity::hsqldb
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */