Line data Source code
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 "hsqlui.hrc"
24 :
25 : #include <connectivity/dbtools.hxx>
26 :
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>
37 :
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>
44 :
45 : #include "resource/sharedresources.hxx"
46 : #include "resource/hsqldb_res.hrc"
47 :
48 : /** === begin UNO using === **/
49 : using ::com::sun::star::util::XFlushListener;
50 : using ::com::sun::star::lang::EventObject;
51 : using ::com::sun::star::uno::Reference;
52 : using ::com::sun::star::uno::Exception;
53 : using ::com::sun::star::uno::RuntimeException;
54 : using ::com::sun::star::uno::UNO_QUERY;
55 : using ::com::sun::star::uno::UNO_QUERY_THROW;
56 : using ::com::sun::star::uno::XComponentContext;
57 : using ::com::sun::star::sdbc::XStatement;
58 : using ::com::sun::star::sdbc::XConnection;
59 : using ::com::sun::star::sdbcx::XDataDefinitionSupplier;
60 : using ::com::sun::star::sdbcx::XTablesSupplier;
61 : using ::com::sun::star::container::XNameAccess;
62 : using ::com::sun::star::uno::Sequence;
63 : using ::com::sun::star::beans::NamedValue;
64 : using ::com::sun::star::lang::WrappedTargetException;
65 : using ::com::sun::star::lang::ServiceNotRegisteredException;
66 : using ::com::sun::star::sdbc::XDriver;
67 : using ::com::sun::star::lang::XMultiServiceFactory;
68 : using ::com::sun::star::graphic::XGraphic;
69 : using ::com::sun::star::graphic::GraphicProvider;
70 : using ::com::sun::star::graphic::XGraphicProvider;
71 : using ::com::sun::star::uno::XInterface;
72 : using ::com::sun::star::lang::IllegalArgumentException;
73 : using ::com::sun::star::ui::dialogs::XExecutableDialog;
74 : using ::com::sun::star::uno::Any;
75 : using ::com::sun::star::uno::makeAny;
76 : using ::com::sun::star::sdbc::XResultSet;
77 : using ::com::sun::star::sdbc::XDatabaseMetaData;
78 : using ::com::sun::star::sdbc::XDatabaseMetaData2;
79 : using ::com::sun::star::sdbc::XRow;
80 : using ::com::sun::star::sdb::application::XDatabaseDocumentUI;
81 : using ::com::sun::star::beans::PropertyValue;
82 : /** === end UNO using === **/
83 : namespace GraphicColorMode = ::com::sun::star::graphic::GraphicColorMode;
84 :
85 : namespace connectivity { namespace hsqldb
86 : {
87 : // =============================================================================
88 : // = FlushListeners
89 : // =============================================================================
90 : typedef ::comphelper::OListenerContainerBase< XFlushListener, EventObject > FlushListeners_Base;
91 0 : class FlushListeners : public FlushListeners_Base
92 : {
93 : public:
94 : FlushListeners( ::osl::Mutex& _rMutex ) :FlushListeners_Base( _rMutex ) { }
95 :
96 : protected:
97 : virtual bool implTypedNotify(
98 : const Reference< XFlushListener >& _rxListener,
99 : const EventObject& _rEvent
100 : ) SAL_THROW( ( Exception ) );
101 : };
102 :
103 : // -----------------------------------------------------------------------------
104 0 : bool FlushListeners::implTypedNotify( const Reference< XFlushListener >& _rxListener, const EventObject& _rEvent ) SAL_THROW( ( Exception ) )
105 : {
106 0 : _rxListener->flushed( _rEvent );
107 0 : return true; // continue notifying the other listeners, if any
108 : }
109 :
110 : // =============================================================================
111 : // = OHsqlConnection
112 : // =============================================================================
113 : // -----------------------------------------------------------------------------
114 0 : void SAL_CALL OHsqlConnection::disposing(void)
115 : {
116 0 : m_aFlushListeners.disposeAndClear( EventObject( *this ) );
117 0 : OHsqlConnection_BASE::disposing();
118 0 : OConnectionWrapper::disposing();
119 0 : }
120 : // -----------------------------------------------------------------------------
121 0 : OHsqlConnection::OHsqlConnection( const Reference< XDriver > _rxDriver,
122 : const Reference< XConnection >& _xConnection ,const Reference< XComponentContext >& _rxContext )
123 : :OHsqlConnection_BASE( m_aMutex )
124 : ,m_aFlushListeners( m_aMutex )
125 : ,m_xDriver( _rxDriver )
126 : ,m_xContext( _rxContext )
127 : ,m_bIni(true)
128 0 : ,m_bReadOnly(false)
129 : {
130 0 : setDelegation(_xConnection,_rxContext,m_refCount);
131 0 : }
132 : // -----------------------------------------------------------------------------
133 0 : OHsqlConnection::~OHsqlConnection()
134 : {
135 0 : if ( !OHsqlConnection_BASE::rBHelper.bDisposed )
136 : {
137 0 : osl_atomic_increment( &m_refCount );
138 0 : dispose();
139 : }
140 0 : }
141 : // -----------------------------------------------------------------------------
142 0 : IMPLEMENT_FORWARD_XINTERFACE2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper)
143 0 : IMPLEMENT_SERVICE_INFO(OHsqlConnection, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection")
144 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection,OHsqlConnection_BASE,OConnectionWrapper)
145 :
146 : //--------------------------------------------------------------------
147 0 : ::osl::Mutex& OHsqlConnection::getMutex() const
148 : {
149 0 : return m_aMutex;
150 : }
151 :
152 : //--------------------------------------------------------------------
153 0 : void OHsqlConnection::checkDisposed() const
154 : {
155 0 : ::connectivity::checkDisposed( rBHelper.bDisposed );
156 0 : }
157 :
158 : // XFlushable
159 : //--------------------------------------------------------------------
160 0 : void SAL_CALL OHsqlConnection::flush( ) throw (RuntimeException)
161 : {
162 0 : MethodGuard aGuard( *this );
163 :
164 : try
165 : {
166 0 : if ( m_xConnection.is() )
167 : {
168 0 : if ( m_bIni )
169 : {
170 0 : m_bIni = false;
171 0 : Reference< XDatabaseMetaData2 > xMeta2(m_xConnection->getMetaData(),UNO_QUERY_THROW);
172 0 : const Sequence< PropertyValue > aInfo = xMeta2->getConnectionInfo();
173 0 : const PropertyValue* pIter = aInfo.getConstArray();
174 0 : const PropertyValue* pEnd = pIter + aInfo.getLength();
175 0 : for(;pIter != pEnd;++pIter)
176 : {
177 0 : if ( pIter->Name.compareToAscii("readonly") == 0 )
178 0 : m_bReadOnly = true;
179 0 : }
180 : }
181 0 : if ( !m_bReadOnly )
182 : {
183 0 : Reference< XStatement > xStmt( m_xConnection->createStatement(), UNO_QUERY_THROW );
184 0 : xStmt->execute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CHECKPOINT DEFRAG" ) ) );
185 : }
186 : }
187 :
188 0 : EventObject aFlushedEvent( *this );
189 0 : m_aFlushListeners.notifyEach( &XFlushListener::flushed, aFlushedEvent );
190 : }
191 0 : catch(const Exception& )
192 : {
193 : DBG_UNHANDLED_EXCEPTION();
194 0 : }
195 0 : }
196 :
197 : //--------------------------------------------------------------------
198 0 : void SAL_CALL OHsqlConnection::addFlushListener( const Reference< XFlushListener >& l ) throw (RuntimeException)
199 : {
200 0 : MethodGuard aGuard( *this );
201 0 : m_aFlushListeners.addInterface( l );
202 0 : }
203 :
204 : //--------------------------------------------------------------------
205 0 : void SAL_CALL OHsqlConnection::removeFlushListener( const Reference< XFlushListener >& l ) throw (RuntimeException)
206 : {
207 0 : MethodGuard aGuard( *this );
208 0 : m_aFlushListeners.removeInterface( l );
209 0 : }
210 :
211 : // -------------------------------------------------------------------
212 0 : Reference< XGraphic > SAL_CALL OHsqlConnection::getTableIcon( const ::rtl::OUString& _TableName, ::sal_Int32 /*_ColorMode*/ ) throw (RuntimeException)
213 : {
214 0 : MethodGuard aGuard( *this );
215 :
216 0 : impl_checkExistingTable_throw( _TableName );
217 0 : if ( !impl_isTextTable_nothrow( _TableName ) )
218 0 : return NULL;
219 :
220 0 : return impl_getTextTableIcon_nothrow();
221 : }
222 :
223 : // -------------------------------------------------------------------
224 0 : Reference< XInterface > SAL_CALL OHsqlConnection::getTableEditor( const Reference< XDatabaseDocumentUI >& _DocumentUI, const ::rtl::OUString& _TableName ) throw (IllegalArgumentException, WrappedTargetException, RuntimeException)
225 : {
226 0 : MethodGuard aGuard( *this );
227 :
228 0 : impl_checkExistingTable_throw( _TableName );
229 0 : if ( !impl_isTextTable_nothrow( _TableName ) )
230 0 : return NULL;
231 :
232 0 : if ( !_DocumentUI.is() )
233 : {
234 0 : ::connectivity::SharedResources aResources;
235 0 : const ::rtl::OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI));
236 : throw IllegalArgumentException(
237 : sError,
238 : *this,
239 : 0
240 0 : );
241 : } // if ( !_DocumentUI.is() )
242 :
243 :
244 : // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
245 : // return xEditor.get();
246 0 : return NULL;
247 : // editor not yet implemented in this CWS
248 : }
249 :
250 : // -------------------------------------------------------------------
251 0 : Reference< XNameAccess > OHsqlConnection::impl_getTableContainer_throw()
252 : {
253 0 : Reference< XNameAccess > xTables;
254 : try
255 : {
256 0 : Reference< XConnection > xMe( *this, UNO_QUERY );
257 0 : Reference< XDataDefinitionSupplier > xDefinitionsSupp( m_xDriver, UNO_QUERY_THROW );
258 0 : Reference< XTablesSupplier > xTablesSupp( xDefinitionsSupp->getDataDefinitionByConnection( xMe ), UNO_QUERY_THROW );
259 0 : xTables.set( xTablesSupp->getTables(), UNO_QUERY_THROW );
260 : }
261 0 : catch( const RuntimeException& ) { throw; }
262 0 : catch( const Exception& )
263 : {
264 0 : ::connectivity::SharedResources aResources;
265 0 : const ::rtl::OUString sError( aResources.getResourceString(STR_NO_TABLE_CONTAINER));
266 0 : throw WrappedTargetException( sError ,*this, ::cppu::getCaughtException() );
267 : }
268 :
269 : OSL_POSTCOND( xTables.is(), "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
270 0 : return xTables;
271 : }
272 :
273 : //TODO: resource
274 : // -------------------------------------------------------------------
275 0 : void OHsqlConnection::impl_checkExistingTable_throw( const ::rtl::OUString& _rTableName )
276 : {
277 0 : bool bDoesExist = false;
278 : try
279 : {
280 0 : Reference< XNameAccess > xTables( impl_getTableContainer_throw(), UNO_QUERY_THROW );
281 0 : if ( xTables.is() )
282 0 : bDoesExist = xTables->hasByName( _rTableName );
283 : }
284 0 : catch( const Exception& )
285 : {
286 : // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only
287 : // allowed to throw an IllegalArgumentException ourself
288 : DBG_UNHANDLED_EXCEPTION();
289 : }
290 :
291 0 : if ( !bDoesExist )
292 : {
293 0 : ::connectivity::SharedResources aResources;
294 : const ::rtl::OUString sError( aResources.getResourceStringWithSubstitution(
295 : STR_NO_TABLENAME,
296 : "$tablename$", _rTableName
297 0 : ));
298 0 : throw IllegalArgumentException( sError,*this, 0 );
299 : } // if ( !bDoesExist )
300 0 : }
301 :
302 : // -------------------------------------------------------------------
303 0 : bool OHsqlConnection::impl_isTextTable_nothrow( const ::rtl::OUString& _rTableName )
304 : {
305 0 : bool bIsTextTable = false;
306 : try
307 : {
308 0 : Reference< XConnection > xMe( *this, UNO_QUERY_THROW );
309 :
310 : // split the fully qualified name
311 0 : Reference< XDatabaseMetaData > xMetaData( xMe->getMetaData(), UNO_QUERY_THROW );
312 0 : ::rtl::OUString sCatalog, sSchema, sName;
313 0 : ::dbtools::qualifiedNameComponents( xMetaData, _rTableName, sCatalog, sSchema, sName, ::dbtools::eComplete );
314 :
315 : // get the table information
316 0 : ::rtl::OUStringBuffer sSQL;
317 0 : sSQL.appendAscii( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
318 0 : HTools::appendTableFilterCrit( sSQL, sCatalog, sSchema, sName, true );
319 0 : sSQL.appendAscii( " AND TABLE_TYPE = 'TABLE'" );
320 :
321 0 : Reference< XStatement > xStatement( xMe->createStatement(), UNO_QUERY_THROW );
322 0 : Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), UNO_QUERY_THROW );
323 :
324 0 : if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs
325 : {
326 0 : Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW );
327 0 : ::rtl::OUString sTableType = xValueAccess->getString( 1 );
328 0 : bIsTextTable = sTableType == "TEXT";
329 0 : }
330 : }
331 0 : catch( const Exception& )
332 : {
333 : DBG_UNHANDLED_EXCEPTION();
334 : }
335 :
336 0 : return bIsTextTable;
337 : }
338 :
339 : // -------------------------------------------------------------------
340 0 : Reference< XGraphic > OHsqlConnection::impl_getTextTableIcon_nothrow()
341 : {
342 0 : Reference< XGraphic > xGraphic;
343 : try
344 : {
345 : // create a graphic provider
346 0 : Reference< XGraphicProvider > xProvider;
347 0 : if ( m_xContext.is() )
348 0 : xProvider.set( GraphicProvider::create(m_xContext) );
349 :
350 : // assemble the image URL
351 0 : ::rtl::OUStringBuffer aImageURL;
352 : // load the graphic from the global graphic repository
353 0 : aImageURL.appendAscii( "private:graphicrepository/" );
354 : // the relative path within the images.zip
355 0 : aImageURL.appendAscii( "database/" );
356 0 : aImageURL.appendAscii( LINKED_TEXT_TABLE_IMAGE_RESOURCE );
357 : // the name of the graphic to use
358 0 : ::rtl::OUString sImageURL( aImageURL.makeStringAndClear() );
359 :
360 : // ask the provider to obtain a graphic
361 0 : Sequence< PropertyValue > aMediaProperties( 1 );
362 0 : aMediaProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
363 0 : aMediaProperties[0].Value <<= sImageURL;
364 0 : xGraphic = xProvider->queryGraphic( aMediaProperties );
365 0 : OSL_ENSURE( xGraphic.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
366 : }
367 0 : catch( const Exception& )
368 : {
369 : DBG_UNHANDLED_EXCEPTION();
370 : }
371 0 : return xGraphic;
372 : }
373 :
374 : } } // namespace connectivity::hsqldb
375 :
376 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|