1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TSkipDeletedSet.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "TSkipDeletedSet.hxx"
34 #include <osl/diagnose.h>
35 #include <rtl/logfile.hxx>
37 using namespace connectivity
;
38 // -----------------------------------------------------------------------------
39 OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper
* _pHelper
)
41 ,m_bDeletedVisible(false)
43 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::OSkipDeletedSet" );
44 m_aBookmarksPositions
.reserve(256);
46 // -----------------------------------------------------------------------------
47 OSkipDeletedSet::~OSkipDeletedSet()
49 m_aBookmarksPositions
.clear();
50 //m_aBookmarks.clear();
52 // -----------------------------------------------------------------------------
53 sal_Bool
OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition
, sal_Int32 _nOffset
, sal_Bool _bRetrieveData
)
55 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::skipDeleted" );
56 OSL_ENSURE(_eCursorPosition
!= IResultSetHelper::BOOKMARK
,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
58 IResultSetHelper::Movement eDelPosition
= _eCursorPosition
;
59 sal_Int32 nDelOffset
= abs(_nOffset
);
61 switch (_eCursorPosition
)
63 case IResultSetHelper::ABSOLUTE
:
64 return moveAbsolute(_nOffset
,_bRetrieveData
);
65 case IResultSetHelper::FIRST
: // set the movement when positioning failed
66 eDelPosition
= IResultSetHelper::NEXT
;
69 case IResultSetHelper::LAST
:
70 eDelPosition
= IResultSetHelper::PRIOR
; // lsat row is invalid so position before
73 case IResultSetHelper::RELATIVE
:
74 eDelPosition
= (_nOffset
>= 0) ? IResultSetHelper::NEXT
: IResultSetHelper::PRIOR
;
80 sal_Bool bDone
= sal_True
;
81 sal_Bool bDataFound
= sal_False
;
83 if (_eCursorPosition
== IResultSetHelper::LAST
)
85 RTL_LOGFILE_CONTEXT_TRACE( aLogger
, "OSkipDeletedSet::skipDeleted: last" );
86 sal_Int32 nBookmark
= 0;
87 // first position on the last known row
88 if ( m_aBookmarksPositions
.empty() )
90 bDataFound
= m_pHelper
->move(IResultSetHelper::FIRST
, 0, _bRetrieveData
);
91 if(bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
92 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
93 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
97 // I already have a bookmark so we can positioned on that and look if it is the last one
98 nBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
100 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nBookmark
, _bRetrieveData
);
101 OSL_ENSURE((m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()),"A bookmark should not be deleted!");
105 // and than move forward until we are after the last row
108 bDataFound
= m_pHelper
->move(IResultSetHelper::NEXT
, 1, sal_False
); // we don't need the data here
109 if( bDataFound
&& ( m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()) )
110 { // we weren't on the last row we remember it and move on
111 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
112 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
114 else if(!bDataFound
&& !m_aBookmarksPositions
.empty() )
116 // i already know the last bookmark :-)
117 // now we only have to repositioning us to the last row
118 nBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
119 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nBookmark
, _bRetrieveData
);
125 else if (_eCursorPosition
!= IResultSetHelper::RELATIVE
)
127 bDataFound
= m_pHelper
->move(_eCursorPosition
, _nOffset
, _bRetrieveData
);
128 bDone
= bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted());
132 bDataFound
= m_pHelper
->move(eDelPosition
, 1, _bRetrieveData
);
133 if (bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
135 bDone
= (--nDelOffset
) == 0;
137 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
138 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
144 while (bDataFound
&& !bDone
) // solange iterieren bis man auf einem gueltigen Satz ist
146 bDataFound
= m_pHelper
->move(eDelPosition
, 1, _bRetrieveData
);
147 if (_eCursorPosition
!= IResultSetHelper::RELATIVE
)
148 bDone
= bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted());
149 else if (bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
151 bDone
= (--nDelOffset
) == 0;
153 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
154 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
160 if(bDataFound
&& bDone
)
162 const sal_Int32 nDriverPos
= m_pHelper
->getDriverPos();
163 if ( ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),nDriverPos
) == m_aBookmarksPositions
.end() )
164 m_aBookmarksPositions
.push_back(nDriverPos
);
165 /*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
166 if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
167 m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
172 // -------------------------------------------------------------------------
173 sal_Bool
OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos
,sal_Bool _bRetrieveData
)
175 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::moveAbsolute" );
176 sal_Bool bDataFound
= sal_False
;
177 sal_Int32 nNewPos
= _nPos
;
180 if((sal_Int32
)m_aBookmarksPositions
.size() < nNewPos
)
182 // bookmark isn't known yet
183 // start at the last known position
184 sal_Int32 nCurPos
= 0,nLastBookmark
= 1;
185 if ( m_aBookmarksPositions
.empty() )
187 bDataFound
= m_pHelper
->move(IResultSetHelper::FIRST
, 0, _bRetrieveData
);
188 if(bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
191 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
192 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
195 } // if ( m_aBookmarksPositions.empty() )
198 nLastBookmark
= (*m_aBookmarksPositions
.rbegin())/*->first*/;
199 nCurPos
= /*(**/m_aBookmarksPositions
.size()/*->second*/;
200 nNewPos
= nNewPos
- nCurPos
;
201 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
, nLastBookmark
, _bRetrieveData
);
204 // now move to that row we need and don't count deleted rows
205 while (bDataFound
&& nNewPos
)
207 bDataFound
= m_pHelper
->move(IResultSetHelper::NEXT
, 1, _bRetrieveData
);
208 if(bDataFound
&& (m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()))
211 m_aBookmarksPositions
.push_back(m_pHelper
->getDriverPos());
212 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
219 const sal_Int32 nBookmark
= m_aBookmarksPositions
[nNewPos
-1]/*->first*/;
220 bDataFound
= m_pHelper
->move(IResultSetHelper::BOOKMARK
,nBookmark
, _bRetrieveData
);
221 OSL_ENSURE((m_bDeletedVisible
|| !m_pHelper
->isRowDeleted()),"moveAbsolute: row can't be deleted!");
227 bDataFound
= skipDeleted(IResultSetHelper::LAST
,0,nNewPos
== 0);
229 for(sal_Int32 i
=nNewPos
+1;bDataFound
&& i
<= 0;++i
)
230 bDataFound
= skipDeleted(IResultSetHelper::PRIOR
,1,i
== 0);
235 // -----------------------------------------------------------------------------
236 void OSkipDeletedSet::clear()
238 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::clear" );
239 ::std::vector
<sal_Int32
>().swap(m_aBookmarksPositions
);
240 //TInt2IntMap().swap(m_aBookmarks);
242 // -----------------------------------------------------------------------------
243 sal_Int32
OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos
) const
245 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::getMappedPosition" );
246 ::std::vector
<sal_Int32
>::const_iterator aFind
= ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),_nPos
);
247 if ( aFind
!= m_aBookmarksPositions
.end() )
248 return (aFind
- m_aBookmarksPositions
.begin()) + 1;
249 /*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos);
250 OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!");
251 return aFind->second;*/
252 OSL_ENSURE(0,"Why!");
255 // -----------------------------------------------------------------------------
256 void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos
)
258 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::insertNewPosition" );
259 //OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position");
260 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first);
261 //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos");
262 m_aBookmarksPositions
.push_back(_nPos
);
264 // -----------------------------------------------------------------------------
265 void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark
)
267 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::deletePosition" );
268 ::std::vector
<sal_Int32
>::iterator aFind
= ::std::find(m_aBookmarksPositions
.begin(),m_aBookmarksPositions
.end(),_nBookmark
);
269 if ( aFind
!= m_aBookmarksPositions
.end() )
271 //TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos);
272 //OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!");
273 //TInt2IntMap::iterator aIter = aFind;
274 m_aBookmarksPositions
.erase(aFind
);
275 //for (; aFind != m_aBookmarksPositions.end() ; ++aIter)
276 // --(aFind->second);
277 } // if ( aFind != m_aBookmarksPositions.end() )
278 //m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1);
279 //m_aBookmarks.erase(_nPos);
281 // -----------------------------------------------------------------------------