tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HConnection.cxx
blob032d02d90e8a84c603b0ded19d6357c14fc60186
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();
81 OHsqlConnection::OHsqlConnection( const Reference< XDriver >& _rxDriver,
82 const Reference< XConnection >& _xConnection ,const Reference< XComponentContext >& _rxContext )
83 :m_aFlushListeners( m_aMutex )
84 ,m_xDriver( _rxDriver )
85 ,m_xContext( _rxContext )
86 ,m_bIni(true)
87 ,m_bReadOnly(false)
89 setDelegation(_xConnection, _rxContext);
92 OHsqlConnection::~OHsqlConnection()
94 if ( !OHsqlConnection_BASE::rBHelper.bDisposed )
96 osl_atomic_increment( &m_refCount );
97 dispose();
101 IMPLEMENT_SERVICE_INFO(OHsqlConnection, u"com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection"_ustr, u"com.sun.star.sdbc.Connection"_ustr)
104 ::osl::Mutex& OHsqlConnection::getMutex() const
106 return m_aMutex;
110 void OHsqlConnection::checkDisposed() const
112 ::connectivity::checkDisposed( rBHelper.bDisposed );
115 // XFlushable
117 void SAL_CALL OHsqlConnection::flush( )
119 MethodGuard aGuard( *this );
123 if ( m_xConnection.is() )
125 if ( m_bIni )
127 m_bIni = false;
128 Reference< XDatabaseMetaData2 > xMeta2(m_xConnection->getMetaData(),UNO_QUERY_THROW);
129 const Sequence< PropertyValue > aInfo = xMeta2->getConnectionInfo();
130 const PropertyValue* pIter = aInfo.getConstArray();
131 const PropertyValue* pEnd = pIter + aInfo.getLength();
132 for(;pIter != pEnd;++pIter)
134 if ( pIter->Name == "readonly" )
135 m_bReadOnly = true;
140 if ( !m_bReadOnly )
142 Reference< XStatement > xStmt( m_xConnection->createStatement(), css::uno::UNO_SET_THROW );
143 xStmt->execute( u"CHECKPOINT DEFRAG"_ustr );
146 catch(const Exception& )
148 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
152 EventObject aFlushedEvent( *this );
153 m_aFlushListeners.notifyEach( &XFlushListener::flushed, aFlushedEvent );
155 catch(const Exception& )
157 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
162 void SAL_CALL OHsqlConnection::addFlushListener( const Reference< XFlushListener >& l )
164 MethodGuard aGuard( *this );
165 m_aFlushListeners.addInterface( l );
169 void SAL_CALL OHsqlConnection::removeFlushListener( const Reference< XFlushListener >& l )
171 MethodGuard aGuard( *this );
172 m_aFlushListeners.removeInterface( l );
176 Reference< XGraphic > SAL_CALL OHsqlConnection::getTableIcon( const OUString& TableName, ::sal_Int32 /*_ColorMode*/ )
178 MethodGuard aGuard( *this );
180 impl_checkExistingTable_throw( TableName );
181 if ( !impl_isTextTable_nothrow( TableName ) )
182 return nullptr;
184 return impl_getTextTableIcon_nothrow();
188 Reference< XInterface > SAL_CALL OHsqlConnection::getTableEditor( const Reference< XDatabaseDocumentUI >& DocumentUI, const OUString& TableName )
190 MethodGuard aGuard( *this );
192 impl_checkExistingTable_throw( TableName );
193 if ( !impl_isTextTable_nothrow( TableName ) )
194 return nullptr;
196 if ( !DocumentUI.is() )
198 ::connectivity::SharedResources aResources;
199 const OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI));
200 throw IllegalArgumentException(
201 sError,
202 *this,
205 } // if ( !_DocumentUI.is() )
208 // Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
209 // return xEditor.get();
210 return nullptr;
211 // editor not yet implemented in this CWS
215 Reference< XNameAccess > OHsqlConnection::impl_getTableContainer_throw()
217 Reference< XNameAccess > xTables;
220 Reference< XConnection > xMe( *this, UNO_QUERY );
221 Reference< XDataDefinitionSupplier > xDefinitionsSupp( m_xDriver, UNO_QUERY_THROW );
222 Reference< XTablesSupplier > xTablesSupp( xDefinitionsSupp->getDataDefinitionByConnection( xMe ), css::uno::UNO_SET_THROW );
223 xTables.set( xTablesSupp->getTables(), css::uno::UNO_SET_THROW );
225 catch( const RuntimeException& ) { throw; }
226 catch( const Exception& )
228 css::uno::Any anyEx = cppu::getCaughtException();
229 ::connectivity::SharedResources aResources;
230 const OUString sError( aResources.getResourceString(STR_NO_TABLE_CONTAINER));
231 throw WrappedTargetException( sError ,*this, anyEx );
234 SAL_WARN_IF( !xTables.is(), "connectivity.hsqldb", "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
235 return xTables;
238 //TODO: resource
240 void OHsqlConnection::impl_checkExistingTable_throw( const OUString& _rTableName )
242 bool bDoesExist = false;
245 Reference< XNameAccess > xTables( impl_getTableContainer_throw(), css::uno::UNO_SET_THROW );
246 bDoesExist = xTables->hasByName( _rTableName );
248 catch( const Exception& )
250 // that's a serious error in impl_getTableContainer_throw, or hasByName, however, we're only
251 // allowed to throw an IllegalArgumentException ourself
252 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
255 if ( !bDoesExist )
257 ::connectivity::SharedResources aResources;
258 const OUString sError( aResources.getResourceStringWithSubstitution(
259 STR_NO_TABLENAME,
260 "$tablename$", _rTableName
262 throw IllegalArgumentException( sError,*this, 0 );
263 } // if ( !bDoesExist )
267 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString& _rTableName )
269 bool bIsTextTable = false;
272 Reference< XConnection > xMe( *this, UNO_QUERY_THROW );
274 // split the fully qualified name
275 Reference< XDatabaseMetaData > xMetaData( xMe->getMetaData(), css::uno::UNO_SET_THROW );
276 OUString sCatalog, sSchema, sName;
277 ::dbtools::qualifiedNameComponents( xMetaData, _rTableName, sCatalog, sSchema, sName, ::dbtools::EComposeRule::Complete );
279 // get the table information
280 OUStringBuffer sSQL( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
281 HTools::appendTableFilterCrit( sSQL, sCatalog, sSchema, sName, true );
282 sSQL.append( " AND TABLE_TYPE = 'TABLE'" );
284 Reference< XStatement > xStatement( xMe->createStatement(), css::uno::UNO_SET_THROW );
285 Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), css::uno::UNO_SET_THROW );
287 if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs
289 Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW );
290 OUString sTableType = xValueAccess->getString( 1 );
291 bIsTextTable = sTableType == "TEXT";
294 catch( const Exception& )
296 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
299 return bIsTextTable;
303 Reference< XGraphic > OHsqlConnection::impl_getTextTableIcon_nothrow()
305 Reference< XGraphic > xGraphic;
308 // create a graphic provider
309 Reference< XGraphicProvider > xProvider;
310 if ( m_xContext.is() )
311 xProvider.set( GraphicProvider::create(m_xContext) );
313 // ask the provider to obtain a graphic
314 Sequence< PropertyValue > aMediaProperties{ comphelper::makePropertyValue(
315 u"URL"_ustr, u"private:graphicrepository/"
316 // the relative path within the images.zip
317 LINKED_TEXT_TABLE_IMAGE_RESOURCE ""_ustr) };
318 xGraphic = xProvider->queryGraphic( aMediaProperties );
319 OSL_ENSURE( xGraphic.is(), "OHsqlConnection::impl_getTextTableIcon_nothrow: the provider did not give us a graphic object!" );
321 catch( const Exception& )
323 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
325 return xGraphic;
328 } // namespace connectivity::hsqldb
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */