1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ucbhelper/contentidentifier.hxx>
21 #include <com/sun/star/sdbc/SQLException.hpp>
22 #include <com/sun/star/uno/Reference.h>
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <ucbhelper/resultsetmetadata.hxx>
25 #include <cppuhelper/queryinterface.hxx>
26 #include "ftpresultsetbase.hxx"
29 using namespace com::sun::star
;
31 ResultSetBase::ResultSetBase(
32 const uno::Reference
< uno::XComponentContext
>& rxContext
,
33 const uno::Reference
< ucb::XContentProvider
>& xProvider
,
34 const uno::Sequence
< beans::Property
>& seq
)
35 : m_xContext( rxContext
),
36 m_xProvider( xProvider
),
43 ResultSetBase::~ResultSetBase()
51 ResultSetBase::acquire()
54 OWeakObject::acquire();
59 ResultSetBase::release()
62 OWeakObject::release();
67 ResultSetBase::queryInterface( const uno::Type
& rType
)
69 uno::Any aRet
= cppu::queryInterface(
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
);
85 ResultSetBase::addEventListener(
86 const uno::Reference
< lang::XEventListener
>& Listener
)
88 osl::MutexGuard
aGuard( m_aMutex
);
90 if ( ! m_pDisposeEventListeners
)
91 m_pDisposeEventListeners
.reset(
92 new comphelper::OInterfaceContainerHelper2( m_aMutex
) );
94 m_pDisposeEventListeners
->addInterface( Listener
);
99 ResultSetBase::removeEventListener(
100 const uno::Reference
< lang::XEventListener
>& Listener
)
102 osl::MutexGuard
aGuard( m_aMutex
);
104 if ( m_pDisposeEventListeners
)
105 m_pDisposeEventListeners
->removeInterface( Listener
);
110 ResultSetBase::dispose()
112 osl::MutexGuard
aGuard( m_aMutex
);
114 lang::EventObject aEvt
;
115 aEvt
.Source
= static_cast< lang::XComponent
* >( this );
117 if ( m_pDisposeEventListeners
&& m_pDisposeEventListeners
->getLength() )
119 m_pDisposeEventListeners
->disposeAndClear( aEvt
);
121 if( m_pRowCountListeners
&& m_pRowCountListeners
->getLength() )
123 m_pRowCountListeners
->disposeAndClear( aEvt
);
125 if( m_pIsFinalListeners
&& m_pIsFinalListeners
->getLength() )
127 m_pIsFinalListeners
->disposeAndClear( aEvt
);
135 ResultSetBase::next()
138 return m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
143 ResultSetBase::isBeforeFirst()
150 ResultSetBase::isAfterLast()
152 return m_nRow
>= sal::static_int_cast
<sal_Int32
>(m_aItems
.size()); // Cannot happen, if m_aFolder.isOpen()
157 ResultSetBase::isFirst()
164 ResultSetBase::isLast()
166 if( m_nRow
== sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) - 1 )
174 ResultSetBase::beforeFirst()
181 ResultSetBase::afterLast()
183 m_nRow
= m_aItems
.size();
188 ResultSetBase::first()
196 ResultSetBase::last()
198 m_nRow
= m_aItems
.size() - 1;
204 ResultSetBase::getRow()
206 // Test, whether behind last row
207 if( -1 == m_nRow
|| m_nRow
>= sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
214 sal_Bool SAL_CALL
ResultSetBase::absolute( sal_Int32 row
)
221 m_nRow
+= ( row
+ 1 );
226 return 0<= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
231 ResultSetBase::relative( sal_Int32 row
)
233 if( isAfterLast() || isBeforeFirst() )
234 throw sdbc::SQLException();
240 while( row
++ && m_nRow
> - 1 )
243 return 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
248 ResultSetBase::previous()
250 if( m_nRow
> sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
251 m_nRow
= m_aItems
.size(); // Correct Handling of afterLast
252 if( 0 <= m_nRow
) -- m_nRow
;
254 return 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size());
259 ResultSetBase::refreshRow()
265 ResultSetBase::rowUpdated()
271 ResultSetBase::rowInserted()
277 ResultSetBase::rowDeleted()
283 uno::Reference
< uno::XInterface
> SAL_CALL
284 ResultSetBase::getStatement()
286 return uno::Reference
< uno::XInterface
>();
293 ResultSetBase::close()
299 ResultSetBase::queryContentIdentifierString()
301 if( 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
302 return m_aPath
[m_nRow
];
308 uno::Reference
< ucb::XContentIdentifier
> SAL_CALL
309 ResultSetBase::queryContentIdentifier()
311 if( 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
313 if(!m_aIdents
[m_nRow
].is()) {
314 OUString url
= queryContentIdentifierString();
317 uno::Reference
< ucb::XContentIdentifier
>(
318 new ::ucbhelper::ContentIdentifier(url
) );
320 return m_aIdents
[m_nRow
];
323 return uno::Reference
<ucb::XContentIdentifier
>();
327 uno::Reference
< ucb::XContent
> SAL_CALL
328 ResultSetBase::queryContent()
330 if( 0 <= m_nRow
&& m_nRow
< sal::static_int_cast
<sal_Int32
>(m_aItems
.size()) )
331 return m_xProvider
->queryContent(queryContentIdentifier());
333 return uno::Reference
< ucb::XContent
>();
338 class XPropertySetInfoImpl
339 : public cppu::OWeakObject
,
340 public beans::XPropertySetInfo
344 explicit XPropertySetInfoImpl( const uno::Sequence
< beans::Property
>& aSeq
)
349 void SAL_CALL
acquire()
352 OWeakObject::acquire();
356 void SAL_CALL
release()
359 OWeakObject::release();
362 uno::Any SAL_CALL
queryInterface( const uno::Type
& rType
) override
364 uno::Any aRet
= cppu::queryInterface(
366 static_cast< beans::XPropertySetInfo
* >(this) );
367 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
370 uno::Sequence
< beans::Property
> SAL_CALL
getProperties() override
375 beans::Property SAL_CALL
getPropertyByName( const OUString
& aName
) override
377 auto pProp
= std::find_if(m_aSeq
.begin(), m_aSeq
.end(),
378 [&aName
](const beans::Property
& rProp
) { return aName
== rProp
.Name
; });
379 if (pProp
!= m_aSeq
.end())
381 throw beans::UnknownPropertyException(aName
);
384 sal_Bool SAL_CALL
hasPropertyByName( const OUString
& Name
) override
386 return std::any_of(m_aSeq
.begin(), m_aSeq
.end(),
387 [&Name
](const beans::Property
& rProp
) { return Name
== rProp
.Name
; });
392 uno::Sequence
< beans::Property
> m_aSeq
;
398 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
399 ResultSetBase::getPropertySetInfo()
401 uno::Sequence
< beans::Property
> seq(2);
402 seq
[0].Name
= "RowCount";
404 seq
[0].Type
= cppu::UnoType
<sal_Int32
>::get();
405 seq
[0].Attributes
= beans::PropertyAttribute::READONLY
;
407 seq
[1].Name
= "IsRowCountFinal";
409 seq
[1].Type
= cppu::UnoType
<sal_Bool
>::get();
410 seq
[1].Attributes
= beans::PropertyAttribute::READONLY
;
413 return uno::Reference
< beans::XPropertySetInfo
> (
414 new XPropertySetInfoImpl( seq
) );
418 void SAL_CALL
ResultSetBase::setPropertyValue(
419 const OUString
& aPropertyName
, const uno::Any
& /*aValue*/ )
421 if( aPropertyName
== "IsRowCountFinal" ||
422 aPropertyName
== "RowCount" )
425 throw beans::UnknownPropertyException(aPropertyName
);
429 uno::Any SAL_CALL
ResultSetBase::getPropertyValue(
430 const OUString
& PropertyName
)
432 if( PropertyName
== "IsRowCountFinal" )
434 return uno::Any(true);
436 else if ( PropertyName
== "RowCount" )
438 sal_Int32 count
= m_aItems
.size();
439 return uno::Any(count
);
442 throw beans::UnknownPropertyException(PropertyName
);
446 void SAL_CALL
ResultSetBase::addPropertyChangeListener(
447 const OUString
& aPropertyName
,
448 const uno::Reference
< beans::XPropertyChangeListener
>& xListener
)
450 if( aPropertyName
== "IsRowCountFinal" )
452 osl::MutexGuard
aGuard( m_aMutex
);
453 if ( ! m_pIsFinalListeners
)
454 m_pIsFinalListeners
.reset(
455 new comphelper::OInterfaceContainerHelper2( m_aMutex
) );
457 m_pIsFinalListeners
->addInterface( xListener
);
459 else if ( aPropertyName
== "RowCount" )
461 osl::MutexGuard
aGuard( m_aMutex
);
462 if ( ! m_pRowCountListeners
)
463 m_pRowCountListeners
.reset(
464 new comphelper::OInterfaceContainerHelper2( m_aMutex
) );
465 m_pRowCountListeners
->addInterface( xListener
);
468 throw beans::UnknownPropertyException(aPropertyName
);
472 void SAL_CALL
ResultSetBase::removePropertyChangeListener(
473 const OUString
& aPropertyName
,
474 const uno::Reference
< beans::XPropertyChangeListener
>& aListener
)
476 if( aPropertyName
== "IsRowCountFinal" &&
477 m_pIsFinalListeners
)
479 osl::MutexGuard
aGuard( m_aMutex
);
480 m_pIsFinalListeners
->removeInterface( aListener
);
482 else if ( aPropertyName
== "RowCount" &&
483 m_pRowCountListeners
)
485 osl::MutexGuard
aGuard( m_aMutex
);
486 m_pRowCountListeners
->removeInterface( aListener
);
489 throw beans::UnknownPropertyException(aPropertyName
);
493 void SAL_CALL
ResultSetBase::addVetoableChangeListener(
494 const OUString
& /*PropertyName*/,
495 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
500 void SAL_CALL
ResultSetBase::removeVetoableChangeListener(
501 const OUString
& /*PropertyName*/,
502 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
507 // XResultSetMetaDataSupplier
508 uno::Reference
< sdbc::XResultSetMetaData
> SAL_CALL
509 ResultSetBase::getMetaData()
511 ::ucbhelper::ResultSetMetaData
* p
=
512 new ::ucbhelper::ResultSetMetaData( m_xContext
, m_sProperty
);
513 return uno::Reference
< sdbc::XResultSetMetaData
>( p
);
517 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */