LibreOffice Module connectivity (master) 1
TSkipDeletedSet.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <TSkipDeletedSet.hxx>
21#include <osl/diagnose.h>
22#include <sal/log.hxx>
23#include <algorithm>
24
25using namespace connectivity;
26
27OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
28 : m_pHelper(_pHelper)
29 ,m_bDeletedVisible(false)
30{
31 m_aBookmarksPositions.reserve(256);
32}
33
35{
37 //m_aBookmarks.clear();
38}
39
40bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)
41{
42 OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
43
44 IResultSetHelper::Movement eDelPosition = _eCursorPosition;
45 sal_Int32 nDelOffset = abs(_nOffset);
46
47 switch (_eCursorPosition)
48 {
50 return moveAbsolute(_nOffset,_bRetrieveData);
51 case IResultSetHelper::FIRST: // set the movement when positioning failed
52 eDelPosition = IResultSetHelper::NEXT;
53 nDelOffset = 1;
54 break;
56 eDelPosition = IResultSetHelper::PRIOR; // last row is invalid so position before
57 nDelOffset = 1;
58 break;
61 break;
62 default:
63 break;
64 }
65
66 bool bDone = true;
67 bool bDataFound = false;
68
69 if (_eCursorPosition == IResultSetHelper::LAST)
70 {
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() )
75 {
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);
80 }
81 else
82 {
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*/;
85
86 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
87 OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
88 }
89
90
91 // and then move forward until we are after the last row
92 while(bDataFound)
93 {
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
98 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
99 }
100 else if(!bDataFound && !m_aBookmarksPositions.empty() )
101 {
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;
107 }
108 }
109 return bDataFound;
110 }
111 else if (_eCursorPosition != IResultSetHelper::RELATIVE1)
112 {
113 bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
114 bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
115 }
116 else
117 {
118 bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
119 if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
120 {
121 bDone = (--nDelOffset) == 0;
122 if ( !bDone )
124 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
125 }
126 else
127 bDone = false;
128 }
129
130 while (bDataFound && !bDone) // Iterate until we are at the valid set
131 {
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()))
136 {
137 bDone = (--nDelOffset) == 0;
138 if ( !bDone )
140 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
141 }
142 else
143 bDone = false;
144 }
145
146 if(bDataFound && bDone)
147 {
148 const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
149 if ( m_bDeletedVisible )
150 {
151 if ( nDriverPos > static_cast<sal_Int32>(m_aBookmarksPositions.size()) )
152 m_aBookmarksPositions.push_back(nDriverPos);
153 }
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);*/
159 }
160
161 return bDataFound;
162}
163
164bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,bool _bRetrieveData)
165{
166 bool bDataFound = false;
167 sal_Int32 nNewPos = _nPos;
168 if(nNewPos > 0)
169 {
170 if(static_cast<sal_Int32>(m_aBookmarksPositions.size()) < nNewPos)
171 {
172 // bookmark isn't known yet
173 // start at the last known position
174 sal_Int32 nCurPos = 0;
175 if ( m_aBookmarksPositions.empty() )
176 {
177 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
178 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
179 {
180 ++nCurPos;
182 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
183 --nNewPos;
184 }
185 } // if ( m_aBookmarksPositions.empty() )
186 else
187 {
188 sal_Int32 nLastBookmark = *m_aBookmarksPositions.rbegin()/*->first*/;
189 nCurPos = /*(**/m_aBookmarksPositions.size()/*->second*/;
190 nNewPos = nNewPos - nCurPos;
191 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
192 }
193
194 // now move to that row we need and don't count deleted rows
195 while (bDataFound && nNewPos)
196 {
197 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
198 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
199 {
200 ++nCurPos;
202 //m_aBookmarksPositions.push_back(m_aBookmarks.emplace( m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
203 --nNewPos;
204 }
205 }
206 }
207 else
208 {
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!");
212 }
213 }
214 else
215 {
216 ++nNewPos;
217 bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
218
219 for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
220 bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
221
222 }
223 return bDataFound;
224}
225
227{
228 std::vector<sal_Int32>().swap(m_aBookmarksPositions);
229}
230
231sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const
232{
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;
238}
239
241{
242 m_aBookmarksPositions.push_back(_nPos);
243}
244
245void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
246{
247 std::vector<sal_Int32>::iterator aFind = std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
248 if ( aFind != m_aBookmarksPositions.end() )
249 {
250 m_aBookmarksPositions.erase(aFind);
251 }
252}
253
254
255/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Int32 getDriverPos() const =0
virtual bool isRowDeleted() const =0
virtual bool move(Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)=0
std::vector< sal_Int32 > m_aBookmarksPositions
void clear()
clear the map and the vector used in this class
sal_Int32 getMappedPosition(sal_Int32 _nBookmark) const
getMappedPosition returns the mapped position of a logical position
void insertNewPosition(sal_Int32 _nPos)
insertNewPosition adds a new position to the map
bool skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)
skipDeleted moves the resultset to the position defined by the parameters it guarantees that the row ...
void deletePosition(sal_Int32 _nPos)
deletePosition deletes this position from the map and decrement all following positions
bool moveAbsolute(sal_Int32 _nOffset, bool _bRetrieveData)
sal_Int32 _nOffset
#define SAL_INFO(area, stream)
int i
SwNodeOffset abs(const SwNodeOffset &a)
sal_Int32 _nPos