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 .
22 #include <com/sun/star/container/ElementExistException.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <connectivity/sdbcx/VCollection.hxx>
25 #include <connectivity/sdbcx/VDescriptor.hxx>
26 #include <com/sun/star/sdbc/SQLException.hpp>
27 #include <connectivity/dbexception.hxx>
28 #include <comphelper/enumhelper.hxx>
29 #include <comphelper/types.hxx>
30 #include <comphelper/property.hxx>
31 #include <cppuhelper/exc_hlp.hxx>
32 #include <TConnection.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <strings.hrc>
35 #include <resource/sharedresources.hxx>
37 using namespace connectivity::sdbcx
;
38 using namespace connectivity
;
39 using namespace comphelper
;
40 using namespace ::cppu
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::sdbc
;
45 using namespace ::com::sun::star::container
;
46 using namespace ::com::sun::star::util
;
50 template < typename T
> class OHardRefMap
: public connectivity::sdbcx::IObjectCollection
52 typedef std::multimap
< OUString
, T
, ::comphelper::UStringMixLess
> ObjectMap
;
53 typedef typename
ObjectMap::iterator ObjectIter
;
54 typedef typename
ObjectMap::value_type ObjectEntry
;
57 // this combination of map and vector is used to have a fast name and index access
58 std::vector
< ObjectIter
> m_aElements
; // hold the iterators which point to map
59 ObjectMap m_aNameMap
; // hold the elements and a name
61 OHardRefMap(bool _bCase
)
66 virtual bool exists(const OUString
& _sName
) override
68 return m_aNameMap
.find(_sName
) != m_aNameMap
.end();
71 virtual bool empty() override
73 return m_aNameMap
.empty();
76 virtual void swapAll() override
78 std::vector
< ObjectIter
>(m_aElements
).swap(m_aElements
);
79 ObjectMap(m_aNameMap
).swap(m_aNameMap
);
82 virtual void swap() override
84 std::vector
< ObjectIter
>().swap(m_aElements
);
86 OSL_ENSURE( m_aNameMap
.empty(), "swap: what did disposeElements do?" );
87 ObjectMap( m_aNameMap
).swap( m_aNameMap
);
88 // Note that it's /important/ to construct the new ObjectMap from m_aNameMap before
89 // swapping. This way, it's ensured that the compare object held by these maps is preserved
90 // during the swap. If we would not do this, the UStringMixLess instance which is used would be
91 // default constructed (instead of being constructed from the same instance in m_aNameMap), and
92 // it's case-sensitive flag would have an unpredictable value.
95 virtual void clear() override
101 virtual void insert(const OUString
& _sName
,const ObjectType
& _xObject
) override
103 m_aElements
.push_back(m_aNameMap
.insert(m_aNameMap
.begin(), ObjectEntry(_sName
,_xObject
)));
106 virtual void reFill(const ::std::vector
< OUString
> &_rVector
) override
108 OSL_ENSURE(m_aNameMap
.empty(),"OCollection::reFill: collection isn't empty");
109 m_aElements
.reserve(_rVector
.size());
111 for (auto const& elem
: _rVector
)
112 m_aElements
.push_back(m_aNameMap
.insert(m_aNameMap
.begin(), ObjectEntry(elem
,ObjectType())));
115 virtual bool rename(const OUString
& _sOldName
, const OUString
& _sNewName
) override
118 ObjectIter aIter
= m_aNameMap
.find(_sOldName
);
119 if ( aIter
!= m_aNameMap
.end() )
121 typename
std::vector
< ObjectIter
>::iterator aFind
= std::find(m_aElements
.begin(),m_aElements
.end(),aIter
);
122 if(m_aElements
.end() != aFind
)
124 (*aFind
) = m_aNameMap
.insert(m_aNameMap
.begin(), ObjectEntry(_sNewName
,(*aFind
)->second
));
125 m_aNameMap
.erase(aIter
);
133 virtual sal_Int32
size() override
135 return static_cast<sal_Int32
>(m_aNameMap
.size());
138 virtual Sequence
< OUString
> getElementNames() override
140 Sequence
< OUString
> aNameList(m_aElements
.size());
142 OUString
* pStringArray
= aNameList
.getArray();
143 for(const auto& rIter
: m_aElements
)
145 *pStringArray
= rIter
->first
;
152 virtual OUString
getName(sal_Int32 _nIndex
) override
154 return m_aElements
[_nIndex
]->first
;
157 virtual void disposeAndErase(sal_Int32 _nIndex
) override
159 OSL_ENSURE(_nIndex
>= 0 && _nIndex
< static_cast<sal_Int32
>(m_aElements
.size()),"Illegal argument!");
160 Reference
<XComponent
> xComp(m_aElements
[_nIndex
]->second
.get(),UNO_QUERY
);
161 ::comphelper::disposeComponent(xComp
);
162 m_aElements
[_nIndex
]->second
= T();
164 OUString sName
= m_aElements
[_nIndex
]->first
;
165 m_aElements
.erase(m_aElements
.begin()+_nIndex
);
166 m_aNameMap
.erase(sName
);
169 virtual void disposeElements() override
171 for (auto & name
: m_aNameMap
)
173 Reference
<XComponent
> xComp(name
.second
.get(),UNO_QUERY
);
176 ::comphelper::disposeComponent(xComp
);
184 virtual sal_Int32
findColumn( const OUString
& columnName
) override
186 ObjectIter aIter
= m_aNameMap
.find(columnName
);
187 OSL_ENSURE(aIter
!= m_aNameMap
.end(),"findColumn:: Illegal name!");
188 return m_aElements
.size() - (m_aElements
.end() - std::find(m_aElements
.begin(),m_aElements
.end(),aIter
));
191 virtual ObjectType
getObject(sal_Int32 _nIndex
) override
193 OSL_ENSURE(_nIndex
>= 0 && _nIndex
< static_cast<sal_Int32
>(m_aElements
.size()),"Illegal argument!");
194 return m_aElements
[_nIndex
]->second
;
197 virtual ObjectType
getObject(const OUString
& columnName
) override
199 return m_aNameMap
.find(columnName
)->second
;
202 virtual void setObject(sal_Int32 _nIndex
,const ObjectType
& _xObject
) override
204 OSL_ENSURE(_nIndex
>= 0 && _nIndex
< static_cast<sal_Int32
>(m_aElements
.size()),"Illegal argument!");
205 m_aElements
[_nIndex
]->second
= _xObject
;
208 bool isCaseSensitive() const override
210 return m_aNameMap
.key_comp().isCaseSensitive();
216 IObjectCollection::~IObjectCollection() {}
218 IMPLEMENT_SERVICE_INFO(OCollection
,"com.sun.star.sdbcx.VContainer" , "com.sun.star.sdbcx.Container")
220 OCollection::OCollection(::cppu::OWeakObject
& _rParent
222 , ::osl::Mutex
& _rMutex
223 , const ::std::vector
< OUString
> &_rVector
224 , bool _bUseIndexOnly
226 :m_aContainerListeners(_rMutex
)
227 ,m_aRefreshListeners(_rMutex
)
230 ,m_bUseIndexOnly(_bUseIndexOnly
)
234 m_pElements
.reset(new OHardRefMap
< ObjectType
>(_bCase
));
238 m_pElements
.reset(new OHardRefMap
< WeakReference
< XPropertySet
> >(_bCase
));
240 m_pElements
->reFill(_rVector
);
243 OCollection::~OCollection()
247 Any SAL_CALL
OCollection::queryInterface( const Type
& rType
)
249 if ( m_bUseIndexOnly
&& rType
== cppu::UnoType
<XNameAccess
>::get() )
253 return OCollectionBase::queryInterface( rType
);
256 Sequence
< Type
> SAL_CALL
OCollection::getTypes()
258 if ( m_bUseIndexOnly
)
260 Sequence
< Type
> aTypes(OCollectionBase::getTypes());
261 Type
* pBegin
= aTypes
.getArray();
262 Type
* pEnd
= pBegin
+ aTypes
.getLength();
264 std::vector
<Type
> aOwnTypes
;
265 aOwnTypes
.reserve(aTypes
.getLength());
266 Type aType
= cppu::UnoType
<XNameAccess
>::get();
267 for(;pBegin
!= pEnd
; ++pBegin
)
269 if ( *pBegin
!= aType
)
270 aOwnTypes
.push_back(*pBegin
);
272 return Sequence
< Type
>(aOwnTypes
.data(), aOwnTypes
.size());
274 return OCollectionBase::getTypes( );
277 void OCollection::clear_NoDispose()
279 ::osl::MutexGuard
aGuard(m_rMutex
);
281 m_pElements
->clear();
282 m_pElements
->swapAll();
286 void OCollection::disposing()
288 m_aContainerListeners
.disposeAndClear(EventObject(static_cast<XTypeProvider
*>(this)));
289 m_aRefreshListeners
.disposeAndClear(EventObject(static_cast<XTypeProvider
*>(this)));
291 ::osl::MutexGuard
aGuard(m_rMutex
);
298 Any SAL_CALL
OCollection::getByIndex( sal_Int32 Index
)
300 ::osl::MutexGuard
aGuard(m_rMutex
);
301 if (Index
< 0 || Index
>= m_pElements
->size() )
302 throw IndexOutOfBoundsException(OUString::number(Index
),static_cast<XTypeProvider
*>(this));
304 return makeAny(getObject(Index
));
307 Any SAL_CALL
OCollection::getByName( const OUString
& aName
)
309 ::osl::MutexGuard
aGuard(m_rMutex
);
311 if ( !m_pElements
->exists(aName
) )
313 ::connectivity::SharedResources aResources
;
314 const OUString
sError( aResources
.getResourceStringWithSubstitution(
318 throw NoSuchElementException( sError
, static_cast< XTypeProvider
* >( this ) );
321 return makeAny(getObject(m_pElements
->findColumn(aName
)));
324 Sequence
< OUString
> SAL_CALL
OCollection::getElementNames( )
326 ::osl::MutexGuard
aGuard(m_rMutex
);
327 return m_pElements
->getElementNames();
330 void SAL_CALL
OCollection::refresh( )
332 ::osl::MutexGuard
aGuard(m_rMutex
);
337 EventObject
aEvt(static_cast<XTypeProvider
*>(this));
338 m_aRefreshListeners
.notifyEach( &XRefreshListener::refreshed
, aEvt
);
341 void OCollection::reFill(const ::std::vector
< OUString
> &_rVector
)
343 m_pElements
->reFill(_rVector
);
346 // XDataDescriptorFactory
347 Reference
< XPropertySet
> SAL_CALL
OCollection::createDataDescriptor( )
349 ::osl::MutexGuard
aGuard(m_rMutex
);
351 return createDescriptor();
354 OUString
OCollection::getNameForObject(const ObjectType
& _xObject
)
356 OSL_ENSURE(_xObject
.is(),"OCollection::getNameForObject: Object is NULL!");
358 _xObject
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)) >>= sName
;
363 void SAL_CALL
OCollection::appendByDescriptor( const Reference
< XPropertySet
>& descriptor
)
365 ::osl::ClearableMutexGuard
aGuard(m_rMutex
);
367 OUString sName
= getNameForObject( descriptor
);
369 if ( m_pElements
->exists(sName
) )
370 throw ElementExistException(sName
,static_cast<XTypeProvider
*>(this));
372 ObjectType xNewlyCreated
= appendObject( sName
, descriptor
);
373 if ( !xNewlyCreated
.is() )
374 throw RuntimeException();
376 ODescriptor
* pDescriptor
= ODescriptor::getImplementation( xNewlyCreated
);
378 pDescriptor
->setNew( false );
380 sName
= getNameForObject( xNewlyCreated
);
381 if ( !m_pElements
->exists( sName
) ) // this may happen when the derived class included it itself
382 m_pElements
->insert( sName
, xNewlyCreated
);
384 // notify our container listeners
385 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(sName
), makeAny(xNewlyCreated
), Any());
387 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvent
);
391 void SAL_CALL
OCollection::dropByName( const OUString
& elementName
)
393 ::osl::MutexGuard
aGuard(m_rMutex
);
395 if ( !m_pElements
->exists(elementName
) )
396 throw NoSuchElementException(elementName
,static_cast<XTypeProvider
*>(this));
398 dropImpl(m_pElements
->findColumn(elementName
));
401 void SAL_CALL
OCollection::dropByIndex( sal_Int32 index
)
403 ::osl::MutexGuard
aGuard(m_rMutex
);
404 if(index
<0 || index
>= getCount())
405 throw IndexOutOfBoundsException(OUString::number(index
),static_cast<XTypeProvider
*>(this));
410 void OCollection::dropImpl(sal_Int32 _nIndex
, bool _bReallyDrop
)
412 OUString elementName
= m_pElements
->getName(_nIndex
);
415 dropObject(_nIndex
,elementName
);
417 m_pElements
->disposeAndErase(_nIndex
);
419 // notify our container listeners
420 notifyElementRemoved(elementName
);
423 void OCollection::notifyElementRemoved(const OUString
& _sName
)
425 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(_sName
), Any(), Any());
426 // note that xExistent may be empty, in case somebody removed the data source while it is not alive at this moment
427 OInterfaceIteratorHelper2
aListenerLoop(m_aContainerListeners
);
428 while (aListenerLoop
.hasMoreElements())
429 static_cast<XContainerListener
*>(aListenerLoop
.next())->elementRemoved(aEvent
);
432 sal_Int32 SAL_CALL
OCollection::findColumn( const OUString
& columnName
)
434 if ( !m_pElements
->exists(columnName
) )
436 ::dbtools::throwInvalidColumnException( columnName
, static_cast< XIndexAccess
*>(this) );
437 #if !(defined(_MSC_VER) && defined(ENABLE_LTO))
442 return m_pElements
->findColumn(columnName
) + 1; // because columns start at one
445 Reference
< XEnumeration
> SAL_CALL
OCollection::createEnumeration( )
447 ::osl::MutexGuard
aGuard(m_rMutex
);
448 return new OEnumerationByIndex( static_cast< XIndexAccess
*>(this));
451 void SAL_CALL
OCollection::addContainerListener( const Reference
< XContainerListener
>& _rxListener
)
453 m_aContainerListeners
.addInterface(_rxListener
);
457 void SAL_CALL
OCollection::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
)
459 m_aContainerListeners
.removeInterface(_rxListener
);
462 void SAL_CALL
OCollection::acquire() throw()
467 void SAL_CALL
OCollection::release() throw()
472 Type SAL_CALL
OCollection::getElementType( )
474 return cppu::UnoType
<XPropertySet
>::get();
477 sal_Bool SAL_CALL
OCollection::hasElements( )
479 ::osl::MutexGuard
aGuard(m_rMutex
);
480 return !m_pElements
->empty();
483 sal_Int32 SAL_CALL
OCollection::getCount( )
485 ::osl::MutexGuard
aGuard(m_rMutex
);
486 return m_pElements
->size();
489 sal_Bool SAL_CALL
OCollection::hasByName( const OUString
& aName
)
491 ::osl::MutexGuard
aGuard(m_rMutex
);
492 return m_pElements
->exists(aName
);
495 void SAL_CALL
OCollection::addRefreshListener( const Reference
< XRefreshListener
>& l
)
497 m_aRefreshListeners
.addInterface(l
);
500 void SAL_CALL
OCollection::removeRefreshListener( const Reference
< XRefreshListener
>& l
)
502 m_aRefreshListeners
.removeInterface(l
);
505 void OCollection::insertElement(const OUString
& _sElementName
,const ObjectType
& _xElement
)
507 OSL_ENSURE(!m_pElements
->exists(_sElementName
),"Element already exists");
508 if ( !m_pElements
->exists(_sElementName
) )
509 m_pElements
->insert(_sElementName
,_xElement
);
512 void OCollection::renameObject(const OUString
& _sOldName
, const OUString
& _sNewName
)
514 OSL_ENSURE(m_pElements
->exists(_sOldName
),"Element doesn't exist");
515 OSL_ENSURE(!m_pElements
->exists(_sNewName
),"Element already exists");
516 OSL_ENSURE(!_sNewName
.isEmpty(),"New name must not be empty!");
517 OSL_ENSURE(!_sOldName
.isEmpty(),"Old name must not be empty!");
519 if ( m_pElements
->rename(_sOldName
,_sNewName
) )
521 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(_sNewName
), makeAny(m_pElements
->getObject(_sNewName
)),makeAny(_sOldName
));
522 // note that xExistent may be empty, in case somebody removed the data source while it is not alive at this moment
523 OInterfaceIteratorHelper2
aListenerLoop(m_aContainerListeners
);
524 while (aListenerLoop
.hasMoreElements())
525 static_cast<XContainerListener
*>(aListenerLoop
.next())->elementReplaced(aEvent
);
529 ObjectType
OCollection::getObject(sal_Int32 _nIndex
)
531 ObjectType xName
= m_pElements
->getObject(_nIndex
);
536 xName
= createObject(m_pElements
->getName(_nIndex
));
538 catch(const SQLException
& e
)
540 css::uno::Any anyEx
= cppu::getCaughtException();
543 dropImpl(_nIndex
,false);
545 catch(const Exception
& )
548 throw WrappedTargetException(e
.Message
,static_cast<XTypeProvider
*>(this),anyEx
);
550 m_pElements
->setObject(_nIndex
,xName
);
555 void OCollection::disposeElements()
557 m_pElements
->disposeElements();
560 Reference
< XPropertySet
> OCollection::createDescriptor()
562 OSL_FAIL("createDescriptor() needs to be overridden when used!");
563 throw SQLException();
566 ObjectType
OCollection::cloneDescriptor( const ObjectType
& _descriptor
)
568 ObjectType
xNewDescriptor( createDescriptor() );
569 ::comphelper::copyProperties( _descriptor
, xNewDescriptor
);
570 return xNewDescriptor
;
573 ObjectType
OCollection::appendObject( const OUString
& /*_rForName*/, const Reference
< XPropertySet
>& descriptor
)
575 return cloneDescriptor( descriptor
);
578 void OCollection::dropObject(sal_Int32
/*_nPos*/, const OUString
& /*_sElementName*/)
583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */