Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HConnection.cxx
blob28feb95fce9b81b2bff69dda6368cede5db33590
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/beans/PropertyValue.hpp>
33 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
35 #include <comphelper/propertyvalue.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <sal/log.hxx>
39 #include <comphelper/diagnose_ex.hxx>
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::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 )
88 ,m_bIni(true)
89 ,m_bReadOnly(false)
91 setDelegation(_xConnection,_rxContext,m_refCount);
94 OHsqlConnection::~OHsqlConnection()
96 if ( !OHsqlConnection_BASE::rBHelper.bDisposed )
98 osl_atomic_increment( &m_refCount );
99 dispose();
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
110 return m_aMutex;
114 void OHsqlConnection::checkDisposed() const
116 ::connectivity::checkDisposed( rBHelper.bDisposed );
119 // XFlushable
121 void SAL_CALL OHsqlConnection::flush( )
123 MethodGuard aGuard( *this );
127 if ( m_xConnection.is() )
129 if ( m_bIni )
131 m_bIni = false;
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" )
139 m_bReadOnly = true;
144 if ( !m_bReadOnly )
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 ) )
186 return nullptr;
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 ) )
198 return nullptr;
200 if ( !DocumentUI.is() )
202 ::connectivity::SharedResources aResources;
203 const OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI));
204 throw IllegalArgumentException(
205 sError,
206 *this,
209 } // if ( !_DocumentUI.is() )
212 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
213 // return xEditor.get();
214 return nullptr;
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!" );
239 return xTables;
242 //TODO: resource
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");
259 if ( !bDoesExist )
261 ::connectivity::SharedResources aResources;
262 const OUString sError( aResources.getResourceStringWithSubstitution(
263 STR_NO_TABLENAME,
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
284 OUStringBuffer sSQL( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
285 HTools::appendTableFilterCrit( sSQL, sCatalog, sSchema, sName, true );
286 sSQL.append( " AND TABLE_TYPE = 'TABLE'" );
288 Reference< XStatement > xStatement( xMe->createStatement(), css::uno::UNO_SET_THROW );
289 Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), css::uno::UNO_SET_THROW );
291 if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs
293 Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW );
294 OUString sTableType = xValueAccess->getString( 1 );
295 bIsTextTable = sTableType == "TEXT";
298 catch( const Exception& )
300 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
303 return bIsTextTable;
307 Reference< XGraphic > OHsqlConnection::impl_getTextTableIcon_nothrow()
309 Reference< XGraphic > xGraphic;
312 // create a graphic provider
313 Reference< XGraphicProvider > xProvider;
314 if ( m_xContext.is() )
315 xProvider.set( GraphicProvider::create(m_xContext) );
317 // ask the provider to obtain a graphic
318 Sequence< PropertyValue > aMediaProperties{ comphelper::makePropertyValue(
319 "URL", OUString(
320 // load the graphic from the global graphic repository
321 "private:graphicrepository/"
322 // the relative path within the images.zip
323 LINKED_TEXT_TABLE_IMAGE_RESOURCE)) };
324 xGraphic = xProvider->queryGraphic( aMediaProperties );
325 OSL_ENSURE( xGraphic.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
327 catch( const Exception& )
329 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
331 return xGraphic;
334 } // namespace connectivity::hsqldb
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */