19b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59b5730f6SAndrew Rist  * distributed with this work for additional information
69b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
99b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
109b5730f6SAndrew Rist  *
119b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129b5730f6SAndrew Rist  *
139b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149b5730f6SAndrew Rist  * software distributed under the License is distributed on an
159b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
179b5730f6SAndrew Rist  * specific language governing permissions and limitations
189b5730f6SAndrew Rist  * under the License.
199b5730f6SAndrew Rist  *
209b5730f6SAndrew Rist  *************************************************************/
219b5730f6SAndrew Rist 
229b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "TSkipDeletedSet.hxx"
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <rtl/logfile.hxx>
29*82c0ddf2SHerbert Dürr #include <algorithm>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace connectivity;
32cdf0e10cSrcweir // -----------------------------------------------------------------------------
OSkipDeletedSet(IResultSetHelper * _pHelper)33cdf0e10cSrcweir OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
34cdf0e10cSrcweir 	: m_pHelper(_pHelper)
35cdf0e10cSrcweir     ,m_bDeletedVisible(false)
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::OSkipDeletedSet" );
38cdf0e10cSrcweir     m_aBookmarksPositions.reserve(256);
39cdf0e10cSrcweir }
40cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OSkipDeletedSet()41cdf0e10cSrcweir OSkipDeletedSet::~OSkipDeletedSet()
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	m_aBookmarksPositions.clear();
44cdf0e10cSrcweir 	//m_aBookmarks.clear();
45cdf0e10cSrcweir }
46cdf0e10cSrcweir // -----------------------------------------------------------------------------
skipDeleted(IResultSetHelper::Movement _eCursorPosition,sal_Int32 _nOffset,sal_Bool _bRetrieveData)47cdf0e10cSrcweir sal_Bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, sal_Bool _bRetrieveData)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::skipDeleted" );
50cdf0e10cSrcweir 	OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	IResultSetHelper::Movement eDelPosition = _eCursorPosition;
53cdf0e10cSrcweir 	sal_Int32 nDelOffset = abs(_nOffset);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	switch (_eCursorPosition)
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		case IResultSetHelper::ABSOLUTE:
58cdf0e10cSrcweir             return moveAbsolute(_nOffset,_bRetrieveData);
59cdf0e10cSrcweir 		case IResultSetHelper::FIRST:					// set the movement when positioning failed
60cdf0e10cSrcweir 			eDelPosition = IResultSetHelper::NEXT;
61cdf0e10cSrcweir 			nDelOffset = 1;
62cdf0e10cSrcweir 			break;
63cdf0e10cSrcweir 		case IResultSetHelper::LAST:
64cdf0e10cSrcweir 			eDelPosition = IResultSetHelper::PRIOR; // lsat row is invalid so position before
65cdf0e10cSrcweir 			nDelOffset = 1;
66cdf0e10cSrcweir 			break;
67cdf0e10cSrcweir 		case IResultSetHelper::RELATIVE:
68cdf0e10cSrcweir 			eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR;
69cdf0e10cSrcweir 			break;
70cdf0e10cSrcweir         default:
71cdf0e10cSrcweir             break;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	sal_Bool bDone			= sal_True;
75cdf0e10cSrcweir 	sal_Bool bDataFound		= sal_False;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	if (_eCursorPosition == IResultSetHelper::LAST)
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT_TRACE( aLogger, "OSkipDeletedSet::skipDeleted: last" );
80cdf0e10cSrcweir 		sal_Int32 nBookmark = 0;
81cdf0e10cSrcweir 		// first position on the last known row
82cdf0e10cSrcweir 		if ( m_aBookmarksPositions.empty() )
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 			bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData);
85cdf0e10cSrcweir 			if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
86cdf0e10cSrcweir 				//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
87cdf0e10cSrcweir                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
88cdf0e10cSrcweir 		}
89cdf0e10cSrcweir 		else
90cdf0e10cSrcweir 		{
91cdf0e10cSrcweir 			// I already have a bookmark so we can positioned on that and look if it is the last one
92cdf0e10cSrcweir 			nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 			bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
95cdf0e10cSrcweir 			OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
96cdf0e10cSrcweir 		}
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 		// and than move forward until we are after the last row
100cdf0e10cSrcweir 		while(bDataFound)
101cdf0e10cSrcweir 		{
102cdf0e10cSrcweir 			bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, sal_False); // we don't need the data here
103cdf0e10cSrcweir 			if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) )
104cdf0e10cSrcweir 			{	// we weren't on the last row we remember it and move on
105cdf0e10cSrcweir                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
106cdf0e10cSrcweir 				//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
107cdf0e10cSrcweir 			}
108cdf0e10cSrcweir 			else if(!bDataFound && !m_aBookmarksPositions.empty() )
109cdf0e10cSrcweir 			{
110cdf0e10cSrcweir 				// i already know the last bookmark :-)
111cdf0e10cSrcweir 				// now we only have to repositioning us to the last row
112cdf0e10cSrcweir 				nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
113cdf0e10cSrcweir 				bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
114cdf0e10cSrcweir 				break;
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 		return bDataFound;
118cdf0e10cSrcweir 	}
119cdf0e10cSrcweir 	else if (_eCursorPosition != IResultSetHelper::RELATIVE)
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir 		bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
122cdf0e10cSrcweir 		bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
123cdf0e10cSrcweir 	}
124cdf0e10cSrcweir 	else
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
127cdf0e10cSrcweir 		if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
128cdf0e10cSrcweir 		{
129cdf0e10cSrcweir             bDone = (--nDelOffset) == 0;
130cdf0e10cSrcweir             if ( !bDone )
131cdf0e10cSrcweir                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
132cdf0e10cSrcweir 			//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
133cdf0e10cSrcweir 		}
134cdf0e10cSrcweir 		else
135cdf0e10cSrcweir 			bDone = sal_False;
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     while (bDataFound && !bDone)            // solange iterieren bis man auf einem gueltigen Satz ist
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir 		bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
141cdf0e10cSrcweir 		if (_eCursorPosition != IResultSetHelper::RELATIVE)
142cdf0e10cSrcweir 			bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
143cdf0e10cSrcweir 		else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
144cdf0e10cSrcweir 		{
145cdf0e10cSrcweir             bDone = (--nDelOffset) == 0;
146cdf0e10cSrcweir             if ( !bDone )
147cdf0e10cSrcweir                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
148cdf0e10cSrcweir 			//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
149cdf0e10cSrcweir 		}
150cdf0e10cSrcweir 		else
151cdf0e10cSrcweir 			bDone = sal_False;
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	if(bDataFound && bDone)
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir         const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
157cdf0e10cSrcweir         if ( m_bDeletedVisible )
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             if ( nDriverPos > (sal_Int32)m_aBookmarksPositions.size() )
160cdf0e10cSrcweir                 m_aBookmarksPositions.push_back(nDriverPos);
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         else if ( ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() )
163cdf0e10cSrcweir             m_aBookmarksPositions.push_back(nDriverPos);
164cdf0e10cSrcweir 		/*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
165cdf0e10cSrcweir 		if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
166cdf0e10cSrcweir 			m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	return bDataFound;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir // -------------------------------------------------------------------------
moveAbsolute(sal_Int32 _nPos,sal_Bool _bRetrieveData)172cdf0e10cSrcweir sal_Bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,sal_Bool _bRetrieveData)
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::moveAbsolute" );
175cdf0e10cSrcweir 	sal_Bool bDataFound = sal_False;
176cdf0e10cSrcweir 	sal_Int32 nNewPos = _nPos;
177cdf0e10cSrcweir 	if(nNewPos > 0)
178cdf0e10cSrcweir 	{
179cdf0e10cSrcweir 		if((sal_Int32)m_aBookmarksPositions.size() < nNewPos)
180cdf0e10cSrcweir 		{
181cdf0e10cSrcweir 			// bookmark isn't known yet
182cdf0e10cSrcweir 			// start at the last known position
183cdf0e10cSrcweir 			sal_Int32 nCurPos = 0,nLastBookmark = 1;
184cdf0e10cSrcweir 			if ( m_aBookmarksPositions.empty() )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir 				bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
187cdf0e10cSrcweir 				if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
188cdf0e10cSrcweir 				{
189cdf0e10cSrcweir 					++nCurPos;
190cdf0e10cSrcweir                     m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
191cdf0e10cSrcweir 					//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
192cdf0e10cSrcweir 					--nNewPos;
193cdf0e10cSrcweir 				}
194cdf0e10cSrcweir 			} // if ( m_aBookmarksPositions.empty() )
195cdf0e10cSrcweir             else
196cdf0e10cSrcweir 			{
197cdf0e10cSrcweir 				nLastBookmark	= (*m_aBookmarksPositions.rbegin())/*->first*/;
198cdf0e10cSrcweir 				nCurPos			= /*(**/m_aBookmarksPositions.size()/*->second*/;
199cdf0e10cSrcweir 				nNewPos		    = nNewPos - nCurPos;
200cdf0e10cSrcweir 				bDataFound		= m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
201cdf0e10cSrcweir 			}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			// now move to that row we need and don't count deleted rows
204cdf0e10cSrcweir 			while (bDataFound && nNewPos)
205cdf0e10cSrcweir 			{
206cdf0e10cSrcweir 				bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
207cdf0e10cSrcweir 				if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
208cdf0e10cSrcweir 				{
209cdf0e10cSrcweir 					++nCurPos;
210cdf0e10cSrcweir                     m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
211cdf0e10cSrcweir 					//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
212cdf0e10cSrcweir 					--nNewPos;
213cdf0e10cSrcweir 				}
214cdf0e10cSrcweir 			}
215cdf0e10cSrcweir 		}
216cdf0e10cSrcweir 		else
217cdf0e10cSrcweir 		{
218cdf0e10cSrcweir 			const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/;
219cdf0e10cSrcweir 			bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData);
220cdf0e10cSrcweir 			OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!");
221cdf0e10cSrcweir 		}
222cdf0e10cSrcweir 	}
223cdf0e10cSrcweir 	else
224cdf0e10cSrcweir 	{
225cdf0e10cSrcweir 		++nNewPos;
226cdf0e10cSrcweir 		bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 		for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
229cdf0e10cSrcweir 			bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir 	return bDataFound;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir // -----------------------------------------------------------------------------
clear()235cdf0e10cSrcweir void OSkipDeletedSet::clear()
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::clear" );
238cdf0e10cSrcweir 	::std::vector<sal_Int32>().swap(m_aBookmarksPositions);
239cdf0e10cSrcweir 	//TInt2IntMap().swap(m_aBookmarks);
240cdf0e10cSrcweir }
241cdf0e10cSrcweir // -----------------------------------------------------------------------------
getMappedPosition(sal_Int32 _nPos) const242cdf0e10cSrcweir sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::getMappedPosition" );
245cdf0e10cSrcweir     ::std::vector<sal_Int32>::const_iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos);
246cdf0e10cSrcweir     if ( aFind !=  m_aBookmarksPositions.end() )
247cdf0e10cSrcweir         return (aFind - m_aBookmarksPositions.begin()) + 1;
248cdf0e10cSrcweir 	/*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos);
249cdf0e10cSrcweir 	OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!");
250cdf0e10cSrcweir 	return aFind->second;*/
251cdf0e10cSrcweir     OSL_ENSURE(0,"Why!");
252cdf0e10cSrcweir     return -1;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir // -----------------------------------------------------------------------------
insertNewPosition(sal_Int32 _nPos)255cdf0e10cSrcweir void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::insertNewPosition" );
258cdf0e10cSrcweir 	//OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position");
259cdf0e10cSrcweir 	//m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first);
260cdf0e10cSrcweir     //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos");
261cdf0e10cSrcweir     m_aBookmarksPositions.push_back(_nPos);
262cdf0e10cSrcweir }
263cdf0e10cSrcweir // -----------------------------------------------------------------------------
deletePosition(sal_Int32 _nBookmark)264cdf0e10cSrcweir void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::deletePosition" );
267cdf0e10cSrcweir     ::std::vector<sal_Int32>::iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
268cdf0e10cSrcweir     if ( aFind !=  m_aBookmarksPositions.end() )
269cdf0e10cSrcweir     {
270cdf0e10cSrcweir 	//TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos);
271cdf0e10cSrcweir 	//OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!");
272cdf0e10cSrcweir 	//TInt2IntMap::iterator aIter = aFind;
273cdf0e10cSrcweir         m_aBookmarksPositions.erase(aFind);
274cdf0e10cSrcweir 	    //for (; aFind != m_aBookmarksPositions.end() ; ++aIter)
275cdf0e10cSrcweir 		   // --(aFind->second);
276cdf0e10cSrcweir     } // if ( aFind !=  m_aBookmarksPositions.end() )
277cdf0e10cSrcweir 	//m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1);
278cdf0e10cSrcweir 	//m_aBookmarks.erase(_nPos);
279cdf0e10cSrcweir }
280cdf0e10cSrcweir // -----------------------------------------------------------------------------
281