LibreOffice Module connectivity (master) 1
HConnection.cxx
Go to the documentation of this file.
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
22#include <hsqldb/HTools.hxx>
23#include <bitmaps.hlst>
24
26
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>
34
37#include <rtl/ustrbuf.hxx>
38#include <sal/log.hxx>
40
42#include <strings.hrc>
43
44using ::com::sun::star::util::XFlushListener;
45using ::com::sun::star::lang::EventObject;
46using ::com::sun::star::uno::Reference;
47using ::com::sun::star::uno::Exception;
48using ::com::sun::star::uno::RuntimeException;
49using ::com::sun::star::uno::UNO_QUERY;
50using ::com::sun::star::uno::UNO_QUERY_THROW;
51using ::com::sun::star::uno::XComponentContext;
52using ::com::sun::star::sdbc::XStatement;
53using ::com::sun::star::sdbc::XConnection;
54using ::com::sun::star::sdbcx::XDataDefinitionSupplier;
55using ::com::sun::star::sdbcx::XTablesSupplier;
56using ::com::sun::star::container::XNameAccess;
57using ::com::sun::star::uno::Sequence;
58using ::com::sun::star::lang::WrappedTargetException;
59using ::com::sun::star::sdbc::XDriver;
60using ::com::sun::star::graphic::XGraphic;
61using ::com::sun::star::graphic::GraphicProvider;
62using ::com::sun::star::graphic::XGraphicProvider;
63using ::com::sun::star::uno::XInterface;
64using ::com::sun::star::lang::IllegalArgumentException;
65using ::com::sun::star::sdbc::XResultSet;
66using ::com::sun::star::sdbc::XDatabaseMetaData;
67using ::com::sun::star::sdbc::XDatabaseMetaData2;
68using ::com::sun::star::sdbc::XRow;
69using ::com::sun::star::sdb::application::XDatabaseDocumentUI;
70using ::com::sun::star::beans::PropertyValue;
71
72
74{
76 {
77 m_aFlushListeners.disposeAndClear( EventObject( *this ) );
78 OHsqlConnection_BASE::disposing();
80 }
81
82 OHsqlConnection::OHsqlConnection( const Reference< XDriver >& _rxDriver,
83 const Reference< XConnection >& _xConnection ,const Reference< XComponentContext >& _rxContext )
85 ,m_aFlushListeners( m_aMutex )
86 ,m_xDriver( _rxDriver )
87 ,m_xContext( _rxContext )
88 ,m_bIni(true)
89 ,m_bReadOnly(false)
90 {
91 setDelegation(_xConnection,_rxContext,m_refCount);
92 }
93
95 {
96 if ( !OHsqlConnection_BASE::rBHelper.bDisposed )
97 {
98 osl_atomic_increment( &m_refCount );
99 dispose();
100 }
101 }
102
104 IMPLEMENT_SERVICE_INFO(OHsqlConnection, "com.sun.star.sdbc.drivers.hsqldb.OHsqlConnection", "com.sun.star.sdbc.Connection")
106
107
108 ::osl::Mutex& OHsqlConnection::getMutex() const
109 {
110 return m_aMutex;
111 }
112
113
115 {
116 ::connectivity::checkDisposed( rBHelper.bDisposed );
117 }
118
119 // XFlushable
120
121 void SAL_CALL OHsqlConnection::flush( )
122 {
123 MethodGuard aGuard( *this );
124
125 try
126 {
127 if ( m_xConnection.is() )
128 {
129 if ( m_bIni )
130 {
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)
137 {
138 if ( pIter->Name == "readonly" )
139 m_bReadOnly = true;
140 }
141 }
142 try
143 {
144 if ( !m_bReadOnly )
145 {
146 Reference< XStatement > xStmt( m_xConnection->createStatement(), css::uno::UNO_SET_THROW );
147 xStmt->execute( "CHECKPOINT DEFRAG" );
148 }
149 }
150 catch(const Exception& )
151 {
152 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
153 }
154 }
155
156 EventObject aFlushedEvent( *this );
157 m_aFlushListeners.notifyEach( &XFlushListener::flushed, aFlushedEvent );
158 }
159 catch(const Exception& )
160 {
161 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
162 }
163 }
164
165
166 void SAL_CALL OHsqlConnection::addFlushListener( const Reference< XFlushListener >& l )
167 {
168 MethodGuard aGuard( *this );
170 }
171
172
173 void SAL_CALL OHsqlConnection::removeFlushListener( const Reference< XFlushListener >& l )
174 {
175 MethodGuard aGuard( *this );
177 }
178
179
180 Reference< XGraphic > SAL_CALL OHsqlConnection::getTableIcon( const OUString& TableName, ::sal_Int32 /*_ColorMode*/ )
181 {
182 MethodGuard aGuard( *this );
183
185 if ( !impl_isTextTable_nothrow( TableName ) )
186 return nullptr;
187
189 }
190
191
192 Reference< XInterface > SAL_CALL OHsqlConnection::getTableEditor( const Reference< XDatabaseDocumentUI >& DocumentUI, const OUString& TableName )
193 {
194 MethodGuard aGuard( *this );
195
197 if ( !impl_isTextTable_nothrow( TableName ) )
198 return nullptr;
199
200 if ( !DocumentUI.is() )
201 {
203 const OUString sError( aResources.getResourceString(STR_NO_DOCUMENTUI));
204 throw IllegalArgumentException(
205 sError,
206 *this,
207 0
208 );
209 } // if ( !_DocumentUI.is() )
210
211
212// Reference< XExecutableDialog > xEditor = impl_createLinkedTableEditor_throw( _DocumentUI, _TableName );
213// return xEditor.get();
214 return nullptr;
215 // editor not yet implemented in this CWS
216 }
217
218
220 {
221 Reference< XNameAccess > xTables;
222 try
223 {
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 );
228 }
229 catch( const RuntimeException& ) { throw; }
230 catch( const Exception& )
231 {
232 css::uno::Any anyEx = cppu::getCaughtException();
234 const OUString sError( aResources.getResourceString(STR_NO_TABLE_CONTAINER));
235 throw WrappedTargetException( sError ,*this, anyEx );
236 }
237
238 SAL_WARN_IF( !xTables.is(), "connectivity.hsqldb", "OHsqlConnection::impl_getTableContainer_throw: post condition not met!" );
239 return xTables;
240 }
241
242 //TODO: resource
243
244 void OHsqlConnection::impl_checkExistingTable_throw( const OUString& _rTableName )
245 {
246 bool bDoesExist = false;
247 try
248 {
249 Reference< XNameAccess > xTables( impl_getTableContainer_throw(), css::uno::UNO_SET_THROW );
250 bDoesExist = xTables->hasByName( _rTableName );
251 }
252 catch( const Exception& )
253 {
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");
257 }
258
259 if ( !bDoesExist )
260 {
262 const OUString sError( aResources.getResourceStringWithSubstitution(
263 STR_NO_TABLENAME,
264 "$tablename$", _rTableName
265 ));
266 throw IllegalArgumentException( sError,*this, 0 );
267 } // if ( !bDoesExist )
268 }
269
270
271 bool OHsqlConnection::impl_isTextTable_nothrow( const OUString& _rTableName )
272 {
273 bool bIsTextTable = false;
274 try
275 {
276 Reference< XConnection > xMe( *this, UNO_QUERY_THROW );
277
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 );
282
283 // get the table information
284 OUStringBuffer sSQL( "SELECT HSQLDB_TYPE FROM INFORMATION_SCHEMA.SYSTEM_TABLES" );
286 sSQL.append( " AND TABLE_TYPE = 'TABLE'" );
287
288 Reference< XStatement > xStatement( xMe->createStatement(), css::uno::UNO_SET_THROW );
289 Reference< XResultSet > xTableHsqlType( xStatement->executeQuery( sSQL.makeStringAndClear() ), css::uno::UNO_SET_THROW );
290
291 if ( xTableHsqlType->next() ) // might not succeed in case of VIEWs
292 {
293 Reference< XRow > xValueAccess( xTableHsqlType, UNO_QUERY_THROW );
294 OUString sTableType = xValueAccess->getString( 1 );
295 bIsTextTable = sTableType == "TEXT";
296 }
297 }
298 catch( const Exception& )
299 {
300 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
301 }
302
303 return bIsTextTable;
304 }
305
306
308 {
309 Reference< XGraphic > xGraphic;
310 try
311 {
312 // create a graphic provider
313 Reference< XGraphicProvider > xProvider;
314 if ( m_xContext.is() )
315 xProvider.set( GraphicProvider::create(m_xContext) );
316
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!" );
326 }
327 catch( const Exception& )
328 {
329 DBG_UNHANDLED_EXCEPTION("connectivity.hsqldb");
330 }
331 return xGraphic;
332 }
333
334} // namespace connectivity::hsqldb
335
336/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname)
OptionalString sSchema
OptionalString sCatalog
Reference< XComponentContext > m_xContext
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
void setDelegation(css::uno::Reference< css::uno::XAggregation > &_rxProxyConnection, oslInterlockedCount &_rRefCount)
css::uno::Reference< css::sdbc::XConnection > m_xConnection
helper class for accessing resources shared by different libraries in the connectivity module
OUString getResourceString(TranslateId pResId) const
loads a string from the shared resource file
OUString getResourceStringWithSubstitution(TranslateId pResId, const char *_pAsciiPatternToReplace, const OUString &_rStringToSubstitute) const
loads a string from the shared resource file, and replaces a given ASCII pattern with a given string
static void appendTableFilterCrit(OUStringBuffer &_inout_rBuffer, std::u16string_view _rCatalog, std::u16string_view _rSchema, std::u16string_view _rName, bool _bShortForm)
appends a proper WHERE clause to the given buffer, which filters for a given table name
Definition: HTools.cxx:26
css::uno::Reference< css::sdbc::XDriver > m_xDriver
Definition: HConnection.hxx:58
virtual void checkDisposed() const override
css::uno::Reference< css::container::XNameAccess > impl_getTableContainer_throw()
retrieves our table container
OHsqlConnection(const css::uno::Reference< css::sdbc::XDriver > &_rxDriver, const css::uno::Reference< css::sdbc::XConnection > &_xConnection, const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
Definition: HConnection.cxx:82
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: HConnection.hxx:59
virtual void SAL_CALL removeFlushListener(const css::uno::Reference< css::util::XFlushListener > &l) override
virtual void SAL_CALL disposing() override
Definition: HConnection.cxx:75
css::uno::Reference< css::graphic::XGraphic > impl_getTextTableIcon_nothrow()
retrieves the icon for HSQL TEXT TABLEs
bool impl_isTextTable_nothrow(const OUString &_rTableName)
checks whether the given table name refers to a HSQL TEXT TABLE
void impl_checkExistingTable_throw(const OUString &_rTableName)
checks whether the given table name denotes an existing table
virtual void SAL_CALL flush() override
virtual void SAL_CALL addFlushListener(const css::uno::Reference< css::util::XFlushListener > &l) override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getTableEditor(const css::uno::Reference< css::sdb::application::XDatabaseDocumentUI > &DocumentUI, const OUString &TableName) override
::comphelper::OInterfaceContainerHelper2 m_aFlushListeners
Definition: HConnection.hxx:57
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL getTableIcon(const OUString &TableName, ::sal_Int32 ColorMode) override
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
OUString sName
std::mutex m_aMutex
bool m_bReadOnly
#define SAL_WARN_IF(condition, area, stream)
@ Exception
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
::cppu::WeakComponentImplHelper< css::util::XFlushable, css::sdb::application::XTableUIProvider > OHsqlConnection_BASE
Definition: HConnection.hxx:49
IMPLEMENT_FORWARD_XTYPEPROVIDER2(OHsqlConnection, OHsqlConnection_BASE, OConnectionWrapper)
void checkDisposed(bool _bThrow)
Definition: dbtools.cxx:1951
Any SAL_CALL getCaughtException()
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
void dispose()