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
403 { "RowCount", -1, cppu::UnoType
<sal_Int32
>::get(), beans::PropertyAttribute::READONLY
},
404 { "IsRowCountFinal", -1, cppu::UnoType
<sal_Bool
>::get(), beans::PropertyAttribute::READONLY
}
407 return uno::Reference
< beans::XPropertySetInfo
> (
408 new XPropertySetInfoImpl( seq
) );
412 void SAL_CALL
ResultSetBase::setPropertyValue(
413 const OUString
& aPropertyName
, const uno::Any
& /*aValue*/ )
415 if( aPropertyName
== "IsRowCountFinal" ||
416 aPropertyName
== "RowCount" )
419 throw beans::UnknownPropertyException(aPropertyName
);
423 uno::Any SAL_CALL
ResultSetBase::getPropertyValue(
424 const OUString
& PropertyName
)
426 if( PropertyName
== "IsRowCountFinal" )
428 return uno::Any(true);
430 else if ( PropertyName
== "RowCount" )
432 sal_Int32 count
= m_aItems
.size();
433 return uno::Any(count
);
436 throw beans::UnknownPropertyException(PropertyName
);
440 void SAL_CALL
ResultSetBase::addPropertyChangeListener(
441 const OUString
& aPropertyName
,
442 const uno::Reference
< beans::XPropertyChangeListener
>& xListener
)
444 if( aPropertyName
== "IsRowCountFinal" )
446 osl::MutexGuard
aGuard( m_aMutex
);
447 if ( ! m_pIsFinalListeners
)
448 m_pIsFinalListeners
.reset(
449 new comphelper::OInterfaceContainerHelper2( m_aMutex
) );
451 m_pIsFinalListeners
->addInterface( xListener
);
453 else if ( aPropertyName
== "RowCount" )
455 osl::MutexGuard
aGuard( m_aMutex
);
456 if ( ! m_pRowCountListeners
)
457 m_pRowCountListeners
.reset(
458 new comphelper::OInterfaceContainerHelper2( m_aMutex
) );
459 m_pRowCountListeners
->addInterface( xListener
);
462 throw beans::UnknownPropertyException(aPropertyName
);
466 void SAL_CALL
ResultSetBase::removePropertyChangeListener(
467 const OUString
& aPropertyName
,
468 const uno::Reference
< beans::XPropertyChangeListener
>& aListener
)
470 if( aPropertyName
== "IsRowCountFinal" &&
471 m_pIsFinalListeners
)
473 osl::MutexGuard
aGuard( m_aMutex
);
474 m_pIsFinalListeners
->removeInterface( aListener
);
476 else if ( aPropertyName
== "RowCount" &&
477 m_pRowCountListeners
)
479 osl::MutexGuard
aGuard( m_aMutex
);
480 m_pRowCountListeners
->removeInterface( aListener
);
483 throw beans::UnknownPropertyException(aPropertyName
);
487 void SAL_CALL
ResultSetBase::addVetoableChangeListener(
488 const OUString
& /*PropertyName*/,
489 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
494 void SAL_CALL
ResultSetBase::removeVetoableChangeListener(
495 const OUString
& /*PropertyName*/,
496 const uno::Reference
< beans::XVetoableChangeListener
>& /*aListener*/ )
501 // XResultSetMetaDataSupplier
502 uno::Reference
< sdbc::XResultSetMetaData
> SAL_CALL
503 ResultSetBase::getMetaData()
505 return new ::ucbhelper::ResultSetMetaData( m_xContext
, m_sProperty
);
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */