LibreOffice Module xmlhelp (master) 1
resultsetbase.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <com/sun/star/sdbc/SQLException.hpp>
22#include <com/sun/star/uno/Reference.h>
23#include <com/sun/star/beans/PropertyAttribute.hpp>
26#include <utility>
27
28#include "resultsetbase.hxx"
29
30using namespace chelp;
31using namespace com::sun::star;
32
33ResultSetBase::ResultSetBase( uno::Reference< uno::XComponentContext > xContext,
34 uno::Reference< ucb::XContentProvider > xProvider,
35 const uno::Sequence< beans::Property >& seq )
36 : m_xContext(std::move( xContext )),
37 m_xProvider(std::move( xProvider )),
38 m_nRow( -1 ),
39 m_nWasNull( true ),
40 m_sProperty( seq )
41{
42}
43
44ResultSetBase::~ResultSetBase()
45{
46}
47
48
49// XInterface
50
51void SAL_CALL
53 noexcept
54{
55 OWeakObject::acquire();
56}
57
58
59void SAL_CALL
61 noexcept
62{
63 OWeakObject::release();
64}
65
66
67uno::Any SAL_CALL
69{
70 uno::Any aRet = cppu::queryInterface( rType,
71 static_cast< lang::XComponent* >(this),
72 static_cast< sdbc::XRow* >(this),
73 static_cast< sdbc::XResultSet* >(this),
74 static_cast< sdbc::XResultSetMetaDataSupplier* >(this),
75 static_cast< beans::XPropertySet* >(this),
76 static_cast< ucb::XContentAccess* >(this) );
77 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
78}
79
80
81// XComponent
82
83
84void SAL_CALL
86 const uno::Reference< lang::XEventListener >& Listener )
87{
88 std::unique_lock aGuard( m_aMutex );
89 m_aDisposeEventListeners.addInterface( aGuard, Listener );
90}
91
92
93void SAL_CALL
95 const uno::Reference< lang::XEventListener >& Listener )
96{
97 std::unique_lock aGuard( m_aMutex );
98 m_aDisposeEventListeners.removeInterface( aGuard, Listener );
99}
100
101
102void SAL_CALL
104{
105 std::unique_lock aGuard( m_aMutex );
106
107 lang::EventObject aEvt;
108 aEvt.Source = static_cast< lang::XComponent * >( this );
109
111 {
113 }
114 if( m_aRowCountListeners.getLength(aGuard) )
115 {
117 }
118 if( m_aIsFinalListeners.getLength(aGuard) )
119 {
120 m_aIsFinalListeners.disposeAndClear( aGuard, aEvt );
121 }
122}
123
124
125// XResultSet
126
127sal_Bool SAL_CALL
129{
130 m_nRow++;
131 return sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
132}
133
134
135sal_Bool SAL_CALL
137{
138 return m_nRow == -1;
139}
140
141
142sal_Bool SAL_CALL
144{
145 return sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size(); // Cannot happen, if m_aFolder.isOpen()
146}
147
148
149sal_Bool SAL_CALL
151{
152 return m_nRow == 0;
153}
154
155
156sal_Bool SAL_CALL
158{
159 if( sal::static_int_cast<sal_uInt32>( m_nRow ) == m_aItems.size() - 1 )
160 return true;
161 else
162 return false;
163}
164
165
166void SAL_CALL
168{
169 m_nRow = -1;
170}
171
172
173void SAL_CALL
175{
176 m_nRow = m_aItems.size();
177}
178
179
180sal_Bool SAL_CALL
182{
183 m_nRow = -1;
184 return next();
185}
186
187
188sal_Bool SAL_CALL
190{
191 m_nRow = m_aItems.size() - 1;
192 return true;
193}
194
195
196sal_Int32 SAL_CALL
198{
199 // Test, whether behind last row
200 if( -1 == m_nRow || sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size() )
201 return 0;
202 else
203 return m_nRow+1;
204}
205
206
207sal_Bool SAL_CALL ResultSetBase::absolute( sal_Int32 row )
208{
209 if( row >= 0 )
210 m_nRow = row - 1;
211 else
212 {
213 last();
214 m_nRow += ( row + 1 );
215 if( m_nRow < -1 )
216 m_nRow = -1;
217 }
218
219 return 0<= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
220}
221
222
223sal_Bool SAL_CALL
225{
226 if( isAfterLast() || isBeforeFirst() )
227 throw sdbc::SQLException();
228
229 if( row > 0 )
230 while( row-- )
231 next();
232 else if( row < 0 )
233 while( row++ && m_nRow > -1 )
234 previous();
235
236 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
237}
238
239
240sal_Bool SAL_CALL
242{
243 if( sal::static_int_cast<sal_uInt32>( m_nRow ) > m_aItems.size() )
244 m_nRow = m_aItems.size(); // Correct Handling of afterLast
245 if( 0 <= m_nRow ) -- m_nRow;
246
247 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size();
248}
249
250
251void SAL_CALL
253{
254}
255
256
257sal_Bool SAL_CALL
259{
260 return false;
261}
262
263sal_Bool SAL_CALL
265{
266 return false;
267}
268
269sal_Bool SAL_CALL
271{
272 return false;
273}
274
275
276uno::Reference< uno::XInterface > SAL_CALL
278{
279 return uno::Reference< uno::XInterface >();
280}
281
282
283// XCloseable
284
285void SAL_CALL
287{
288}
289
290
291OUString SAL_CALL
293{
294 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
295 return m_aPath[m_nRow];
296 else
297 return OUString();
298}
299
300
301uno::Reference< ucb::XContentIdentifier > SAL_CALL
303{
304 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
305 {
306 OUString url = queryContentIdentifierString();
307 if( ! m_aIdents[m_nRow].is() && !url.isEmpty() )
308 m_aIdents[m_nRow].set( new ::ucbhelper::ContentIdentifier( url ) );
309 return m_aIdents[m_nRow];
310 }
311
312 return uno::Reference< ucb::XContentIdentifier >();
313}
314
315
316uno::Reference< ucb::XContent > SAL_CALL
318{
319 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() )
320 return m_xProvider->queryContent( queryContentIdentifier() );
321 else
322 return uno::Reference< ucb::XContent >();
323}
324
325namespace {
326
327class XPropertySetInfoImpl
328 : public cppu::OWeakObject,
329 public beans::XPropertySetInfo
330{
331public:
332
333 explicit XPropertySetInfoImpl( const uno::Sequence< beans::Property >& aSeq )
334 : m_aSeq( aSeq )
335 {
336 }
337
338 void SAL_CALL acquire()
339 noexcept override
340 {
341 OWeakObject::acquire();
342 }
343
344
345 void SAL_CALL release()
346 noexcept override
347 {
348 OWeakObject::release();
349 }
350
351 uno::Any SAL_CALL queryInterface( const uno::Type& rType ) override
352 {
353 uno::Any aRet = cppu::queryInterface( rType,
354 static_cast< beans::XPropertySetInfo* >(this) );
355 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
356 }
357
358 uno::Sequence< beans::Property > SAL_CALL getProperties() override
359 {
360 return m_aSeq;
361 }
362
363 beans::Property SAL_CALL getPropertyByName( const OUString& aName ) override
364 {
365 auto pProp = std::find_if(std::cbegin(m_aSeq), std::cend(m_aSeq),
366 [&aName](const beans::Property& rProp) { return aName == rProp.Name; });
367 if (pProp != std::cend(m_aSeq))
368 return *pProp;
369 throw beans::UnknownPropertyException(aName);
370 }
371
372 sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) override
373 {
374 return std::any_of(std::cbegin(m_aSeq), std::cend(m_aSeq),
375 [&Name](const beans::Property& rProp) { return Name == rProp.Name; });
376 }
377
378private:
379
380 uno::Sequence< beans::Property > m_aSeq;
381};
382
383}
384
385// XPropertySet
386uno::Reference< beans::XPropertySetInfo > SAL_CALL
388{
389 uno::Sequence< beans::Property > seq
390 {
391 { "RowCount", -1, cppu::UnoType<sal_Int32>::get(), beans::PropertyAttribute::READONLY },
392 { "IsRowCountFinal", -1, cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::READONLY }
393 };
394
395 //t
396 return uno::Reference< beans::XPropertySetInfo > ( new XPropertySetInfoImpl( seq ) );
397}
398
399
401 const OUString& aPropertyName, const uno::Any& )
402{
403 if( aPropertyName == "IsRowCountFinal" ||
404 aPropertyName == "RowCount" )
405 return;
406
407 throw beans::UnknownPropertyException(aPropertyName);
408}
409
410
412 const OUString& PropertyName )
413{
414 if( PropertyName == "IsRowCountFinal" )
415 {
416 return uno::Any(true);
417 }
418 else if ( PropertyName == "RowCount" )
419 {
420 sal_Int32 count = m_aItems.size();
421 return uno::Any(count);
422 }
423 else
424 throw beans::UnknownPropertyException(PropertyName);
425}
426
427
429 const OUString& aPropertyName,
430 const uno::Reference< beans::XPropertyChangeListener >& xListener )
431{
432 if( aPropertyName == "IsRowCountFinal" )
433 {
434 std::unique_lock aGuard( m_aMutex );
435 m_aIsFinalListeners.addInterface( aGuard, xListener );
436 }
437 else if ( aPropertyName == "RowCount" )
438 {
439 std::unique_lock aGuard( m_aMutex );
440 m_aRowCountListeners.addInterface( aGuard, xListener );
441 }
442 else
443 throw beans::UnknownPropertyException(aPropertyName);
444}
445
446
448 const OUString& aPropertyName,
449 const uno::Reference< beans::XPropertyChangeListener >& aListener )
450{
451 if( aPropertyName == "IsRowCountFinal" )
452 {
453 std::unique_lock aGuard( m_aMutex );
454 m_aIsFinalListeners.removeInterface( aGuard, aListener );
455 }
456 else if ( aPropertyName == "RowCount" )
457 {
458 std::unique_lock aGuard( m_aMutex );
459 m_aRowCountListeners.removeInterface( aGuard, aListener );
460 }
461 else
462 throw beans::UnknownPropertyException(aPropertyName);
463}
464
465
467 const OUString&,
468 const uno::Reference< beans::XVetoableChangeListener >& )
469{}
470
471
473 const OUString&,
474 const uno::Reference< beans::XVetoableChangeListener >& )
475{}
476
477
478// XResultSetMetaDataSupplier
479uno::Reference< sdbc::XResultSetMetaData > SAL_CALL
481{
482 return new ::ucbhelper::ResultSetMetaData( m_xContext, m_sProperty );
483}
484
485/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
Sequence< Any > m_aSeq
virtual sal_Bool SAL_CALL isAfterLast() override
virtual sal_Bool SAL_CALL rowUpdated() override
virtual void SAL_CALL acquire() noexcept override
virtual sal_Bool SAL_CALL previous() override
virtual css::uno::Reference< css::sdbc::XResultSetMetaData > SAL_CALL getMetaData() override
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > m_aDisposeEventListeners
virtual sal_Bool SAL_CALL next() override
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
virtual void SAL_CALL refreshRow() override
virtual sal_Bool SAL_CALL rowInserted() override
comphelper::OInterfaceContainerHelper4< css::beans::XPropertyChangeListener > m_aIsFinalListeners
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual sal_Bool SAL_CALL relative(sal_Int32 rows) override
virtual void SAL_CALL beforeFirst() override
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual void SAL_CALL close() override
std::vector< OUString > m_aPath
virtual sal_Bool SAL_CALL first() override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual void SAL_CALL dispose() override
virtual sal_Bool SAL_CALL isLast() override
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual OUString SAL_CALL queryContentIdentifierString() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Bool SAL_CALL absolute(sal_Int32 row) override
virtual sal_Bool SAL_CALL last() override
css::uno::Reference< css::ucb::XContentProvider > m_xProvider
virtual sal_Bool SAL_CALL rowDeleted() override
virtual sal_Bool SAL_CALL isBeforeFirst() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
css::uno::Sequence< css::beans::Property > m_sProperty
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getStatement() override
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent() override
virtual void SAL_CALL afterLast() override
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
virtual css::uno::Reference< css::ucb::XContentIdentifier > SAL_CALL queryContentIdentifier() override
virtual sal_Bool SAL_CALL isFirst() override
virtual void SAL_CALL release() noexcept override
virtual sal_Int32 SAL_CALL getRow() override
comphelper::OInterfaceContainerHelper4< css::beans::XPropertyChangeListener > m_aRowCountListeners
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
sal_Int32 getLength(std::unique_lock< std::mutex > &rGuard) const
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
css::uno::Type const & get()
OUString aName
Sequence< sal_Int8 > aSeq
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool hasValue()
OUString Name
unsigned char sal_Bool