1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir #include "TSkipDeletedSet.hxx" 31*cdf0e10cSrcweir #include <osl/diagnose.h> 32*cdf0e10cSrcweir #include <rtl/logfile.hxx> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir using namespace connectivity; 35*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 36*cdf0e10cSrcweir OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper) 37*cdf0e10cSrcweir : m_pHelper(_pHelper) 38*cdf0e10cSrcweir ,m_bDeletedVisible(false) 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::OSkipDeletedSet" ); 41*cdf0e10cSrcweir m_aBookmarksPositions.reserve(256); 42*cdf0e10cSrcweir } 43*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 44*cdf0e10cSrcweir OSkipDeletedSet::~OSkipDeletedSet() 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir m_aBookmarksPositions.clear(); 47*cdf0e10cSrcweir //m_aBookmarks.clear(); 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 50*cdf0e10cSrcweir sal_Bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, sal_Bool _bRetrieveData) 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::skipDeleted" ); 53*cdf0e10cSrcweir OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK"); 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir IResultSetHelper::Movement eDelPosition = _eCursorPosition; 56*cdf0e10cSrcweir sal_Int32 nDelOffset = abs(_nOffset); 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir switch (_eCursorPosition) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir case IResultSetHelper::ABSOLUTE: 61*cdf0e10cSrcweir return moveAbsolute(_nOffset,_bRetrieveData); 62*cdf0e10cSrcweir case IResultSetHelper::FIRST: // set the movement when positioning failed 63*cdf0e10cSrcweir eDelPosition = IResultSetHelper::NEXT; 64*cdf0e10cSrcweir nDelOffset = 1; 65*cdf0e10cSrcweir break; 66*cdf0e10cSrcweir case IResultSetHelper::LAST: 67*cdf0e10cSrcweir eDelPosition = IResultSetHelper::PRIOR; // lsat row is invalid so position before 68*cdf0e10cSrcweir nDelOffset = 1; 69*cdf0e10cSrcweir break; 70*cdf0e10cSrcweir case IResultSetHelper::RELATIVE: 71*cdf0e10cSrcweir eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR; 72*cdf0e10cSrcweir break; 73*cdf0e10cSrcweir default: 74*cdf0e10cSrcweir break; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir sal_Bool bDone = sal_True; 78*cdf0e10cSrcweir sal_Bool bDataFound = sal_False; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir if (_eCursorPosition == IResultSetHelper::LAST) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE( aLogger, "OSkipDeletedSet::skipDeleted: last" ); 83*cdf0e10cSrcweir sal_Int32 nBookmark = 0; 84*cdf0e10cSrcweir // first position on the last known row 85*cdf0e10cSrcweir if ( m_aBookmarksPositions.empty() ) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData); 88*cdf0e10cSrcweir if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted())) 89*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 90*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir else 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir // I already have a bookmark so we can positioned on that and look if it is the last one 95*cdf0e10cSrcweir nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData); 98*cdf0e10cSrcweir OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!"); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir // and than move forward until we are after the last row 103*cdf0e10cSrcweir while(bDataFound) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, sal_False); // we don't need the data here 106*cdf0e10cSrcweir if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) ) 107*cdf0e10cSrcweir { // we weren't on the last row we remember it and move on 108*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 109*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir else if(!bDataFound && !m_aBookmarksPositions.empty() ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir // i already know the last bookmark :-) 114*cdf0e10cSrcweir // now we only have to repositioning us to the last row 115*cdf0e10cSrcweir nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/; 116*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData); 117*cdf0e10cSrcweir break; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir return bDataFound; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir else if (_eCursorPosition != IResultSetHelper::RELATIVE) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData); 125*cdf0e10cSrcweir bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData); 130*cdf0e10cSrcweir if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted())) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir bDone = (--nDelOffset) == 0; 133*cdf0e10cSrcweir if ( !bDone ) 134*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 135*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir else 138*cdf0e10cSrcweir bDone = sal_False; 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir while (bDataFound && !bDone) // solange iterieren bis man auf einem gueltigen Satz ist 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData); 144*cdf0e10cSrcweir if (_eCursorPosition != IResultSetHelper::RELATIVE) 145*cdf0e10cSrcweir bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()); 146*cdf0e10cSrcweir else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted())) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir bDone = (--nDelOffset) == 0; 149*cdf0e10cSrcweir if ( !bDone ) 150*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 151*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir else 154*cdf0e10cSrcweir bDone = sal_False; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if(bDataFound && bDone) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir const sal_Int32 nDriverPos = m_pHelper->getDriverPos(); 160*cdf0e10cSrcweir if ( m_bDeletedVisible ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir if ( nDriverPos > (sal_Int32)m_aBookmarksPositions.size() ) 163*cdf0e10cSrcweir m_aBookmarksPositions.push_back(nDriverPos); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir else if ( ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() ) 166*cdf0e10cSrcweir m_aBookmarksPositions.push_back(nDriverPos); 167*cdf0e10cSrcweir /*sal_Int32 nDriverPos = m_pHelper->getDriverPos(); 168*cdf0e10cSrcweir if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end()) 169*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/ 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir return bDataFound; 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir // ------------------------------------------------------------------------- 175*cdf0e10cSrcweir sal_Bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,sal_Bool _bRetrieveData) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::moveAbsolute" ); 178*cdf0e10cSrcweir sal_Bool bDataFound = sal_False; 179*cdf0e10cSrcweir sal_Int32 nNewPos = _nPos; 180*cdf0e10cSrcweir if(nNewPos > 0) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir if((sal_Int32)m_aBookmarksPositions.size() < nNewPos) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir // bookmark isn't known yet 185*cdf0e10cSrcweir // start at the last known position 186*cdf0e10cSrcweir sal_Int32 nCurPos = 0,nLastBookmark = 1; 187*cdf0e10cSrcweir if ( m_aBookmarksPositions.empty() ) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData ); 190*cdf0e10cSrcweir if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted())) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir ++nCurPos; 193*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 194*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 195*cdf0e10cSrcweir --nNewPos; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir } // if ( m_aBookmarksPositions.empty() ) 198*cdf0e10cSrcweir else 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir nLastBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/; 201*cdf0e10cSrcweir nCurPos = /*(**/m_aBookmarksPositions.size()/*->second*/; 202*cdf0e10cSrcweir nNewPos = nNewPos - nCurPos; 203*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData); 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir // now move to that row we need and don't count deleted rows 207*cdf0e10cSrcweir while (bDataFound && nNewPos) 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData); 210*cdf0e10cSrcweir if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted())) 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir ++nCurPos; 213*cdf0e10cSrcweir m_aBookmarksPositions.push_back(m_pHelper->getDriverPos()); 214*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first); 215*cdf0e10cSrcweir --nNewPos; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir else 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/; 222*cdf0e10cSrcweir bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData); 223*cdf0e10cSrcweir OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!"); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir else 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir ++nNewPos; 229*cdf0e10cSrcweir bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0); 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i) 232*cdf0e10cSrcweir bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0); 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir return bDataFound; 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 238*cdf0e10cSrcweir void OSkipDeletedSet::clear() 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::clear" ); 241*cdf0e10cSrcweir ::std::vector<sal_Int32>().swap(m_aBookmarksPositions); 242*cdf0e10cSrcweir //TInt2IntMap().swap(m_aBookmarks); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 245*cdf0e10cSrcweir sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::getMappedPosition" ); 248*cdf0e10cSrcweir ::std::vector<sal_Int32>::const_iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos); 249*cdf0e10cSrcweir if ( aFind != m_aBookmarksPositions.end() ) 250*cdf0e10cSrcweir return (aFind - m_aBookmarksPositions.begin()) + 1; 251*cdf0e10cSrcweir /*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos); 252*cdf0e10cSrcweir OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!"); 253*cdf0e10cSrcweir return aFind->second;*/ 254*cdf0e10cSrcweir OSL_ENSURE(0,"Why!"); 255*cdf0e10cSrcweir return -1; 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 258*cdf0e10cSrcweir void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::insertNewPosition" ); 261*cdf0e10cSrcweir //OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position"); 262*cdf0e10cSrcweir //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first); 263*cdf0e10cSrcweir //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos"); 264*cdf0e10cSrcweir m_aBookmarksPositions.push_back(_nPos); 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 267*cdf0e10cSrcweir void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "commontools", "Ocke.Janssen@sun.com", "OSkipDeletedSet::deletePosition" ); 270*cdf0e10cSrcweir ::std::vector<sal_Int32>::iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark); 271*cdf0e10cSrcweir if ( aFind != m_aBookmarksPositions.end() ) 272*cdf0e10cSrcweir { 273*cdf0e10cSrcweir //TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos); 274*cdf0e10cSrcweir //OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!"); 275*cdf0e10cSrcweir //TInt2IntMap::iterator aIter = aFind; 276*cdf0e10cSrcweir m_aBookmarksPositions.erase(aFind); 277*cdf0e10cSrcweir //for (; aFind != m_aBookmarksPositions.end() ; ++aIter) 278*cdf0e10cSrcweir // --(aFind->second); 279*cdf0e10cSrcweir } // if ( aFind != m_aBookmarksPositions.end() ) 280*cdf0e10cSrcweir //m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1); 281*cdf0e10cSrcweir //m_aBookmarks.erase(_nPos); 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 284