LCOV - code coverage report
Current view: top level - libreoffice/sc/source/core/tool - doubleref.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 138 243 56.8 %
Date: 2012-12-17 Functions: 21 35 60.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 "doubleref.hxx"
      21             : #include "cell.hxx"
      22             : #include "global.hxx"
      23             : #include "document.hxx"
      24             : #include "queryparam.hxx"
      25             : #include "queryentry.hxx"
      26             : #include "globstr.hrc"
      27             : 
      28             : #include <memory>
      29             : #include <vector>
      30             : 
      31             : using ::rtl::OUString;
      32             : using ::std::auto_ptr;
      33             : using ::std::vector;
      34             : 
      35             : namespace {
      36             : 
      37         144 : void lcl_uppercase(OUString& rStr)
      38             : {
      39         144 :     rStr = ScGlobal::pCharClass->uppercase(rStr.trim());
      40         144 : }
      41             : 
      42          16 : bool lcl_createStarQuery(ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
      43             : {
      44             :     // A valid StarQuery must be at least 4 columns wide. To be precise it
      45             :     // should be exactly 4 columns ...
      46             :     // Additionally, if this wasn't checked, a formula pointing to a valid 1-3
      47             :     // column Excel style query range immediately left to itself would result
      48             :     // in a circular reference when the field name or operator or value (first
      49             :     // to third query range column) is obtained (#i58354#). Furthermore, if the
      50             :     // range wasn't sufficiently specified data changes wouldn't flag formula
      51             :     // cells for recalculation.
      52             : 
      53          16 :     if (pQueryRef->getColSize() < 4)
      54          16 :         return false;
      55             : 
      56             :     sal_Bool bValid;
      57           0 :     OUString aCellStr;
      58           0 :     SCSIZE nIndex = 0;
      59           0 :     SCROW nRow = 0;
      60           0 :     SCROW nRows = pDBRef->getRowSize();
      61           0 :     SCSIZE nNewEntries = static_cast<SCSIZE>(nRows);
      62           0 :     pParam->Resize(nNewEntries);
      63             : 
      64           0 :     do
      65             :     {
      66           0 :         ScQueryEntry& rEntry = pParam->GetEntry(nIndex);
      67             : 
      68           0 :         bValid = false;
      69             : 
      70           0 :         if (nIndex > 0)
      71             :         {
      72             :             // For all entries after the first one, check the and/or connector in the first column.
      73           0 :             aCellStr = pQueryRef->getString(0, nRow);
      74           0 :             lcl_uppercase(aCellStr);
      75           0 :             if ( aCellStr.equals(ScGlobal::GetRscString(STR_TABLE_UND)) )
      76             :             {
      77           0 :                 rEntry.eConnect = SC_AND;
      78           0 :                 bValid = sal_True;
      79             :             }
      80           0 :             else if ( aCellStr.equals(ScGlobal::GetRscString(STR_TABLE_ODER)) )
      81             :             {
      82           0 :                 rEntry.eConnect = SC_OR;
      83           0 :                 bValid = sal_True;
      84             :             }
      85             :         }
      86             : 
      87           0 :         if ((nIndex < 1) || bValid)
      88             :         {
      89             :             // field name in the 2nd column.
      90           0 :             aCellStr = pQueryRef->getString(1, nRow);
      91           0 :             SCCOL nField = pDBRef->findFieldColumn(aCellStr); // TODO: must be case insensitive comparison.
      92           0 :             if (ValidCol(nField))
      93             :             {
      94           0 :                 rEntry.nField = nField;
      95           0 :                 bValid = true;
      96             :             }
      97             :             else
      98           0 :                 bValid = false;
      99             :         }
     100             : 
     101           0 :         if (bValid)
     102             :         {
     103             :             // equality, non-equality operator in the 3rd column.
     104           0 :             aCellStr = pQueryRef->getString(2, nRow);
     105           0 :             lcl_uppercase(aCellStr);
     106           0 :             const sal_Unicode* p = aCellStr.getStr();
     107           0 :             if (p[0] == sal_Unicode('<'))
     108             :             {
     109           0 :                 if (p[1] == sal_Unicode('>'))
     110           0 :                     rEntry.eOp = SC_NOT_EQUAL;
     111           0 :                 else if (p[1] == sal_Unicode('='))
     112           0 :                     rEntry.eOp = SC_LESS_EQUAL;
     113             :                 else
     114           0 :                     rEntry.eOp = SC_LESS;
     115             :             }
     116           0 :             else if (p[0] == sal_Unicode('>'))
     117             :             {
     118           0 :                 if (p[1] == sal_Unicode('='))
     119           0 :                     rEntry.eOp = SC_GREATER_EQUAL;
     120             :                 else
     121           0 :                     rEntry.eOp = SC_GREATER;
     122             :             }
     123           0 :             else if (p[0] == sal_Unicode('='))
     124           0 :                 rEntry.eOp = SC_EQUAL;
     125             : 
     126             :         }
     127             : 
     128           0 :         if (bValid)
     129             :         {
     130             :             // Finally, the right-hand-side value in the 4th column.
     131           0 :             rEntry.GetQueryItem().maString = pQueryRef->getString(3, nRow);
     132           0 :             rEntry.bDoQuery = true;
     133             :         }
     134           0 :         nIndex++;
     135           0 :         nRow++;
     136             :     }
     137             :     while (bValid && (nRow < nRows) /* && (nIndex < MAXQUERY) */ );
     138           0 :     return bValid;
     139             : }
     140             : 
     141          16 : bool lcl_createExcelQuery(
     142             :     ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
     143             : {
     144          16 :     bool bValid = true;
     145          16 :     SCCOL nCols = pQueryRef->getColSize();
     146          16 :     SCROW nRows = pQueryRef->getRowSize();
     147          16 :     vector<SCCOL> aFields(nCols);
     148          16 :     SCCOL nCol = 0;
     149          80 :     while (bValid && (nCol < nCols))
     150             :     {
     151          48 :         OUString aQueryStr = pQueryRef->getString(nCol, 0);
     152          48 :         SCCOL nField = pDBRef->findFieldColumn(aQueryStr);
     153          48 :         if (ValidCol(nField))
     154          48 :             aFields[nCol] = nField;
     155             :         else
     156           0 :             bValid = false;
     157          48 :         ++nCol;
     158          48 :     }
     159             : 
     160          16 :     if (bValid)
     161             :     {
     162             :         // Count the number of visible cells (excluding the header row).  Each
     163             :         // visible cell corresponds with a single query.
     164          16 :         SCSIZE nVisible = pQueryRef->getVisibleDataCellCount();
     165          16 :         if ( nVisible > SCSIZE_MAX / sizeof(void*) )
     166             :         {
     167             :             OSL_FAIL("zu viele Filterkritierien");
     168           0 :             nVisible = 0;
     169             :         }
     170             : 
     171          16 :         SCSIZE nNewEntries = nVisible;
     172          16 :         pParam->Resize( nNewEntries );
     173             : 
     174          16 :         SCSIZE nIndex = 0;
     175          16 :         SCROW nRow = 1;
     176          16 :         String aCellStr;
     177          48 :         while (nRow < nRows)
     178             :         {
     179          16 :             nCol = 0;
     180          80 :             while (nCol < nCols)
     181             :             {
     182          48 :                 aCellStr = pQueryRef->getString(nCol, nRow);
     183          48 :                 aCellStr = ScGlobal::pCharClass->uppercase( aCellStr );
     184          48 :                 if (aCellStr.Len() > 0)
     185             :                 {
     186          24 :                     if (nIndex < nNewEntries)
     187             :                     {
     188          24 :                         pParam->GetEntry(nIndex).nField = aFields[nCol];
     189          24 :                         pParam->FillInExcelSyntax(rtl::OUString(aCellStr), nIndex);
     190          24 :                         nIndex++;
     191          24 :                         if (nIndex < nNewEntries)
     192          24 :                             pParam->GetEntry(nIndex).eConnect = SC_AND;
     193             :                     }
     194             :                     else
     195           0 :                         bValid = false;
     196             :                 }
     197          48 :                 nCol++;
     198             :             }
     199          16 :             nRow++;
     200          16 :             if (nIndex < nNewEntries)
     201          16 :                 pParam->GetEntry(nIndex).eConnect = SC_OR;
     202          16 :         }
     203             :     }
     204          16 :     return bValid;
     205             : }
     206             : 
     207          16 : bool lcl_fillQueryEntries(
     208             :     ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef, const ScDBRangeBase* pQueryRef)
     209             : {
     210          16 :     SCSIZE nCount = pParam->GetEntryCount();
     211         144 :     for (SCSIZE i = 0; i < nCount; ++i)
     212         128 :         pParam->GetEntry(i).Clear();
     213             : 
     214             :     // Standard QueryTabelle
     215          16 :     bool bValid = lcl_createStarQuery(pParam, pDBRef, pQueryRef);
     216             :     // Excel QueryTabelle
     217          16 :     if (!bValid)
     218          16 :         bValid = lcl_createExcelQuery(pParam, pDBRef, pQueryRef);
     219             : 
     220          16 :     nCount = pParam->GetEntryCount();
     221          16 :     if (bValid)
     222             :     {
     223             :         //  bQueryByString muss gesetzt sein
     224         144 :         for (SCSIZE i = 0; i < nCount; ++i)
     225         128 :             pParam->GetEntry(i).GetQueryItem().meType = ScQueryEntry::ByString;
     226             :     }
     227             :     else
     228             :     {
     229             :         //  nix
     230           0 :         for (SCSIZE i = 0; i < nCount; ++i)
     231           0 :             pParam->GetEntry(i).Clear();
     232             :     }
     233          16 :     return bValid;
     234             : }
     235             : 
     236             : }
     237             : 
     238             : // ============================================================================
     239             : 
     240          32 : ScDBRangeBase::ScDBRangeBase(ScDocument* pDoc, RefType eType) :
     241          32 :     mpDoc(pDoc), meType(eType)
     242             : {
     243          32 : }
     244             : 
     245          32 : ScDBRangeBase::~ScDBRangeBase()
     246             : {
     247          32 : }
     248             : 
     249          16 : bool ScDBRangeBase::fillQueryEntries(ScQueryParamBase* pParam, const ScDBRangeBase* pDBRef) const
     250             : {
     251          16 :     if (!pDBRef)
     252           0 :         return false;
     253             : 
     254          16 :     return lcl_fillQueryEntries(pParam, pDBRef, this);
     255             : }
     256             : 
     257          16 : void ScDBRangeBase::fillQueryOptions(ScQueryParamBase* pParam)
     258             : {
     259          16 :     pParam->bHasHeader = true;
     260          16 :     pParam->bByRow = true;
     261          16 :     pParam->bInplace = true;
     262          16 :     pParam->bCaseSens = false;
     263          16 :     pParam->bRegExp = false;
     264          16 :     pParam->bDuplicate = true;
     265          16 : }
     266             : 
     267         192 : ScDocument* ScDBRangeBase::getDoc() const
     268             : {
     269         192 :     return mpDoc;
     270             : }
     271             : 
     272             : // ============================================================================
     273             : 
     274          32 : ScDBInternalRange::ScDBInternalRange(ScDocument* pDoc, const ScRange& rRange) :
     275          32 :     ScDBRangeBase(pDoc, INTERNAL), maRange(rRange)
     276             : {
     277          32 : }
     278             : 
     279          64 : ScDBInternalRange::~ScDBInternalRange()
     280             : {
     281          64 : }
     282             : 
     283          24 : const ScRange& ScDBInternalRange::getRange() const
     284             : {
     285          24 :     return maRange;
     286             : }
     287             : 
     288          48 : SCCOL ScDBInternalRange::getColSize() const
     289             : {
     290          48 :     return maRange.aEnd.Col() - maRange.aStart.Col() + 1;
     291             : }
     292             : 
     293          32 : SCROW ScDBInternalRange::getRowSize() const
     294             : {
     295          32 :     return maRange.aEnd.Row() - maRange.aStart.Row() + 1;
     296             : }
     297             : 
     298          16 : SCSIZE ScDBInternalRange::getVisibleDataCellCount() const
     299             : {
     300          16 :     SCCOL nCols = getColSize();
     301          16 :     SCROW nRows = getRowSize();
     302          16 :     if (nRows <= 1)
     303           0 :         return 0;
     304             : 
     305          16 :     return (nRows-1)*nCols;
     306             : }
     307             : 
     308          96 : OUString ScDBInternalRange::getString(SCCOL nCol, SCROW nRow) const
     309             : {
     310          96 :     OUString aStr;
     311          96 :     const ScAddress& s = maRange.aStart;
     312             :     // #i109200# this is used in formula calculation, use GetInputString, not GetString
     313             :     // (consistent with ScDBInternalRange::getCellString)
     314             :     // GetStringForFormula is not used here, to allow querying for date values.
     315          96 :     getDoc()->GetInputString(s.Col() + nCol, s.Row() + nRow, maRange.aStart.Tab(), aStr);
     316          96 :     return aStr;
     317             : }
     318             : 
     319          16 : SCCOL ScDBInternalRange::getFirstFieldColumn() const
     320             : {
     321          16 :     return getRange().aStart.Col();
     322             : }
     323             : 
     324           8 : SCCOL ScDBInternalRange::findFieldColumn(SCCOL nIndex) const
     325             : {
     326           8 :     const ScRange& rRange = getRange();
     327           8 :     const ScAddress& s = rRange.aStart;
     328             : 
     329           8 :     SCCOL nDBCol1 = s.Col();
     330             : 
     331             :     // Don't handle out-of-bound condition here.  We'll do that later.
     332           8 :     return nIndex + nDBCol1 - 1;
     333             : }
     334             : 
     335          48 : SCCOL ScDBInternalRange::findFieldColumn(const OUString& rStr, sal_uInt16* pErr) const
     336             : {
     337          48 :     const ScAddress& s = maRange.aStart;
     338          48 :     const ScAddress& e = maRange.aEnd;
     339          48 :     OUString aUpper = rStr;
     340          48 :     lcl_uppercase(aUpper);
     341             : 
     342          48 :     SCCOL nDBCol1 = s.Col();
     343          48 :     SCROW nDBRow1 = s.Row();
     344          48 :     SCTAB nDBTab1 = s.Tab();
     345          48 :     SCCOL nDBCol2 = e.Col();
     346             : 
     347          48 :     SCCOL   nField = nDBCol1;
     348          48 :     sal_Bool    bFound = sal_True;
     349             : 
     350          48 :     bFound = false;
     351          48 :     OUString aCellStr;
     352          48 :     ScAddress aLook( nDBCol1, nDBRow1, nDBTab1 );
     353         192 :     while (!bFound && (aLook.Col() <= nDBCol2))
     354             :     {
     355          96 :         sal_uInt16 nErr = getDoc()->GetStringForFormula( aLook, aCellStr );
     356          96 :         if (pErr)
     357           0 :             *pErr = nErr;
     358          96 :         lcl_uppercase(aCellStr);
     359          96 :         bFound = ScGlobal::GetpTransliteration()->isEqual(aCellStr, aUpper);
     360          96 :         if (!bFound)
     361          48 :             aLook.IncCol();
     362             :     }
     363          48 :     nField = aLook.Col();
     364             : 
     365          48 :     return bFound ? nField : -1;
     366             : }
     367             : 
     368          16 : ScDBQueryParamBase* ScDBInternalRange::createQueryParam(const ScDBRangeBase* pQueryRef) const
     369             : {
     370          16 :     auto_ptr<ScDBQueryParamInternal> pParam(new ScDBQueryParamInternal);
     371             : 
     372             :     // Set the database range first.
     373          16 :     const ScAddress& s = maRange.aStart;
     374          16 :     const ScAddress& e = maRange.aEnd;
     375          16 :     pParam->nCol1 = s.Col();
     376          16 :     pParam->nRow1 = s.Row();
     377          16 :     pParam->nCol2 = e.Col();
     378          16 :     pParam->nRow2 = e.Row();
     379          16 :     pParam->nTab  = s.Tab();
     380             : 
     381          16 :     fillQueryOptions(pParam.get());
     382             : 
     383             :     // Now construct the query entries from the query range.
     384          16 :     if (!pQueryRef->fillQueryEntries(pParam.get(), this))
     385           0 :         return NULL;
     386             : 
     387          16 :     return pParam.release();
     388             : }
     389             : 
     390           0 : bool ScDBInternalRange::isRangeEqual(const ScRange& rRange) const
     391             : {
     392           0 :     return maRange == rRange;
     393             : }
     394             : 
     395             : // ============================================================================
     396             : 
     397           0 : ScDBExternalRange::ScDBExternalRange(ScDocument* pDoc, const ScMatrixRef& pMat) :
     398           0 :     ScDBRangeBase(pDoc, EXTERNAL), mpMatrix(pMat)
     399             : {
     400             :     SCSIZE nC, nR;
     401           0 :     mpMatrix->GetDimensions(nC, nR);
     402           0 :     mnCols = static_cast<SCCOL>(nC);
     403           0 :     mnRows = static_cast<SCROW>(nR);
     404           0 : }
     405             : 
     406           0 : ScDBExternalRange::~ScDBExternalRange()
     407             : {
     408           0 : }
     409             : 
     410           0 : SCCOL ScDBExternalRange::getColSize() const
     411             : {
     412           0 :     return mnCols;
     413             : }
     414             : 
     415           0 : SCROW ScDBExternalRange::getRowSize() const
     416             : {
     417           0 :     return mnRows;
     418             : }
     419             : 
     420           0 : SCSIZE ScDBExternalRange::getVisibleDataCellCount() const
     421             : {
     422           0 :     SCCOL nCols = getColSize();
     423           0 :     SCROW nRows = getRowSize();
     424           0 :     if (nRows <= 1)
     425           0 :         return 0;
     426             : 
     427           0 :     return (nRows-1)*nCols;
     428             : }
     429             : 
     430           0 : OUString ScDBExternalRange::getString(SCCOL nCol, SCROW nRow) const
     431             : {
     432           0 :     if (nCol >= mnCols || nRow >= mnRows)
     433           0 :         return OUString();
     434             : 
     435           0 :     return mpMatrix->GetString(nCol, nRow);
     436             : }
     437             : 
     438           0 : SCCOL ScDBExternalRange::getFirstFieldColumn() const
     439             : {
     440           0 :     return 0;
     441             : }
     442             : 
     443           0 : SCCOL ScDBExternalRange::findFieldColumn(SCCOL nIndex) const
     444             : {
     445           0 :     return nIndex - 1;
     446             : }
     447             : 
     448           0 : SCCOL ScDBExternalRange::findFieldColumn(const OUString& rStr, sal_uInt16* pErr) const
     449             : {
     450           0 :     if (pErr)
     451           0 :         pErr = 0;
     452             : 
     453           0 :     OUString aUpper = rStr;
     454           0 :     lcl_uppercase(aUpper);
     455           0 :     for (SCCOL i = 0; i < mnCols; ++i)
     456             :     {
     457           0 :         OUString aUpperVal = mpMatrix->GetString(i, 0);
     458           0 :         lcl_uppercase(aUpperVal);
     459           0 :         if (aUpper.equals(aUpperVal))
     460           0 :             return i;
     461           0 :     }
     462           0 :     return -1;
     463             : }
     464             : 
     465           0 : ScDBQueryParamBase* ScDBExternalRange::createQueryParam(const ScDBRangeBase* pQueryRef) const
     466             : {
     467           0 :     auto_ptr<ScDBQueryParamMatrix> pParam(new ScDBQueryParamMatrix);
     468           0 :     pParam->mpMatrix = mpMatrix;
     469           0 :     fillQueryOptions(pParam.get());
     470             : 
     471             :     // Now construct the query entries from the query range.
     472           0 :     if (!pQueryRef->fillQueryEntries(pParam.get(), this))
     473           0 :         return NULL;
     474             : 
     475           0 :     return pParam.release();
     476             : }
     477             : 
     478           0 : bool ScDBExternalRange::isRangeEqual(const ScRange& /*rRange*/) const
     479             : {
     480           0 :     return false;
     481             : }
     482             : 
     483             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10