LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / dbaccess / source / core / api / tablecontainer.cxx
blob7aefea3e2306a682561a7ee334f486e6469a25d2
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 .
20 #include <apitools.hxx>
21 #include <tablecontainer.hxx>
22 #include <table.hxx>
23 #include <comphelper/property.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <core_resource.hxx>
26 #include <strings.hrc>
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>
37 #include <connectivity/dbtools.hxx>
38 #include <connectivity/dbexception.hxx>
39 #include <TableDeco.hxx>
40 #include <sdbcoretools.hxx>
41 #include <ContainerMediator.hxx>
42 #include <objectnameapproval.hxx>
43 #include <tools/diagnose_ex.h>
45 using namespace dbaccess;
46 using namespace dbtools;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdbc;
51 using namespace ::com::sun::star::sdb;
52 using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::util;
55 using namespace ::osl;
56 using namespace ::comphelper;
57 using namespace ::cppu;
58 using namespace ::connectivity::sdbcx;
60 namespace
62 bool lcl_isPropertySetDefaulted(const Sequence< OUString>& _aNames,const Reference<XPropertySet>& _xProp)
64 Reference<XPropertyState> xState(_xProp,UNO_QUERY);
65 if ( xState.is() )
67 const OUString* pIter = _aNames.getConstArray();
68 const OUString* pEnd = pIter + _aNames.getLength();
69 for(;pIter != pEnd;++pIter)
71 try
73 PropertyState aState = xState->getPropertyState(*pIter);
74 if ( aState != PropertyState_DEFAULT_VALUE )
75 break;
77 catch(const Exception&)
79 TOOLS_WARN_EXCEPTION("dbaccess", "" );
82 return ( pIter == pEnd );
84 return false;
88 // OTableContainer
90 OTableContainer::OTableContainer(::cppu::OWeakObject& _rParent,
91 ::osl::Mutex& _rMutex,
92 const Reference< XConnection >& _xCon,
93 bool _bCase,
94 const Reference< XNameContainer >& _xTableDefinitions,
95 IRefreshListener* _pRefreshListener,
96 std::atomic<std::size_t>& _nInAppend)
97 :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_nInAppend)
98 ,m_xTableDefinitions(_xTableDefinitions)
102 OTableContainer::~OTableContainer()
106 void OTableContainer::removeMasterContainerListener()
110 Reference<XContainer> xCont( m_xMasterContainer, UNO_QUERY_THROW );
111 xCont->removeContainerListener( this );
113 catch( const Exception& )
115 DBG_UNHANDLED_EXCEPTION("dbaccess");
119 OUString OTableContainer::getTableTypeRestriction() const
121 // no restriction at all (other than the ones provided externally)
122 return OUString();
125 // XServiceInfo
126 OUString SAL_CALL OTableContainer::getImplementationName()
128 return "com.sun.star.sdb.dbaccess.OTableContainer";
130 sal_Bool SAL_CALL OTableContainer::supportsService(const OUString& _rServiceName)
132 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
133 for (const OUString& s : aSupported)
134 if (s == _rServiceName)
135 return true;
137 return false;
139 css::uno::Sequence< OUString > SAL_CALL OTableContainer::getSupportedServiceNames()
141 return { SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES };
145 namespace
147 void lcl_createDefinitionObject(const OUString& _rName
148 ,const Reference< XNameContainer >& _xTableDefinitions
149 ,Reference<XPropertySet>& _xTableDefinition
150 ,Reference<XNameAccess>& _xColumnDefinitions)
152 if ( !_xTableDefinitions.is() )
153 return;
155 if ( _xTableDefinitions->hasByName(_rName) )
156 _xTableDefinition.set(_xTableDefinitions->getByName(_rName),UNO_QUERY);
157 else
159 // set as folder
160 _xTableDefinition = TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName );
161 _xTableDefinitions->insertByName(_rName,makeAny(_xTableDefinition));
163 Reference<XColumnsSupplier> xColumnsSupplier(_xTableDefinition,UNO_QUERY);
164 if ( xColumnsSupplier.is() )
165 _xColumnDefinitions = xColumnsSupplier->getColumns();
170 connectivity::sdbcx::ObjectType OTableContainer::createObject(const OUString& _rName)
172 Reference<XColumnsSupplier > xSup;
173 if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName))
174 xSup.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
176 connectivity::sdbcx::ObjectType xRet;
177 if ( m_xMetaData.is() )
179 Reference<XPropertySet> xTableDefinition;
180 Reference<XNameAccess> xColumnDefinitions;
181 lcl_createDefinitionObject(_rName,m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
183 if ( xSup.is() )
185 rtl::Reference<ODBTableDecorator> pTable = new ODBTableDecorator( m_xConnection, xSup, ::dbtools::getNumberFormats( m_xConnection ) ,xColumnDefinitions);
186 xRet = pTable;
187 pTable->construct();
189 else
191 OUString sCatalog,sSchema,sTable;
192 ::dbtools::qualifiedNameComponents(m_xMetaData,
193 _rName,
194 sCatalog,
195 sSchema,
196 sTable,
197 ::dbtools::EComposeRule::InDataManipulation);
198 Any aCatalog;
199 if(!sCatalog.isEmpty())
200 aCatalog <<= sCatalog;
201 OUString sType,sDescription;
202 Sequence< OUString> aTypeFilter;
203 getAllTableTypeFilter( aTypeFilter );
205 Reference< XResultSet > xRes;
206 if ( m_xMetaData.is() )
207 xRes = m_xMetaData->getTables(aCatalog,sSchema,sTable,aTypeFilter);
208 if(xRes.is() && xRes->next())
210 Reference< XRow > xRow(xRes,UNO_QUERY);
211 if(xRow.is())
213 sType = xRow->getString(4);
214 sDescription = xRow->getString(5);
217 ::comphelper::disposeComponent(xRes);
218 rtl::Reference<ODBTable> pTable = new ODBTable(this
219 ,m_xConnection
220 ,sCatalog
221 ,sSchema
222 ,sTable
223 ,sType
224 ,sDescription
225 ,xColumnDefinitions);
226 xRet = pTable;
227 pTable->construct();
229 Reference<XPropertySet> xDest(xRet,UNO_QUERY);
230 if ( xTableDefinition.is() )
231 ::comphelper::copyProperties(xTableDefinition,xDest);
233 if ( !m_pTableMediator.is() )
234 m_pTableMediator = new OContainerMediator(
235 this, m_xTableDefinitions );
236 if ( m_pTableMediator.is() )
237 m_pTableMediator->notifyElementCreated(_rName,xDest);
240 return xRet;
243 Reference< XPropertySet > OTableContainer::createDescriptor()
245 Reference< XPropertySet > xRet;
247 // first we have to look if the master tables support this
248 // and if so then create a table object as well with the master tables
249 Reference<XColumnsSupplier > xMasterColumnsSup;
250 Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
251 if ( xDataFactory.is() && m_xMetaData.is() )
253 xMasterColumnsSup.set( xDataFactory->createDataDescriptor(), UNO_QUERY );
254 rtl::Reference<ODBTableDecorator> pTable = new ODBTableDecorator( m_xConnection, xMasterColumnsSup, ::dbtools::getNumberFormats( m_xConnection ) ,nullptr);
255 xRet = pTable;
256 pTable->construct();
258 else
260 rtl::Reference<ODBTable> pTable = new ODBTable(this, m_xConnection);
261 xRet = pTable;
262 pTable->construct();
264 return xRet;
267 // XAppend
268 ObjectType OTableContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
270 // append the new table with a create stmt
271 OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
272 if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(aName))
274 OUString sMessage(DBA_RES(RID_STR_TABLE_IS_FILTERED));
275 throw SQLException(sMessage.replaceAll("$name$", aName),static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)),SQLSTATE_GENERAL,1000,Any());
278 Reference< XConnection > xConnection( m_xConnection.get(), UNO_QUERY );
279 PContainerApprove pApprove = std::make_shared<ObjectNameApproval>( xConnection, ObjectNameApproval::TypeTable );
280 pApprove->approveElement( aName );
283 EnsureReset aReset(m_nInAppend);
284 Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
285 if(xAppend.is())
287 xAppend->appendByDescriptor(descriptor);
289 else
291 OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,m_xConnection);
293 Reference<XConnection> xCon = m_xConnection;
294 OSL_ENSURE(xCon.is(),"Connection is null!");
295 if ( xCon.is() )
297 Reference< XStatement > xStmt = xCon->createStatement( );
298 if ( xStmt.is() )
299 xStmt->execute(aSql);
300 ::comphelper::disposeComponent(xStmt);
305 Reference<XPropertySet> xTableDefinition;
306 Reference<XNameAccess> xColumnDefinitions;
307 lcl_createDefinitionObject(getNameForObject(descriptor),m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
308 Reference<XColumnsSupplier> xSup(descriptor,UNO_QUERY);
309 Reference<XDataDescriptorFactory> xFac(xColumnDefinitions,UNO_QUERY);
310 Reference<XAppend> xAppend(xColumnDefinitions,UNO_QUERY);
311 bool bModified = false;
312 if ( xSup.is() && xColumnDefinitions.is() && xFac.is() && xAppend.is() )
314 Reference<XNameAccess> xNames = xSup->getColumns();
315 if ( xNames.is() )
317 Reference<XPropertySet> xProp = xFac->createDataDescriptor();
318 Sequence< OUString> aSeq = xNames->getElementNames();
319 const OUString* pIter = aSeq.getConstArray();
320 const OUString* pEnd = pIter + aSeq.getLength();
321 for(;pIter != pEnd;++pIter)
323 if ( !xColumnDefinitions->hasByName(*pIter) )
325 Reference<XPropertySet> xColumn(xNames->getByName(*pIter),UNO_QUERY);
326 if ( !OColumnSettings::hasDefaultSettings( xColumn ) )
328 ::comphelper::copyProperties( xColumn, xProp );
329 xAppend->appendByDescriptor( xProp );
330 bModified = true;
336 Sequence< OUString> aNames{
337 PROPERTY_FILTER, PROPERTY_ORDER, PROPERTY_APPLYFILTER, PROPERTY_FONT,
338 PROPERTY_ROW_HEIGHT, PROPERTY_TEXTCOLOR, PROPERTY_TEXTLINECOLOR,
339 PROPERTY_TEXTEMPHASIS, PROPERTY_TEXTRELIEF};
340 if ( bModified || !lcl_isPropertySetDefaulted(aNames,xTableDefinition) )
341 ::dbaccess::notifyDataSourceModified(m_xTableDefinitions);
343 return createObject( _rForName );
346 // XDrop
347 void OTableContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
349 Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
350 if(xDrop.is())
351 xDrop->dropByName(_sElementName);
352 else
354 OUString sComposedName;
356 bool bIsView = false;
357 Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
358 if ( xTable.is() && m_xMetaData.is() )
360 OUString sSchema,sCatalog,sTable;
361 if (m_xMetaData->supportsCatalogsInTableDefinitions())
362 xTable->getPropertyValue(PROPERTY_CATALOGNAME) >>= sCatalog;
363 if (m_xMetaData->supportsSchemasInTableDefinitions())
364 xTable->getPropertyValue(PROPERTY_SCHEMANAME) >>= sSchema;
365 xTable->getPropertyValue(PROPERTY_NAME) >>= sTable;
367 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InTableDefinitions );
369 OUString sType;
370 xTable->getPropertyValue(PROPERTY_TYPE) >>= sType;
371 bIsView = sType.equalsIgnoreAsciiCase("VIEW");
374 if(sComposedName.isEmpty())
375 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
377 OUString aSql("DROP ");
379 if ( bIsView ) // here we have a view
380 aSql += "VIEW ";
381 else
382 aSql += "TABLE ";
383 aSql += sComposedName;
384 Reference<XConnection> xCon = m_xConnection;
385 OSL_ENSURE(xCon.is(),"Connection is null!");
386 if ( xCon.is() )
388 Reference< XStatement > xStmt = xCon->createStatement( );
389 if(xStmt.is())
390 xStmt->execute(aSql);
391 ::comphelper::disposeComponent(xStmt);
395 if ( m_xTableDefinitions.is() && m_xTableDefinitions->hasByName(_sElementName) )
397 m_xTableDefinitions->removeByName(_sElementName);
401 void SAL_CALL OTableContainer::elementInserted( const ContainerEvent& Event )
403 ::osl::MutexGuard aGuard(m_rMutex);
404 OUString sName;
405 Event.Accessor >>= sName;
406 if ( !m_nInAppend && !hasByName(sName) )
408 if(!m_xMasterContainer.is() || m_xMasterContainer->hasByName(sName))
410 ObjectType xName = createObject(sName);
411 insertElement(sName,xName);
412 // and notify our listeners
413 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(sName), makeAny(xName), Any());
414 m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
419 void SAL_CALL OTableContainer::elementRemoved( const ContainerEvent& /*Event*/ )
423 void SAL_CALL OTableContainer::elementReplaced( const ContainerEvent& Event )
425 // create a new config entry
426 OUString sOldComposedName,sNewComposedName;
427 Event.ReplacedElement >>= sOldComposedName;
428 Event.Accessor >>= sNewComposedName;
430 renameObject(sOldComposedName,sNewComposedName);
433 void OTableContainer::disposing()
435 OFilteredContainer::disposing();
436 // say goodbye to our listeners
437 m_xTableDefinitions = nullptr;
438 m_pTableMediator = nullptr;
441 void SAL_CALL OTableContainer::disposing( const css::lang::EventObject& /*Source*/ )
445 void OTableContainer::addMasterContainerListener()
449 Reference< XContainer > xCont( m_xMasterContainer, UNO_QUERY_THROW );
450 xCont->addContainerListener( this );
452 catch( const Exception& )
454 DBG_UNHANDLED_EXCEPTION("dbaccess");
458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */