Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / StaticSet.cxx
blobaea9c4d8ed1132c7fadcbdebe7e90a817df1c22a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "StaticSet.hxx"
22 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
25 #include <com/sun/star/sdbc/XPreparedStatement.hpp>
26 #include "dbastrings.hrc"
27 #include "apitools.hxx"
28 #include <connectivity/CommonTools.hxx>
29 #include <comphelper/types.hxx>
31 using namespace dbaccess;
32 using namespace connectivity;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::sdbcx;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::lang;
39 using namespace ::osl;
41 void OStaticSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 /*_nPosition*/)
43 _rRow = *m_aSetIter;
46 // ::com::sun::star::sdbcx::XRowLocate
47 Any SAL_CALL OStaticSet::getBookmark() throw(SQLException, RuntimeException)
49 return makeAny(getRow());
52 bool SAL_CALL OStaticSet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
54 m_bInserted = m_bUpdated = m_bDeleted = false;
55 return absolute(::comphelper::getINT32(bookmark));
58 bool SAL_CALL OStaticSet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
60 m_bInserted = m_bUpdated = m_bDeleted = false;
61 return absolute(::comphelper::getINT32(bookmark)+rows);
64 sal_Int32 SAL_CALL OStaticSet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
66 sal_Int32 nFirst = 0, nSecond = 0;
67 _first >>= nFirst;
68 _second >>= nSecond;
69 return (nFirst < nSecond) ? CompareBookmark::LESS : ((nFirst > nSecond) ? CompareBookmark::GREATER : CompareBookmark::EQUAL);
72 bool SAL_CALL OStaticSet::hasOrderedBookmarks( ) throw(SQLException, RuntimeException)
74 return true;
77 sal_Int32 SAL_CALL OStaticSet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
79 return ::comphelper::getINT32(bookmark);
82 bool OStaticSet::fetchRow()
84 bool bRet = false;
85 if ( !m_bEnd && (!m_nMaxRows || sal_Int32(m_aSet.size()) < m_nMaxRows) )
86 bRet = m_xDriverSet->next();
87 if ( bRet )
89 m_aSet.push_back(new connectivity::ORowVector< connectivity::ORowSetValue >(m_xSetMetaData->getColumnCount()));
90 m_aSetIter = m_aSet.end() - 1;
91 ((*m_aSetIter)->get())[0] = getRow();
92 OCacheSet::fillValueRow(*m_aSetIter,((*m_aSetIter)->get())[0]);
94 else
95 m_bEnd = true;
96 return bRet;
99 void OStaticSet::fillAllRows()
101 if(!m_bEnd)
103 sal_Int32 nColumnCount = m_xSetMetaData->getColumnCount();
104 while(m_xDriverSet->next())
106 ORowSetRow pRow = new connectivity::ORowVector< connectivity::ORowSetValue >(nColumnCount);
107 m_aSet.push_back(pRow);
108 m_aSetIter = m_aSet.end() - 1;
109 (pRow->get())[0] = getRow();
110 OCacheSet::fillValueRow(pRow,(pRow->get())[0]);
112 m_bEnd = true;
116 // XResultSet
117 bool SAL_CALL OStaticSet::next( ) throw(SQLException, RuntimeException)
119 m_bInserted = m_bUpdated = m_bDeleted = false;
121 if(isAfterLast())
122 return false;
123 if(!m_bEnd) // not yet all records fetched
125 ++m_aSetIter;
126 if(m_aSetIter == m_aSet.end() && !fetchRow())
127 m_aSetIter = m_aSet.end();
129 else if(!isAfterLast())
130 ++m_aSetIter;
131 return !isAfterLast();
134 bool SAL_CALL OStaticSet::isBeforeFirst( ) throw(SQLException, RuntimeException)
136 return m_aSetIter == m_aSet.begin();
139 bool SAL_CALL OStaticSet::isAfterLast( ) throw(SQLException, RuntimeException)
141 return m_aSetIter == m_aSet.end() && m_bEnd;
144 bool SAL_CALL OStaticSet::isFirst( ) throw(SQLException, RuntimeException)
146 return m_aSetIter == m_aSet.begin()+1;
149 bool SAL_CALL OStaticSet::isLast( ) throw(SQLException, RuntimeException)
151 return m_aSetIter == m_aSet.end()-1 && m_bEnd;
154 void SAL_CALL OStaticSet::beforeFirst( ) throw(SQLException, RuntimeException)
156 m_bInserted = m_bUpdated = m_bDeleted = false;
157 m_aSetIter = m_aSet.begin();
160 void SAL_CALL OStaticSet::afterLast( ) throw(SQLException, RuntimeException)
162 m_bInserted = m_bUpdated = m_bDeleted = false;
163 fillAllRows();
164 m_aSetIter = m_aSet.end();
167 bool SAL_CALL OStaticSet::first( ) throw(SQLException, RuntimeException)
169 m_bInserted = m_bUpdated = m_bDeleted = false;
170 m_aSetIter = m_aSet.begin()+1;
171 if(m_aSetIter == m_aSet.end() && !fetchRow())
172 m_aSetIter = m_aSet.end();
174 return m_aSetIter != m_aSet.end();
177 bool SAL_CALL OStaticSet::last( ) throw(SQLException, RuntimeException)
179 m_bInserted = m_bUpdated = m_bDeleted = false;
180 fillAllRows();
181 m_aSetIter = m_aSet.end()-1;
183 return !isBeforeFirst() && !isAfterLast();
186 sal_Int32 SAL_CALL OStaticSet::getRow( ) throw(SQLException, RuntimeException)
188 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
189 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
191 sal_Int32 nPos = m_aSet.size() - (m_aSet.end() - m_aSetIter);
192 OSL_ENSURE(nPos > 0,"RowPos is < 0");
193 return nPos;
196 bool SAL_CALL OStaticSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
198 m_bInserted = m_bUpdated = m_bDeleted = false;
199 OSL_ENSURE(row,"OStaticSet::absolute: INVALID row number!");
200 // if row greater 0 than count from end - row means last
201 if(row < 0)
203 if(!m_bEnd)
204 fillAllRows();
206 sal_Int32 nRow = getRow();
207 nRow += row;
208 if(nRow <= (sal_Int32)m_aSet.size())
209 m_aSetIter = m_aSet.begin() + nRow;
210 else
211 m_aSetIter = m_aSet.begin();
213 else if(row > 0)
215 if(row >= (sal_Int32)m_aSet.size())
217 if(!m_bEnd)
219 bool bNext = true;
220 for(sal_Int32 i=m_aSet.size()-1;i < row && bNext;++i)
221 bNext = fetchRow();
224 if(row > (sal_Int32)m_aSet.size())
225 m_aSetIter = m_aSet.end(); // check again
226 else
227 m_aSetIter = m_aSet.begin() + row;
229 else
230 m_aSetIter = m_aSet.begin() + row;
233 return m_aSetIter != m_aSet.end() && m_aSetIter != m_aSet.begin();
236 bool SAL_CALL OStaticSet::relative( sal_Int32 rows ) throw(SQLException, RuntimeException)
238 if(!rows)
239 return true;
241 sal_Int32 nCurPos = getRow();
242 return absolute(nCurPos+rows);
245 bool SAL_CALL OStaticSet::previous( ) throw(SQLException, RuntimeException)
247 m_bInserted = m_bUpdated = m_bDeleted = false;
249 if(m_aSetIter != m_aSet.begin())
250 --m_aSetIter;
252 return m_aSetIter != m_aSet.begin();
255 void SAL_CALL OStaticSet::refreshRow( ) throw(SQLException, RuntimeException)
259 bool SAL_CALL OStaticSet::rowUpdated( ) throw(SQLException, RuntimeException)
261 return m_bUpdated;
264 bool SAL_CALL OStaticSet::rowInserted( ) throw(SQLException, RuntimeException)
266 return m_bInserted;
269 bool SAL_CALL OStaticSet::rowDeleted( ) throw(SQLException, RuntimeException)
271 return m_bDeleted;
274 Sequence< sal_Int32 > SAL_CALL OStaticSet::deleteRows( const Sequence< Any >& rows,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
276 Sequence< sal_Int32 > aRet(rows.getLength());
277 const Any* pBegin = rows.getConstArray();
278 const Any* pEnd = pBegin + rows.getLength();
279 for(sal_Int32 i=0; pBegin != pEnd; ++pBegin,++i)
281 deleteRow(*(m_aSet.begin() + comphelper::getINT32(*pBegin)),_xTable);
282 aRet.getArray()[i] = m_bDeleted ? 1 : 0;
284 return aRet;
287 void SAL_CALL OStaticSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
289 OCacheSet::insertRow( _rInsertRow,_xTable);
290 if(m_bInserted)
292 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
293 m_aSetIter = m_aSet.end() - 1;
294 ((*m_aSetIter)->get())[0] = (_rInsertRow->get())[0] = getBookmark();
295 m_bEnd = false;
299 void SAL_CALL OStaticSet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOriginalRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
301 OCacheSet::updateRow( _rInsertRow,_rOriginalRow,_xTable);
304 void SAL_CALL OStaticSet::deleteRow(const ORowSetRow& _rDeleteRow ,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
306 OCacheSet::deleteRow(_rDeleteRow,_xTable);
307 if(m_bDeleted)
309 ORowSetMatrix::iterator aPos = m_aSet.begin()+(_rDeleteRow->get())[0].getInt32();
310 if(aPos == (m_aSet.end()-1))
311 m_aSetIter = m_aSet.end();
312 m_aSet.erase(aPos);
316 void SAL_CALL OStaticSet::cancelRowUpdates( ) throw(SQLException, RuntimeException)
320 void SAL_CALL OStaticSet::moveToInsertRow( ) throw(SQLException, RuntimeException)
324 void SAL_CALL OStaticSet::moveToCurrentRow( ) throw(SQLException, RuntimeException)
328 void OStaticSet::reset(const Reference< XResultSet> &_xDriverSet)
330 OCacheSet::construct(_xDriverSet, m_sRowSetFilter);
332 ORowSetMatrix t;
333 m_aSet.swap(t);
335 m_aSetIter = m_aSet.end();
336 m_bEnd = false;
337 m_aSet.push_back(NULL); // this is the beforefirst record
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */