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