Update ooo320-m1
[ooovba.git] / connectivity / source / inc / ado / ACollection.hxx
blob98c2a7f83c8df01eadd121b833898d1ca9c11746
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ACollection.hxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _CONNECTIVITY_ADO_COLLECTION_HXX_
32 #define _CONNECTIVITY_ADO_COLLECTION_HXX_
34 #include <cppuhelper/implbase3.hxx>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #include <com/sun/star/container/XIndexAccess.hpp>
37 #include "ado/Awrapadox.hxx"
38 #include "ado/Aolevariant.hxx"
39 #include <com/sun/star/lang/XServiceInfo.hpp>
41 namespace connectivity
43 namespace ado
45 namespace starcontainer = ::com::sun::star::container;
46 namespace starlang = ::com::sun::star::lang;
47 namespace staruno = ::com::sun::star::uno;
48 namespace starbeans = ::com::sun::star::beans;
50 typedef ::cppu::WeakImplHelper3< starcontainer::XNameAccess,
51 starcontainer::XIndexAccess,
52 starlang::XServiceInfo> OCollectionBase;
54 //************************************************************
55 // OCollection
56 //************************************************************
57 template <class T,class SimT,class OCl> class OCollection : public OCollectionBase
59 private:
60 OCollection( const OCollection& ); // never implemented
61 OCollection& operator=( const OCollection& ); // never implemented
63 protected:
64 vector<OCl*> m_aElements;
65 ::cppu::OWeakObject& m_rParent;
66 ::osl::Mutex& m_rMutex; // mutex of the parent
67 T* m_pCollection;
70 public:
71 OCollection(::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,T* _pCollection)
72 : m_rParent(_rParent)
73 ,m_rMutex(_rMutex)
74 ,m_pCollection(_pCollection)
76 m_pCollection->AddRef();
79 ~OCollection()
81 m_pCollection->Release();
84 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (staruno::RuntimeException)
86 return ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.ACollection");
88 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(staruno::RuntimeException)
90 staruno::Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
91 const ::rtl::OUString* pSupported = aSupported.getConstArray();
92 for (sal_Int32 i=0; i<aSupported.getLength(); ++i, ++pSupported)
93 if (pSupported->equals(_rServiceName))
94 return sal_True;
96 return sal_False;
98 virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw(staruno::RuntimeException)
100 staruno::Sequence< ::rtl::OUString > aSupported(1);
101 aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Container");
102 return aSupported;
105 // dispatch the refcounting to the parent
106 virtual void SAL_CALL acquire() throw()
108 m_rParent.acquire();
110 virtual void SAL_CALL release() throw()
112 m_rParent.release();
115 // ::com::sun::star::container::XElementAccess
116 virtual staruno::Type SAL_CALL getElementType( ) throw(staruno::RuntimeException)
118 return::getCppuType(static_cast< staruno::Reference< starbeans::XPropertySet>*>(NULL));
121 virtual sal_Bool SAL_CALL hasElements( ) throw(staruno::RuntimeException)
123 ::osl::MutexGuard aGuard(m_rMutex);
124 return getCount() > 0;
127 // starcontainer::XIndexAccess
128 virtual sal_Int32 SAL_CALL getCount( ) throw(staruno::RuntimeException)
130 ::osl::MutexGuard aGuard(m_rMutex);
131 sal_Int32 nCnt = 0;
132 m_pCollection->get_Count(&nCnt);
133 return nCnt;
136 virtual staruno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw(starlang::IndexOutOfBoundsException, starlang::WrappedTargetException, staruno::RuntimeException)
138 ::osl::MutexGuard aGuard(m_rMutex);
139 if (Index < 0 || Index >= getCount())
140 throw starlang::IndexOutOfBoundsException();
141 SimT* pCol = NULL;
142 m_pCollection->get_Item(OLEVariant(Index),&pCol);
143 if(!pCol)
144 throw starlang::IndexOutOfBoundsException();
146 OCl* pIndex = new OCl(pCol);
148 m_aElements.push_back(pIndex);
150 return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
154 // starcontainer::XNameAccess
155 virtual staruno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
157 ::osl::MutexGuard aGuard(m_rMutex);
159 SimT* pCol = NULL;
160 m_pCollection->get_Item(OLEVariant(aName),&pCol);
161 if(!pCol)
162 throw starlang::IndexOutOfBoundsException();
164 OCl* pIndex = new OCl(pCol);
166 m_aElements.push_back(pIndex);
168 return staruno::makeAny( staruno::Reference< starbeans::XPropertySet >(pIndex));
170 virtual staruno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw(staruno::RuntimeException)
172 ::osl::MutexGuard aGuard(m_rMutex);
173 sal_Int32 nLen = getCount();
174 staruno::Sequence< ::rtl::OUString > aNameList(nLen);
176 ::rtl::OUString* pStringArray = aNameList.getArray();
177 OLEVariant aVar;
178 for (sal_Int32 i=0;i<nLen;++i)
180 aVar.setInt32(i);
181 SimT* pIdx = NULL;
182 m_pCollection->get_Item(aVar,&pIdx);
183 pIdx->AddRef();
184 _bstr_t sBSTR;
185 pIdx->get_Name(&sBSTR);
186 (*pStringArray) = (sal_Unicode*)sBSTR;
187 pIdx->Release();
188 ++pStringArray;
190 return aNameList;
192 virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(staruno::RuntimeException)
194 ::osl::MutexGuard aGuard(m_rMutex);
195 SimT* pCol = NULL;
196 m_pCollection->get_Item(OLEVariant(aName),&pCol);
197 return pCol != NULL;
200 void SAL_CALL disposing()
202 ::osl::MutexGuard aGuard(m_rMutex);
203 for (::std::vector<OCl*>::const_iterator i = m_aElements.begin(); i != m_aElements.end(); ++i)
205 (*i)->disposing();
206 (*i)->release();
208 m_aElements.clear();
213 class OIndex;
214 class OKey;
215 class OColumn;
216 class OTable;
217 class OView;
218 class OGroup;
219 class OUser;
221 typedef OCollection< ADOIndexes,ADOIndex,OIndex> OIndexes;
222 typedef OCollection< ADOKeys,ADOKey,OKey> OKeys;
223 typedef OCollection< ADOColumns,ADOColumn,OColumn> OColumns;
224 typedef OCollection< ADOTables,ADOTable,OTable> OTables;
225 typedef OCollection< ADOViews,ADOView,OView> OViews;
226 typedef OCollection< ADOGroups,ADOGroup,OGroup> OGroups;
227 typedef OCollection< ADOUsers,ADOUser,OUser> OUsers;
231 #endif // _CONNECTIVITY_ADO_COLLECTION_HXX_