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

Generated by: LCOV version 1.10