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