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 _CACHED_CONTENT_RESULTSET_HXX
25 #define _CACHED_CONTENT_RESULTSET_HXX
26 
27 #include <contentresultsetwrapper.hxx>
28 #include <com/sun/star/lang/XTypeProvider.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/ucb/XFetchProvider.hpp>
31 #include <com/sun/star/ucb/XFetchProviderForContentAccess.hpp>
32 #include <com/sun/star/ucb/FetchResult.hpp>
33 #include <com/sun/star/ucb/XContentIdentifierMapping.hpp>
34 #include <com/sun/star/ucb/XCachedContentResultSetFactory.hpp>
35 
36 #define CACHED_CONTENT_RESULTSET_SERVICE_NAME "com.sun.star.ucb.CachedContentResultSet"
37 #define CACHED_CONTENT_RESULTSET_FACTORY_NAME "com.sun.star.ucb.CachedContentResultSetFactory"
38 
39 //=========================================================================
40 
41 namespace com { namespace sun { namespace star { namespace script {
42 	class XTypeConverter;
43 } } } }
44 
45 class CCRS_PropertySetInfo;
46 class CachedContentResultSet
47 				: public ContentResultSetWrapper
48 				, public com::sun::star::lang::XTypeProvider
49 				, public com::sun::star::lang::XServiceInfo
50 {
51 	//--------------------------------------------------------------------------
52 	// class CCRS_Cache
53 
54 	class CCRS_Cache
55 	{
56 	private:
57 		com::sun::star::ucb::FetchResult*			m_pResult;
58 		com::sun::star::uno::Reference<
59 			com::sun::star::ucb::XContentIdentifierMapping >
60 													m_xContentIdentifierMapping;
61 		com::sun::star::uno::Sequence< sal_Bool >*	m_pMappedReminder;
62 
63 	private:
64 		com::sun::star::uno::Any& SAL_CALL
65 		getRowAny( sal_Int32 nRow )
66 			throw( com::sun::star::sdbc::SQLException,
67 			com::sun::star::uno::RuntimeException );
68 
69 		void SAL_CALL clear();
70 
71 
72 		void SAL_CALL remindMapped( sal_Int32 nRow );
73 		sal_Bool SAL_CALL isRowMapped( sal_Int32 nRow );
74 		void SAL_CALL clearMappedReminder();
75 		com::sun::star::uno::Sequence< sal_Bool >* SAL_CALL getMappedReminder();
76 
77 	public:
78 		CCRS_Cache( const com::sun::star::uno::Reference<
79 				com::sun::star::ucb::XContentIdentifierMapping > & xMapping );
80 		~CCRS_Cache();
81 
82 		void SAL_CALL loadData(
83 			const com::sun::star::ucb::FetchResult& rResult );
84 
85 		sal_Bool SAL_CALL
86 		hasRow( sal_Int32 nRow );
87 
88 		sal_Bool SAL_CALL
89 		hasCausedException( sal_Int32 nRow );
90 
91 		sal_Int32 SAL_CALL
92 		getMaxRow();
93 
94 		sal_Bool SAL_CALL
95 		hasKnownLast();
96 
97 		//---
98 		const com::sun::star::uno::Any& SAL_CALL
99 		getAny( sal_Int32 nRow, sal_Int32 nColumnIndex )
100 			throw( com::sun::star::sdbc::SQLException,
101 			com::sun::star::uno::RuntimeException );
102 
103 		const rtl::OUString& SAL_CALL
104 		getContentIdentifierString( sal_Int32 nRow )
105 			throw( com::sun::star::uno::RuntimeException );
106 
107 		const com::sun::star::uno::Reference<
108 				com::sun::star::ucb::XContentIdentifier >& SAL_CALL
109 		getContentIdentifier( sal_Int32 nRow )
110 			throw( com::sun::star::uno::RuntimeException );
111 
112 		const com::sun::star::uno::Reference<
113 				com::sun::star::ucb::XContent >& SAL_CALL
114 		getContent( sal_Int32 nRow )
115 			throw( com::sun::star::uno::RuntimeException );
116 	};
117 	//-----------------------------------------------------------------
118 	//members
119 
120     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
121                             m_xSMgr;
122 
123 	//different Interfaces from Origin:
124 	com::sun::star::uno::Reference< com::sun::star::ucb::XFetchProvider >
125 							m_xFetchProvider; //XFetchProvider-interface from m_xOrigin
126 
127 	com::sun::star::uno::Reference< com::sun::star::ucb::XFetchProviderForContentAccess >
128 							m_xFetchProviderForContentAccess; //XFetchProviderForContentAccess-interface from m_xOrigin
129 
130 	//my PropertySetInfo
131 	com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo >
132 							m_xMyPropertySetInfo;//holds m_pMyPropSetInfo alive
133 	CCRS_PropertySetInfo*	m_pMyPropSetInfo;
134 
135 
136 	//
137 	com::sun::star::uno::Reference< com::sun::star::ucb::XContentIdentifierMapping >
138 							m_xContentIdentifierMapping;// can be used for remote optimized ContentAccess
139 
140 	//some Properties and helping variables
141 	sal_Int32				m_nRow;
142 	sal_Bool				m_bAfterLast; // TRUE, if m_nRow is after final count; can be TRUE without knowing the exact final count
143 
144 	sal_Int32				m_nLastAppliedPos;
145 	sal_Bool				m_bAfterLastApplied;
146 
147 	sal_Int32				m_nKnownCount; // count we know from the Origin
148 	sal_Bool				m_bFinalCount; // TRUE if the Origin has reached final count and we got that count in m_nKnownCount
149 
150 	sal_Int32				m_nFetchSize;
151 	sal_Int32				m_nFetchDirection;
152 
153 	sal_Bool				m_bLastReadWasFromCache;
154 	sal_Bool				m_bLastCachedReadWasNull;
155 
156 	//cache:
157 	CCRS_Cache				m_aCache;
158 	CCRS_Cache				m_aCacheContentIdentifierString;
159 	CCRS_Cache				m_aCacheContentIdentifier;
160 	CCRS_Cache				m_aCacheContent;
161 
162 
163 private:
164 	//-----------------------------------------------------------------
165 	//helping XPropertySet methods.
166 	virtual void SAL_CALL impl_initPropertySetInfo();
167 
168 
169 	//-----------------------------------------------------------------
170 	sal_Bool SAL_CALL
171 	applyPositionToOrigin( sal_Int32 nRow )
172 		throw( com::sun::star::sdbc::SQLException,
173 		com::sun::star::uno::RuntimeException );
174 
175 	void SAL_CALL
176 	impl_fetchData( sal_Int32 nRow, sal_Int32 nCount
177 					, sal_Int32 nFetchDirection )
178 		throw( com::sun::star::uno::RuntimeException );
179 
180 	sal_Bool SAL_CALL
181 	impl_isKnownValidPosition( sal_Int32 nRow );
182 
183 	sal_Bool SAL_CALL
184 	impl_isKnownInvalidPosition( sal_Int32 nRow );
185 
186 	void SAL_CALL
187 	impl_changeRowCount( sal_Int32 nOld, sal_Int32 nNew );
188 
189 	void SAL_CALL
190 	impl_changeIsRowCountFinal( sal_Bool bOld, sal_Bool bNew );
191 
192 public:
193     CachedContentResultSet(
194                         const com::sun::star::uno::Reference<
195                             com::sun::star::lang::XMultiServiceFactory > &
196                                 xSMgr,
197                         const com::sun::star::uno::Reference<
198                             com::sun::star::sdbc::XResultSet > & xOrigin,
199                         const com::sun::star::uno::Reference<
200                             com::sun::star::ucb::XContentIdentifierMapping > &
201                                 xContentIdentifierMapping );
202 
203 	virtual ~CachedContentResultSet();
204 
205 	//-----------------------------------------------------------------
206 	// XInterface inherited
207 	//-----------------------------------------------------------------
208 	XINTERFACE_DECL()
209 	//-----------------------------------------------------------------
210 	// XTypeProvider
211 	//-----------------------------------------------------------------
212 	XTYPEPROVIDER_DECL()
213 	//-----------------------------------------------------------------
214     // XServiceInfo
215 	//-----------------------------------------------------------------
216 	XSERVICEINFO_NOFACTORY_DECL()
217 
218 	//-----------------------------------------------------------------
219 	// XPropertySet inherited
220 	//-----------------------------------------------------------------
221 
222     virtual void SAL_CALL
223 	setPropertyValue( const rtl::OUString& aPropertyName,
224 					  const com::sun::star::uno::Any& aValue )
225 		throw( com::sun::star::beans::UnknownPropertyException,
226 			   com::sun::star::beans::PropertyVetoException,
227 			   com::sun::star::lang::IllegalArgumentException,
228 			   com::sun::star::lang::WrappedTargetException,
229 			   com::sun::star::uno::RuntimeException );
230 
231     virtual com::sun::star::uno::Any SAL_CALL
232 	getPropertyValue( const rtl::OUString& PropertyName )
233 		throw( com::sun::star::beans::UnknownPropertyException,
234 		com::sun::star::lang::WrappedTargetException,
235 		com::sun::star::uno::RuntimeException );
236 
237 	//-----------------------------------------------------------------
238 	// own inherited
239 	//-----------------------------------------------------------------
240 	virtual void SAL_CALL
241 	impl_disposing( const com::sun::star::lang::EventObject& Source )
242 		throw( com::sun::star::uno::RuntimeException );
243 
244 	virtual void SAL_CALL
245 	impl_propertyChange( const com::sun::star::beans::PropertyChangeEvent& evt )
246 		throw( com::sun::star::uno::RuntimeException );
247 
248 	virtual void SAL_CALL
249 	impl_vetoableChange( const com::sun::star::beans::PropertyChangeEvent& aEvent )
250 		throw( com::sun::star::beans::PropertyVetoException,
251 			   com::sun::star::uno::RuntimeException );
252 
253 	//-----------------------------------------------------------------
254 	// XContentAccess inherited
255 	//-----------------------------------------------------------------
256     virtual rtl::OUString SAL_CALL
257 	queryContentIdentifierString()
258 		throw( com::sun::star::uno::RuntimeException );
259 
260     virtual com::sun::star::uno::Reference<
261 				com::sun::star::ucb::XContentIdentifier > SAL_CALL
262 	queryContentIdentifier()
263 		throw( com::sun::star::uno::RuntimeException );
264 
265     virtual com::sun::star::uno::Reference<
266 				com::sun::star::ucb::XContent > SAL_CALL
267 	queryContent()
268 		throw( com::sun::star::uno::RuntimeException );
269 
270 	//-----------------------------------------------------------------
271 	// XResultSet inherited
272 	//-----------------------------------------------------------------
273     virtual sal_Bool SAL_CALL
274 	next()
275 		throw( com::sun::star::sdbc::SQLException,
276 			   com::sun::star::uno::RuntimeException );
277     virtual sal_Bool SAL_CALL
278 	isBeforeFirst()
279 		throw( com::sun::star::sdbc::SQLException,
280 			   com::sun::star::uno::RuntimeException );
281     virtual sal_Bool SAL_CALL
282 	isAfterLast()
283 		throw( com::sun::star::sdbc::SQLException,
284 			   com::sun::star::uno::RuntimeException );
285     virtual sal_Bool SAL_CALL
286 	isFirst()
287 		throw( com::sun::star::sdbc::SQLException,
288 			   com::sun::star::uno::RuntimeException );
289     virtual sal_Bool SAL_CALL
290 	isLast()
291 		throw( com::sun::star::sdbc::SQLException,
292 			   com::sun::star::uno::RuntimeException );
293     virtual void SAL_CALL
294 	beforeFirst()
295 		throw( com::sun::star::sdbc::SQLException,
296 			   com::sun::star::uno::RuntimeException );
297     virtual void SAL_CALL
298 	afterLast()
299 		throw( com::sun::star::sdbc::SQLException,
300 			   com::sun::star::uno::RuntimeException );
301     virtual sal_Bool SAL_CALL
302 	first()
303 		throw( com::sun::star::sdbc::SQLException,
304 			   com::sun::star::uno::RuntimeException );
305     virtual sal_Bool SAL_CALL
306 	last()
307 		throw( com::sun::star::sdbc::SQLException,
308 			   com::sun::star::uno::RuntimeException );
309     virtual sal_Int32 SAL_CALL
310 	getRow()
311 		throw( com::sun::star::sdbc::SQLException,
312 			   com::sun::star::uno::RuntimeException );
313     virtual sal_Bool SAL_CALL
314 	absolute( sal_Int32 row )
315 		throw( com::sun::star::sdbc::SQLException,
316 			   com::sun::star::uno::RuntimeException );
317     virtual sal_Bool SAL_CALL
318 	relative( sal_Int32 rows )
319 		throw( com::sun::star::sdbc::SQLException,
320 			   com::sun::star::uno::RuntimeException );
321     virtual sal_Bool SAL_CALL
322 	previous()
323 		throw( com::sun::star::sdbc::SQLException,
324 			   com::sun::star::uno::RuntimeException );
325     virtual void SAL_CALL
326 	refreshRow()
327 		throw( com::sun::star::sdbc::SQLException,
328 			   com::sun::star::uno::RuntimeException );
329     virtual sal_Bool SAL_CALL
330 	rowUpdated()
331 		throw( com::sun::star::sdbc::SQLException,
332 			   com::sun::star::uno::RuntimeException );
333     virtual sal_Bool SAL_CALL
334 	rowInserted()
335 		throw( com::sun::star::sdbc::SQLException,
336 			   com::sun::star::uno::RuntimeException );
337     virtual sal_Bool SAL_CALL
338 	rowDeleted()
339 		throw( com::sun::star::sdbc::SQLException,
340 			   com::sun::star::uno::RuntimeException );
341     virtual com::sun::star::uno::Reference<
342 				com::sun::star::uno::XInterface > SAL_CALL
343 	getStatement()
344 		throw( com::sun::star::sdbc::SQLException,
345 			   com::sun::star::uno::RuntimeException );
346 
347 	//-----------------------------------------------------------------
348 	// XRow inherited
349 	//-----------------------------------------------------------------
350     virtual sal_Bool SAL_CALL
351 	wasNull()
352 		throw( com::sun::star::sdbc::SQLException,
353 			   com::sun::star::uno::RuntimeException );
354 
355     virtual rtl::OUString SAL_CALL
356 	getString( sal_Int32 columnIndex )
357 		throw( com::sun::star::sdbc::SQLException,
358 			   com::sun::star::uno::RuntimeException );
359 
360     virtual sal_Bool SAL_CALL
361 	getBoolean( sal_Int32 columnIndex )
362 		throw( com::sun::star::sdbc::SQLException,
363 			   com::sun::star::uno::RuntimeException );
364 
365     virtual sal_Int8 SAL_CALL
366 	getByte( sal_Int32 columnIndex )
367 		throw( com::sun::star::sdbc::SQLException,
368 			   com::sun::star::uno::RuntimeException );
369 
370     virtual sal_Int16 SAL_CALL
371 	getShort( sal_Int32 columnIndex )
372 		throw( com::sun::star::sdbc::SQLException,
373 			   com::sun::star::uno::RuntimeException );
374 
375     virtual sal_Int32 SAL_CALL
376 	getInt( sal_Int32 columnIndex )
377 		throw( com::sun::star::sdbc::SQLException,
378 			   com::sun::star::uno::RuntimeException );
379 
380     virtual sal_Int64 SAL_CALL
381 	getLong( sal_Int32 columnIndex )
382 		throw( com::sun::star::sdbc::SQLException,
383 			   com::sun::star::uno::RuntimeException );
384 
385     virtual float SAL_CALL
386 	getFloat( sal_Int32 columnIndex )
387 		throw( com::sun::star::sdbc::SQLException,
388 			   com::sun::star::uno::RuntimeException );
389 
390     virtual double SAL_CALL
391 	getDouble( sal_Int32 columnIndex )
392 		throw( com::sun::star::sdbc::SQLException,
393 			   com::sun::star::uno::RuntimeException );
394 
395     virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
396 	getBytes( sal_Int32 columnIndex )
397 		throw( com::sun::star::sdbc::SQLException,
398 			   com::sun::star::uno::RuntimeException );
399 
400     virtual com::sun::star::util::Date SAL_CALL
401 	getDate( sal_Int32 columnIndex )
402 		throw( com::sun::star::sdbc::SQLException,
403 			   com::sun::star::uno::RuntimeException );
404 
405     virtual com::sun::star::util::Time SAL_CALL
406 	getTime( sal_Int32 columnIndex )
407 		throw( com::sun::star::sdbc::SQLException,
408 			   com::sun::star::uno::RuntimeException );
409 
410     virtual com::sun::star::util::DateTime SAL_CALL
411 	getTimestamp( sal_Int32 columnIndex )
412 		throw( com::sun::star::sdbc::SQLException,
413 			   com::sun::star::uno::RuntimeException );
414 
415     virtual com::sun::star::uno::Reference<
416 				com::sun::star::io::XInputStream > SAL_CALL
417 	getBinaryStream( sal_Int32 columnIndex )
418 		throw( com::sun::star::sdbc::SQLException,
419 			   com::sun::star::uno::RuntimeException );
420 
421     virtual com::sun::star::uno::Reference<
422 				com::sun::star::io::XInputStream > SAL_CALL
423 	getCharacterStream( sal_Int32 columnIndex )
424 		throw( com::sun::star::sdbc::SQLException,
425 			   com::sun::star::uno::RuntimeException );
426 
427     virtual com::sun::star::uno::Any SAL_CALL
428 	getObject( sal_Int32 columnIndex,
429 			   const com::sun::star::uno::Reference<
430 			   	com::sun::star::container::XNameAccess >& typeMap )
431 		throw( com::sun::star::sdbc::SQLException,
432 			   com::sun::star::uno::RuntimeException );
433 
434     virtual com::sun::star::uno::Reference<
435 				com::sun::star::sdbc::XRef > SAL_CALL
436 	getRef( sal_Int32 columnIndex )
437 		throw( com::sun::star::sdbc::SQLException,
438 			   com::sun::star::uno::RuntimeException );
439 
440     virtual com::sun::star::uno::Reference<
441 				com::sun::star::sdbc::XBlob > SAL_CALL
442 	getBlob( sal_Int32 columnIndex )
443 		throw( com::sun::star::sdbc::SQLException,
444 			   com::sun::star::uno::RuntimeException );
445 
446     virtual com::sun::star::uno::Reference<
447 				com::sun::star::sdbc::XClob > SAL_CALL
448 	getClob( sal_Int32 columnIndex )
449 		throw( com::sun::star::sdbc::SQLException,
450 			   com::sun::star::uno::RuntimeException );
451 
452     virtual com::sun::star::uno::Reference<
453 				com::sun::star::sdbc::XArray > SAL_CALL
454 	getArray( sal_Int32 columnIndex )
455 		throw( com::sun::star::sdbc::SQLException,
456 			   com::sun::star::uno::RuntimeException );
457 
458 	//-----------------------------------------------------------------
459     // Type Converter support
460 	//-----------------------------------------------------------------
461 
462 private:
463     sal_Bool m_bTriedToGetTypeConverter;
464     com::sun::star::uno::Reference<
465         com::sun::star::script::XTypeConverter > m_xTypeConverter;
466 
467     const com::sun::star::uno::Reference<
468         com::sun::star::script::XTypeConverter >& getTypeConverter();
469 };
470 
471 //=========================================================================
472 
473 class CachedContentResultSetFactory
474 				: public cppu::OWeakObject
475 				, public com::sun::star::lang::XTypeProvider
476 				, public com::sun::star::lang::XServiceInfo
477 				, public com::sun::star::ucb::XCachedContentResultSetFactory
478 {
479 protected:
480 	com::sun::star::uno::Reference<
481 		com::sun::star::lang::XMultiServiceFactory >	m_xSMgr;
482 
483 public:
484 
485 	CachedContentResultSetFactory(
486 		const com::sun::star::uno::Reference<
487 		com::sun::star::lang::XMultiServiceFactory > & rSMgr);
488 
489 	virtual ~CachedContentResultSetFactory();
490 
491 	//-----------------------------------------------------------------
492 	// XInterface
493 	XINTERFACE_DECL()
494 
495 	//-----------------------------------------------------------------
496 	// XTypeProvider
497 	XTYPEPROVIDER_DECL()
498 
499 	//-----------------------------------------------------------------
500     // XServiceInfo
501 	XSERVICEINFO_DECL()
502 
503 	//-----------------------------------------------------------------
504     // XCachedContentResultSetFactory
505 
506 	virtual com::sun::star::uno::Reference<
507 				com::sun::star::sdbc::XResultSet > SAL_CALL
508 	createCachedContentResultSet(
509 			const com::sun::star::uno::Reference<
510 				com::sun::star::sdbc::XResultSet > & xSource,
511 			const com::sun::star::uno::Reference<
512 				com::sun::star::ucb::XContentIdentifierMapping > & xMapping	)
513 		throw( com::sun::star::uno::RuntimeException );
514 };
515 
516 #endif
517 
518