summaryrefslogtreecommitdiffstats
path: root/ucb/source/cacher/cachedcontentresultsetstub.cxx
blob: 942fc19833461522f5da2ac79c82aab6355bdc80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include "cachedcontentresultsetstub.hxx"
#include <com/sun/star/sdbc/FetchDirection.hpp>
#include <com/sun/star/sdbc/SQLException.hpp>
#include <com/sun/star/ucb/FetchError.hpp>
#include <osl/diagnose.h>
#include <cppuhelper/queryinterface.hxx>
#include <ucbhelper/macros.hxx>

using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::ucb;
using namespace com::sun::star::uno;
using namespace com::sun::star::util;
using namespace cppu;


CachedContentResultSetStub::CachedContentResultSetStub( Reference< XResultSet > const & xOrigin )
                : ContentResultSetWrapper( xOrigin )
                , m_nColumnCount( 0 )
                , m_bColumnCountCached( false )
                , m_bNeedToPropagateFetchSize( true )
                , m_bFirstFetchSizePropagationDone( false )
                , m_nLastFetchSize( 1 )//this value is not important at all
                , m_bLastFetchDirection( true )//this value is not important at all
                , m_aPropertyNameForFetchSize( OUString("FetchSize") )
                , m_aPropertyNameForFetchDirection( OUString("FetchDirection") )
{
    impl_init();
}

CachedContentResultSetStub::~CachedContentResultSetStub()
{
    impl_deinit();
}


// XInterface methods.
void SAL_CALL CachedContentResultSetStub::acquire()
    throw()
{
    OWeakObject::acquire();
}

void SAL_CALL CachedContentResultSetStub::release()
    throw()
{
    OWeakObject::release();
}

Any SAL_CALL CachedContentResultSetStub
    ::queryInterface( const Type&  rType )
{
    //list all interfaces inclusive baseclasses of interfaces

    Any aRet = ContentResultSetWrapper::queryInterface( rType );
    if( aRet.hasValue() )
        return aRet;

    aRet = cppu::queryInterface( rType
                , static_cast< XTypeProvider* >( this )
                , static_cast< XServiceInfo* >( this )
                , static_cast< XFetchProvider* >( this )
                , static_cast< XFetchProviderForContentAccess* >( this )
                );

    return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}


// own methods.  ( inherited )


//virtual
void CachedContentResultSetStub
    ::impl_propertyChange( const PropertyChangeEvent& rEvt )
{
    impl_EnsureNotDisposed();

    //don't notify events on fetchsize and fetchdirection to the above CachedContentResultSet
    //because it will ignore them anyway and we can save this remote calls
    if(    rEvt.PropertyName == m_aPropertyNameForFetchSize
        || rEvt.PropertyName == m_aPropertyNameForFetchDirection )
        return;

    PropertyChangeEvent aEvt( rEvt );
    aEvt.Source = static_cast< XPropertySet * >( this );
    aEvt.Further = false;

    impl_notifyPropertyChangeListeners( aEvt );
}


//virtual
void CachedContentResultSetStub
    ::impl_vetoableChange( const PropertyChangeEvent& rEvt )
{
    impl_EnsureNotDisposed();

    //don't notify events on fetchsize and fetchdirection to the above CachedContentResultSet
    //because it will ignore them anyway and we can save this remote calls
    if(    rEvt.PropertyName == m_aPropertyNameForFetchSize
        || rEvt.PropertyName == m_aPropertyNameForFetchDirection )
        return;

    PropertyChangeEvent aEvt( rEvt );
    aEvt.Source = static_cast< XPropertySet * >( this );
    aEvt.Further = false;

    impl_notifyVetoableChangeListeners( aEvt );
}


// XTypeProvider methods.


XTYPEPROVIDER_COMMON_IMPL( CachedContentResultSetStub )
//list all interfaces exclusive baseclasses
Sequence< Type > SAL_CALL CachedContentResultSetStub
    ::getTypes()
{
    static Sequence<Type> ourTypes(
                {   CPPU_TYPE_REF( XTypeProvider ),
                    CPPU_TYPE_REF( XServiceInfo ),
                    CPPU_TYPE_REF( XComponent ),
                    CPPU_TYPE_REF( XCloseable ),
                    CPPU_TYPE_REF( XResultSetMetaDataSupplier ),
                    CPPU_TYPE_REF( XPropertySet ),
                    CPPU_TYPE_REF( XPropertyChangeListener ),
                    CPPU_TYPE_REF( XVetoableChangeListener ),
                    CPPU_TYPE_REF( XResultSet ),
                    CPPU_TYPE_REF( XContentAccess ),
                    CPPU_TYPE_REF( XRow ),
                    CPPU_TYPE_REF( XFetchProvider ),
                    CPPU_TYPE_REF( XFetchProviderForContentAccess ) } );

    return ourTypes;
}


// XServiceInfo methods.

OUString SAL_CALL CachedContentResultSetStub::getImplementationName()
{
    return "com.sun.star.comp.ucb.CachedContentResultSetStub";
}

sal_Bool SAL_CALL CachedContentResultSetStub::supportsService( const OUString& ServiceName )
{
    return cppu::supportsService( this, ServiceName );
}

css::uno::Sequence< OUString > SAL_CALL CachedContentResultSetStub::getSupportedServiceNames()
{
    return { CACHED_CRS_STUB_SERVICE_NAME };
}



// XFetchProvider methods.


FetchResult CachedContentResultSetStub::impl_fetchHelper(
        sal_Int32 nRowStartPosition, sal_Int32 nRowCount, bool bDirection,
        std::function<void( css::uno::Any& rRowContent)> impl_loadRow)
{
    impl_EnsureNotDisposed();
    if( !m_xResultSetOrigin.is() )
    {
        OSL_FAIL( "broadcaster was disposed already" );
        throw RuntimeException();
    }
    impl_propagateFetchSizeAndDirection( nRowCount, bDirection );
    FetchResult aRet;
    aRet.StartIndex = nRowStartPosition;
    aRet.Orientation = bDirection;
    aRet.FetchError = FetchError::SUCCESS; /*ENDOFDATA, EXCEPTION*/
    sal_Int32 nOldOriginal_Pos = m_xResultSetOrigin->getRow();
    if( impl_isForwardOnly() )
    {
        if( nOldOriginal_Pos != nRowStartPosition )
        {
            /*@todo*/
            aRet.FetchError = FetchError::EXCEPTION;
            return aRet;
        }
        if( nRowCount != 1 )
            aRet.FetchError = FetchError::EXCEPTION;

        aRet.Rows.realloc( 1 );

        try
        {
            impl_loadRow( aRet.Rows[0] );
        }
        catch( SQLException& )
        {
            aRet.Rows.realloc( 0 );
            aRet.FetchError = FetchError::EXCEPTION;
            return aRet;
        }
        return aRet;
    }
    aRet.Rows.realloc( nRowCount );
    bool bOldOriginal_AfterLast = false;
    if( !nOldOriginal_Pos )
        bOldOriginal_AfterLast = m_xResultSetOrigin->isAfterLast();
    sal_Int32 nN = 1;
    bool bValidNewPos = false;
    try
    {
        try
        {
            /*if( nOldOriginal_Pos != nRowStartPosition )*/
            bValidNewPos = m_xResultSetOrigin->absolute( nRowStartPosition );
        }
        catch( SQLException& )
        {
            aRet.Rows.realloc( 0 );
            aRet.FetchError = FetchError::EXCEPTION;
            return aRet;
        }
        if( !bValidNewPos )
        {
            aRet.Rows.realloc( 0 );
            aRet.FetchError = FetchError::EXCEPTION;

            /*restore old position*/
            if( nOldOriginal_Pos )
                m_xResultSetOrigin->absolute( nOldOriginal_Pos );
            else if( bOldOriginal_AfterLast )
                m_xResultSetOrigin->afterLast();
            else
                m_xResultSetOrigin->beforeFirst();

            return aRet;
        }
        for( ; nN <= nRowCount; )
        {
            impl_loadRow( aRet.Rows[nN-1] );
            nN++;
            if( nN <= nRowCount )
            {
                if( bDirection )
                {
                    if( !m_xResultSetOrigin->next() )
                    {
                        aRet.Rows.realloc( nN-1 );
                        aRet.FetchError = FetchError::ENDOFDATA;
                        break;
                    }
                }
                else
                {
                    if( !m_xResultSetOrigin->previous() )
                    {
                        aRet.Rows.realloc( nN-1 );
                        aRet.FetchError = FetchError::ENDOFDATA;
                        break;
                    }
                }
            }
        }
    }
    catch( SQLException& )
    {
        aRet.Rows.realloc( nN-1 );
        aRet.FetchError = FetchError::EXCEPTION;
    }
    /*restore old position*/
    if( nOldOriginal_Pos )
        m_xResultSetOrigin->absolute( nOldOriginal_Pos );
    else if( bOldOriginal_AfterLast )
        m_xResultSetOrigin->afterLast();
    else
        m_xResultSetOrigin->beforeFirst();
    return aRet;
}

