xref: /trunk/main/ucb/source/sorter/sortresult.hxx (revision 6df1ea1f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SORTRESULT_HXX
25 #define _SORTRESULT_HXX
26 
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XTypeProvider.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/sdbc/XCloseable.hpp>
33 #include <com/sun/star/sdbc/XResultSet.hpp>
34 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
35 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/ucb/XContentAccess.hpp>
38 #include <com/sun/star/ucb/NumberedSortingInfo.hpp>
39 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
40 #include <com/sun/star/ucb/ListAction.hpp>
41 #include <cppuhelper/weak.hxx>
42 #include <osl/mutex.hxx>
43 
44 #include <deque>
45 #include <ucbhelper/macros.hxx>
46 
47 namespace cppu {
48 	class OInterfaceContainerHelper;
49 }
50 
51 //-----------------------------------------------------------------------------
52 struct	SortInfo;
53 struct	SortListData;
54 class	SRSPropertySetInfo;
55 class	PropertyChangeListeners_Impl;
56 
57 //-----------------------------------------------------------------------------
58 class SortedEntryList
59 {
60 	std::deque < SortListData* > maData;
61 
62 public:
SortedEntryList()63 						 SortedEntryList(){}
~SortedEntryList()64 						~SortedEntryList(){ Clear(); }
65 
Count() const66 	sal_uInt32			Count() const { return (sal_uInt32) maData.size(); }
67 
68 	void				Clear();
69 	void				Insert( SortListData *pEntry, long nPos );
70 	SortListData*		Remove( long nPos );
71 	SortListData*		GetData( long nPos );
72 
73 	long				operator [] ( long nPos ) const;
74 };
75 
76 //-----------------------------------------------------------------------------
77 #define LISTACTION	com::sun::star::ucb::ListAction
78 
79 class EventList
80 {
81 	std::deque < LISTACTION* > maData;
82 
83 public:
EventList()84 					 EventList(){}
~EventList()85 					~EventList(){ Clear(); }
86 
Count()87 	sal_uInt32		Count() { return (sal_uInt32) maData.size(); }
88 
89 	void			AddEvent( long nType, long nPos, long nCount );
Insert(LISTACTION * pAction)90 	void			Insert( LISTACTION *pAction ) { maData.push_back( pAction ); }
91 	void			Clear();
GetAction(long nIndex)92 	LISTACTION*		GetAction( long nIndex ) { return maData[ nIndex ]; }
93 };
94 
95 //-----------------------------------------------------------------------------
96 
97 class SimpleList
98 {
99 	std::deque < void* > maData;
100 
101 public:
SimpleList()102 					 SimpleList(){}
~SimpleList()103 					~SimpleList(){ Clear(); }
104 
Count()105 	sal_uInt32		Count() { return (sal_uInt32) maData.size(); }
Clear()106 	void			Clear() { maData.clear(); }
107 
108 	void			Remove( sal_uInt32 nPos );
109 	void			Remove( void* pData );
110 
Append(void * pData)111 	void			Append( void* pData )
112 						{ maData.push_back( pData ); }
113 	void			Insert( void* pData, sal_uInt32 nPos );
114 	void*			GetObject( sal_uInt32 nPos ) const;
115 	void			Replace( void* pData, sal_uInt32 nPos );
116 };
117 
118 //-----------------------------------------------------------------------------
119 
120 #define PROPERTYCHANGEEVENT		com::sun::star::beans::PropertyChangeEvent
121 #define RUNTIME_EXCEPTION		com::sun::star::uno::RuntimeException
122 #define REFERENCE				com::sun::star::uno::Reference
123 #define SEQUENCE				com::sun::star::uno::Sequence
124 #define XEVENTLISTENER			com::sun::star::lang::XEventListener
125 #define XRESULTSET				com::sun::star::sdbc::XResultSet
126 #define SQLEXCEPTION			com::sun::star::sdbc::SQLException
127 #define XRESULTSETMETADATA		com::sun::star::sdbc::XResultSetMetaData
128 #define NUMBERED_SORTINGINFO	com::sun::star::ucb::NumberedSortingInfo
129 #define XANYCOMPAREFACTORY		com::sun::star::ucb::XAnyCompareFactory
130 
131 #define RESULTSET_SERVICE_NAME 	"com.sun.star.ucb.SortedResultSet"
132 
133 //-----------------------------------------------------------------------------
134 
135 class SortedResultSet:
136 				public cppu::OWeakObject,
137 				public com::sun::star::lang::XTypeProvider,
138 				public com::sun::star::lang::XServiceInfo,
139 				public com::sun::star::lang::XComponent,
140 				public com::sun::star::ucb::XContentAccess,
141 				public XRESULTSET,
142 				public com::sun::star::sdbc::XRow,
143 				public com::sun::star::sdbc::XCloseable,
144 				public com::sun::star::sdbc::XResultSetMetaDataSupplier,
145 				public com::sun::star::beans::XPropertySet
146 {
147 	cppu::OInterfaceContainerHelper	*mpDisposeEventListeners;
148 	PropertyChangeListeners_Impl	*mpPropChangeListeners;
149 	PropertyChangeListeners_Impl	*mpVetoChangeListeners;
150 
151 	REFERENCE < XRESULTSET >			mxOriginal;
152 	REFERENCE < XRESULTSET >			mxOther;
153 
154 	SRSPropertySetInfo*	mpPropSetInfo;
155 	SortInfo*			mpSortInfo;
156 	osl::Mutex			maMutex;
157 	SortedEntryList		maS2O;			// maps the sorted entries to the original ones
158 	SimpleList			maO2S;			// maps the original Entries to the sorted ones
159 	SimpleList			maModList;		// keeps track of modified entries
160 	long				mnLastSort;		// index of the last sorted entry;
161 	long				mnCurEntry;		// index of the current entry
162 	long				mnCount;		// total count of the elements
163 	sal_Bool			mbIsCopy;
164 
165 
166 private:
167 
168 	long				FindPos( SortListData *pEntry, long nStart, long nEnd )
169 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
170 	long				Compare( SortListData *pOne,
171 								 SortListData *pTwo )
172 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
173 	void				BuildSortInfo( REFERENCE< XRESULTSET > aResult,
174 									   const SEQUENCE < NUMBERED_SORTINGINFO > &xSortInfo,
175 									   const REFERENCE< XANYCOMPAREFACTORY > &xCompFac );
176 	long				CompareImpl( REFERENCE < XRESULTSET > xResultOne,
177 									 REFERENCE < XRESULTSET > xResultTwo,
178 									 long nIndexOne, long nIndexTwo,
179 									 SortInfo* pSortInfo )
180 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
181 	long				CompareImpl( REFERENCE < XRESULTSET > xResultOne,
182 									 REFERENCE < XRESULTSET > xResultTwo,
183 									 long nIndexOne, long nIndexTwo )
184 							throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
185 	void				PropertyChanged( const PROPERTYCHANGEEVENT& rEvt );
186 
187 public:
188 						SortedResultSet( REFERENCE< XRESULTSET > aResult );
189 						~SortedResultSet();
190 
GetS2OList() const191 	const SortedEntryList*		GetS2OList() const { return &maS2O; }
GetO2SList() const192 	const SimpleList*			GetO2SList() const { return &maO2S; }
GetResultSet() const193 	REFERENCE < XRESULTSET >	GetResultSet() const { return mxOriginal; }
GetSortInfo() const194 	SortInfo*					GetSortInfo() const { return mpSortInfo; }
GetCount() const195 	long						GetCount() const { return mnCount; }
196 
197 	void				CopyData( SortedResultSet* pSource );
198 	void				Initialize( const SEQUENCE < NUMBERED_SORTINGINFO > &xSortInfo,
199 									const REFERENCE< XANYCOMPAREFACTORY > &xCompFac );
200 	void				CheckProperties( long nOldCount, sal_Bool bWasFinal );
201 
202 	void				InsertNew( long nPos, long nCount );
203 	void				SetChanged( long nPos, long nCount );
204 	void				Remove( long nPos, long nCount, EventList *pList );
205 	void				Move( long nPos, long nCount, long nOffset );
206 
207 	void				ResortModified( EventList* pList );
208 	void				ResortNew( EventList* pList );
209 
210 	// XInterface
211 	XINTERFACE_DECL()
212 
213 	// XTypeProvider
214 	XTYPEPROVIDER_DECL()
215 
216     // XServiceInfo
217 	XSERVICEINFO_NOFACTORY_DECL()
218 
219 	// XComponent
220 	virtual void SAL_CALL
221 	dispose() throw( RUNTIME_EXCEPTION );
222 
223     virtual void SAL_CALL
224 	addEventListener( const REFERENCE< XEVENTLISTENER >& Listener )
225 		throw( RUNTIME_EXCEPTION );
226 
227     virtual void SAL_CALL
228 	removeEventListener( const REFERENCE< XEVENTLISTENER >& Listener )
229 		throw( RUNTIME_EXCEPTION );
230 
231 	// XContentAccess
232     virtual rtl::OUString SAL_CALL
233 	queryContentIdentifierString()
234 		throw( RUNTIME_EXCEPTION );
235     virtual REFERENCE<
236 				com::sun::star::ucb::XContentIdentifier > SAL_CALL
237 	queryContentIdentifier()
238 		throw( RUNTIME_EXCEPTION );
239     virtual REFERENCE<
240 				com::sun::star::ucb::XContent > SAL_CALL
241 	queryContent()
242 		throw( RUNTIME_EXCEPTION );
243 
244 	// XResultSet
245     virtual sal_Bool SAL_CALL
246 	next()
247 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
248     virtual sal_Bool SAL_CALL
249 	isBeforeFirst()
250 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
251     virtual sal_Bool SAL_CALL
252 	isAfterLast()
253 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
254     virtual sal_Bool SAL_CALL
255 	isFirst()
256 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
257     virtual sal_Bool SAL_CALL
258 	isLast()
259 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
260     virtual void SAL_CALL
261 	beforeFirst()
262 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
263     virtual void SAL_CALL
264 	afterLast()
265 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
266     virtual sal_Bool SAL_CALL
267 	first()
268 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
269     virtual sal_Bool SAL_CALL
270 	last()
271 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
272     virtual sal_Int32 SAL_CALL
273 	getRow()
274 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
275     virtual sal_Bool SAL_CALL
276 	absolute( sal_Int32 row )
277 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
278     virtual sal_Bool SAL_CALL
279 	relative( sal_Int32 rows )
280 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
281     virtual sal_Bool SAL_CALL
282 	previous()
283 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
284     virtual void SAL_CALL
285 	refreshRow()
286 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
287     virtual sal_Bool SAL_CALL
288 	rowUpdated()
289 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
290     virtual sal_Bool SAL_CALL
291 	rowInserted()
292 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
293     virtual sal_Bool SAL_CALL
294 	rowDeleted()
295 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
296     virtual REFERENCE<
297 				com::sun::star::uno::XInterface > SAL_CALL
298 	getStatement()
299 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
300 
301 	// XRow
302     virtual sal_Bool SAL_CALL
303 	wasNull() throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
304 
305     virtual rtl::OUString SAL_CALL
306 	getString( sal_Int32 columnIndex )
307 	throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
308 
309     virtual sal_Bool SAL_CALL
310 	getBoolean( sal_Int32 columnIndex )
311 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
312 
313 	virtual sal_Int8 SAL_CALL
314 	getByte( sal_Int32 columnIndex )
315 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
316 
317 	virtual sal_Int16 SAL_CALL
318 	getShort( sal_Int32 columnIndex )
319 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
320 
321 	virtual sal_Int32 SAL_CALL
322 	getInt( sal_Int32 columnIndex )
323 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
324 
325 	virtual sal_Int64 SAL_CALL
326 	getLong( sal_Int32 columnIndex )
327 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
328 
329 	virtual float SAL_CALL
330 	getFloat( sal_Int32 columnIndex )
331 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
332 
333 	virtual double SAL_CALL
334 	getDouble( sal_Int32 columnIndex )
335 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
336 
337 	virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
338 	getBytes( sal_Int32 columnIndex )
339 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
340 
341 	virtual com::sun::star::util::Date SAL_CALL
342 	getDate( sal_Int32 columnIndex )
343 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
344 
345 	virtual com::sun::star::util::Time SAL_CALL
346 	getTime( sal_Int32 columnIndex )
347 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
348 
349 	virtual com::sun::star::util::DateTime SAL_CALL
350 	getTimestamp( sal_Int32 columnIndex )
351 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
352 
353 	virtual REFERENCE<
354 				com::sun::star::io::XInputStream > SAL_CALL
355 	getBinaryStream( sal_Int32 columnIndex )
356 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
357 
358 	virtual REFERENCE<
359 				com::sun::star::io::XInputStream > SAL_CALL
360 	getCharacterStream( sal_Int32 columnIndex )
361 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
362 
363 	virtual com::sun::star::uno::Any SAL_CALL
364 	getObject( sal_Int32 columnIndex,
365 			   const REFERENCE<
366 			   	com::sun::star::container::XNameAccess >& typeMap )
367 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
368     virtual REFERENCE<
369 				com::sun::star::sdbc::XRef > SAL_CALL
370 	getRef( sal_Int32 columnIndex )
371 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
372     virtual REFERENCE<
373 				com::sun::star::sdbc::XBlob > SAL_CALL
374 	getBlob( sal_Int32 columnIndex )
375 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
376     virtual REFERENCE<
377 				com::sun::star::sdbc::XClob > SAL_CALL
378 	getClob( sal_Int32 columnIndex )
379 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
380     virtual REFERENCE<
381 				com::sun::star::sdbc::XArray > SAL_CALL
382 	getArray( sal_Int32 columnIndex )
383 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
384 
385 	// XCloseable
386     virtual void SAL_CALL
387 	close()
388 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
389 
390 	// XResultSetMetaDataSupplier
391 	virtual REFERENCE< XRESULTSETMETADATA > SAL_CALL
392 	getMetaData()
393 		throw( SQLEXCEPTION, RUNTIME_EXCEPTION );
394 
395 
396 	// XPropertySet
397     virtual REFERENCE<
398 				com::sun::star::beans::XPropertySetInfo > SAL_CALL
399 	getPropertySetInfo()
400 		throw( RUNTIME_EXCEPTION );
401 
402     virtual void SAL_CALL
403 	setPropertyValue( const rtl::OUString& PropertyName,
404 					  const com::sun::star::uno::Any& Value )
405 		throw( com::sun::star::beans::UnknownPropertyException,
406 			   com::sun::star::beans::PropertyVetoException,
407 			   com::sun::star::lang::IllegalArgumentException,
408 			   com::sun::star::lang::WrappedTargetException,
409 			   RUNTIME_EXCEPTION );
410 
411     virtual com::sun::star::uno::Any SAL_CALL
412 	getPropertyValue( const rtl::OUString& PropertyName )
413 		throw( com::sun::star::beans::UnknownPropertyException,
414 		com::sun::star::lang::WrappedTargetException,
415 		RUNTIME_EXCEPTION );
416 
417     virtual void SAL_CALL
418 	addPropertyChangeListener( const rtl::OUString& PropertyName,
419 							   const REFERENCE<
420 							   		com::sun::star::beans::XPropertyChangeListener >& Listener )
421 		throw( com::sun::star::beans::UnknownPropertyException,
422 			   com::sun::star::lang::WrappedTargetException,
423 			   RUNTIME_EXCEPTION );
424 
425     virtual void SAL_CALL
426 	removePropertyChangeListener( const rtl::OUString& PropertyName,
427 								  const REFERENCE<
428 								  	com::sun::star::beans::XPropertyChangeListener >& Listener )
429 		throw( com::sun::star::beans::UnknownPropertyException,
430 			   com::sun::star::lang::WrappedTargetException,
431 			   RUNTIME_EXCEPTION );
432 
433     virtual void SAL_CALL
434 	addVetoableChangeListener( const rtl::OUString& PropertyName,
435 							   const REFERENCE<
436 							   		com::sun::star::beans::XVetoableChangeListener >& Listener )
437 		throw( com::sun::star::beans::UnknownPropertyException,
438 			   com::sun::star::lang::WrappedTargetException,
439 			   RUNTIME_EXCEPTION );
440 
441     virtual void SAL_CALL
442 	removeVetoableChangeListener( const rtl::OUString& PropertyName,
443 								  const REFERENCE<
444 								  	com::sun::star::beans::XVetoableChangeListener >& aListener )
445 		throw( com::sun::star::beans::UnknownPropertyException,
446 			   com::sun::star::lang::WrappedTargetException,
447 			   RUNTIME_EXCEPTION );
448 };
449 
450 #endif
451 
452