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>
23 #include <bitmaps.hlst>
25 #include <connectivity/dbtools.hxx>
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>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <sal/log.hxx>
39 #include <tools/diagnose_ex.h>
41 #include <resource/sharedresources.hxx>
42 #include <strings.hrc>
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
;
73 namespace connectivity
{ namespace hsqldb
75 void SAL_CALL
OHsqlConnection::disposing()
77 m_aFlushListeners
.disposeAndClear( EventObject( *this ) );
78 OHsqlConnection_BASE::disposing();
79 OConnectionWrapper::disposing();
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
)
91 setDelegation(_xConnection
,_rxContext
,m_refCount
);
94 OHsqlConnection::~OHsqlConnection()
96 if ( !OHsqlConnection_BASE::rBHelper
.bDisposed
)
98 osl_atomic_increment( &m_refCount
);
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
)
108 ::osl::Mutex
& OHsqlConnection::getMutex() const
114 void OHsqlConnection::checkDisposed() const
116 ::connectivity::checkDisposed( rBHelper
.bDisposed
);
121 void SAL_CALL
OHsqlConnection::flush( )
123 MethodGuard
aGuard( *this );
127 if ( m_xConnection
.is() )
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
)
138 if ( pIter
->Name
== "readonly" )
146 Reference
< XStatement
> xStmt( m_xConnection
->createStatement(), css::uno::UNO_SET_THROW
);
147 xStmt
->execute( "CHECKPOINT DEFRAG" );
150 catch(const Exception
& )
152 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
156 EventObject
aFlushedEvent( *this );
157 m_aFlushListeners
.notifyEach( &XFlushListener::flushed
, aFlushedEvent
);
159 catch(const Exception
& )
161 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
166 void SAL_CALL
OHsqlConnection::addFlushListener( const Reference
< XFlushListener
>& l
)
168 MethodGuard
aGuard( *this );
169 m_aFlushListeners
.addInterface( l
);
173 void SAL_CALL
OHsqlConnection::removeFlushListener( const Reference
< XFlushListener
>& l
)
175 MethodGuard
aGuard( *this );
176 m_aFlushListeners
.removeInterface( l
);
180 Reference
< XGraphic
> SAL_CALL
OHsqlConnection::getTableIcon( const OUString
& TableName
, ::sal_Int32
/*_ColorMode*/ )
182 MethodGuard
aGuard( *this );
184 impl_checkExistingTable_throw( TableName
);
185 if ( !impl_isTextTable_nothrow( TableName
) )
188 return impl_getTextTableIcon_nothrow();
192 Reference
< XInterface
> SAL_CALL
OHsqlConnection::getTableEditor( const Reference
< XDatabaseDocumentUI
>& DocumentUI
, const OUString
& TableName
)
194 MethodGuard
aGuard( *this );
196 impl_checkExistingTable_throw( TableName
);
197 if ( !impl_isTextTable_nothrow( TableName
) )
200 if ( !DocumentUI
.is() )
202 ::connectivity::SharedResources aResources
;
203 const OUString
sError( aResources
.getResourceString(STR_NO_DOCUMENTUI
));
204 throw IllegalArgumentException(
209 } // if ( !_DocumentUI.is() )
212 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
213 // return xEditor.get();
215 // editor not yet implemented in this CWS
219 Reference
< XNameAccess
> OHsqlConnection::impl_getTableContainer_throw()
221 Reference
< XNameAccess
> xTables
;
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
);
229 catch( const RuntimeException
& ) { throw; }
230 catch( const Exception
& )
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
);
238 SAL_WARN_IF( !xTables
.is(), "connectivity.hsqldb", "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
244 void OHsqlConnection::impl_checkExistingTable_throw( const OUString
& _rTableName
)
246 bool bDoesExist
= false;
249 Reference
< XNameAccess
> xTables( impl_getTableContainer_throw(), css::uno::UNO_SET_THROW
);
250 bDoesExist
= xTables
->hasByName( _rTableName
);
252 catch( const Exception
& )
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");
261 ::connectivity::SharedResources aResources
;
262 const OUString
sError( aResources
.getResourceStringWithSubstitution(
264 "$tablename$", _rTableName
266 throw IllegalArgumentException( sError
,*this, 0 );
267 } // if ( !bDoesExist )
271 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString
& _rTableName
)
273 bool bIsTextTable
= false;
276 Reference
< XConnection
> xMe( *this, UNO_QUERY_THROW
);
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
);
283 // get the table information
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'" );
289 Reference
< XStatement
> xStatement( xMe
->createStatement(), css::uno::UNO_SET_THROW
);
290 Reference
< XResultSet
> xTableHsqlType( xStatement
->executeQuery( sSQL
.makeStringAndClear() ), css::uno::UNO_SET_THROW
);
292 if ( xTableHsqlType
->next() ) // might not succeed in case of VIEWs
294 Reference
< XRow
> xValueAccess( xTableHsqlType
, UNO_QUERY_THROW
);
295 OUString sTableType
= xValueAccess
->getString( 1 );
296 bIsTextTable
= sTableType
== "TEXT";
299 catch( const Exception
& )
301 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
308 Reference
< XGraphic
> OHsqlConnection::impl_getTextTableIcon_nothrow()
310 Reference
< XGraphic
> xGraphic
;
313 // create a graphic provider
314 Reference
< XGraphicProvider
> xProvider
;
315 if ( m_xContext
.is() )
316 xProvider
.set( GraphicProvider::create(m_xContext
) );
318 // assemble the image URL
319 OUStringBuffer aImageURL
;
320 // load the graphic from the global graphic repository
321 aImageURL
.append( "private:graphicrepository/" );
322 // the relative path within the images.zip
323 aImageURL
.append( LINKED_TEXT_TABLE_IMAGE_RESOURCE
);
324 // the name of the graphic to use
325 OUString
sImageURL( aImageURL
.makeStringAndClear() );
327 // ask the provider to obtain a graphic
328 Sequence
< PropertyValue
> aMediaProperties( 1 );
329 aMediaProperties
[0].Name
= "URL";
330 aMediaProperties
[0].Value
<<= sImageURL
;
331 xGraphic
= xProvider
->queryGraphic( aMediaProperties
);
332 OSL_ENSURE( xGraphic
.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
334 catch( const Exception
& )
336 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
341 } } // namespace connectivity::hsqldb
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */