1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <tablecontainer.hxx>
22 #include <comphelper/property.hxx>
23 #include <comphelper/processfactory.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>
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 <comphelper/diagnose_ex.hxx>
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
;
62 bool lcl_isPropertySetDefaulted(const Sequence
< OUString
>& _aNames
,const Reference
<XPropertySet
>& _xProp
)
64 Reference
<XPropertyState
> xState(_xProp
,UNO_QUERY
);
67 const OUString
* pIter
= _aNames
.getConstArray();
68 const OUString
* pEnd
= pIter
+ _aNames
.getLength();
69 for(;pIter
!= pEnd
;++pIter
)
73 PropertyState aState
= xState
->getPropertyState(*pIter
);
74 if ( aState
!= PropertyState_DEFAULT_VALUE
)
77 catch(const Exception
&)
79 TOOLS_WARN_EXCEPTION("dbaccess", "" );
82 return ( pIter
== pEnd
);
88 OTableContainer::OTableContainer(::cppu::OWeakObject
& _rParent
,
89 ::osl::Mutex
& _rMutex
,
90 const Reference
< XConnection
>& _xCon
,
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
)
100 OTableContainer::~OTableContainer()
104 void OTableContainer::removeMasterContainerListener()
108 Reference
<XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
109 xCont
->removeContainerListener( this );
111 catch( const Exception
& )
113 DBG_UNHANDLED_EXCEPTION("dbaccess");
117 OUString
OTableContainer::getTableTypeRestriction() const
119 // no restriction at all (other than the ones provided externally)
124 OUString SAL_CALL
OTableContainer::getImplementationName()
126 return "com.sun.star.sdb.dbaccess.OTableContainer";
128 sal_Bool SAL_CALL
OTableContainer::supportsService(const OUString
& _rServiceName
)
130 const css::uno::Sequence
< OUString
> aSupported(getSupportedServiceNames());
131 for (const OUString
& s
: aSupported
)
132 if (s
== _rServiceName
)
137 css::uno::Sequence
< OUString
> SAL_CALL
OTableContainer::getSupportedServiceNames()
139 return { SERVICE_SDBCX_CONTAINER
, SERVICE_SDBCX_TABLES
};
145 void lcl_createDefinitionObject(const OUString
& _rName
146 ,const Reference
< XNameContainer
>& _xTableDefinitions
147 ,Reference
<XPropertySet
>& _xTableDefinition
148 ,Reference
<XNameAccess
>& _xColumnDefinitions
)
150 if ( !_xTableDefinitions
.is() )
153 if ( _xTableDefinitions
->hasByName(_rName
) )
154 _xTableDefinition
.set(_xTableDefinitions
->getByName(_rName
),UNO_QUERY
);
158 _xTableDefinition
= TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName
);
159 _xTableDefinitions
->insertByName(_rName
,Any(_xTableDefinition
));
161 Reference
<XColumnsSupplier
> xColumnsSupplier(_xTableDefinition
,UNO_QUERY
);
162 if ( xColumnsSupplier
.is() )
163 _xColumnDefinitions
= xColumnsSupplier
->getColumns();
168 connectivity::sdbcx::ObjectType
OTableContainer::createObject(const OUString
& _rName
)
170 Reference
<XColumnsSupplier
> xSup
;
171 if(m_xMasterContainer
.is() && m_xMasterContainer
->hasByName(_rName
))
172 xSup
.set(m_xMasterContainer
->getByName(_rName
),UNO_QUERY
);
174 connectivity::sdbcx::ObjectType xRet
;
175 if ( m_xMetaData
.is() )
177 Reference
<XPropertySet
> xTableDefinition
;
178 Reference
<XNameAccess
> xColumnDefinitions
;
179 lcl_createDefinitionObject(_rName
,m_xTableDefinitions
,xTableDefinition
,xColumnDefinitions
);
183 rtl::Reference
<ODBTableDecorator
> pTable
= new ODBTableDecorator( m_xConnection
, xSup
, ::dbtools::getNumberFormats( m_xConnection
) ,xColumnDefinitions
);
189 OUString sCatalog
,sSchema
,sTable
;
190 ::dbtools::qualifiedNameComponents(m_xMetaData
,
195 ::dbtools::EComposeRule::InDataManipulation
);
197 if(!sCatalog
.isEmpty())
198 aCatalog
<<= sCatalog
;
199 OUString sType
,sDescription
;
200 Sequence
< OUString
> aTypeFilter
;
201 getAllTableTypeFilter( aTypeFilter
);
203 Reference
< XResultSet
> xRes
;
204 if ( m_xMetaData
.is() )
205 xRes
= m_xMetaData
->getTables(aCatalog
,sSchema
,sTable
,aTypeFilter
);
206 if(xRes
.is() && xRes
->next())
208 Reference
< XRow
> xRow(xRes
,UNO_QUERY
);
211 sType
= xRow
->getString(4);
212 sDescription
= xRow
->getString(5);
215 ::comphelper::disposeComponent(xRes
);
216 rtl::Reference
<ODBTable
> pTable
= new ODBTable(this
223 ,xColumnDefinitions
);
227 Reference
<XPropertySet
> xDest(xRet
,UNO_QUERY
);
228 if ( xTableDefinition
.is() )
229 ::comphelper::copyProperties(xTableDefinition
,xDest
);
231 if ( !m_pTableMediator
.is() )
232 m_pTableMediator
= new OContainerMediator(
233 this, m_xTableDefinitions
);
234 if ( m_pTableMediator
.is() )
235 m_pTableMediator
->notifyElementCreated(_rName
,xDest
);
241 Reference
< XPropertySet
> OTableContainer::createDescriptor()
243 Reference
< XPropertySet
> xRet
;
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() )
251 xMasterColumnsSup
.set( xDataFactory
->createDataDescriptor(), UNO_QUERY
);
252 rtl::Reference
<ODBTableDecorator
> pTable
= new ODBTableDecorator( m_xConnection
, xMasterColumnsSup
, ::dbtools::getNumberFormats( m_xConnection
) ,nullptr);
258 rtl::Reference
<ODBTable
> pTable
= new ODBTable(this, m_xConnection
);
266 ObjectType
OTableContainer::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
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
))
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());
276 Reference
< XConnection
> xConnection( m_xConnection
.get(), UNO_QUERY
);
277 PContainerApprove pApprove
= std::make_shared
<ObjectNameApproval
>( xConnection
, ObjectNameApproval::TypeTable
);
278 pApprove
->approveElement( aName
);
281 EnsureReset
aReset(m_nInAppend
);
282 Reference
<XAppend
> xAppend(m_xMasterContainer
,UNO_QUERY
);
285 xAppend
->appendByDescriptor(descriptor
);
289 OUString aSql
= ::dbtools::createSqlCreateTableStatement(descriptor
,m_xConnection
);
291 Reference
<XConnection
> xCon
= m_xConnection
;
292 OSL_ENSURE(xCon
.is(),"Connection is null!");
295 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
297 xStmt
->execute(aSql
);
298 ::comphelper::disposeComponent(xStmt
);
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() )
312 Reference
<XNameAccess
> xNames
= xSup
->getColumns();
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
)
321 if ( !xColumnDefinitions
->hasByName(*pIter
) )
323 Reference
<XPropertySet
> xColumn(xNames
->getByName(*pIter
),UNO_QUERY
);
324 if ( !OColumnSettings::hasDefaultSettings( xColumn
) )
326 ::comphelper::copyProperties( xColumn
, xProp
);
327 xAppend
->appendByDescriptor( xProp
);
334 Sequence
< OUString
> aNames
{
335 PROPERTY_FILTER
, PROPERTY_ORDER
, PROPERTY_APPLYFILTER
, PROPERTY_FONT
,
336 PROPERTY_ROW_HEIGHT
, PROPERTY_TEXTCOLOR
, PROPERTY_TEXTLINECOLOR
,
337 PROPERTY_TEXTEMPHASIS
, PROPERTY_TEXTRELIEF
};
338 if ( bModified
|| !lcl_isPropertySetDefaulted(aNames
,xTableDefinition
) )
339 ::dbaccess::notifyDataSourceModified(m_xTableDefinitions
);
341 return createObject( _rForName
);
345 void OTableContainer::dropObject(sal_Int32 _nPos
, const OUString
& _sElementName
)
347 Reference
< XDrop
> xDrop(m_xMasterContainer
,UNO_QUERY
);
349 xDrop
->dropByName(_sElementName
);
352 OUString sComposedName
;
354 bool bIsView
= false;
355 Reference
<XPropertySet
> xTable(getObject(_nPos
),UNO_QUERY
);
356 if ( xTable
.is() && m_xMetaData
.is() )
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
;
365 sComposedName
= ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
368 xTable
->getPropertyValue(PROPERTY_TYPE
) >>= sType
;
369 bIsView
= sType
.equalsIgnoreAsciiCase("VIEW");
372 if(sComposedName
.isEmpty())
373 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(static_cast<OFilteredContainer
*>(this)));
375 OUString
aSql("DROP ");
377 if ( bIsView
) // here we have a view
381 aSql
+= sComposedName
;
382 Reference
<XConnection
> xCon
= m_xConnection
;
383 OSL_ENSURE(xCon
.is(),"Connection is null!");
386 Reference
< XStatement
> xStmt
= xCon
->createStatement( );
388 xStmt
->execute(aSql
);
389 ::comphelper::disposeComponent(xStmt
);
393 if ( m_xTableDefinitions
.is() && m_xTableDefinitions
->hasByName(_sElementName
) )
395 m_xTableDefinitions
->removeByName(_sElementName
);
399 void SAL_CALL
OTableContainer::elementInserted( const ContainerEvent
& Event
)
401 ::osl::MutexGuard
aGuard(m_rMutex
);
403 Event
.Accessor
>>= sName
;
404 if ( !m_nInAppend
&& !hasByName(sName
) )
406 if(!m_xMasterContainer
.is() || m_xMasterContainer
->hasByName(sName
))
408 ObjectType xName
= createObject(sName
);
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
);
417 void SAL_CALL
OTableContainer::elementRemoved( const ContainerEvent
& /*Event*/ )
421 void SAL_CALL
OTableContainer::elementReplaced( const ContainerEvent
& Event
)
423 // create a new config entry
424 OUString sOldComposedName
,sNewComposedName
;
425 Event
.ReplacedElement
>>= sOldComposedName
;
426 Event
.Accessor
>>= sNewComposedName
;
428 renameObject(sOldComposedName
,sNewComposedName
);
431 void OTableContainer::disposing()
433 OFilteredContainer::disposing();
434 // say goodbye to our listeners
435 m_xTableDefinitions
= nullptr;
436 m_pTableMediator
= nullptr;
439 void SAL_CALL
OTableContainer::disposing( const css::lang::EventObject
& /*Source*/ )
443 void OTableContainer::addMasterContainerListener()
447 Reference
< XContainer
> xCont( m_xMasterContainer
, UNO_QUERY_THROW
);
448 xCont
->addContainerListener( this );
450 catch( const Exception
& )
452 DBG_UNHANDLED_EXCEPTION("dbaccess");
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */