LCOV - code coverage report
Current view: top level - connectivity/source/drivers/file - fanalyzer.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 74 123 60.2 %
Date: 2014-04-11 Functions: 13 14 92.9 %
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 "file/fanalyzer.hxx"
      21             : #include "connectivity/sqlparse.hxx"
      22             : #include <osl/diagnose.h>
      23             : #include <tools/debug.hxx>
      24             : #include <comphelper/extract.hxx>
      25             : #include "connectivity/sqlnode.hxx"
      26             : #include "connectivity/dbexception.hxx"
      27             : #include "file/FConnection.hxx"
      28             : #include "resource/file_res.hrc"
      29             : 
      30             : using namespace ::connectivity;
      31             : using namespace ::connectivity::file;
      32             : using namespace ::com::sun::star::uno;
      33             : using namespace ::com::sun::star::beans;
      34             : using namespace ::com::sun::star::sdbc;
      35             : using namespace ::com::sun::star::container;
      36             : 
      37          40 : OSQLAnalyzer::OSQLAnalyzer(OConnection* _pConnection)
      38             :                :m_pConnection(_pConnection)
      39             :                ,m_bHasSelectionCode(sal_False)
      40          40 :                ,m_bSelectionFirstTime(sal_True)
      41             : {
      42          40 :     m_aCompiler = new OPredicateCompiler(this);
      43          40 :     m_aInterpreter = new OPredicateInterpreter(m_aCompiler);
      44          40 : }
      45             : 
      46             : 
      47          80 : OSQLAnalyzer::~OSQLAnalyzer()
      48             : {
      49          80 : }
      50             : 
      51             : 
      52          40 : void OSQLAnalyzer::setIndexes(const Reference< XNameAccess>& _xIndexes)
      53             : {
      54          40 :     m_aCompiler->m_xIndexes = _xIndexes;
      55          40 : }
      56             : 
      57          40 : void OSQLAnalyzer::start(OSQLParseNode* pSQLParseNode)
      58             : {
      59          40 :     if (SQL_ISRULE(pSQLParseNode,select_statement))
      60             :     {
      61             :         DBG_ASSERT(pSQLParseNode->count() >= 4,"OFILECursor: Fehler im Parse Tree");
      62             : 
      63             :         // check that we don't use anything other than count(*) as function
      64          40 :         OSQLParseNode* pSelection = pSQLParseNode->getChild(2);
      65          40 :         if ( SQL_ISRULE(pSelection,scalar_exp_commalist) )
      66             :         {
      67          32 :             for (sal_uInt32 i = 0; i < pSelection->count(); i++)
      68             :             {
      69          18 :                 OSQLParseNode *pColumnRef = pSelection->getChild(i)->getChild(0);
      70          54 :                 if (    ( SQL_ISRULE(pColumnRef,set_fct_spec) && pColumnRef->count() == 4 )
      71          18 :                     ||  SQL_ISRULE(pColumnRef,char_value_fct)
      72          18 :                     ||  SQL_ISRULE(pColumnRef,char_substring_fct)
      73          18 :                     ||  SQL_ISRULE(pColumnRef,position_exp)
      74          18 :                     ||  SQL_ISRULE(pColumnRef,fold)
      75          18 :                     ||  SQL_ISRULE(pColumnRef,length_exp)
      76          18 :                     ||  SQL_ISRULE(pColumnRef,num_value_exp)
      77          18 :                     ||  SQL_ISRULE(pColumnRef,term)
      78          18 :                     ||  SQL_ISRULE(pColumnRef,factor)
      79          36 :                     ||  SQL_ISRULE(pColumnRef,set_fct_spec) )
      80             :                 {
      81           0 :                     ::rtl::Reference<OPredicateCompiler>        pCompiler = new OPredicateCompiler(this);
      82           0 :                     pCompiler->setOrigColumns(m_aCompiler->getOrigColumns());
      83           0 :                     ::rtl::Reference<OPredicateInterpreter> pInterpreter = new OPredicateInterpreter(pCompiler);
      84           0 :                     pCompiler->execute( pColumnRef );
      85           0 :                     m_aSelectionEvaluations.push_back( TPredicates(pCompiler,pInterpreter) );
      86             :                 }
      87          18 :                 else if ( ( SQL_ISRULE(pColumnRef,general_set_fct) && pColumnRef->count() != 4 ) )
      88             :                 {
      89           0 :                     m_pConnection->throwGenericSQLException(STR_QUERY_COMPLEX_COUNT,NULL);
      90             :                 }
      91             :                 else
      92             :                 {
      93          36 :                     if  (   SQL_ISPUNCTUATION( pColumnRef, "*" )
      94          36 :                         ||  (   SQL_ISRULE( pColumnRef, column_ref )
      95          18 :                             &&  ( pColumnRef->count() == 3 )
      96           0 :                             &&  ( pColumnRef->getChild(0)->getNodeType() == SQL_NODE_NAME )
      97           0 :                             &&  SQL_ISPUNCTUATION( pColumnRef->getChild(1), "." )
      98           0 :                             &&  SQL_ISRULE( pColumnRef->getChild(2), column_val )
      99           0 :                             &&  SQL_ISPUNCTUATION( pColumnRef->getChild(2)->getChild(0), "*" )
     100             :                             )
     101             :                         )
     102             :                     {
     103             :                         // push one element for each column of our table
     104           0 :                         const Reference< XNameAccess > xColumnNames( m_aCompiler->getOrigColumns() );
     105           0 :                         const Sequence< OUString > aColumnNames( xColumnNames->getElementNames() );
     106           0 :                         for ( sal_Int32 j=0; j<aColumnNames.getLength(); ++j )
     107           0 :                             m_aSelectionEvaluations.push_back( TPredicates() );
     108             :                     }
     109             :                     else
     110          18 :                         m_aSelectionEvaluations.push_back( TPredicates() );
     111             :                 }
     112             :             }
     113             :         }
     114             :     }
     115             : 
     116          40 :     m_aCompiler->start(pSQLParseNode);
     117          40 : }
     118             : 
     119             : 
     120          21 : void OSQLAnalyzer::bindRow(OCodeList& rCodeList,const OValueRefRow& _pRow,OEvaluateSetList& _rEvaluateSetList)
     121             : {
     122             :     // count criteria
     123             :     // if only one criterion, and the corresponding field is indexed
     124             :     // then the index will be used
     125          21 :     OEvaluateSet*       pEvaluateSet = NULL;
     126             : 
     127          39 :     for (OCodeList::iterator aIter = rCodeList.begin(); aIter != rCodeList.end(); ++aIter)
     128             :     {
     129          18 :         OOperandAttr* pAttr = PTR_CAST(OOperandAttr,(*aIter));
     130          18 :         if (pAttr)
     131             :         {
     132           6 :             if (pAttr->isIndexed() && !m_aCompiler->hasORCondition())
     133             :             {
     134           0 :                 OCode* pCode1 = *(aIter + 1);
     135           0 :                 OCode* pCode2 = *(aIter + 2);
     136             : 
     137           0 :                 if (PTR_CAST(OOperand,pCode1))
     138           0 :                     pEvaluateSet = pAttr->preProcess(PTR_CAST(OBoolOperator,pCode2), PTR_CAST(OOperand,pCode1));
     139             :                 else
     140           0 :                     pEvaluateSet = pAttr->preProcess(PTR_CAST(OBoolOperator,pCode1));
     141             :             }
     142             : 
     143           6 :             if (pEvaluateSet)
     144             :             {
     145           0 :                 _rEvaluateSetList.push_back(pEvaluateSet);
     146           0 :                 pEvaluateSet = NULL;
     147             :             }
     148           6 :             pAttr->bindValue(_pRow);
     149             :         }
     150             :     }
     151          21 : }
     152             : 
     153          21 : void OSQLAnalyzer::bindSelectRow(const OValueRefRow& _pRow)
     154             : {
     155             :     // first the select part
     156          21 :     OEvaluateSetList    aEvaluateSetList;
     157          30 :     for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
     158             :     {
     159           9 :         if ( aIter->first.is() )
     160           0 :             bindRow( aIter->first->m_aCodeList,_pRow,aEvaluateSetList);
     161          21 :     }
     162          21 : }
     163             : 
     164          21 : ::std::vector<sal_Int32>* OSQLAnalyzer::bindEvaluationRow(OValueRefRow& _pRow)
     165             : {
     166          21 :     OEvaluateSetList    aEvaluateSetList;
     167          21 :     bindRow( m_aCompiler->m_aCodeList,_pRow,aEvaluateSetList);
     168             : 
     169          21 :     ::std::vector<sal_Int32>*   pKeySet      = NULL;
     170          21 :     OEvaluateSet*               pEvaluateSet = NULL;
     171             : 
     172             :     // create Keyset with smallest list
     173          21 :     if(!aEvaluateSetList.empty())
     174             :     {
     175             :         // which list has the smallest count?
     176           0 :         OEvaluateSetList::iterator i = aEvaluateSetList.begin();
     177           0 :         pEvaluateSet = *(i);
     178           0 :         for(++i; i != aEvaluateSetList.end();++i)
     179             :         {
     180           0 :             OEvaluateSet*   pEvaluateSetComp = (*i);
     181           0 :             for(OEvaluateSet::reverse_iterator j = pEvaluateSet->rbegin(); j != pEvaluateSet->rend(); ++j)
     182             :             {
     183           0 :                 if (pEvaluateSetComp->find(j->second) != pEvaluateSetComp->end())
     184           0 :                     pEvaluateSet->erase(j->second);
     185             :             }
     186             :         }
     187           0 :         pKeySet = new ::std::vector<sal_Int32>(pEvaluateSet->size());
     188           0 :         sal_Int32 k=0;
     189           0 :         for(OEvaluateSet::iterator j = pEvaluateSet->begin(); j != pEvaluateSet->end(); ++j,++k)
     190             :         {
     191           0 :             (*pKeySet)[k] = j->second;
     192             :         }
     193             : 
     194             :         // delete all
     195           0 :         for(i = aEvaluateSetList.begin(); i != aEvaluateSetList.end();++i)
     196           0 :             delete (*i);
     197             :     }
     198             : 
     199          21 :     return pKeySet;
     200             : }
     201             : 
     202             : 
     203           6 : OOperandAttr* OSQLAnalyzer::createOperandAttr(sal_Int32 _nPos,
     204             :                                               const Reference< XPropertySet>& _xCol,
     205             :                                               const Reference< XNameAccess>& /*_xIndexes*/)
     206             : {
     207           6 :     return new OOperandAttr(static_cast<sal_uInt16>(_nPos),_xCol);
     208             : }
     209             : 
     210        1726 : sal_Bool OSQLAnalyzer::hasRestriction() const
     211             : {
     212        1726 :     return m_aCompiler->hasCode();
     213             : }
     214             : 
     215        1568 : sal_Bool OSQLAnalyzer::hasFunctions() const
     216             : {
     217        1568 :     if ( m_bSelectionFirstTime )
     218             :     {
     219          17 :         m_bSelectionFirstTime = sal_False;
     220          22 :         for ( ::std::vector< TPredicates >::const_iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end() && !m_bHasSelectionCode ;++aIter)
     221             :         {
     222           5 :             if ( aIter->first.is() )
     223           0 :                 m_bHasSelectionCode = aIter->first->hasCode();
     224             :         }
     225             :     }
     226        1568 :     return m_bHasSelectionCode;
     227             : }
     228             : 
     229           0 : void OSQLAnalyzer::setSelectionEvaluationResult(OValueRefRow& _pRow,const ::std::vector<sal_Int32>& _rColumnMapping)
     230             : {
     231           0 :     sal_Int32 nPos = 1;
     232           0 :     for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter,++nPos)
     233             :     {
     234           0 :         if ( aIter->second.is() )
     235             :         {
     236             :             // the first column (index 0) is for convenience only. The first real select column is no 1.
     237           0 :             sal_Int32   map = nPos;
     238           0 :             if ( nPos < static_cast< sal_Int32 >( _rColumnMapping.size() ) )
     239           0 :                 map = _rColumnMapping[nPos];
     240           0 :             if ( map > 0 )
     241           0 :                 aIter->second->startSelection( (_pRow->get())[map] );
     242             :         }
     243             :     }
     244           0 : }
     245             : 
     246          40 : void OSQLAnalyzer::dispose()
     247             : {
     248          40 :     m_aCompiler->dispose();
     249          58 :     for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
     250             :     {
     251          18 :         if ( aIter->first.is() )
     252           0 :             aIter->first->dispose();
     253             :     }
     254          40 : }
     255             : 
     256          40 : void OSQLAnalyzer::setOrigColumns(const OFileColumns& rCols)
     257             : {
     258          40 :     m_aCompiler->setOrigColumns(rCols);
     259          40 :     for ( ::std::vector< TPredicates >::iterator aIter = m_aSelectionEvaluations.begin(); aIter != m_aSelectionEvaluations.end();++aIter)
     260             :     {
     261           0 :         if ( aIter->first.is() )
     262           0 :             aIter->first->setOrigColumns(rCols);
     263             :     }
     264          40 : }
     265             : 
     266             : 
     267             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10