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