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 // =============================================================================
88 // =============================================================================
89 typedef ::comphelper::OListenerContainerBase
< XFlushListener
, EventObject
> FlushListeners_Base
;
90 class FlushListeners
: public FlushListeners_Base
93 FlushListeners( ::osl::Mutex
& _rMutex
) :FlushListeners_Base( _rMutex
) { }
96 virtual bool implTypedNotify(
97 const Reference
< XFlushListener
>& _rxListener
,
98 const EventObject
& _rEvent
99 ) SAL_THROW( ( Exception
) );
102 // -----------------------------------------------------------------------------
103 bool FlushListeners::implTypedNotify( const Reference
< XFlushListener
>& _rxListener
, const EventObject
& _rEvent
) SAL_THROW( ( Exception
) )
105 _rxListener
->flushed( _rEvent
);
106 return true; // continue notifying the other listeners, if any
109 // =============================================================================
111 // =============================================================================
112 // -----------------------------------------------------------------------------
113 void SAL_CALL
OHsqlConnection::disposing(void)
115 m_aFlushListeners
.disposeAndClear( EventObject( *this ) );
116 OHsqlConnection_BASE::disposing();
117 OConnectionWrapper::disposing();
119 // -----------------------------------------------------------------------------
120 OHsqlConnection::OHsqlConnection( const Reference
< XDriver
> _rxDriver
,
121 const Reference
< XConnection
>& _xConnection
,const Reference
< XComponentContext
>& _rxContext
)
122 :OHsqlConnection_BASE( m_aMutex
)
123 ,m_aFlushListeners( m_aMutex
)
124 ,m_xDriver( _rxDriver
)
125 ,m_xContext( _rxContext
)
129 setDelegation(_xConnection
,_rxContext
,m_refCount
);
131 // -----------------------------------------------------------------------------
132 OHsqlConnection::~OHsqlConnection()
134 if ( !OHsqlConnection_BASE::rBHelper
.bDisposed
)
136 osl_atomic_increment( &m_refCount
);
140 // -----------------------------------------------------------------------------
141 IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection
,OHsqlConnection_BASE
,OConnectionWrapper
)
142 IMPLEMENT_SERVICE_INFO(OHsqlConnection
, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection")
143 IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection
,OHsqlConnection_BASE
,OConnectionWrapper
)
145 //--------------------------------------------------------------------
146 ::osl::Mutex
& OHsqlConnection::getMutex() const
151 //--------------------------------------------------------------------
152 void OHsqlConnection::checkDisposed() const
154 ::connectivity::checkDisposed( rBHelper
.bDisposed
);
158 //--------------------------------------------------------------------
159 void SAL_CALL
OHsqlConnection::flush( ) throw (RuntimeException
)
161 MethodGuard
aGuard( *this );
165 if ( m_xConnection
.is() )
170 Reference
< XDatabaseMetaData2
> xMeta2(m_xConnection
->getMetaData(),UNO_QUERY_THROW
);
171 const Sequence
< PropertyValue
> aInfo
= xMeta2
->getConnectionInfo();
172 const PropertyValue
* pIter
= aInfo
.getConstArray();
173 const PropertyValue
* pEnd
= pIter
+ aInfo
.getLength();
174 for(;pIter
!= pEnd
;++pIter
)
176 if ( pIter
->Name
.compareToAscii("readonly") == 0 )
182 Reference
< XStatement
> xStmt( m_xConnection
->createStatement(), UNO_QUERY_THROW
);
183 xStmt
->execute( OUString( "CHECKPOINT DEFRAG" ) );
187 EventObject
aFlushedEvent( *this );
188 m_aFlushListeners
.notifyEach( &XFlushListener::flushed
, aFlushedEvent
);
190 catch(const Exception
& )
192 DBG_UNHANDLED_EXCEPTION();
196 //--------------------------------------------------------------------
197 void SAL_CALL
OHsqlConnection::addFlushListener( const Reference
< XFlushListener
>& l
) throw (RuntimeException
)
199 MethodGuard
aGuard( *this );
200 m_aFlushListeners
.addInterface( l
);
203 //--------------------------------------------------------------------
204 void SAL_CALL
OHsqlConnection::removeFlushListener( const Reference
< XFlushListener
>& l
) throw (RuntimeException
)
206 MethodGuard
aGuard( *this );
207 m_aFlushListeners
.removeInterface( l
);
210 // -------------------------------------------------------------------
211 Reference
< XGraphic
> SAL_CALL
OHsqlConnection::getTableIcon( const OUString
& _TableName
, ::sal_Int32
/*_ColorMode*/ ) throw (RuntimeException
)
213 MethodGuard
aGuard( *this );
215 impl_checkExistingTable_throw( _TableName
);
216 if ( !impl_isTextTable_nothrow( _TableName
) )
219 return impl_getTextTableIcon_nothrow();
222 // -------------------------------------------------------------------
223 Reference
< XInterface
> SAL_CALL
OHsqlConnection::getTableEditor( const Reference
< XDatabaseDocumentUI
>& _DocumentUI
, const OUString
& _TableName
) throw (IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
225 MethodGuard
aGuard( *this );
227 impl_checkExistingTable_throw( _TableName
);
228 if ( !impl_isTextTable_nothrow( _TableName
) )
231 if ( !_DocumentUI
.is() )
233 ::connectivity::SharedResources aResources
;
234 const OUString
sError( aResources
.getResourceString(STR_NO_DOCUMENTUI
));
235 throw IllegalArgumentException(
240 } // if ( !_DocumentUI.is() )
243 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
244 // return xEditor.get();
246 // editor not yet implemented in this CWS
249 // -------------------------------------------------------------------
250 Reference
< XNameAccess
> OHsqlConnection::impl_getTableContainer_throw()
252 Reference
< XNameAccess
> xTables
;
255 Reference
< XConnection
> xMe( *this, UNO_QUERY
);
256 Reference
< XDataDefinitionSupplier
> xDefinitionsSupp( m_xDriver
, UNO_QUERY_THROW
);
257 Reference
< XTablesSupplier
> xTablesSupp( xDefinitionsSupp
->getDataDefinitionByConnection( xMe
), UNO_QUERY_THROW
);
258 xTables
.set( xTablesSupp
->getTables(), UNO_QUERY_THROW
);
260 catch( const RuntimeException
& ) { throw; }
261 catch( const Exception
& )
263 ::connectivity::SharedResources aResources
;
264 const OUString
sError( aResources
.getResourceString(STR_NO_TABLE_CONTAINER
));
265 throw WrappedTargetException( sError
,*this, ::cppu::getCaughtException() );
268 OSL_POSTCOND( xTables
.is(), "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
273 // -------------------------------------------------------------------
274 void OHsqlConnection::impl_checkExistingTable_throw( const OUString
& _rTableName
)
276 bool bDoesExist
= false;
279 Reference
< XNameAccess
> xTables( impl_getTableContainer_throw(), UNO_QUERY_THROW
);
281 bDoesExist
= xTables
->hasByName( _rTableName
);
283 catch( const Exception
& )
285 // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only
286 // allowed to throw an IllegalArgumentException ourself
287 DBG_UNHANDLED_EXCEPTION();
292 ::connectivity::SharedResources aResources
;
293 const OUString
sError( aResources
.getResourceStringWithSubstitution(
295 "$tablename$", _rTableName
297 throw IllegalArgumentException( sError
,*this, 0 );
298 } // if ( !bDoesExist )
301 // -------------------------------------------------------------------
302 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString
& _rTableName
)
304 bool bIsTextTable
= false;
307 Reference
< XConnection
> xMe( *this, UNO_QUERY_THROW
);
309 // split the fully qualified name
310 Reference
< XDatabaseMetaData
> xMetaData( xMe
->getMetaData(), UNO_QUERY_THROW
);
311 OUString sCatalog
, sSchema
, sName
;
312 ::dbtools::qualifiedNameComponents( xMetaData
, _rTableName
, sCatalog
, sSchema
, sName
, ::dbtools::eComplete
);
314 // get the table information
316 sSQL
.appendAscii( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
317 HTools::appendTableFilterCrit( sSQL
, sCatalog
, sSchema
, sName
, true );
318 sSQL
.appendAscii( " AND TABLE_TYPE = 'TABLE'" );
320 Reference
< XStatement
> xStatement( xMe
->createStatement(), UNO_QUERY_THROW
);
321 Reference
< XResultSet
> xTableHsqlType( xStatement
->executeQuery( sSQL
.makeStringAndClear() ), UNO_QUERY_THROW
);
323 if ( xTableHsqlType
->next() ) // might not succeed in case of VIEWs
325 Reference
< XRow
> xValueAccess( xTableHsqlType
, UNO_QUERY_THROW
);
326 OUString sTableType
= xValueAccess
->getString( 1 );
327 bIsTextTable
= sTableType
== "TEXT";
330 catch( const Exception
& )
332 DBG_UNHANDLED_EXCEPTION();
338 // -------------------------------------------------------------------
339 Reference
< XGraphic
> OHsqlConnection::impl_getTextTableIcon_nothrow()
341 Reference
< XGraphic
> xGraphic
;
344 // create a graphic provider
345 Reference
< XGraphicProvider
> xProvider
;
346 if ( m_xContext
.is() )
347 xProvider
.set( GraphicProvider::create(m_xContext
) );
349 // assemble the image URL
350 OUStringBuffer aImageURL
;
351 // load the graphic from the global graphic repository
352 aImageURL
.appendAscii( "private:graphicrepository/" );
353 // the relative path within the images.zip
354 aImageURL
.appendAscii( "database/" );
355 aImageURL
.appendAscii( LINKED_TEXT_TABLE_IMAGE_RESOURCE
);
356 // the name of the graphic to use
357 OUString
sImageURL( aImageURL
.makeStringAndClear() );
359 // ask the provider to obtain a graphic
360 Sequence
< PropertyValue
> aMediaProperties( 1 );
361 aMediaProperties
[0].Name
= OUString( "URL" );
362 aMediaProperties
[0].Value
<<= sImageURL
;
363 xGraphic
= xProvider
->queryGraphic( aMediaProperties
);
364 OSL_ENSURE( xGraphic
.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
366 catch( const Exception
& )
368 DBG_UNHANDLED_EXCEPTION();
373 } } // namespace connectivity::hsqldb
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */