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