1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/logfile.hxx>
24 using namespace connectivity
;
25 // -----------------------------------------------------------------------------
26 OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper
* _pHelper
)
28 ,m_bDeletedVisible(false)
30 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::OSkipDeletedSet" );
31 m_aBookmarksPositions
.reserve(256);
33 // -----------------------------------------------------------------------------
34 OSkipDeletedSet::~OSkipDeletedSet()
36 m_aBookmarksPositions
.clear();
37 //m_aBookmarks.clear();
39 // -----------------------------------------------------------------------------
40 sal_Bool
OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition
, sal_Int32 _nOffset
, sal_Bool _bRetrieveData
)
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");
45 IResultSetHelper::Movement eDelPosition
= _eCursorPosition
;
46 sal_Int32 nDelOffset
= abs(_nOffset
);
48 switch (_eCursorPosition
)
50 case IResultSetHelper::ABSOLUTE
:
51 return moveAbsolute(_nOffset
,_bRetrieveData
);
52 case IResultSetHelper::FIRST
: // set the movement when positioning failed
53 eDelPosition
= IResultSetHelper::NEXT
;
56 case IResultSetHelper::LAST
:
57 eDelPosition
= IResultSetHelper::PRIOR
; // last row is invalid so position before
60 case IResultSetHelper::RELATIVE
:
61 eDelPosition
= (_nOffset
>= 0) ? IResultSetHelper::NEXT
: IResultSetHelper::PRIOR
;
67 sal_Bool bDone
= sal_True
;
68 sal_Bool bDataFound
= sal_False
;
70 if (_eCursorPosition
== IResultSetHelper::LAST
)
72 RTL_LOGFILE_CONTEXT_TRACE( aLogger
, "OSkipDeletedSet::skipDeleted: last" );
73 sal_Int32 nBookmark
= 0;
74 // first position on the last known row
75 if ( m_aBookmarksPositions
.empty() )
77 bDataFound
= m_pHelper
->move(IResultSetHelper::FIRST
, 0, _bRetrieveData
);
78 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 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
84 // I already have a bookmark so we can positioned on that and look if it is the last one
85 nBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
87 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nBookmark
, _bRetrieveData
);
88 OSL_ENSURE((m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()),"A bookmark should not be deleted!");
92 // and then move forward until we are after the last row
95 bDataFound
= m_pHelper
->move(IResultSetHelper::NEXT
, 1, sal_False
); // we don't need the data here
96 if( bDataFound
&& ( m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()) )
97 { // we weren't on the last row we remember it and move on
98 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);
101 else if(!bDataFound
&& !m_aBookmarksPositions
.empty() )
103 // i already know the last bookmark :-)
104 // now we only have to repositioning us to the last row
105 nBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
106 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nBookmark
, _bRetrieveData
);
112 else if (_eCursorPosition
!= IResultSetHelper::RELATIVE
)
114 bDataFound
= m_pHelper
->move(_eCursorPosition
, _nOffset
, _bRetrieveData
);
115 bDone
= bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted());
119 bDataFound
= m_pHelper
->move(eDelPosition
, 1, _bRetrieveData
);
120 if (bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
122 bDone
= (--nDelOffset
) == 0;
124 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);
131 while (bDataFound
&& !bDone
) // Iterate until we are at the valid set
133 bDataFound
= m_pHelper
->move(eDelPosition
, 1, _bRetrieveData
);
134 if (_eCursorPosition
!= IResultSetHelper::RELATIVE
)
135 bDone
= bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted());
136 else if (bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
138 bDone
= (--nDelOffset
) == 0;
140 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);
147 if(bDataFound
&& bDone
)
149 const sal_Int32 nDriverPos
= m_pHelper
->getDriverPos();
150 if ( m_bDeletedVisible
)
152 if ( nDriverPos
> (sal_Int32
)m_aBookmarksPositions
.size() )
153 m_aBookmarksPositions
.push_back(nDriverPos
);
155 else if ( ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),nDriverPos
) == m_aBookmarksPositions
.end() )
156 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);*/
164 // -------------------------------------------------------------------------
165 sal_Bool
OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos
,sal_Bool _bRetrieveData
)
167 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::moveAbsolute" );
168 sal_Bool bDataFound
= sal_False
;
169 sal_Int32 nNewPos
= _nPos
;
172 if((sal_Int32
)m_aBookmarksPositions
.size() < nNewPos
)
174 // bookmark isn't known yet
175 // start at the last known position
176 sal_Int32 nCurPos
= 0,nLastBookmark
= 1;
177 if ( m_aBookmarksPositions
.empty() )
179 bDataFound
= m_pHelper
->move(IResultSetHelper::FIRST
, 0, _bRetrieveData
);
180 if(bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
183 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);
187 } // if ( m_aBookmarksPositions.empty() )
190 nLastBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
191 nCurPos
= /*(**/m_aBookmarksPositions
.size()/*->second*/;
192 nNewPos
= nNewPos
- nCurPos
;
193 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nLastBookmark
, _bRetrieveData
);
196 // now move to that row we need and don't count deleted rows
197 while (bDataFound
&& nNewPos
)
199 bDataFound
= m_pHelper
->move(IResultSetHelper::NEXT
, 1, _bRetrieveData
);
200 if(bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
203 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);
211 const sal_Int32 nBookmark
= m_aBookmarksPositions
[nNewPos
-1]/*->first*/;
212 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
,nBookmark
, _bRetrieveData
);
213 OSL_ENSURE((m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()),"moveAbsolute: row can't be deleted!");
219 bDataFound
= skipDeleted(IResultSetHelper::LAST
,0,nNewPos
== 0);
221 for(sal_Int32 i
=nNewPos
+1;bDataFound
&& i
<= 0;++i
)
222 bDataFound
= skipDeleted(IResultSetHelper::PRIOR
,1,i
== 0);
227 // -----------------------------------------------------------------------------
228 void OSkipDeletedSet::clear()
230 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::clear" );
231 ::std::vector
<sal_Int32
>().swap(m_aBookmarksPositions
);
232 //TInt2IntMap().swap(m_aBookmarks);
234 // -----------------------------------------------------------------------------
235 sal_Int32
OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos
) const
237 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::getMappedPosition" );
238 ::std::vector
<sal_Int32
>::const_iterator aFind
= ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),_nPos
);
239 if ( aFind
!= m_aBookmarksPositions
.end() )
240 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;*/
247 // -----------------------------------------------------------------------------
248 void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos
)
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 m_aBookmarksPositions
.push_back(_nPos
);
256 // -----------------------------------------------------------------------------
257 void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark
)
259 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::deletePosition" );
260 ::std::vector
<sal_Int32
>::iterator aFind
= ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),_nBookmark
);
261 if ( aFind
!= m_aBookmarksPositions
.end() )
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 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);
273 // -----------------------------------------------------------------------------
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */