Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / TSkipDeletedSet.cxx
blob58e1a73dc05729b6c1e72da08247e81179e56de2
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 .
20 #include "TSkipDeletedSet.hxx"
21 #include <osl/diagnose.h>
22 #include "sal/log.hxx"
24 using namespace connectivity;
26 OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
27 : m_pHelper(_pHelper)
28 ,m_bDeletedVisible(false)
30 m_aBookmarksPositions.reserve(256);
33 OSkipDeletedSet::~OSkipDeletedSet()
35 m_aBookmarksPositions.clear();
36 //m_aBookmarks.clear();
39 bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)
41 OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
43 IResultSetHelper::Movement eDelPosition = _eCursorPosition;
44 sal_Int32 nDelOffset = abs(_nOffset);
46 switch (_eCursorPosition)
48 case IResultSetHelper::ABSOLUTE:
49 return moveAbsolute(_nOffset,_bRetrieveData);
50 case IResultSetHelper::FIRST: // set the movement when positioning failed
51 eDelPosition = IResultSetHelper::NEXT;
52 nDelOffset = 1;
53 break;
54 case IResultSetHelper::LAST:
55 eDelPosition = IResultSetHelper::PRIOR; // last row is invalid so position before
56 nDelOffset = 1;
57 break;
58 case IResultSetHelper::RELATIVE:
59 eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR;
60 break;
61 default:
62 break;
65 bool bDone = true;
66 bool bDataFound = false;
68 if (_eCursorPosition == IResultSetHelper::LAST)
70 SAL_INFO( "connectivity.commontools", "OSkipDeletedSet::skipDeleted: last" );
71 sal_Int32 nBookmark = 0;
72 // first position on the last known row
73 if ( m_aBookmarksPositions.empty() )
75 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData);
76 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
77 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
78 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
80 else
82 // I already have a bookmark so we can positioned on that and look if it is the last one
83 nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
85 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
86 OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
90 // and then move forward until we are after the last row
91 while(bDataFound)
93 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, false); // we don't need the data here
94 if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) )
95 { // we weren't on the last row we remember it and move on
96 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
97 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
99 else if(!bDataFound && !m_aBookmarksPositions.empty() )
101 // i already know the last bookmark :-)
102 // now we only have to repositioning us to the last row
103 nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
104 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
105 break;
108 return bDataFound;
110 else if (_eCursorPosition != IResultSetHelper::RELATIVE)
112 bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
113 bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
115 else
117 bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
118 if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
120 bDone = (--nDelOffset) == 0;
121 if ( !bDone )
122 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
123 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
125 else
126 bDone = false;
129 while (bDataFound && !bDone) // Iterate until we are at the valid set
131 bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
132 if (_eCursorPosition != IResultSetHelper::RELATIVE)
133 bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
134 else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
136 bDone = (--nDelOffset) == 0;
137 if ( !bDone )
138 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
139 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
141 else
142 bDone = false;
145 if(bDataFound && bDone)
147 const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
148 if ( m_bDeletedVisible )
150 if ( nDriverPos > (sal_Int32)m_aBookmarksPositions.size() )
151 m_aBookmarksPositions.push_back(nDriverPos);
153 else if ( ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() )
154 m_aBookmarksPositions.push_back(nDriverPos);
155 /*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
156 if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
157 m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
160 return bDataFound;
163 bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,bool _bRetrieveData)
165 bool bDataFound = false;
166 sal_Int32 nNewPos = _nPos;
167 if(nNewPos > 0)
169 if((sal_Int32)m_aBookmarksPositions.size() < nNewPos)
171 // bookmark isn't known yet
172 // start at the last known position
173 sal_Int32 nCurPos = 0,nLastBookmark = 1;
174 if ( m_aBookmarksPositions.empty() )
176 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
177 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
179 ++nCurPos;
180 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
181 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
182 --nNewPos;
184 } // if ( m_aBookmarksPositions.empty() )
185 else
187 nLastBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
188 nCurPos = /*(**/m_aBookmarksPositions.size()/*->second*/;
189 nNewPos = nNewPos - nCurPos;
190 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
193 // now move to that row we need and don't count deleted rows
194 while (bDataFound && nNewPos)
196 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
197 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
199 ++nCurPos;
200 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
201 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
202 --nNewPos;
206 else
208 const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/;
209 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData);
210 OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!");
213 else
215 ++nNewPos;
216 bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
218 for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
219 bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
222 return bDataFound;
225 void OSkipDeletedSet::clear()
227 ::std::vector<sal_Int32>().swap(m_aBookmarksPositions);
228 //TInt2IntMap().swap(m_aBookmarks);
231 sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const
233 ::std::vector<sal_Int32>::const_iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos);
234 if ( aFind != m_aBookmarksPositions.end() )
235 return (aFind - m_aBookmarksPositions.begin()) + 1;
236 /*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos);
237 OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!");
238 return aFind->second;*/
239 OSL_FAIL("Why!");
240 return -1;
243 void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos)
245 //OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position");
246 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first);
247 //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos");
248 m_aBookmarksPositions.push_back(_nPos);
251 void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
253 ::std::vector<sal_Int32>::iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
254 if ( aFind != m_aBookmarksPositions.end() )
256 //TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos);
257 //OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!");
258 //TInt2IntMap::iterator aIter = aFind;
259 m_aBookmarksPositions.erase(aFind);
260 //for (; aFind != m_aBookmarksPositions.end() ; ++aIter)
261 // --(aFind->second);
262 } // if ( aFind != m_aBookmarksPositions.end() )
263 //m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1);
264 //m_aBookmarks.erase(_nPos);
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */