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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBACCESS_CORE_API_STATICSET_HXX
28 #include "StaticSet.hxx"
29 #endif
30 #ifndef _COM_SUN_STAR_SDBCX_COMPAREBOOKMARK_HPP_
31 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
32 #endif
33 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #endif
36 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
37 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
38 #endif
39 #ifndef _COM_SUN_STAR_SDBC_XPREPAREDSTATEMENT_HPP_
40 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
41 #endif
42 #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
43 #include "dbastrings.hrc"
44 #endif
45 #ifndef _DBASHARED_APITOOLS_HXX_
46 #include "apitools.hxx"
47 #endif
48 #ifndef _CONNECTIVITY_COMMONTOOLS_HXX_
49 #include <connectivity/CommonTools.hxx>
50 #endif
51 #ifndef _COMPHELPER_TYPES_HXX_
52 #include <comphelper/types.hxx>
53 #endif
54 #include <rtl/logfile.hxx>
55 
56 using namespace dbaccess;
57 using namespace connectivity;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::sdbc;
61 //	using namespace ::com::sun::star::sdb;
62 using namespace ::com::sun::star::sdbcx;
63 using namespace ::com::sun::star::container;
64 using namespace ::com::sun::star::lang;
65 //	using namespace ::cppu;
66 using namespace ::osl;
67 
68 // -------------------------------------------------------------------------
fillValueRow(ORowSetRow & _rRow,sal_Int32)69 void OStaticSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 /*_nPosition*/)
70 {
71     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillValueRow" );
72 	_rRow = *m_aSetIter;
73 }
74 // -------------------------------------------------------------------------
75 // ::com::sun::star::sdbcx::XRowLocate
getBookmark()76 Any SAL_CALL OStaticSet::getBookmark() throw(SQLException, RuntimeException)
77 {
78     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getBookmark" );
79 	return makeAny(getRow());
80 }
81 // -------------------------------------------------------------------------
moveToBookmark(const Any & bookmark)82 sal_Bool SAL_CALL OStaticSet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
83 {
84     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToBookmark" );
85 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
86 	return absolute(::comphelper::getINT32(bookmark));
87 }
88 // -------------------------------------------------------------------------
moveRelativeToBookmark(const Any & bookmark,sal_Int32 rows)89 sal_Bool SAL_CALL OStaticSet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
90 {
91     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveRelativeToBookmark" );
92 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
93 	return absolute(::comphelper::getINT32(bookmark)+rows);
94 }
95 // -------------------------------------------------------------------------
compareBookmarks(const Any & _first,const Any & _second)96 sal_Int32 SAL_CALL OStaticSet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
97 {
98     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::compareBookmarks" );
99 	sal_Int32 nFirst = 0, nSecond = 0;
100 	_first >>= nFirst;
101 	_second >>= nSecond;
102 	return (nFirst < nSecond) ? CompareBookmark::LESS : ((nFirst > nSecond) ? CompareBookmark::GREATER : CompareBookmark::EQUAL);
103 }
104 // -------------------------------------------------------------------------
hasOrderedBookmarks()105 sal_Bool SAL_CALL OStaticSet::hasOrderedBookmarks(  ) throw(SQLException, RuntimeException)
106 {
107     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hasOrderedBookmarks" );
108 	return sal_True;
109 }
110 // -------------------------------------------------------------------------
hashBookmark(const Any & bookmark)111 sal_Int32 SAL_CALL OStaticSet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
112 {
113     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::hashBookmark" );
114 	return ::comphelper::getINT32(bookmark);
115 }
116 // -------------------------------------------------------------------------
fetchRow()117 sal_Bool OStaticSet::fetchRow()
118 {
119     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fetchRow" );
120 	sal_Bool bRet = sal_False;
121 	if ( !m_bEnd && (!m_nMaxRows || sal_Int32(m_aSet.size()) < m_nMaxRows) )
122         bRet = m_xDriverSet->next();
123     if ( bRet )
124 	{
125 		m_aSet.push_back(new connectivity::ORowVector< connectivity::ORowSetValue >(m_xSetMetaData->getColumnCount()));
126 		m_aSetIter = m_aSet.end() - 1;
127 		((*m_aSetIter)->get())[0] = getRow();
128 		OCacheSet::fillValueRow(*m_aSetIter,((*m_aSetIter)->get())[0]);
129 	}
130 	else
131 		m_bEnd = sal_True;
132 	return bRet;
133 }
134 // -------------------------------------------------------------------------
fillAllRows()135 void OStaticSet::fillAllRows()
136 {
137     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::fillAllRows" );
138 	if(!m_bEnd)
139 	{
140         sal_Int32 nColumnCount = m_xSetMetaData->getColumnCount();
141 		while(m_xDriverSet->next())
142 		{
143 			ORowSetRow pRow = new connectivity::ORowVector< connectivity::ORowSetValue >(nColumnCount);
144 			m_aSet.push_back(pRow);
145 			m_aSetIter = m_aSet.end() - 1;
146 			(pRow->get())[0] = getRow();
147 			OCacheSet::fillValueRow(pRow,(pRow->get())[0]);
148 		}
149 		m_bEnd = sal_True;
150 	}
151 }
152 // -------------------------------------------------------------------------
153 // XResultSet
next()154 sal_Bool SAL_CALL OStaticSet::next(  ) throw(SQLException, RuntimeException)
155 {
156     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::next" );
157 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
158 
159 	if(isAfterLast())
160 		return sal_False;
161 	if(!m_bEnd) // not yet all records fetched
162 	{
163 		++m_aSetIter;
164 		if(m_aSetIter == m_aSet.end() && !fetchRow())
165 			m_aSetIter = m_aSet.end();
166 	}
167 	else if(!isAfterLast())
168 		++m_aSetIter;
169 	return !isAfterLast();
170 }
171 // -------------------------------------------------------------------------
isBeforeFirst()172 sal_Bool SAL_CALL OStaticSet::isBeforeFirst(  ) throw(SQLException, RuntimeException)
173 {
174     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isBeforeFirst" );
175 	return m_aSetIter == m_aSet.begin();
176 }
177 // -------------------------------------------------------------------------
isAfterLast()178 sal_Bool SAL_CALL OStaticSet::isAfterLast(  ) throw(SQLException, RuntimeException)
179 {
180     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isAfterLast" );
181 	return m_aSetIter == m_aSet.end() && m_bEnd;
182 }
183 // -------------------------------------------------------------------------
isFirst()184 sal_Bool SAL_CALL OStaticSet::isFirst(  ) throw(SQLException, RuntimeException)
185 {
186     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isFirst" );
187 	return m_aSetIter == m_aSet.begin()+1;
188 }
189 // -------------------------------------------------------------------------
isLast()190 sal_Bool SAL_CALL OStaticSet::isLast(  ) throw(SQLException, RuntimeException)
191 {
192     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::isLast" );
193 	return m_aSetIter == m_aSet.end()-1 && m_bEnd;
194 }
195 // -------------------------------------------------------------------------
beforeFirst()196 void SAL_CALL OStaticSet::beforeFirst(  ) throw(SQLException, RuntimeException)
197 {
198     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::beforeFirst" );
199 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
200 	m_aSetIter = m_aSet.begin();
201 }
202 // -------------------------------------------------------------------------
afterLast()203 void SAL_CALL OStaticSet::afterLast(  ) throw(SQLException, RuntimeException)
204 {
205     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::afterLast" );
206 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
207 	fillAllRows();
208 	m_aSetIter = m_aSet.end();
209 }
210 // -------------------------------------------------------------------------
first()211 sal_Bool SAL_CALL OStaticSet::first(  ) throw(SQLException, RuntimeException)
212 {
213     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::first" );
214 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
215 	m_aSetIter = m_aSet.begin()+1;
216 	if(m_aSetIter == m_aSet.end() && !fetchRow())
217 		m_aSetIter = m_aSet.end();
218 
219 	return m_aSetIter != m_aSet.end();
220 }
221 // -------------------------------------------------------------------------
last()222 sal_Bool SAL_CALL OStaticSet::last(  ) throw(SQLException, RuntimeException)
223 {
224     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::last" );
225 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
226 	fillAllRows();
227 	m_aSetIter = m_aSet.end()-1;
228 
229 	return !isBeforeFirst() && !isAfterLast();
230 }
231 // -------------------------------------------------------------------------
getRow()232 sal_Int32 SAL_CALL OStaticSet::getRow(  ) throw(SQLException, RuntimeException)
233 {
234     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::getRow" );
235 	OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
236 	OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
237 
238 	sal_Int32 nPos = m_aSet.size() - (m_aSet.end() - m_aSetIter);
239 	OSL_ENSURE(nPos > 0,"RowPos is < 0");
240 	return nPos;
241 }
242 // -------------------------------------------------------------------------
absolute(sal_Int32 row)243 sal_Bool SAL_CALL OStaticSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
244 {
245     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::absolute" );
246 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
247 	OSL_ENSURE(row,"OStaticSet::absolute: INVALID row number!");
248 	// if row greater 0 than count from end - row means last
249 	if(row < 0)
250 	{
251 		if(!m_bEnd)
252 			fillAllRows();
253 
254 		sal_Int32 nRow = getRow();
255 		nRow += row;
256 		if(nRow <= (sal_Int32)m_aSet.size())
257 			m_aSetIter = m_aSet.begin() + nRow;
258 		else
259 			m_aSetIter = m_aSet.begin();
260 	}
261 	else if(row > 0)
262 	{
263 		if(row >= (sal_Int32)m_aSet.size())
264 		{
265 			if(!m_bEnd)
266 			{
267 				sal_Bool bNext = sal_True;
268 				for(sal_Int32 i=m_aSet.size()-1;i < row && bNext;++i)
269 					bNext = fetchRow();
270 			}
271 
272 			if(row > (sal_Int32)m_aSet.size())
273 				m_aSetIter = m_aSet.end();	// check again
274 			else
275 				m_aSetIter = m_aSet.begin() + row;
276 		}
277 		else
278 			m_aSetIter = m_aSet.begin() + row;
279 	}
280 
281 	return m_aSetIter != m_aSet.end() && m_aSetIter != m_aSet.begin();
282 }
283 // -------------------------------------------------------------------------
relative(sal_Int32 rows)284 sal_Bool SAL_CALL OStaticSet::relative( sal_Int32 rows ) throw(SQLException, RuntimeException)
285 {
286     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::relative" );
287 	if(!rows)
288 		return sal_True;
289 
290 	sal_Int32 nCurPos = getRow();
291 	return absolute(nCurPos+rows);
292 }
293 // -------------------------------------------------------------------------
previous()294 sal_Bool SAL_CALL OStaticSet::previous(  ) throw(SQLException, RuntimeException)
295 {
296     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::previous" );
297 	m_bInserted = m_bUpdated = m_bDeleted = sal_False;
298 
299 	if(m_aSetIter != m_aSet.begin())
300 		--m_aSetIter;
301 
302 	return m_aSetIter != m_aSet.begin();
303 }
304 // -------------------------------------------------------------------------
refreshRow()305 void SAL_CALL OStaticSet::refreshRow(  ) throw(SQLException, RuntimeException)
306 {
307     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::refreshRow" );
308 }
309 // -------------------------------------------------------------------------
rowUpdated()310 sal_Bool SAL_CALL OStaticSet::rowUpdated(  ) throw(SQLException, RuntimeException)
311 {
312     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowUpdated" );
313 	return m_bUpdated;
314 }
315 // -------------------------------------------------------------------------
rowInserted()316 sal_Bool SAL_CALL OStaticSet::rowInserted(  ) throw(SQLException, RuntimeException)
317 {
318     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowInserted" );
319 	return m_bInserted;
320 }
321 // -------------------------------------------------------------------------
rowDeleted()322 sal_Bool SAL_CALL OStaticSet::rowDeleted(  ) throw(SQLException, RuntimeException)
323 {
324     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::rowDeleted" );
325 	return m_bDeleted;
326 }
327 // -------------------------------------------------------------------------
deleteRows(const Sequence<Any> & rows,const connectivity::OSQLTable & _xTable)328 Sequence< sal_Int32 > SAL_CALL OStaticSet::deleteRows( const Sequence< Any >& rows,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
329 {
330     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRows" );
331 	Sequence< sal_Int32 > aRet(rows.getLength());
332 	const Any* pBegin	= rows.getConstArray();
333 	const Any* pEnd		= pBegin + rows.getLength();
334 	for(sal_Int32 i=0;pBegin != pEnd; ++pBegin,++i)
335 	{
336 		deleteRow(*(m_aSet.begin() + comphelper::getINT32(*pBegin)),_xTable);
337 		aRet.getArray()[i] = m_bDeleted;
338 	}
339 	return aRet;
340 }
341 // -------------------------------------------------------------------------
insertRow(const ORowSetRow & _rInsertRow,const connectivity::OSQLTable & _xTable)342 void SAL_CALL OStaticSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
343 {
344     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::insertRow" );
345 	OCacheSet::insertRow( _rInsertRow,_xTable);
346 	if(m_bInserted)
347 	{
348 		m_aSet.push_back(new ORowVector< ORowSetValue >(*_rInsertRow)); // we don't know where the new row is so we append it to the current rows
349 		m_aSetIter = m_aSet.end() - 1;
350 		((*m_aSetIter)->get())[0] = (_rInsertRow->get())[0] = getBookmark();
351 		m_bEnd = sal_False;
352 	}
353 }
354 // -------------------------------------------------------------------------
updateRow(const ORowSetRow & _rInsertRow,const ORowSetRow & _rOrginalRow,const connectivity::OSQLTable & _xTable)355 void SAL_CALL OStaticSet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& _xTable  ) throw(SQLException, RuntimeException)
356 {
357     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::updateRow" );
358 	OCacheSet::updateRow( _rInsertRow,_rOrginalRow,_xTable);
359 }
360 // -------------------------------------------------------------------------
deleteRow(const ORowSetRow & _rDeleteRow,const connectivity::OSQLTable & _xTable)361 void SAL_CALL OStaticSet::deleteRow(const ORowSetRow& _rDeleteRow ,const connectivity::OSQLTable& _xTable  ) throw(SQLException, RuntimeException)
362 {
363     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::deleteRow" );
364 	OCacheSet::deleteRow(_rDeleteRow,_xTable);
365 	if(m_bDeleted)
366 	{
367 		ORowSetMatrix::iterator aPos = m_aSet.begin()+(_rDeleteRow->get())[0].getInt32();
368 		if(aPos == (m_aSet.end()-1))
369 			m_aSetIter = m_aSet.end();
370 		m_aSet.erase(aPos);
371 	}
372 }
373 // -------------------------------------------------------------------------
cancelRowUpdates()374 void SAL_CALL OStaticSet::cancelRowUpdates(  ) throw(SQLException, RuntimeException)
375 {
376     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::cancelRowUpdates" );
377 }
378 // -------------------------------------------------------------------------
moveToInsertRow()379 void SAL_CALL OStaticSet::moveToInsertRow(  ) throw(SQLException, RuntimeException)
380 {
381     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToInsertRow" );
382 }
383 // -------------------------------------------------------------------------
moveToCurrentRow()384 void SAL_CALL OStaticSet::moveToCurrentRow(  ) throw(SQLException, RuntimeException)
385 {
386     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OStaticSet::moveToCurrentRow" );
387 }
388 // -------------------------------------------------------------------------
389 
390