bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / TSkipDeletedSet.cxx
blobd3ae392f7a09eaa04fec805f13466ec8f26ee569
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>
23 #include <algorithm>
25 using namespace connectivity;
27 OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
28 : m_pHelper(_pHelper)
29 ,m_bDeletedVisible(false)
31 m_aBookmarksPositions.reserve(256);
34 OSkipDeletedSet::~OSkipDeletedSet()
36 m_aBookmarksPositions.clear();
37 //m_aBookmarks.clear();
40 bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)
42 OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
44 IResultSetHelper::Movement eDelPosition = _eCursorPosition;
45 sal_Int32 nDelOffset = abs(_nOffset);
47 switch (_eCursorPosition)
49 case IResultSetHelper::ABSOLUTE1:
50 return moveAbsolute(_nOffset,_bRetrieveData);
51 case IResultSetHelper::FIRST: // set the movement when positioning failed
52 eDelPosition = IResultSetHelper::NEXT;
53 nDelOffset = 1;
54 break;
55 case IResultSetHelper::LAST:
56 eDelPosition = IResultSetHelper::PRIOR; // last row is invalid so position before
57 nDelOffset = 1;
58 break;
59 case IResultSetHelper::RELATIVE1:
60 eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR;
61 break;
62 default:
63 break;
66 bool bDone = true;
67 bool bDataFound = false;
69 if (_eCursorPosition == IResultSetHelper::LAST)
71 SAL_INFO( "connectivity.commontools", "OSkipDeletedSet::skipDeleted: last" );
72 sal_Int32 nBookmark = 0;
73 // first position on the last known row
74 if ( m_aBookmarksPositions.empty() )
76 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData);
77 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
78 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
79 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
81 else
83 // I already have a bookmark so we can positioned on that and look if it is the last one
84 nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
86 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
87 OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
91 // and then move forward until we are after the last row
92 while(bDataFound)
94 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, false); // we don't need the data here
95 if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) )
96 { // we weren't on the last row we remember it and move on
97 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
98 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
100 else if(!bDataFound && !m_aBookmarksPositions.empty() )
102 // i already know the last bookmark :-)
103 // now we only have to repositioning us to the last row
104 nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
105 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
106 break;
109 return bDataFound;
111 else if (_eCursorPosition != IResultSetHelper::RELATIVE1)
113 bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
114 bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
116 else
118 bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
119 if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
121 bDone = (--nDelOffset) == 0;
122 if ( !bDone )
123 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
124 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
126 else
127 bDone = false;
130 while (bDataFound && !bDone) // Iterate until we are at the valid set
132 bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
133 if (_eCursorPosition != IResultSetHelper::RELATIVE1)
134 bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
135 else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
137 bDone = (--nDelOffset) == 0;
138 if ( !bDone )
139 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
140 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
142 else
143 bDone = false;
146 if(bDataFound && bDone)
148 const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
149 if ( m_bDeletedVisible )
151 if ( nDriverPos > static_cast<sal_Int32>(m_aBookmarksPositions.size()) )
152 m_aBookmarksPositions.push_back(nDriverPos);
154 else if ( std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() )
155 m_aBookmarksPositions.push_back(nDriverPos);
156 /*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
157 if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
158 m_aBookmarksPositions.push_back(m_aBookmarks.emplace( nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
161 return bDataFound;
164 bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,bool _bRetrieveData)
166 bool bDataFound = false;
167 sal_Int32 nNewPos = _nPos;
168 if(nNewPos > 0)
170 if(static_cast<sal_Int32>(m_aBookmarksPositions.size()) < nNewPos)
172 // bookmark isn't known yet
173 // start at the last known position
174 sal_Int32 nCurPos = 0,nLastBookmark = 1;
175 if ( m_aBookmarksPositions.empty() )
177 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
178 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
180 ++nCurPos;
181 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
182 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
183 --nNewPos;
185 } // if ( m_aBookmarksPositions.empty() )
186 else
188 nLastBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
189 nCurPos = /*(**/m_aBookmarksPositions.size()/*->second*/;
190 nNewPos = nNewPos - nCurPos;
191 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
194 // now move to that row we need and don't count deleted rows
195 while (bDataFound && nNewPos)
197 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
198 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
200 ++nCurPos;
201 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
202 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
203 --nNewPos;
207 else
209 const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/;
210 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData);
211 OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!");
214 else
216 ++nNewPos;
217 bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
219 for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
220 bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
223 return bDataFound;
226 void OSkipDeletedSet::clear()
228 std::vector<sal_Int32>().swap(m_aBookmarksPositions);
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 OSL_FAIL("Why!");
237 return -1;
240 void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos)
242 m_aBookmarksPositions.push_back(_nPos);
245 void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
247 std::vector<sal_Int32>::iterator aFind = std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
248 if ( aFind != m_aBookmarksPositions.end() )
250 m_aBookmarksPositions.erase(aFind);
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */