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 .
22 #include <cppuhelper/implbase10.hxx>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdbc/XRow.hpp>
25 #include <com/sun/star/sdbc/XColumnLocate.hpp>
26 #include <com/sun/star/sdbc/XCloseable.hpp>
27 #include <com/sun/star/sdbcx/XRowLocate.hpp>
28 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
29 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XUnoTunnel.hpp>
32 #include <cppuhelper/interfacecontainer.h>
33 #include <connectivity/sqlerror.hxx>
34 #include <connectivity/CommonTools.hxx>
35 #include <comphelper/propertystatecontainer.hxx>
36 #include <comphelper/proparrhlp.hxx>
37 #include <com/sun/star/sdbc/XRowSet.hpp>
38 #include <com/sun/star/util/XNumberFormatTypes.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
40 #include "RowSetRow.hxx"
41 #include "RowSetCacheIterator.hxx"
45 namespace com::sun::star
{
46 namespace sdb
{ struct RowChangeEvent
; }
47 namespace lang
{ struct Locale
; }
52 class OEmptyCollection
;
54 typedef ::cppu::ImplHelper10
< css::sdbcx::XRowLocate
,
56 css::sdbc::XResultSetMetaDataSupplier
,
57 css::sdbc::XWarningsSupplier
,
58 css::sdbc::XColumnLocate
,
59 css::sdbcx::XColumnsSupplier
,
60 css::lang::XServiceInfo
,
62 css::sdbc::XCloseable
,
63 css::lang::XUnoTunnel
> ORowSetBase_BASE
;
66 class ORowSetDataColumns
;
67 class ORowSetCacheIterator
;
68 class ORowSetDataColumn
;
69 class ORowSetBase
: public ORowSetBase_BASE
,
70 public ::comphelper::OPropertyStateContainer
,
71 public ::comphelper::OPropertyArrayUsageHelper
<ORowSetBase
> // this class hold the static property info
74 typedef std::vector
<ORowSetDataColumn
*> TDataColumns
;
75 ::osl::Mutex
* m_pMutex
; // this is the mutex from the rowset itself
76 ::osl::Mutex
// we need an extra mutex for columns to prevent deadlock when setting new values
80 css::uno::Any m_aBookmark
;
81 ORowSetCacheIterator m_aCurrentRow
; // contains the actual fetched row
82 TORowSetOldRowHelperRef m_aOldRow
;
83 TDataColumns m_aDataColumns
; // holds the columns as m_pColumns but know the implementation class
84 connectivity::ORowSetValue m_aEmptyValue
; // only for error case
86 ::cppu::OWeakObject
* m_pMySelf
; // set by derived classes
87 std::shared_ptr
<ORowSetCache
> m_pCache
; // the cache is used by the rowset and his clone (shared)
88 std::unique_ptr
<ORowSetDataColumns
> m_pColumns
; // represent the select columns
89 ::cppu::OBroadcastHelper
& m_rBHelper
; // must be set from the derived classes
90 // is used when the formatkey for database types is set
91 css::uno::Reference
< css::util::XNumberFormatTypes
> m_xNumberFormatTypes
;
92 std::unique_ptr
<OEmptyCollection
> m_pEmptyCollection
;
94 css::uno::Reference
< css::uno::XComponentContext
> m_aContext
;
95 ::connectivity::SQLError m_aErrors
;
97 sal_Int32 m_nLastColumnIndex
; // the last column ask for, used for wasNull()
98 sal_Int32 m_nDeletedPosition
; // is set only when a row was deleted
99 sal_Int32 m_nResultSetType
; // fetch property
100 sal_Int32 m_nResultSetConcurrency
;
101 bool m_bClone
; // I'm clone or not
102 bool m_bIgnoreResult
;
103 bool m_bBeforeFirst
: 1;
104 bool m_bAfterLast
: 1;
105 bool m_bIsInsertRow
: 1;
109 const css::uno::Reference
<css::uno::XComponentContext
>& _rContext
,
110 ::cppu::OBroadcastHelper
& _rBHelper
,
111 ::osl::Mutex
* _pMutex
114 // fire a notification for all that are listening on column::VALUE property
115 void firePropertyChange(const ORowSetRow
& _rOldRow
);
116 // fire a change for one column
117 // _nPos starts at zero
118 void firePropertyChange(sal_Int32 _nPos
,const ::connectivity::ORowSetValue
& _rNewValue
);
120 // fire if rowcount changed
121 virtual void fireRowcount();
122 // notify row changed
123 virtual bool notifyAllListenersCursorBeforeMove(::osl::ResettableMutexGuard
& _rGuard
);
124 // notify cursor moved
125 virtual void notifyAllListenersCursorMoved(::osl::ResettableMutexGuard
& _rGuard
);
127 // cancel the insertion, if necessary (means if we're on the insert row)
128 virtual void doCancelModification( ) = 0;
129 // return <TRUE/> if and only if we're using the insert row (means: we're updating _or_ inserting)
130 virtual bool isModification( ) = 0;
131 // return <TRUE/> if and only if the current row is modified
132 // TODO: isn't this the same as isModification?
133 virtual bool isModified( ) = 0;
134 // return <TRUE/> if and only if the current row is the insert row
135 virtual bool isNew( ) = 0;
136 // return <TRUE/> if the property change notification should be fired
137 // upon property change.
138 virtual bool isPropertyChangeNotificationEnabled() const;
139 // notify the change of a boolean property
140 void fireProperty( sal_Int32 _nProperty
, bool _bNew
, bool _bOld
);
142 // OPropertyStateContainer
143 virtual void getPropertyDefaultByHandle( sal_Int32 _nHandle
, css::uno::Any
& _rDefault
) const override
;
144 virtual void SAL_CALL
getFastPropertyValue(css::uno::Any
& rValue
,sal_Int32 nHandle
) const override
;
146 enum CursorMoveDirection
148 /// denotes a cursor move forward
150 /// denotes a cursor move backwards
152 /// denotes no cursor move at all, but move cache to current row (if it is not there already)
154 /// denotes no cursor move at all, but force the cache to move to current row (and refresh the row)
157 /** positions the cache in preparation of a cursor move
159 Normally, the cache is simply moved to our bookmark (m_aBookmark). If however the current
160 row is deleted, then the cache is properly positioned for a following cursor movement in the
163 @param _ePrepareForDirection
164 the direction into which the cursor should be moved after the call. If we're currently not on
165 a deleted row, this parameter is ignored, since in this case the cache is simply moved to
167 If, however, we're currently on a deleted row, this is used to properly position the cache
168 using <member>m_nDeletedPosition</member>.<br/>
169 In this case, MOVE_NONE(_REFRESH) is not supported. This is because the deleted row
170 (to which the RowSet currently points to) is not present in the cache. So, you cannot move the
173 void positionCache( CursorMoveDirection _ePrepareForDirection
);
175 // returns a value of a column of the current row
176 const connectivity::ORowSetValue
& getValue(sal_Int32 columnIndex
);
177 // the cache has to be checked before calling this method
178 const connectivity::ORowSetValue
& impl_getValue(sal_Int32 columnIndex
);
179 // sets the current and the bookmark
180 void setCurrentRow( bool _bMoved
, bool _bDoNotify
, const ORowSetRow
& _rOldValues
, ::osl::ResettableMutexGuard
& _rGuard
);
181 /// @throws css::sdbc::SQLException
182 /// @throws css::uno::RuntimeException
183 void checkPositioningAllowed();
184 // checks if the cache is null
186 // sets the bookmark to Any()
187 // m_aCurrentRow to end of matrix
189 void movementFailed();
191 ORowSetRow
getOldRow(bool _bWasNew
);
192 /** move the cache the position defined by the member functor
193 @param _aCheckFunctor
194 Return <TRUE/> when we already stand on the row we want to.
195 @param _aMovementFunctor
196 The method used to move.
198 <TRUE/> if movement was successful.
200 bool SAL_CALL
move( std::function
<bool(ORowSetBase
*)> const & _aCheckFunctor
,
201 std::function
<bool(ORowSetCache
*)> const & _aMovementFunctor
);
203 /** same meaning as isFirst. Only need by mem_fun
208 /** same meaning as isLast. Only need by mem_fun
214 /** returns the current row count
216 This function takes into account that we might actually be positioned on a
217 deleted row, so that m_pCache->m_nRowCount does not really reflect the actual
223 sal_Int32
impl_getRowCount() const;
225 // the checkCache has to be called before calling this methods
226 sal_Int32
impl_getRow();
227 bool impl_rowDeleted();
230 virtual ~ORowSetBase() override
;
233 virtual void SAL_CALL
disposing();
235 // css::beans::XPropertySet
236 virtual css::uno::Reference
< css::beans::XPropertySetInfo
> SAL_CALL
getPropertySetInfo( ) override
238 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
241 // comphelper::OPropertyArrayUsageHelper
242 virtual ::cppu::IPropertyArrayHelper
* createArrayHelper( ) const override
;
244 // cppu::OPropertySetHelper
245 virtual ::cppu::IPropertyArrayHelper
& SAL_CALL
getInfoHelper() override
;
247 // css::lang::XTypeProvider
248 virtual css::uno::Sequence
< css::uno::Type
> SAL_CALL
getTypes() override
;
250 // css::uno::XInterface
251 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& rType
) override
;
253 // css::sdbc::XWarningsSupplier
254 virtual css::uno::Any SAL_CALL
getWarnings( ) override
;
255 virtual void SAL_CALL
clearWarnings( ) override
;
257 // css::sdbc::XResultSetMetaDataSupplier
258 virtual css::uno::Reference
< css::sdbc::XResultSetMetaData
> SAL_CALL
getMetaData( ) override
;
260 // css::sdbc::XColumnLocate
261 virtual sal_Int32 SAL_CALL
findColumn( const OUString
& columnName
) override
;
263 // css::sdbcx::XColumnsSupplier
264 virtual css::uno::Reference
< css::container::XNameAccess
> SAL_CALL
getColumns( ) override
;
267 virtual sal_Bool SAL_CALL
wasNull( ) override
;
268 virtual OUString SAL_CALL
getString( sal_Int32 columnIndex
) override
;
269 virtual sal_Bool SAL_CALL
getBoolean( sal_Int32 columnIndex
) override
;
270 virtual sal_Int8 SAL_CALL
getByte( sal_Int32 columnIndex
) override
;
271 virtual sal_Int16 SAL_CALL
getShort( sal_Int32 columnIndex
) override
;
272 virtual sal_Int32 SAL_CALL
getInt( sal_Int32 columnIndex
) override
;
273 virtual sal_Int64 SAL_CALL
getLong( sal_Int32 columnIndex
) override
;
274 virtual float SAL_CALL
getFloat( sal_Int32 columnIndex
) override
;
275 virtual double SAL_CALL
getDouble( sal_Int32 columnIndex
) override
;
276 virtual css::uno::Sequence
< sal_Int8
> SAL_CALL
getBytes( sal_Int32 columnIndex
) override
;
277 virtual css::util::Date SAL_CALL
getDate( sal_Int32 columnIndex
) override
;
278 virtual css::util::Time SAL_CALL
getTime( sal_Int32 columnIndex
) override
;
279 virtual css::util::DateTime SAL_CALL
getTimestamp( sal_Int32 columnIndex
) override
;
280 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getBinaryStream( sal_Int32 columnIndex
) override
;
281 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getCharacterStream( sal_Int32 columnIndex
) override
;
282 virtual css::uno::Any SAL_CALL
getObject( sal_Int32 columnIndex
, const css::uno::Reference
< css::container::XNameAccess
>& typeMap
) override
;
283 virtual css::uno::Reference
< css::sdbc::XRef
> SAL_CALL
getRef( sal_Int32 columnIndex
) override
;
284 virtual css::uno::Reference
< css::sdbc::XBlob
> SAL_CALL
getBlob( sal_Int32 columnIndex
) override
;
285 virtual css::uno::Reference
< css::sdbc::XClob
> SAL_CALL
getClob( sal_Int32 columnIndex
) override
;
286 virtual css::uno::Reference
< css::sdbc::XArray
> SAL_CALL
getArray( sal_Int32 columnIndex
) override
;
288 // css::sdbcx::XRowLocate
289 virtual css::uno::Any SAL_CALL
getBookmark( ) override
;
290 virtual sal_Bool SAL_CALL
moveToBookmark( const css::uno::Any
& bookmark
) override
;
291 virtual sal_Bool SAL_CALL
moveRelativeToBookmark( const css::uno::Any
& bookmark
, sal_Int32 rows
) override
;
292 virtual sal_Int32 SAL_CALL
compareBookmarks( const css::uno::Any
& first
, const css::uno::Any
& second
) override
;
293 virtual sal_Bool SAL_CALL
hasOrderedBookmarks( ) override
;
294 virtual sal_Int32 SAL_CALL
hashBookmark( const css::uno::Any
& bookmark
) override
;
296 // css::sdbc::XResultSet
297 virtual sal_Bool SAL_CALL
next( ) override
;
298 virtual sal_Bool SAL_CALL
isBeforeFirst( ) override
;
299 virtual sal_Bool SAL_CALL
isAfterLast( ) override
;
300 virtual sal_Bool SAL_CALL
isFirst( ) override
;
301 virtual sal_Bool SAL_CALL
isLast( ) override
;
302 virtual void SAL_CALL
beforeFirst( ) override
;
303 virtual void SAL_CALL
afterLast( ) override
;
304 virtual sal_Bool SAL_CALL
first( ) override
;
305 virtual sal_Bool SAL_CALL
last( ) override
;
306 virtual sal_Int32 SAL_CALL
getRow( ) override
;
307 virtual sal_Bool SAL_CALL
absolute( sal_Int32 row
) override
;
308 virtual sal_Bool SAL_CALL
relative( sal_Int32 rows
) override
;
309 virtual sal_Bool SAL_CALL
previous( ) override
;
310 virtual void SAL_CALL
refreshRow( ) override
;
311 virtual sal_Bool SAL_CALL
rowUpdated( ) override
;
312 virtual sal_Bool SAL_CALL
rowInserted( ) override
;
313 virtual sal_Bool SAL_CALL
rowDeleted( ) override
;
314 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
getStatement( ) override
;
316 // css::sdbc::XRowSet
317 virtual void SAL_CALL
execute( ) override
= 0;
318 virtual void SAL_CALL
addRowSetListener( const css::uno::Reference
< css::sdbc::XRowSetListener
>& listener
) override
= 0;
319 virtual void SAL_CALL
removeRowSetListener( const css::uno::Reference
< css::sdbc::XRowSetListener
>& listener
) override
= 0;
321 // is called when the rowset is going to delete this bookmark _rBookmark
322 void onDeleteRow( const css::uno::Any
& _rBookmark
);
323 // is called when the rowset has deleted this bookmark _rBookmark
324 void onDeletedRow( const css::uno::Any
& _rBookmark
, sal_Int32 _nPos
);
326 // granular access control
327 struct GrantNotifierAccess
{ friend class ORowSetNotifier
; private: GrantNotifierAccess () { } };
329 // cancel the insertion, if necessary (means if we're on the insert row)
330 void doCancelModification( const GrantNotifierAccess
& ) { doCancelModification(); }
331 bool isModification( const GrantNotifierAccess
& ) { return isModification(); }
332 bool isModified( const GrantNotifierAccess
& ) { return isModified(); }
333 bool isNew( const GrantNotifierAccess
& ) { return isNew(); }
334 bool isInsertRow() const { return m_bIsInsertRow
; } // isNew() || isModified(); }
335 void fireProperty( sal_Int32 _nProperty
, bool _bNew
, bool _bOld
, const GrantNotifierAccess
& )
337 fireProperty( _nProperty
, _bNew
, _bOld
);
339 void firePropertyChange(sal_Int32 _nPos
,const ::connectivity::ORowSetValue
& _rNewValue
, const GrantNotifierAccess
& )
341 firePropertyChange(_nPos
,_rNewValue
);
343 using ::comphelper::OPropertyStateContainer::getFastPropertyValue
;
346 /** eases the handling of the doCancelModification and notifyCancelInsert methods
348 <p>The class can only be used on the stack, within a method of ORowSetBase (or derivees)</p>
350 struct ORowSetNotifierImpl
;
351 class ORowSetNotifier
354 std::unique_ptr
<ORowSetNotifierImpl
> m_pImpl
;
355 ORowSetBase
* m_pRowSet
;
356 // not acquired! This is not necessary because this class here is to be used on the stack within
357 // a method of ORowSetBase (or derivees)
362 /** constructs the object, and cancels the insertion
364 @see ORowSetBase::doCancelModification
366 explicit ORowSetNotifier( ORowSetBase
* m_pRowSet
);
368 /** use this one to construct a vector for change value notification
370 ORowSetNotifier( ORowSetBase
* m_pRowSet
,const ORowSetValueVector::Vector
& i_aRow
);
372 // destructs the object. <member>fire</member> has to be called before.
375 /** notifies the insertion
377 <p>This has <em>not</em> been put into the destructor by intention!<br/>
379 The destructor is called during stack unwinding in case of an exception, so if we would do
380 listener notification there, this would have the potential of another exception during stack
381 unwinding, which would terminate the application.</p>
383 @see ORowSetBase::notifyCancelInsert
387 /** notifies value change events and notifies IsModified
388 @param i_aChangedColumns the index of the changed value columns
389 @param i_aRow the old values
390 @see ORowSetBase::notifyCancelInsert
392 void firePropertyChange();
394 /** use this one to store the inde of the changed column values
396 std::vector
<sal_Int32
>& getChangedColumns() const;
400 } // end of namespace
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */