xref: /trunk/main/dbaccess/source/core/api/RowSetCache.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1*2e2212a7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2e2212a7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2e2212a7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2e2212a7SAndrew Rist  * distributed with this work for additional information
6*2e2212a7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2e2212a7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2e2212a7SAndrew Rist  * "License"); you may not use this file except in compliance
9*2e2212a7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2e2212a7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2e2212a7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2e2212a7SAndrew Rist  * software distributed under the License is distributed on an
15*2e2212a7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2e2212a7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2e2212a7SAndrew Rist  * specific language governing permissions and limitations
18*2e2212a7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2e2212a7SAndrew Rist  *************************************************************/
21*2e2212a7SAndrew Rist 
22*2e2212a7SAndrew Rist 
23cdf0e10cSrcweir #ifndef DBACCESS_CORE_API_ROWSETCACHE_HXX
24cdf0e10cSrcweir #define DBACCESS_CORE_API_ROWSETCACHE_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifndef _CONNECTIVITY_COMMONTOOLS_HXX_
27cdf0e10cSrcweir #include <connectivity/CommonTools.hxx>
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XSERVICEINFO_HPP_
30cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XPREPAREDSTATEMENT_HPP_
33cdf0e10cSrcweir #include <com/sun/star/sdbc/XPreparedStatement.hpp>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
36cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYANALYZER_HPP_
39cdf0e10cSrcweir #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYANALYZER_HPP_
42cdf0e10cSrcweir #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
45cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XWARNINGSSUPPLIER_HPP_
48cdf0e10cSrcweir #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XRESULTSETACCESS_HPP_
51cdf0e10cSrcweir #include <com/sun/star/sdb/XResultSetAccess.hpp>
52cdf0e10cSrcweir #endif
53cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
54cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XCOLUMNLOCATE_HPP_
57cdf0e10cSrcweir #include <com/sun/star/sdbc/XColumnLocate.hpp>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XROWLOCATE_HPP_
60cdf0e10cSrcweir #include <com/sun/star/sdbcx/XRowLocate.hpp>
61cdf0e10cSrcweir #endif
62cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XROWUPDATE_HPP_
63cdf0e10cSrcweir #include <com/sun/star/sdbc/XRowUpdate.hpp>
64cdf0e10cSrcweir #endif
65cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XRESULTSETUPDATE_HPP_
66cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XROWSETAPPROVEBROADCASTER_HPP_
69cdf0e10cSrcweir #include <com/sun/star/sdb/XRowSetApproveBroadcaster.hpp>
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_RESULTSETTYPE_HPP_
72cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetType.hpp>
73cdf0e10cSrcweir #endif
74cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XDELETEROWS_HPP_
75cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDeleteRows.hpp>
76cdf0e10cSrcweir #endif
77cdf0e10cSrcweir #ifndef _CPPUHELPER_COMPBASE11_HXX_
78cdf0e10cSrcweir #include <cppuhelper/compbase11.hxx>
79cdf0e10cSrcweir #endif
80cdf0e10cSrcweir #ifndef _COMPHELPER_PROPERTYCONTAINER_HXX_
81cdf0e10cSrcweir #include <comphelper/propertycontainer.hxx>
82cdf0e10cSrcweir #endif
83cdf0e10cSrcweir #ifndef COMPHELPER_COMPONENTCONTEXT_HXX
84cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
85cdf0e10cSrcweir #endif
86cdf0e10cSrcweir #ifndef _CPPUHELPER_IMPLBASE5_HXX_
87cdf0e10cSrcweir #include <cppuhelper/implbase5.hxx>
88cdf0e10cSrcweir #endif
89cdf0e10cSrcweir #ifndef _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
90cdf0e10cSrcweir #include <comphelper/proparrhlp.hxx>
91cdf0e10cSrcweir #endif
92cdf0e10cSrcweir #ifndef DBACCESS_CORE_API_ROWSETROW_HXX
93cdf0e10cSrcweir #include "RowSetRow.hxx"
94cdf0e10cSrcweir #endif
95cdf0e10cSrcweir #ifndef DBACCESS_ROWSETCACHEITERATOR_HXX
96cdf0e10cSrcweir #include "RowSetCacheIterator.hxx"
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir 
99cdf0e10cSrcweir namespace connectivity
100cdf0e10cSrcweir {
101cdf0e10cSrcweir     class OSQLParseNode;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir namespace dbaccess
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     class OCacheSet;
106cdf0e10cSrcweir     class ORowSetDataColumns;
107cdf0e10cSrcweir     class ORowSetCacheClone;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     class ORowSetCache
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         friend class ORowSetBase;
112cdf0e10cSrcweir         friend class ORowSet;
113cdf0e10cSrcweir         friend class ORowSetClone;
114cdf0e10cSrcweir         friend class ORowSetCacheIterator;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         typedef ::std::vector< TORowSetOldRowHelperRef >    TOldRowSetRows;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         ::std::map<sal_Int32,sal_Int32> m_aKeyColumns;
119cdf0e10cSrcweir         //the set can be static, bookmarkable or keyset
120cdf0e10cSrcweir         ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XResultSet>       m_xSet;
121cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData >  m_xMetaData; // must be before m_aInsertRow
122cdf0e10cSrcweir         ::comphelper::ComponentContext                                                  m_aContext;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>                 m_xCacheSet;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         OCacheSet*                      m_pCacheSet;            // is a bookmarkable, keyset or static resultset
127cdf0e10cSrcweir         ORowSetMatrix*                  m_pMatrix;              // represent the table struct
128cdf0e10cSrcweir         ORowSetMatrix::iterator         m_aMatrixIter;          // represent a row of the table
129cdf0e10cSrcweir         ORowSetMatrix::iterator         m_aMatrixEnd;           // present the row behind the last row of the table
130cdf0e10cSrcweir         ORowSetCacheMap                 m_aCacheIterators;
131cdf0e10cSrcweir         TOldRowSetRows                  m_aOldRows;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         ORowSetMatrix*                  m_pInsertMatrix;        // represent the rows which should be inserted normally this is only one
134cdf0e10cSrcweir         ORowSetMatrix::iterator         m_aInsertRow;           // represent a insert row
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         //  ORowSetRow                      m_aInsertRow;           // present the row that should be inserted
137cdf0e10cSrcweir         sal_Int32                       m_nLastColumnIndex;     // the last column ask for, used for wasNull()
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         connectivity::OSQLTable         m_aUpdateTable;         // used for updates/deletes and inserts
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         sal_Int32                   m_nFetchSize;
142cdf0e10cSrcweir         sal_Int32                   m_nRowCount;
143cdf0e10cSrcweir         sal_Int32                   m_nPrivileges;
144cdf0e10cSrcweir         sal_Int32                   m_nPosition;                // 0 means beforefirst
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         sal_Int32                   m_nStartPos;                // start pos of the window zero based
147cdf0e10cSrcweir         sal_Int32                   m_nEndPos;                  // end   pos of the window zero based
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         sal_Bool                    m_bRowCountFinal ;
150cdf0e10cSrcweir         sal_Bool                    m_bBeforeFirst ;
151cdf0e10cSrcweir         sal_Bool                    m_bAfterLast ;
152cdf0e10cSrcweir         sal_Bool                    m_bUpdated ;
153cdf0e10cSrcweir         sal_Bool&                   m_bModified ;           // points to the rowset member m_bModified
154cdf0e10cSrcweir         sal_Bool&                   m_bNew ;                // points to the rowset member m_bNew
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         sal_Bool fill(ORowSetMatrix::iterator& _aIter,const ORowSetMatrix::iterator& _aEnd,sal_Int32& _nPos,sal_Bool _bCheck);
157cdf0e10cSrcweir         sal_Bool reFillMatrix(sal_Int32 _nNewStartPos,sal_Int32 nNewEndPos);
158cdf0e10cSrcweir         sal_Bool fillMatrix(sal_Int32 &_nNewStartPos,sal_Int32 _nNewEndPos);
159cdf0e10cSrcweir         sal_Bool moveWindow();
160cdf0e10cSrcweir         // returns true when a keyset needs to be created.
161cdf0e10cSrcweir         sal_Bool impl_createBookmarkSet_nothrow(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& _xRs);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         void firePropertyChange(sal_Int32 _nColumnIndex,const ::connectivity::ORowSetValue& _rOldValue);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         void rotateCacheIterator(ORowSetMatrix::difference_type _nDist);
166cdf0e10cSrcweir         void updateValue(sal_Int32 columnIndex
167cdf0e10cSrcweir                         ,const connectivity::ORowSetValue& x
168cdf0e10cSrcweir                         ,ORowSetValueVector::Vector& io_aRow
169cdf0e10cSrcweir                         ,::std::vector<sal_Int32>& o_ChangedColumns
170cdf0e10cSrcweir                         );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         void impl_updateRowFromCache_throw(ORowSetValueVector::Vector& io_aRow
173cdf0e10cSrcweir                                    ,::std::vector<sal_Int32>& o_ChangedColumns
174cdf0e10cSrcweir                                    );
175cdf0e10cSrcweir         // checks and set the flags isAfterLast isLast and position when afterlast is true
176cdf0e10cSrcweir         void checkPositionFlags();
177cdf0e10cSrcweir         void checkUpdateConditions(sal_Int32 columnIndex);
178cdf0e10cSrcweir         sal_Bool checkJoin( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
179cdf0e10cSrcweir                             const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
180cdf0e10cSrcweir                             const ::rtl::OUString& _sUpdateTableName);
181cdf0e10cSrcweir         sal_Bool checkInnerJoin(const ::connectivity::OSQLParseNode *pNode
182cdf0e10cSrcweir                             ,const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection
183cdf0e10cSrcweir                             ,const ::rtl::OUString& _sUpdateTableName);
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         // clears the insert row
186cdf0e10cSrcweir         void                    clearInsertRow();
187cdf0e10cSrcweir         ORowSetMatrix::iterator calcPosition() const;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     protected:
getEnd()190cdf0e10cSrcweir         ORowSetMatrix::iterator& getEnd() { return m_aMatrixEnd;}
191cdf0e10cSrcweir         // is called when after a moveToInsertRow a movement (next, etc) was called
192cdf0e10cSrcweir         void cancelRowModification();
193cdf0e10cSrcweir     public:
194cdf0e10cSrcweir         ORowSetCache(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >& _xRs,
195cdf0e10cSrcweir                      const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xAnalyzer,
196cdf0e10cSrcweir                      const ::comphelper::ComponentContext& _rContext,
197cdf0e10cSrcweir                      const ::rtl::OUString& _rUpdateTableName,
198cdf0e10cSrcweir                      sal_Bool&  _bModified,
199cdf0e10cSrcweir                      sal_Bool&  _bNew,
200cdf0e10cSrcweir                      const ORowSetValueVector& _aParameterValueForCache,
201cdf0e10cSrcweir                      const ::rtl::OUString& i_sRowSetFilter,
202cdf0e10cSrcweir                      sal_Int32 i_nMaxRows);
203cdf0e10cSrcweir         ~ORowSetCache();
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         // called from the rowset when a updateXXX was called for the first time
207cdf0e10cSrcweir         void setUpdateIterator(const ORowSetMatrix::iterator& _rOriginalRow);
208cdf0e10cSrcweir         ORowSetCacheIterator createIterator(ORowSetBase* _pRowSet);
209cdf0e10cSrcweir         void deleteIterator(const ORowSetBase* _pRowSet);
210cdf0e10cSrcweir         // sets the size of the matrix
211cdf0e10cSrcweir         void setFetchSize(sal_Int32 _nSize);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         TORowSetOldRowHelperRef registerOldRow();
214cdf0e10cSrcweir         void deregisterOldRow(const TORowSetOldRowHelperRef& _rRow);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     // ::com::sun::star::sdbc::XResultSetMetaDataSupplier
217cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData > getMetaData(  );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     // ::com::sun::star::sdbcx::XRowLocate
220cdf0e10cSrcweir         ::com::sun::star::uno::Any getBookmark(  );
221cdf0e10cSrcweir         sal_Bool moveToBookmark( const ::com::sun::star::uno::Any& bookmark );
222cdf0e10cSrcweir         sal_Bool moveRelativeToBookmark( const ::com::sun::star::uno::Any& bookmark, sal_Int32 rows );
223cdf0e10cSrcweir         sal_Int32 compareBookmarks( const ::com::sun::star::uno::Any& first, const ::com::sun::star::uno::Any& second );
224cdf0e10cSrcweir         sal_Bool hasOrderedBookmarks(  );
225cdf0e10cSrcweir         sal_Int32 hashBookmark( const ::com::sun::star::uno::Any& bookmark );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     // ::com::sun::star::sdbc::XRowUpdate
228cdf0e10cSrcweir         void updateCharacterStream( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length,ORowSetValueVector::Vector& io_aRow,::std::vector<sal_Int32>& o_ChangedColumns
229cdf0e10cSrcweir              );
230cdf0e10cSrcweir         void updateObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Any& x,ORowSetValueVector::Vector& io_aRow ,::std::vector<sal_Int32>& o_ChangedColumns);
231cdf0e10cSrcweir         void updateNumericObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Any& x, sal_Int32 scale,ORowSetValueVector::Vector& io_aRow ,::std::vector<sal_Int32>& o_ChangedColumns);
232cdf0e10cSrcweir         void updateNull(sal_Int32 columnIndex
233cdf0e10cSrcweir                         ,ORowSetValueVector::Vector& io_aRow
234cdf0e10cSrcweir                         ,::std::vector<sal_Int32>& o_ChangedColumns
235cdf0e10cSrcweir                         );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     // ::com::sun::star::sdbc::XResultSet
238cdf0e10cSrcweir         sal_Bool next(  );
239cdf0e10cSrcweir         sal_Bool isBeforeFirst(  );
240cdf0e10cSrcweir         sal_Bool isAfterLast(  );
241cdf0e10cSrcweir         sal_Bool isFirst(  );
242cdf0e10cSrcweir         sal_Bool isLast(  );
243cdf0e10cSrcweir         sal_Bool beforeFirst(  );
244cdf0e10cSrcweir         sal_Bool afterLast(  );
245cdf0e10cSrcweir         sal_Bool first(  );
246cdf0e10cSrcweir         sal_Bool last(  );
247cdf0e10cSrcweir         sal_Int32 getRow(  );
248cdf0e10cSrcweir         sal_Bool absolute( sal_Int32 row );
249cdf0e10cSrcweir         sal_Bool relative( sal_Int32 rows );
250cdf0e10cSrcweir         sal_Bool previous(  );
251cdf0e10cSrcweir         void refreshRow(  );
252cdf0e10cSrcweir         sal_Bool rowUpdated(  );
253cdf0e10cSrcweir         sal_Bool rowInserted(  );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     // ::com::sun::star::sdbc::XResultSetUpdate
256cdf0e10cSrcweir         sal_Bool insertRow(::std::vector< ::com::sun::star::uno::Any >& o_aBookmarks);
257cdf0e10cSrcweir         void resetInsertRow(sal_Bool _bClearInsertRow);
258cdf0e10cSrcweir 
259cdf0e10cSrcweir         void updateRow( ORowSetMatrix::iterator& _rUpdateRow,::std::vector< ::com::sun::star::uno::Any >& o_aBookmarks );
260cdf0e10cSrcweir         bool deleteRow();
261cdf0e10cSrcweir         void cancelRowUpdates(  );
262cdf0e10cSrcweir         void moveToInsertRow(  );
263cdf0e10cSrcweir 
getKeyColumns() const264cdf0e10cSrcweir         const ::std::map<sal_Int32,sal_Int32>& getKeyColumns() const { return m_aKeyColumns; }
265cdf0e10cSrcweir         bool isResultSetChanged() const;
266cdf0e10cSrcweir         void reset(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet);
267cdf0e10cSrcweir     };
268cdf0e10cSrcweir }
269cdf0e10cSrcweir #endif
270