FetchResult SAL_CALL CachedContentResultSetStub
    ::fetch( sal_Int32 nRowStartPosition
    , sal_Int32 nRowCount, sal_Bool bDirection )
{
    impl_init_xRowOrigin();
    return impl_fetchHelper( nRowStartPosition, nRowCount, bDirection,
        [&](css::uno::Any& rRowContent)
        { return impl_getCurrentRowContent(rRowContent, m_xRowOrigin); });
}

sal_Int32 CachedContentResultSetStub
    ::impl_getColumnCount()
{
    sal_Int32 nCount;
    bool bCached;
    {
        osl::Guard< osl::Mutex > aGuard( m_aMutex );
        nCount = m_nColumnCount;
        bCached = m_bColumnCountCached;
    }
    if( !bCached )
    {
        try
        {
            Reference< XResultSetMetaData > xMetaData = getMetaData();
            if( xMetaData.is() )
                nCount = xMetaData->getColumnCount();
        }
        catch( SQLException& )
        {
            OSL_FAIL( "couldn't determine the column count" );
            nCount = 0;
        }
    }
    osl::Guard< osl::Mutex > aGuard( m_aMutex );
    m_nColumnCount = nCount;
    m_bColumnCountCached = true;
    return m_nColumnCount;
}

void CachedContentResultSetStub
    ::impl_getCurrentRowContent( Any& rRowContent
        , const Reference< XRow >& xRow )
{
    sal_Int32 nCount = impl_getColumnCount();

    Sequence< Any > aContent( nCount );
    for( sal_Int32 nN = 1; nN <= nCount; nN++ )
    {
        aContent[nN-1] = xRow->getObject( nN, nullptr );
    }

    rRowContent <<= aContent;
}

void CachedContentResultSetStub
    ::impl_propagateFetchSizeAndDirection( sal_Int32 nFetchSize, bool bFetchDirection )
{
    //this is done only for the case, that there is another CachedContentResultSet in the chain of underlying ResultSets

    //we do not propagate the property 'FetchSize' or 'FetchDirection' via 'setPropertyValue' from the above CachedContentResultSet to save remote calls

    //if the underlying ResultSet has a property FetchSize and FetchDirection,
    //we will set these properties, if the new given parameters are different from the last ones

    if( !m_bNeedToPropagateFetchSize )
        return;

    bool bNeedAction;
    sal_Int32 nLastSize;
    bool bLastDirection;
    bool bFirstPropagationDone;
    {
        osl::Guard< osl::Mutex > aGuard( m_aMutex );
        bNeedAction             = m_bNeedToPropagateFetchSize;
        nLastSize               = m_nLastFetchSize;
        bLastDirection          = m_bLastFetchDirection;
        bFirstPropagationDone   = m_bFirstFetchSizePropagationDone;
    }
    if( bNeedAction )
    {
        if( nLastSize == nFetchSize
            && bLastDirection == bFetchDirection
            && bFirstPropagationDone )
            return;

        if(!bFirstPropagationDone)
        {
            //check whether the properties 'FetchSize' and 'FetchDirection' do exist

            Reference< XPropertySetInfo > xPropertySetInfo = getPropertySetInfo();
            bool bHasSize = xPropertySetInfo->hasPropertyByName( m_aPropertyNameForFetchSize );
            bool bHasDirection = xPropertySetInfo->hasPropertyByName( m_aPropertyNameForFetchDirection );

            if(!bHasSize || !bHasDirection)
            {
                osl::Guard< osl::Mutex > aGuard( m_aMutex );
                m_bNeedToPropagateFetchSize = false;
                return;
            }
        }

        bool bSetSize       = ( nLastSize       !=nFetchSize        ) || !bFirstPropagationDone;
        bool bSetDirection  = ( bLastDirection  !=bFetchDirection   ) || !bFirstPropagationDone;

        {
            osl::Guard< osl::Mutex > aGuard( m_aMutex );
            m_bFirstFetchSizePropagationDone = true;
            m_nLastFetchSize        = nFetchSize;
            m_bLastFetchDirection   = bFetchDirection;
        }

        if( bSetSize )
        {
            Any aValue;
            aValue <<= nFetchSize;
            try
            {
                setPropertyValue( m_aPropertyNameForFetchSize, aValue );
            }
            catch( css::uno::Exception& ) {}
        }
        if( bSetDirection )
        {
            sal_Int32 nFetchDirection = FetchDirection::FORWARD;
            if( !bFetchDirection )
                nFetchDirection = FetchDirection::REVERSE;
            Any aValue;
            aValue <<= nFetchDirection;
            try
            {
                setPropertyValue( m_aPropertyNameForFetchDirection, aValue );
            }
            catch( css::uno::Exception& ) {}
        }

    }
}


// XFetchProviderForContentAccess methods.


void CachedContentResultSetStub
    ::impl_getCurrentContentIdentifierString( Any& rAny
        , const Reference< XContentAccess >& xContentAccess )
{
     rAny <<= xContentAccess->queryContentIdentifierString();
}

void CachedContentResultSetStub
    ::impl_getCurrentContentIdentifier( Any& rAny
        , const Reference< XContentAccess >& xContentAccess )
{
     rAny <<= xContentAccess->queryContentIdentifier();
}

void CachedContentResultSetStub
    ::impl_getCurrentContent( Any& rAny
        , const Reference< XContentAccess >& xContentAccess )
{
     rAny <<= xContentAccess->queryContent();
}

//virtual
FetchResult SAL_CALL CachedContentResultSetStub
    ::fetchContentIdentifierStrings( sal_Int32 nRowStartPosition
        , sal_Int32 nRowCount, sal_Bool bDirection )
{
    impl_init_xContentAccessOrigin();
    return impl_fetchHelper( nRowStartPosition, nRowCount, bDirection,
        [&](css::uno::Any& rRowContent)
        { return impl_getCurrentContentIdentifierString(rRowContent, m_xContentAccessOrigin); });
}

//virtual
FetchResult SAL_CALL CachedContentResultSetStub
    ::fetchContentIdentifiers( sal_Int32 nRowStartPosition
        , sal_Int32 nRowCount, sal_Bool bDirection )
{
    impl_init_xContentAccessOrigin();
    return impl_fetchHelper( nRowStartPosition, nRowCount, bDirection,
        [&](css::uno::Any& rRowContent)
        { return impl_getCurrentContentIdentifier(rRowContent, m_xContentAccessOrigin); });
}

//virtual
FetchResult SAL_CALL CachedContentResultSetStub
    ::fetchContents( sal_Int32 nRowStartPosition
        , sal_Int32 nRowCount, sal_Bool bDirection )
{
    impl_init_xContentAccessOrigin();
    return impl_fetchHelper( nRowStartPosition, nRowCount, bDirection,
        [&](css::uno::Any& rRowContent)
        { return impl_getCurrentContent(rRowContent, m_xContentAccessOrigin); });
}




CachedContentResultSetStubFactory::CachedContentResultSetStubFactory()
{
}

CachedContentResultSetStubFactory::~CachedContentResultSetStubFactory()
{
}


// CachedContentResultSetStubFactory XServiceInfo methods.

XSERVICEINFO_COMMOM_IMPL( CachedContentResultSetStubFactory,
                          "com.sun.star.comp.ucb.CachedContentResultSetStubFactory" )
/// @throws css::uno::Exception
static css::uno::Reference< css::uno::XInterface >
CachedContentResultSetStubFactory_CreateInstance( const css::uno::Reference< css::lang::XMultiServiceFactory> & )
{
    css::lang::XServiceInfo* pX = new CachedContentResultSetStubFactory;
    return css::uno::Reference< css::uno::XInterface >::query( pX );
}
css::uno::Sequence< OUString >
CachedContentResultSetStubFactory::getSupportedServiceNames_Static()
{
    return { CACHED_CRS_STUB_FACTORY_NAME };
}

// Service factory implementation.


ONE_INSTANCE_SERVICE_FACTORY_IMPL( CachedContentResultSetStubFactory );


// CachedContentResultSetStubFactory XCachedContentResultSetStubFactory methods.


    //virtual
Reference< XResultSet > SAL_CALL CachedContentResultSetStubFactory
    ::createCachedContentResultSetStub(
            const Reference< XResultSet > & xSource )
{
    if( xSource.is() )
    {
        Reference< XResultSet > xRet = new CachedContentResultSetStub( xSource );
        return xRet;
    }
    return nullptr;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */