calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / dbaccess / source / core / api / StaticSet.cxx
blob97226b466dbded1973e8834f5737a5eb5f32c18b
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 <connectivity/CommonTools.hxx>
25 #include <comphelper/types.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <osl/diagnose.h>
29 using namespace dbaccess;
30 using namespace connectivity;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::beans;
33 using namespace ::com::sun::star::sdbc;
34 using namespace ::com::sun::star::sdbcx;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::lang;
37 using namespace ::osl;
39 void OStaticSet::fillValueRow(ORowSetRow& _rRow,sal_Int32 /*_nPosition*/)
41 _rRow = *m_aSetIter;
44 // css::sdbcx::XRowLocate
45 Any OStaticSet::getBookmark()
47 return Any(getRow());
50 bool OStaticSet::moveToBookmark( const Any& bookmark )
52 m_bInserted = m_bUpdated = m_bDeleted = false;
53 return absolute(::comphelper::getINT32(bookmark));
56 sal_Int32 OStaticSet::compareBookmarks( const Any& _first, const Any& _second )
58 sal_Int32 nFirst = 0, nSecond = 0;
59 _first >>= nFirst;
60 _second >>= nSecond;
61 return (nFirst < nSecond) ? CompareBookmark::LESS : ((nFirst > nSecond) ? CompareBookmark::GREATER : CompareBookmark::EQUAL);
64 bool OStaticSet::hasOrderedBookmarks( )
66 return true;
69 sal_Int32 OStaticSet::hashBookmark( const Any& bookmark )
71 return ::comphelper::getINT32(bookmark);
74 bool OStaticSet::fetchRow()
76 bool bRet = false;
77 if ( !m_bEnd && (!m_nMaxRows || sal_Int32(m_aSet.size()) < m_nMaxRows) )
78 bRet = m_xDriverSet->next();
79 if ( bRet )
81 m_aSet.push_back(new connectivity::ORowVector< connectivity::ORowSetValue >(m_xSetMetaData->getColumnCount()));
82 m_aSetIter = m_aSet.end() - 1;
83 (**m_aSetIter)[0] = getRow();
84 OCacheSet::fillValueRow(*m_aSetIter,(**m_aSetIter)[0].getInt32());
86 else
87 m_bEnd = true;
88 return bRet;
91 void OStaticSet::fillAllRows()
93 if(m_bEnd)
94 return;
96 sal_Int32 nColumnCount = m_xSetMetaData->getColumnCount();
97 while(m_xDriverSet->next())
99 ORowSetRow pRow = new connectivity::ORowVector< connectivity::ORowSetValue >(nColumnCount);
100 m_aSet.push_back(pRow);
101 m_aSetIter = m_aSet.end() - 1;
102 (*pRow)[0] = getRow();
103 OCacheSet::fillValueRow(pRow,(*pRow)[0].getInt32());
105 m_bEnd = true;
108 // XResultSet
109 bool OStaticSet::next()
111 m_bInserted = m_bUpdated = m_bDeleted = false;
113 if(isAfterLast())
114 return false;
115 if(!m_bEnd) // not yet all records fetched
117 ++m_aSetIter;
118 if(m_aSetIter == m_aSet.end() && !fetchRow())
119 m_aSetIter = m_aSet.end();
121 else if(!isAfterLast())
122 ++m_aSetIter;
123 return !isAfterLast();
126 bool OStaticSet::isBeforeFirst( )
128 return m_aSetIter == m_aSet.begin();
131 bool OStaticSet::isAfterLast( )
133 return m_aSetIter == m_aSet.end() && m_bEnd;
136 void OStaticSet::beforeFirst( )
138 m_bInserted = m_bUpdated = m_bDeleted = false;
139 m_aSetIter = m_aSet.begin();
142 void OStaticSet::afterLast( )
144 m_bInserted = m_bUpdated = m_bDeleted = false;
145 fillAllRows();
146 m_aSetIter = m_aSet.end();
149 bool OStaticSet::first()
151 m_bInserted = m_bUpdated = m_bDeleted = false;
152 m_aSetIter = m_aSet.begin()+1;
153 if(m_aSetIter == m_aSet.end() && !fetchRow())
154 m_aSetIter = m_aSet.end();
156 return m_aSetIter != m_aSet.end();
159 bool OStaticSet::last()
161 m_bInserted = m_bUpdated = m_bDeleted = false;
162 fillAllRows();
163 m_aSetIter = m_aSet.end()-1;
165 return !isBeforeFirst() && !isAfterLast();
168 sal_Int32 OStaticSet::getRow( )
170 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
171 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
173 sal_Int32 nPos = m_aSet.size() - (m_aSet.end() - m_aSetIter);
174 OSL_ENSURE(nPos > 0,"RowPos is < 0");
175 return nPos;
178 bool OStaticSet::absolute( sal_Int32 row )
180 m_bInserted = m_bUpdated = m_bDeleted = false;
181 OSL_ENSURE(row,"OStaticSet::absolute: INVALID row number!");
182 // if row greater 0 than count from end - row means last
183 if(row < 0)
185 if(!m_bEnd)
186 fillAllRows();
188 sal_Int32 nRow = getRow();
189 nRow += row;
190 if(nRow <= static_cast<sal_Int32>(m_aSet.size()))
191 m_aSetIter = m_aSet.begin() + nRow;
192 else
193 m_aSetIter = m_aSet.begin();
195 else if(row > 0)
197 if(o3tl::make_unsigned(row) >= m_aSet.size())
199 if(!m_bEnd)
201 bool bNext = true;
202 for(sal_Int32 i=m_aSet.size()-1;i < row && bNext;++i)
203 bNext = fetchRow();
206 if(o3tl::make_unsigned(row) > m_aSet.size())
207 m_aSetIter = m_aSet.end(); // check again
208 else
209 m_aSetIter = m_aSet.begin() + row;
211 else
212 m_aSetIter = m_aSet.begin() + row;
215 return m_aSetIter != m_aSet.end() && m_aSetIter != m_aSet.begin();
218 bool OStaticSet::previous( )
220 m_bInserted = m_bUpdated = m_bDeleted = false;
222 if(m_aSetIter != m_aSet.begin())
223 --m_aSetIter;
225 return m_aSetIter != m_aSet.begin();
228 void OStaticSet::refreshRow( )
232 bool OStaticSet::rowUpdated( )
234 return m_bUpdated;
237 bool OStaticSet::rowInserted( )
239 return m_bInserted;
242 bool OStaticSet::rowDeleted( )
244 return m_bDeleted;
247 void OStaticSet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable )
249 OCacheSet::insertRow( _rInsertRow,_xTable);
250 if(m_bInserted)
252 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
253 m_aSetIter = m_aSet.end() - 1;
254 (**m_aSetIter)[0] = (*_rInsertRow)[0] = getBookmark();
255 m_bEnd = false;
259 void OStaticSet::deleteRow(const ORowSetRow& _rDeleteRow ,const connectivity::OSQLTable& _xTable )
261 OCacheSet::deleteRow(_rDeleteRow,_xTable);
262 if(m_bDeleted)
264 ORowSetMatrix::iterator aPos = m_aSet.begin()+(*_rDeleteRow)[0].getInt32();
265 if(aPos == (m_aSet.end()-1))
266 m_aSetIter = m_aSet.end();
267 m_aSet.erase(aPos);
271 void OStaticSet::reset(const Reference< XResultSet> &_xDriverSet)
273 OCacheSet::construct(_xDriverSet, m_sRowSetFilter);
274 ORowSetMatrix().swap(m_aSet);
275 m_aSetIter = m_aSet.end();
276 m_bEnd = false;
277 m_aSet.emplace_back(nullptr); // this is the beforefirst record
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */