LibreOffice Module dbaccess (master) 1
tablecontainer.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#include <tablecontainer.hxx>
21#include <table.hxx>
24#include <core_resource.hxx>
25#include <strings.hrc>
26#include <strings.hxx>
27#include <com/sun/star/beans/XPropertySet.hpp>
28#include <com/sun/star/beans/PropertyState.hpp>
29#include <com/sun/star/beans/XPropertyState.hpp>
30#include <com/sun/star/sdb/TableDefinition.hpp>
31#include <com/sun/star/sdbc/XConnection.hpp>
32#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
33#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
34#include <com/sun/star/sdbc/XRow.hpp>
35#include <com/sun/star/sdbc/SQLException.hpp>
36#include <comphelper/types.hxx>
39#include <TableDeco.hxx>
40#include <sdbcoretools.hxx>
41#include <ContainerMediator.hxx>
44
45using namespace dbaccess;
46using namespace dbtools;
47using namespace ::com::sun::star::uno;
48using namespace ::com::sun::star::lang;
49using namespace ::com::sun::star::beans;
50using namespace ::com::sun::star::sdbc;
51using namespace ::com::sun::star::sdb;
52using namespace ::com::sun::star::sdbcx;
53using namespace ::com::sun::star::container;
54using namespace ::com::sun::star::util;
55using namespace ::osl;
56using namespace ::comphelper;
57using namespace ::cppu;
58using namespace ::connectivity::sdbcx;
59
60namespace
61{
62 bool lcl_isPropertySetDefaulted(const Sequence< OUString>& _aNames,const Reference<XPropertySet>& _xProp)
63 {
64 Reference<XPropertyState> xState(_xProp,UNO_QUERY);
65 if ( !xState )
66 return false;
67 const OUString* pIter = _aNames.getConstArray();
68 const OUString* pEnd = pIter + _aNames.getLength();
69 for(;pIter != pEnd;++pIter)
70 {
71 try
72 {
73 PropertyState aState = xState->getPropertyState(*pIter);
74 if ( aState != PropertyState_DEFAULT_VALUE )
75 break;
76 }
77 catch(const Exception&)
78 {
79 TOOLS_WARN_EXCEPTION("dbaccess", "" );
80 }
81 }
82 return ( pIter == pEnd );
83 }
84}
85
86// OTableContainer
87
88OTableContainer::OTableContainer(::cppu::OWeakObject& _rParent,
89 ::osl::Mutex& _rMutex,
90 const Reference< XConnection >& _xCon,
91 bool _bCase,
92 const Reference< XNameContainer >& _xTableDefinitions,
93 IRefreshListener* _pRefreshListener,
94 std::atomic<std::size_t>& _nInAppend)
95 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_nInAppend)
96 ,m_xTableDefinitions(_xTableDefinitions)
97{
98}
99
101{
102}
103
105{
106 try
107 {
108 Reference<XContainer> xCont( m_xMasterContainer, UNO_QUERY_THROW );
109 xCont->removeContainerListener( this );
110 }
111 catch( const Exception& )
112 {
113 DBG_UNHANDLED_EXCEPTION("dbaccess");
114 }
115}
116
118{
119 // no restriction at all (other than the ones provided externally)
120 return OUString();
121}
122
123// XServiceInfo
124OUString SAL_CALL OTableContainer::getImplementationName()
125 {
126 return "com.sun.star.sdb.dbaccess.OTableContainer";
127 }
128sal_Bool SAL_CALL OTableContainer::supportsService(const OUString& _rServiceName)
129 {
130 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
131 for (const OUString& s : aSupported)
132 if (s == _rServiceName)
133 return true;
134
135 return false;
136 }
137css::uno::Sequence< OUString > SAL_CALL OTableContainer::getSupportedServiceNames()
138{
140}
141
142
143namespace
144{
145void lcl_createDefinitionObject(const OUString& _rName
146 ,const Reference< XNameContainer >& _xTableDefinitions
147 ,Reference<XPropertySet>& _xTableDefinition
148 ,Reference<XNameAccess>& _xColumnDefinitions)
149{
150 if ( !_xTableDefinitions.is() )
151 return;
152
153 if ( _xTableDefinitions->hasByName(_rName) )
154 _xTableDefinition.set(_xTableDefinitions->getByName(_rName),UNO_QUERY);
155 else
156 {
157 // set as folder
158 _xTableDefinition = TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName );
159 _xTableDefinitions->insertByName(_rName,Any(_xTableDefinition));
160 }
161 Reference<XColumnsSupplier> xColumnsSupplier(_xTableDefinition,UNO_QUERY);
162 if ( xColumnsSupplier.is() )
163 _xColumnDefinitions = xColumnsSupplier->getColumns();
164}
165
166}
167
169{
170 Reference<XColumnsSupplier > xSup;
171 if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName))
172 xSup.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
173
175 if ( m_xMetaData.is() )
176 {
177 Reference<XPropertySet> xTableDefinition;
178 Reference<XNameAccess> xColumnDefinitions;
179 lcl_createDefinitionObject(_rName,m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
180
181 if ( xSup.is() )
182 {
183 rtl::Reference<ODBTableDecorator> pTable = new ODBTableDecorator( m_xConnection, xSup, ::dbtools::getNumberFormats( m_xConnection ) ,xColumnDefinitions);
184 xRet = pTable;
185 pTable->construct();
186 }
187 else
188 {
189 OUString sCatalog,sSchema,sTable;
190 ::dbtools::qualifiedNameComponents(m_xMetaData,
191 _rName,
192 sCatalog,
193 sSchema,
194 sTable,
195 ::dbtools::EComposeRule::InDataManipulation);
196 Any aCatalog;
197 if(!sCatalog.isEmpty())
198 aCatalog <<= sCatalog;
199 OUString sType,sDescription;
200 Sequence< OUString> aTypeFilter;
201 getAllTableTypeFilter( aTypeFilter );
202
203 Reference< XResultSet > xRes;
204 if ( m_xMetaData.is() )
205 xRes = m_xMetaData->getTables(aCatalog,sSchema,sTable,aTypeFilter);
206 if(xRes.is() && xRes->next())
207 {
208 Reference< XRow > xRow(xRes,UNO_QUERY);
209 if(xRow.is())
210 {
211 sType = xRow->getString(4);
212 sDescription = xRow->getString(5);
213 }
214 }
215 ::comphelper::disposeComponent(xRes);
216 rtl::Reference<ODBTable> pTable = new ODBTable(this
218 ,sCatalog
219 ,sSchema
220 ,sTable
221 ,sType
222 ,sDescription
223 ,xColumnDefinitions);
224 xRet = pTable;
225 pTable->construct();
226 }
227 Reference<XPropertySet> xDest(xRet,UNO_QUERY);
228 if ( xTableDefinition.is() )
229 ::comphelper::copyProperties(xTableDefinition,xDest);
230
231 if ( !m_pTableMediator.is() )
233 this, m_xTableDefinitions );
234 if ( m_pTableMediator.is() )
235 m_pTableMediator->notifyElementCreated(_rName,xDest);
236 }
237
238 return xRet;
239}
240
241Reference< XPropertySet > OTableContainer::createDescriptor()
242{
243 Reference< XPropertySet > xRet;
244
245 // first we have to look if the master tables support this
246 // and if so then create a table object as well with the master tables
247 Reference<XColumnsSupplier > xMasterColumnsSup;
248 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
249 if ( xDataFactory.is() && m_xMetaData.is() )
250 {
251 xMasterColumnsSup.set( xDataFactory->createDataDescriptor(), UNO_QUERY );
252 rtl::Reference<ODBTableDecorator> pTable = new ODBTableDecorator( m_xConnection, xMasterColumnsSup, ::dbtools::getNumberFormats( m_xConnection ) ,nullptr);
253 xRet = pTable;
254 pTable->construct();
255 }
256 else
257 {
259 xRet = pTable;
260 pTable->construct();
261 }
262 return xRet;
263}
264
265// XAppend
266ObjectType OTableContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
267{
268 // append the new table with a create stmt
269 OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
270 if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(aName))
271 {
272 OUString sMessage(DBA_RES(RID_STR_TABLE_IS_FILTERED));
273 throw SQLException(sMessage.replaceAll("$name$", aName),static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)),SQLSTATE_GENERAL,1000,Any());
274 }
275
276 Reference< XConnection > xConnection( m_xConnection.get(), UNO_QUERY );
277 PContainerApprove pApprove = std::make_shared<ObjectNameApproval>( xConnection, ObjectNameApproval::TypeTable );
278 pApprove->approveElement( aName );
279
280 {
281 EnsureReset aReset(m_nInAppend);
282 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
283 if(xAppend.is())
284 {
285 xAppend->appendByDescriptor(descriptor);
286 }
287 else
288 {
289 OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,m_xConnection);
290
291 Reference<XConnection> xCon = m_xConnection;
292 OSL_ENSURE(xCon.is(),"Connection is null!");
293 if ( xCon.is() )
294 {
295 Reference< XStatement > xStmt = xCon->createStatement( );
296 if ( xStmt.is() )
297 xStmt->execute(aSql);
298 ::comphelper::disposeComponent(xStmt);
299 }
300 }
301 }
302
303 Reference<XPropertySet> xTableDefinition;
304 Reference<XNameAccess> xColumnDefinitions;
305 lcl_createDefinitionObject(getNameForObject(descriptor),m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
306 Reference<XColumnsSupplier> xSup(descriptor,UNO_QUERY);
307 Reference<XDataDescriptorFactory> xFac(xColumnDefinitions,UNO_QUERY);
308 Reference<XAppend> xAppend(xColumnDefinitions,UNO_QUERY);
309 bool bModified = false;
310 if ( xSup.is() && xColumnDefinitions.is() && xFac.is() && xAppend.is() )
311 {
312 Reference<XNameAccess> xNames = xSup->getColumns();
313 if ( xNames.is() )
314 {
315 Reference<XPropertySet> xProp = xFac->createDataDescriptor();
316 Sequence< OUString> aSeq = xNames->getElementNames();
317 const OUString* pIter = aSeq.getConstArray();
318 const OUString* pEnd = pIter + aSeq.getLength();
319 for(;pIter != pEnd;++pIter)
320 {
321 if ( !xColumnDefinitions->hasByName(*pIter) )
322 {
323 Reference<XPropertySet> xColumn(xNames->getByName(*pIter),UNO_QUERY);
325 {
326 ::comphelper::copyProperties( xColumn, xProp );
327 xAppend->appendByDescriptor( xProp );
328 bModified = true;
329 }
330 }
331 }
332 }
333 }
334 Sequence< OUString> aNames{
338 if ( bModified || !lcl_isPropertySetDefaulted(aNames,xTableDefinition) )
340
341 return createObject( _rForName );
342}
343
344// XDrop
345void OTableContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
346{
347 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
348 if(xDrop.is())
349 xDrop->dropByName(_sElementName);
350 else
351 {
352 OUString sComposedName;
353
354 bool bIsView = false;
355 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
356 if ( xTable.is() && m_xMetaData.is() )
357 {
358 OUString sSchema,sCatalog,sTable;
359 if (m_xMetaData->supportsCatalogsInTableDefinitions())
360 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
361 if (m_xMetaData->supportsSchemasInTableDefinitions())
362 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
363 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
364
365 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
366
367 OUString sType;
368 xTable->getPropertyValue(PROPERTY_TYPE) >>= sType;
369 bIsView = sType.equalsIgnoreAsciiCase("VIEW");
370 }
371
372 if(sComposedName.isEmpty())
373 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
374
375 OUString aSql("DROP ");
376
377 if ( bIsView ) // here we have a view
378 aSql += "VIEW ";
379 else
380 aSql += "TABLE ";
381 aSql += sComposedName;
382 Reference<XConnection> xCon = m_xConnection;
383 OSL_ENSURE(xCon.is(),"Connection is null!");
384 if ( xCon.is() )
385 {
386 Reference< XStatement > xStmt = xCon->createStatement( );
387 if(xStmt.is())
388 xStmt->execute(aSql);
389 ::comphelper::disposeComponent(xStmt);
390 }
391 }
392
393 if ( m_xTableDefinitions.is() && m_xTableDefinitions->hasByName(_sElementName) )
394 {
395 m_xTableDefinitions->removeByName(_sElementName);
396 }
397}
398
399void SAL_CALL OTableContainer::elementInserted( const ContainerEvent& Event )
400{
401 ::osl::MutexGuard aGuard(m_rMutex);
402 OUString sName;
403 Event.Accessor >>= sName;
404 if ( !m_nInAppend && !hasByName(sName) )
405 {
406 if(!m_xMasterContainer.is() || m_xMasterContainer->hasByName(sName))
407 {
409 insertElement(sName,xName);
410 // and notify our listeners
411 ContainerEvent aEvent(static_cast<XContainer*>(this), Any(sName), Any(xName), Any());
412 m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
413 }
414 }
415}
416
417void SAL_CALL OTableContainer::elementRemoved( const ContainerEvent& /*Event*/ )
418{
419}
420
421void SAL_CALL OTableContainer::elementReplaced( const ContainerEvent& Event )
422{
423 // create a new config entry
424 OUString sOldComposedName,sNewComposedName;
425 Event.ReplacedElement >>= sOldComposedName;
426 Event.Accessor >>= sNewComposedName;
427
428 renameObject(sOldComposedName,sNewComposedName);
429}
430
432{
434 // say goodbye to our listeners
435 m_xTableDefinitions = nullptr;
436 m_pTableMediator = nullptr;
437}
438
439void SAL_CALL OTableContainer::disposing( const css::lang::EventObject& /*Source*/ )
440{
441}
442
444{
445 try
446 {
447 Reference< XContainer > xCont( m_xMasterContainer, UNO_QUERY_THROW );
448 xCont->addContainerListener( this );
449 }
450 catch( const Exception& )
451 {
452 DBG_UNHANDLED_EXCEPTION("dbaccess");
453 }
454}
455
456/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
OptionalString sComposedName
OptionalString sType
OptionalString sName
AnyEventRef aEvent
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
void insertElement(const OUString &_sElementName, const ObjectType &_xElement)
ObjectType getObject(sal_Int32 _nIndex)
void renameObject(const OUString &_sOldName, const OUString &_sNewName)
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
::comphelper::OInterfaceContainerHelper3< css::container::XContainerListener > m_aContainerListeners
static bool hasDefaultSettings(const css::uno::Reference< css::beans::XPropertySet > &_rxColumn)
check if the persistent settings have their default value
css::uno::Reference< css::container::XNameAccess > m_xMasterContainer
void getAllTableTypeFilter(css::uno::Sequence< OUString > &_rFilter) const
retrieve a table type filter to pass to <member scope="css::sdbc">XDatabaseMetaData::getTables</membe...
virtual OUString getNameForObject(const ::connectivity::sdbcx::ObjectType &_xObject) override
std::atomic< std::size_t > & m_nInAppend
virtual void disposing() override
tell the container to free all elements and all additional resources.
css::uno::WeakReference< css::sdbc::XConnection > m_xConnection
css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData
virtual void addMasterContainerListener() override
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
css::uno::Reference< css::container::XNameContainer > m_xTableDefinitions
virtual void disposing() override
tell the container to free all elements and all additional resources.
virtual connectivity::sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
virtual connectivity::sdbcx::ObjectType createObject(const OUString &_rName) override
virtual void removeMasterContainerListener() override
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
::rtl::Reference< OContainerMediator > m_pTableMediator
virtual ~OTableContainer() override
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
virtual OUString getTableTypeRestriction() const override
returns a string denoting the only type of tables allowed in this container, or an empty string if th...
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &Event) override
#define DBA_RES(id)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XColumn > xColumn
OUString aName
Sequence< sal_Int8 > aSeq
@ Exception
OUString getString(const Any &_rAny)
css::uno::Sequence< OUString > getSupportedServiceNames()
css::uno::Reference< css::beans::XPropertySet > ObjectType
void notifyDataSourceModified(const css::uno::Reference< css::uno::XInterface > &_rxObject)
std::shared_ptr< IContainerApprove > PContainerApprove
ObjectType
OUString sMessage
Definition: sqlmessage.cxx:159
constexpr OUStringLiteral PROPERTY_TEXTCOLOR(u"TextColor")
constexpr OUStringLiteral SERVICE_SDBCX_CONTAINER
Definition: strings.hxx:171
constexpr OUStringLiteral PROPERTY_SCHEMANAME(u"SchemaName")
constexpr OUStringLiteral PROPERTY_APPLYFILTER(u"ApplyFilter")
constexpr OUStringLiteral PROPERTY_TEXTEMPHASIS(u"FontEmphasisMark")
constexpr OUStringLiteral PROPERTY_FILTER(u"Filter")
constexpr OUStringLiteral PROPERTY_FONT(u"FontDescriptor")
constexpr OUStringLiteral SQLSTATE_GENERAL
Definition: strings.hxx:267
constexpr OUStringLiteral PROPERTY_CATALOGNAME(u"CatalogName")
constexpr OUStringLiteral PROPERTY_TEXTLINECOLOR(u"TextLineColor")
constexpr OUStringLiteral PROPERTY_TYPE(u"Type")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
constexpr OUStringLiteral PROPERTY_TEXTRELIEF(u"FontRelief")
constexpr OUStringLiteral SERVICE_SDBCX_TABLES
Definition: strings.hxx:193
constexpr OUStringLiteral PROPERTY_ROW_HEIGHT(u"RowHeight")
constexpr OUStringLiteral PROPERTY_ORDER(u"Order")
unsigned char sal_Bool
sal_Int32 _nPos