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