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