Branch data Line data Source code
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 <rtl/logfile.hxx>
23 : :
24 : : using namespace connectivity;
25 : : // -----------------------------------------------------------------------------
26 : 88 : OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
27 : : : m_pHelper(_pHelper)
28 : 88 : ,m_bDeletedVisible(false)
29 : : {
30 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::OSkipDeletedSet" );
31 [ + - ]: 88 : m_aBookmarksPositions.reserve(256);
32 : 88 : }
33 : : // -----------------------------------------------------------------------------
34 : 88 : OSkipDeletedSet::~OSkipDeletedSet()
35 : : {
36 : 88 : m_aBookmarksPositions.clear();
37 : : //m_aBookmarks.clear();
38 : 88 : }
39 : : // -----------------------------------------------------------------------------
40 : 628 : sal_Bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, sal_Bool _bRetrieveData)
41 : : {
42 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::skipDeleted" );
43 : : OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
44 : :
45 : 628 : IResultSetHelper::Movement eDelPosition = _eCursorPosition;
46 : 628 : sal_Int32 nDelOffset = abs(_nOffset);
47 : :
48 [ + + + - : 628 : switch (_eCursorPosition)
+ ]
49 : : {
50 : : case IResultSetHelper::ABSOLUTE:
51 : 122 : return moveAbsolute(_nOffset,_bRetrieveData);
52 : : case IResultSetHelper::FIRST: // set the movement when positioning failed
53 : 98 : eDelPosition = IResultSetHelper::NEXT;
54 : 98 : nDelOffset = 1;
55 : 98 : break;
56 : : case IResultSetHelper::LAST:
57 : 20 : eDelPosition = IResultSetHelper::PRIOR; // lsat row is invalid so position before
58 : 20 : nDelOffset = 1;
59 : 20 : break;
60 : : case IResultSetHelper::RELATIVE:
61 : 0 : eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR;
62 : 0 : break;
63 : : default:
64 : 388 : break;
65 : : }
66 : :
67 : 506 : sal_Bool bDone = sal_True;
68 : 506 : sal_Bool bDataFound = sal_False;
69 : :
70 [ + + ]: 506 : if (_eCursorPosition == IResultSetHelper::LAST)
71 : : {
72 : : RTL_LOGFILE_CONTEXT_TRACE( aLogger, "OSkipDeletedSet::skipDeleted: last" );
73 : 20 : sal_Int32 nBookmark = 0;
74 : : // first position on the last known row
75 [ - + ]: 20 : if ( m_aBookmarksPositions.empty() )
76 : : {
77 : 0 : bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData);
78 [ # # ][ # # ]: 0 : if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
[ # # ][ # # ]
79 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
80 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
81 : : }
82 : : else
83 : : {
84 : : // I already have a bookmark so we can positioned on that and look if it is the last one
85 [ + - ]: 20 : nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
86 : :
87 : 20 : bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
88 : : OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
89 : : }
90 : :
91 : :
92 : : // and than move forward until we are after the last row
93 [ + - ]: 20 : while(bDataFound)
94 : : {
95 : 20 : bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, sal_False); // we don't need the data here
96 [ # # ][ # # ]: 20 : if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) )
[ - + ][ - + ]
97 : : { // we weren't on the last row we remember it and move on
98 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
99 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
100 : : }
101 [ + - ][ + - ]: 20 : else if(!bDataFound && !m_aBookmarksPositions.empty() )
[ + - ]
102 : : {
103 : : // i already know the last bookmark :-)
104 : : // now we only have to repositioning us to the last row
105 [ + - ]: 20 : nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
106 : 20 : bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
107 : 20 : break;
108 : : }
109 : : }
110 : 20 : return bDataFound;
111 : : }
112 [ + - ]: 486 : else if (_eCursorPosition != IResultSetHelper::RELATIVE)
113 : : {
114 : 486 : bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
115 [ + - ][ + + ]: 486 : bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
[ + + ]
116 : : }
117 : : else
118 : : {
119 : 0 : bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
120 [ # # ][ # # ]: 0 : if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
[ # # ][ # # ]
121 : : {
122 : 0 : bDone = (--nDelOffset) == 0;
123 [ # # ]: 0 : if ( !bDone )
124 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
125 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
126 : : }
127 : : else
128 : 0 : bDone = sal_False;
129 : : }
130 : :
131 [ + + ][ + + ]: 2474 : while (bDataFound && !bDone) // Iterate until we are at the valid set
[ + + ]
132 : : {
133 : 1988 : bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
134 [ + - ]: 1988 : if (_eCursorPosition != IResultSetHelper::RELATIVE)
135 [ + + ][ + - ]: 1988 : bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
[ + + ]
136 [ # # ][ # # ]: 0 : else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
[ # # ][ # # ]
137 : : {
138 : 0 : bDone = (--nDelOffset) == 0;
139 [ # # ]: 0 : if ( !bDone )
140 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
141 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
142 : : }
143 : : else
144 : 0 : bDone = sal_False;
145 : : }
146 : :
147 [ + + ][ + - ]: 486 : if(bDataFound && bDone)
148 : : {
149 [ + - ]: 382 : const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
150 [ - + ]: 382 : if ( m_bDeletedVisible )
151 : : {
152 [ # # ]: 0 : if ( nDriverPos > (sal_Int32)m_aBookmarksPositions.size() )
153 [ # # ]: 0 : m_aBookmarksPositions.push_back(nDriverPos);
154 : : }
155 [ + - ][ + - ]: 382 : else if ( ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() )
[ + + ]
156 [ + - ]: 382 : m_aBookmarksPositions.push_back(nDriverPos);
157 : : /*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
158 : : if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
159 : : m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
160 : : }
161 : :
162 : 628 : return bDataFound;
163 : : }
164 : : // -------------------------------------------------------------------------
165 : 122 : sal_Bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,sal_Bool _bRetrieveData)
166 : : {
167 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::moveAbsolute" );
168 : 122 : sal_Bool bDataFound = sal_False;
169 : 122 : sal_Int32 nNewPos = _nPos;
170 [ + - ]: 122 : if(nNewPos > 0)
171 : : {
172 [ + + ]: 122 : if((sal_Int32)m_aBookmarksPositions.size() < nNewPos)
173 : : {
174 : : // bookmark isn't known yet
175 : : // start at the last known position
176 : 36 : sal_Int32 nCurPos = 0,nLastBookmark = 1;
177 [ + + ]: 36 : if ( m_aBookmarksPositions.empty() )
178 : : {
179 : 12 : bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
180 [ # # ][ # # ]: 12 : if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
[ - + ][ - + ]
181 : : {
182 : 0 : ++nCurPos;
183 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
184 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
185 : 0 : --nNewPos;
186 : : }
187 : : } // if ( m_aBookmarksPositions.empty() )
188 : : else
189 : : {
190 [ + - ]: 24 : nLastBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
191 : 24 : nCurPos = /*(**/m_aBookmarksPositions.size()/*->second*/;
192 : 24 : nNewPos = nNewPos - nCurPos;
193 : 24 : bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
194 : : }
195 : :
196 : : // now move to that row we need and don't count deleted rows
197 [ + + ][ + - ]: 60 : while (bDataFound && nNewPos)
[ + + ]
198 : : {
199 : 24 : bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
200 [ # # ][ # # ]: 24 : if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
[ - + ][ - + ]
201 : : {
202 : 0 : ++nCurPos;
203 [ # # ]: 0 : m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
204 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
205 : 0 : --nNewPos;
206 : : }
207 : : }
208 : : }
209 : : else
210 : : {
211 : 86 : const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/;
212 : 86 : bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData);
213 : : OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!");
214 : : }
215 : : }
216 : : else
217 : : {
218 : 0 : ++nNewPos;
219 : 0 : bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
220 : :
221 [ # # ][ # # ]: 0 : for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
[ # # ]
222 : 0 : bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
223 : :
224 : : }
225 : 122 : return bDataFound;
226 : : }
227 : : // -----------------------------------------------------------------------------
228 : 216 : void OSkipDeletedSet::clear()
229 : : {
230 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::clear" );
231 : 216 : ::std::vector<sal_Int32>().swap(m_aBookmarksPositions);
232 : : //TInt2IntMap().swap(m_aBookmarks);
233 : 216 : }
234 : : // -----------------------------------------------------------------------------
235 : 438 : sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const
236 : : {
237 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::getMappedPosition" );
238 [ + - ]: 438 : ::std::vector<sal_Int32>::const_iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos);
239 [ + - ][ + - ]: 438 : if ( aFind != m_aBookmarksPositions.end() )
240 [ + - ]: 438 : return (aFind - m_aBookmarksPositions.begin()) + 1;
241 : : /*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos);
242 : : OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!");
243 : : return aFind->second;*/
244 : : OSL_FAIL("Why!");
245 : 438 : return -1;
246 : : }
247 : : // -----------------------------------------------------------------------------
248 : 2 : void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos)
249 : : {
250 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::insertNewPosition" );
251 : : //OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position");
252 : : //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first);
253 : : //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos");
254 : 2 : m_aBookmarksPositions.push_back(_nPos);
255 : 2 : }
256 : : // -----------------------------------------------------------------------------
257 : 2 : void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
258 : : {
259 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::deletePosition" );
260 [ + - ]: 2 : ::std::vector<sal_Int32>::iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
261 [ + - ][ + - ]: 2 : if ( aFind != m_aBookmarksPositions.end() )
262 : : {
263 : : //TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos);
264 : : //OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!");
265 : : //TInt2IntMap::iterator aIter = aFind;
266 [ + - ]: 2 : m_aBookmarksPositions.erase(aFind);
267 : : //for (; aFind != m_aBookmarksPositions.end() ; ++aIter)
268 : : // --(aFind->second);
269 : : } // if ( aFind != m_aBookmarksPositions.end() )
270 : : //m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1);
271 : : //m_aBookmarks.erase(_nPos);
272 : 2 : }
273 : : // -----------------------------------------------------------------------------
274 : :
275 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|