LCOV - code coverage report
Current view: top level - connectivity/source/commontools - TSkipDeletedSet.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 84 111 75.7 %
Date: 2014-11-03 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "TSkipDeletedSet.hxx"
      21             : #include <osl/diagnose.h>
      22             : #include <sal/log.hxx>
      23             : 
      24             : using namespace connectivity;
      25             : 
      26          46 : OSkipDeletedSet::OSkipDeletedSet(IResultSetHelper* _pHelper)
      27             :     : m_pHelper(_pHelper)
      28          46 :     ,m_bDeletedVisible(false)
      29             : {
      30          46 :     m_aBookmarksPositions.reserve(256);
      31          46 : }
      32             : 
      33          92 : OSkipDeletedSet::~OSkipDeletedSet()
      34             : {
      35          46 :     m_aBookmarksPositions.clear();
      36             :     //m_aBookmarks.clear();
      37          46 : }
      38             : 
      39         730 : bool OSkipDeletedSet::skipDeleted(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, bool _bRetrieveData)
      40             : {
      41             :     OSL_ENSURE(_eCursorPosition != IResultSetHelper::BOOKMARK,"OSkipDeletedSet::SkipDeleted can't be called for BOOKMARK");
      42             : 
      43         730 :     IResultSetHelper::Movement eDelPosition = _eCursorPosition;
      44         730 :     sal_Int32 nDelOffset = abs(_nOffset);
      45             : 
      46         730 :     switch (_eCursorPosition)
      47             :     {
      48             :         case IResultSetHelper::ABSOLUTE1:
      49         140 :             return moveAbsolute(_nOffset,_bRetrieveData);
      50             :         case IResultSetHelper::FIRST:                   // set the movement when positioning failed
      51         118 :             eDelPosition = IResultSetHelper::NEXT;
      52         118 :             nDelOffset = 1;
      53         118 :             break;
      54             :         case IResultSetHelper::LAST:
      55          20 :             eDelPosition = IResultSetHelper::PRIOR; // last row is invalid so position before
      56          20 :             nDelOffset = 1;
      57          20 :             break;
      58             :         case IResultSetHelper::RELATIVE1:
      59           0 :             eDelPosition = (_nOffset >= 0) ? IResultSetHelper::NEXT : IResultSetHelper::PRIOR;
      60           0 :             break;
      61             :         default:
      62         452 :             break;
      63             :     }
      64             : 
      65         590 :     bool bDone          = true;
      66         590 :     bool bDataFound     = false;
      67             : 
      68         590 :     if (_eCursorPosition == IResultSetHelper::LAST)
      69             :     {
      70             :         SAL_INFO( "connectivity.commontools", "OSkipDeletedSet::skipDeleted: last" );
      71          20 :         sal_Int32 nBookmark = 0;
      72             :         // first position on the last known row
      73          20 :         if ( m_aBookmarksPositions.empty() )
      74             :         {
      75           0 :             bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData);
      76           0 :             if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
      77             :                 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
      78           0 :                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
      79             :         }
      80             :         else
      81             :         {
      82             :             // I already have a bookmark so we can positioned on that and look if it is the last one
      83          20 :             nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
      84             : 
      85          20 :             bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
      86             :             OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"A bookmark should not be deleted!");
      87             :         }
      88             : 
      89             : 
      90             :         // and then move forward until we are after the last row
      91          40 :         while(bDataFound)
      92             :         {
      93          20 :             bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, false); // we don't need the data here
      94          20 :             if( bDataFound && ( m_bDeletedVisible || !m_pHelper->isRowDeleted()) )
      95             :             {   // we weren't on the last row we remember it and move on
      96           0 :                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
      97             :                 //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
      98             :             }
      99          20 :             else if(!bDataFound && !m_aBookmarksPositions.empty() )
     100             :             {
     101             :                 // i already know the last bookmark :-)
     102             :                 // now we only have to repositioning us to the last row
     103          20 :                 nBookmark = (*m_aBookmarksPositions.rbegin())/*->first*/;
     104          20 :                 bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK, nBookmark, _bRetrieveData);
     105          20 :                 break;
     106             :             }
     107             :         }
     108          20 :         return bDataFound;
     109             :     }
     110         570 :     else if (_eCursorPosition != IResultSetHelper::RELATIVE1)
     111             :     {
     112         570 :         bDataFound = m_pHelper->move(_eCursorPosition, _nOffset, _bRetrieveData);
     113         570 :         bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
     114             :     }
     115             :     else
     116             :     {
     117           0 :         bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
     118           0 :         if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
     119             :         {
     120           0 :             bDone = (--nDelOffset) == 0;
     121           0 :             if ( !bDone )
     122           0 :                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
     123             :             //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
     124             :         }
     125             :         else
     126           0 :             bDone = false;
     127             :     }
     128             : 
     129        4558 :     while (bDataFound && !bDone)            // Iterate until we are at the valid set
     130             :     {
     131        3418 :         bDataFound = m_pHelper->move(eDelPosition, 1, _bRetrieveData);
     132        3418 :         if (_eCursorPosition != IResultSetHelper::RELATIVE1)
     133        3418 :             bDone = bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted());
     134           0 :         else if (bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
     135             :         {
     136           0 :             bDone = (--nDelOffset) == 0;
     137           0 :             if ( !bDone )
     138           0 :                 m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
     139             :             //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
     140             :         }
     141             :         else
     142           0 :             bDone = false;
     143             :     }
     144             : 
     145         570 :     if(bDataFound && bDone)
     146             :     {
     147         444 :         const sal_Int32 nDriverPos = m_pHelper->getDriverPos();
     148         444 :         if ( m_bDeletedVisible )
     149             :         {
     150           0 :             if ( nDriverPos > (sal_Int32)m_aBookmarksPositions.size() )
     151           0 :                 m_aBookmarksPositions.push_back(nDriverPos);
     152             :         }
     153         444 :         else if ( ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),nDriverPos) == m_aBookmarksPositions.end() )
     154         340 :             m_aBookmarksPositions.push_back(nDriverPos);
     155             :         /*sal_Int32 nDriverPos = m_pHelper->getDriverPos();
     156             :         if(m_aBookmarks.find(nDriverPos) == m_aBookmarks.end())
     157             :             m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(nDriverPos,m_aBookmarksPositions.size()+1)).first);*/
     158             :     }
     159             : 
     160         570 :     return bDataFound;
     161             : }
     162             : 
     163         140 : bool OSkipDeletedSet::moveAbsolute(sal_Int32 _nPos,bool _bRetrieveData)
     164             : {
     165         140 :     bool bDataFound = false;
     166         140 :     sal_Int32 nNewPos = _nPos;
     167         140 :     if(nNewPos > 0)
     168             :     {
     169         140 :         if((sal_Int32)m_aBookmarksPositions.size() < nNewPos)
     170             :         {
     171             :             // bookmark isn't known yet
     172             :             // start at the last known position
     173          50 :             sal_Int32 nCurPos = 0,nLastBookmark = 1;
     174          50 :             if ( m_aBookmarksPositions.empty() )
     175             :             {
     176          18 :                 bDataFound = m_pHelper->move(IResultSetHelper::FIRST, 0, _bRetrieveData );
     177          18 :                 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
     178             :                 {
     179           2 :                     ++nCurPos;
     180           2 :                     m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
     181             :                     //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
     182           2 :                     --nNewPos;
     183             :                 }
     184             :             } // if ( m_aBookmarksPositions.empty() )
     185             :             else
     186             :             {
     187          32 :                 nLastBookmark   = (*m_aBookmarksPositions.rbegin())/*->first*/;
     188          32 :                 nCurPos         = /*(**/m_aBookmarksPositions.size()/*->second*/;
     189          32 :                 nNewPos         = nNewPos - nCurPos;
     190          32 :                 bDataFound      = m_pHelper->move(IResultSetHelper::BOOKMARK, nLastBookmark, _bRetrieveData);
     191             :             }
     192             : 
     193             :             // now move to that row we need and don't count deleted rows
     194         132 :             while (bDataFound && nNewPos)
     195             :             {
     196          32 :                 bDataFound = m_pHelper->move(IResultSetHelper::NEXT, 1, _bRetrieveData);
     197          32 :                 if(bDataFound && (m_bDeletedVisible || !m_pHelper->isRowDeleted()))
     198             :                 {
     199           0 :                     ++nCurPos;
     200           0 :                     m_aBookmarksPositions.push_back(m_pHelper->getDriverPos());
     201             :                     //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(m_pHelper->getDriverPos(),m_aBookmarksPositions.size()+1)).first);
     202           0 :                     --nNewPos;
     203             :                 }
     204             :             }
     205             :         }
     206             :         else
     207             :         {
     208          90 :             const sal_Int32 nBookmark = m_aBookmarksPositions[nNewPos-1]/*->first*/;
     209          90 :             bDataFound = m_pHelper->move(IResultSetHelper::BOOKMARK,nBookmark, _bRetrieveData);
     210             :             OSL_ENSURE((m_bDeletedVisible || !m_pHelper->isRowDeleted()),"moveAbsolute: row can't be deleted!");
     211             :         }
     212             :     }
     213             :     else
     214             :     {
     215           0 :         ++nNewPos;
     216           0 :         bDataFound = skipDeleted(IResultSetHelper::LAST,0,nNewPos == 0);
     217             : 
     218           0 :         for(sal_Int32 i=nNewPos+1;bDataFound && i <= 0;++i)
     219           0 :             bDataFound = skipDeleted(IResultSetHelper::PRIOR,1,i == 0);
     220             : 
     221             :     }
     222         140 :     return bDataFound;
     223             : }
     224             : 
     225          92 : void OSkipDeletedSet::clear()
     226             : {
     227          92 :     ::std::vector<sal_Int32>().swap(m_aBookmarksPositions);
     228             :     //TInt2IntMap().swap(m_aBookmarks);
     229          92 : }
     230             : 
     231          76 : sal_Int32 OSkipDeletedSet::getMappedPosition(sal_Int32 _nPos) const
     232             : {
     233          76 :     ::std::vector<sal_Int32>::const_iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos);
     234          76 :     if ( aFind !=  m_aBookmarksPositions.end() )
     235          76 :         return (aFind - m_aBookmarksPositions.begin()) + 1;
     236             :     /*TInt2IntMap::const_iterator aFind = m_aBookmarks.find(_nPos);
     237             :     OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::getMappedPosition() invalid bookmark!");
     238             :     return aFind->second;*/
     239             :     OSL_FAIL("Why!");
     240           0 :     return -1;
     241             : }
     242             : 
     243           2 : void OSkipDeletedSet::insertNewPosition(sal_Int32 _nPos)
     244             : {
     245             :     //OSL_ENSURE(m_aBookmarks.find(_nPos) == m_aBookmarks.end(),"OSkipDeletedSet::insertNewPosition: Invalid position");
     246             :     //m_aBookmarksPositions.push_back(m_aBookmarks.insert(TInt2IntMap::value_type(_nPos,m_aBookmarksPositions.size()+1)).first);
     247             :     //OSL_ENSURE(::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nPos) == m_aBookmarksPositions.end(),"Invalid driver pos");
     248           2 :     m_aBookmarksPositions.push_back(_nPos);
     249           2 : }
     250             : 
     251           2 : void OSkipDeletedSet::deletePosition(sal_Int32 _nBookmark)
     252             : {
     253           2 :     ::std::vector<sal_Int32>::iterator aFind = ::std::find(m_aBookmarksPositions.begin(),m_aBookmarksPositions.end(),_nBookmark);
     254           2 :     if ( aFind !=  m_aBookmarksPositions.end() )
     255             :     {
     256             :     //TInt2IntMap::iterator aFind = m_aBookmarks.find(_nPos);
     257             :     //OSL_ENSURE(aFind != m_aBookmarks.end(),"OSkipDeletedSet::deletePosition() bookmark not found!");
     258             :     //TInt2IntMap::iterator aIter = aFind;
     259           2 :         m_aBookmarksPositions.erase(aFind);
     260             :         //for (; aFind != m_aBookmarksPositions.end() ; ++aIter)
     261             :            // --(aFind->second);
     262             :     } // if ( aFind !=  m_aBookmarksPositions.end() )
     263             :     //m_aBookmarksPositions.erase(m_aBookmarksPositions.begin() + aFind->second-1);
     264             :     //m_aBookmarks.erase(_nPos);
     265           2 : }
     266             : 
     267             : 
     268             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10