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