LCOV - code coverage report
Current view: top level - connectivity/source/parse - sqlnode.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 452 1256 36.0 %
Date: 2015-06-13 12:38:46 Functions: 43 71 60.6 %
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 <sal/macros.h>
      21             : #include <connectivity/sqlnode.hxx>
      22             : #include <connectivity/sqlerror.hxx>
      23             : #include <connectivity/sqlbison_exports.hxx>
      24             : #include <connectivity/internalnode.hxx>
      25             : #define YYBISON   1
      26             : #include <sqlbison.hxx>
      27             : #include <connectivity/sqlparse.hxx>
      28             : #include <connectivity/sqlscan.hxx>
      29             : #include <com/sun/star/lang/Locale.hpp>
      30             : #include <com/sun/star/util/XNumberFormatter.hpp>
      31             : #include <com/sun/star/util/XNumberFormatTypes.hpp>
      32             : #include <com/sun/star/i18n/LocaleData.hpp>
      33             : #include <com/sun/star/i18n/NumberFormatIndex.hpp>
      34             : #include <com/sun/star/beans/XPropertySet.hpp>
      35             : #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
      36             : #include <com/sun/star/sdbc/DataType.hpp>
      37             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      38             : #include <com/sun/star/sdb/ErrorCondition.hpp>
      39             : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
      40             : #include <com/sun/star/util/XNumberFormats.hpp>
      41             : #include <com/sun/star/util/NumberFormat.hpp>
      42             : #include <com/sun/star/i18n/KParseType.hpp>
      43             : #include <com/sun/star/i18n/KParseTokens.hpp>
      44             : #include <com/sun/star/i18n/CharacterClassification.hpp>
      45             : #include <connectivity/dbconversion.hxx>
      46             : #include <com/sun/star/util/DateTime.hpp>
      47             : #include <com/sun/star/util/Time.hpp>
      48             : #include <com/sun/star/util/Date.hpp>
      49             : #include "TConnection.hxx"
      50             : #include <comphelper/numbers.hxx>
      51             : #include <comphelper/processfactory.hxx>
      52             : #include <connectivity/dbtools.hxx>
      53             : #include <connectivity/dbmetadata.hxx>
      54             : #include <tools/diagnose_ex.h>
      55             : #include <string.h>
      56             : #include <boost/bind.hpp>
      57             : #include <boost/scoped_ptr.hpp>
      58             : #include <algorithm>
      59             : #include <functional>
      60             : #include <rtl/ustrbuf.hxx>
      61             : 
      62             : using namespace ::com::sun::star::sdbc;
      63             : using namespace ::com::sun::star::util;
      64             : using namespace ::com::sun::star::beans;
      65             : using namespace ::com::sun::star::sdb;
      66             : using namespace ::com::sun::star::uno;
      67             : using namespace ::com::sun::star::lang;
      68             : using namespace ::com::sun::star::i18n;
      69             : using namespace ::com::sun::star;
      70             : using namespace ::osl;
      71             : using namespace ::dbtools;
      72             : using namespace ::comphelper;
      73             : 
      74             : connectivity::OSQLParser* xxx_pGLOBAL_SQLPARSER;
      75             : 
      76             : namespace
      77             : {
      78             : 
      79           0 :     bool lcl_saveConvertToNumber(const Reference< XNumberFormatter > & _xFormatter,sal_Int32 _nKey,const OUString& _sValue,double& _nrValue)
      80             :     {
      81           0 :         bool bRet = false;
      82             :         try
      83             :         {
      84           0 :             _nrValue = _xFormatter->convertStringToNumber(_nKey, _sValue);
      85           0 :             bRet = true;
      86             :         }
      87           0 :         catch(Exception&)
      88             :         {
      89             :         }
      90           0 :         return bRet;
      91             :     }
      92             : 
      93          20 :     void replaceAndReset(connectivity::OSQLParseNode*& _pResetNode,connectivity::OSQLParseNode* _pNewNode)
      94             :     {
      95          20 :         _pResetNode->getParent()->replace(_pResetNode, _pNewNode);
      96          20 :         delete _pResetNode;
      97          20 :         _pResetNode = _pNewNode;
      98          20 :     }
      99             : 
     100             :     /** quotes a string and search for quotes inside the string and replace them with the new quote
     101             :         @param  rValue
     102             :             The value to be quoted.
     103             :         @param  rQuot
     104             :             The quote
     105             :         @param  rQuotToReplace
     106             :             The quote to replace with
     107             :         @return
     108             :             The quoted string.
     109             :     */
     110        3186 :     OUString SetQuotation(const OUString& rValue, const OUString& rQuot, const OUString& rQuotToReplace)
     111             :     {
     112        3186 :         OUString rNewValue = rQuot;
     113        3186 :         rNewValue += rValue;
     114        3186 :         sal_Int32 nIndex = (sal_Int32)-1;   // Replace quotes with double quotes or the parser gets into problems
     115             : 
     116        3186 :         if (!rQuot.isEmpty())
     117             :         {
     118        3186 :             do
     119             :             {
     120        3186 :                 nIndex += 2;
     121        3186 :                 nIndex = rNewValue.indexOf(rQuot,nIndex);
     122        3186 :                 if(nIndex != -1)
     123           0 :                     rNewValue = rNewValue.replaceAt(nIndex,rQuot.getLength(),rQuotToReplace);
     124             :             } while (nIndex != -1);
     125             :         }
     126             : 
     127        3186 :         rNewValue += rQuot;
     128        3186 :         return rNewValue;
     129             :     }
     130             : 
     131           0 :     bool columnMatchP(const connectivity::OSQLParseNode* pSubTree, const connectivity::SQLParseNodeParameter& rParam)
     132             :     {
     133             :         using namespace connectivity;
     134             :         assert(SQL_ISRULE(pSubTree,column_ref));
     135             : 
     136           0 :         if(!rParam.xField.is())
     137           0 :             return false;
     138             : 
     139             :         // retrieve the field's name & table range
     140           0 :         OUString aFieldName;
     141             :         try
     142             :         {
     143           0 :             sal_Int32 nNamePropertyId = PROPERTY_ID_NAME;
     144           0 :             if ( rParam.xField->getPropertySetInfo()->hasPropertyByName( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_REALNAME ) ) )
     145           0 :                 nNamePropertyId = PROPERTY_ID_REALNAME;
     146           0 :             rParam.xField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( nNamePropertyId ) ) >>= aFieldName;
     147             :         }
     148           0 :         catch ( Exception& )
     149             :         {
     150             :         }
     151             : 
     152           0 :         if(pSubTree->count())
     153             :         {
     154           0 :             const OSQLParseNode* pCol = pSubTree->getChild(pSubTree->count()-1);
     155           0 :             if (SQL_ISRULE(pCol,column_val))
     156             :             {
     157             :                 assert(pCol->count() == 1);
     158           0 :                 pCol = pCol->getChild(0);
     159             :             }
     160           0 :             const OSQLParseNode* pTable(NULL);
     161           0 :             switch (pSubTree->count())
     162             :             {
     163             :             case 1:
     164           0 :                 break;
     165             :             case 3:
     166           0 :                 pTable = pSubTree->getChild(0);
     167           0 :                 break;
     168             :             case 5:
     169             :             case 7:
     170             :                 SAL_WARN("connectivity.parse", "SQL: catalog and/or schema in column_ref in predicate");
     171           0 :                 break;
     172             :             default:
     173             :                 SAL_WARN("connectivity.parse", "columnMatchP: SQL grammar changed; column_ref has " << pSubTree->count() << " children");
     174             :                 assert(false);
     175           0 :                 break;
     176             :             }
     177             :             // TODO: not all DBMS match column names case-insensitively...
     178             :             // see XDatabaseMetaData::supportsMixedCaseIdentifiers()
     179             :             // and XDatabaseMetaData::supportsMixedCaseQuotedIdentifiers()
     180           0 :             if  (   // table name matches (or no table name)?
     181           0 :                     ( !pTable || pTable->getTokenValue().equalsIgnoreAsciiCase(rParam.sPredicateTableAlias) )
     182           0 :                  && // column name matches?
     183           0 :                     pCol->getTokenValue().equalsIgnoreAsciiCase(aFieldName)
     184             :                 )
     185           0 :                 return true;
     186             :         }
     187           0 :         return false;
     188             :     }
     189             : }
     190             : 
     191             : namespace connectivity
     192             : {
     193             : 
     194        2071 : SQLParseNodeParameter::SQLParseNodeParameter( const Reference< XConnection >& _rxConnection,
     195             :         const Reference< XNumberFormatter >& _xFormatter, const Reference< XPropertySet >& _xField,
     196             :         const OUString &_sPredicateTableAlias,
     197             :         const Locale& _rLocale, const IParseContext* _pContext,
     198             :         bool _bIntl, bool _bQuote, sal_Char _cDecSep, bool _bPredicate, bool _bParseToSDBC )
     199             :     :rLocale(_rLocale)
     200             :     ,aMetaData( _rxConnection )
     201             :     ,pParser( NULL )
     202        2071 :     ,pSubQueryHistory( new QueryNameSet )
     203             :     ,xFormatter(_xFormatter)
     204             :     ,xField(_xField)
     205             :     ,sPredicateTableAlias(_sPredicateTableAlias)
     206             :     ,m_rContext( _pContext ? (const IParseContext&)(*_pContext) : (const IParseContext&)OSQLParser::s_aDefaultContext )
     207             :     ,cDecSep(_cDecSep)
     208             :     ,bQuote(_bQuote)
     209             :     ,bInternational(_bIntl)
     210             :     ,bPredicate(_bPredicate)
     211        4142 :     ,bParseToSDBCLevel( _bParseToSDBC )
     212             : {
     213        2071 : }
     214             : 
     215             : 
     216       14577 : SQLParseNodeParameter::~SQLParseNodeParameter()
     217             : {
     218       14577 : }
     219             : 
     220           0 : OUString OSQLParseNode::convertDateString(const SQLParseNodeParameter& rParam, const OUString& rString)
     221             : {
     222           0 :     Date aDate = DBTypeConversion::toDate(rString);
     223           0 :     Reference< XNumberFormatsSupplier > xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
     224           0 :     Reference< XNumberFormatTypes >     xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
     225             : 
     226           0 :     double fDate = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
     227           0 :     sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 36; // XXX hack
     228           0 :     return rParam.xFormatter->convertNumberToString(nKey, fDate);
     229             : }
     230             : 
     231             : 
     232           0 : OUString OSQLParseNode::convertDateTimeString(const SQLParseNodeParameter& rParam, const OUString& rString)
     233             : {
     234           0 :     DateTime aDate = DBTypeConversion::toDateTime(rString);
     235           0 :     Reference< XNumberFormatsSupplier >  xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
     236           0 :     Reference< XNumberFormatTypes >  xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
     237             : 
     238           0 :     double fDateTime = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
     239           0 :     sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 51; // XXX hack
     240           0 :     return rParam.xFormatter->convertNumberToString(nKey, fDateTime);
     241             : }
     242             : 
     243             : 
     244           0 : OUString OSQLParseNode::convertTimeString(const SQLParseNodeParameter& rParam, const OUString& rString)
     245             : {
     246           0 :     css::util::Time aTime = DBTypeConversion::toTime(rString);
     247           0 :     Reference< XNumberFormatsSupplier >  xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
     248             : 
     249           0 :     Reference< XNumberFormatTypes >  xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
     250             : 
     251           0 :     double fTime = DBTypeConversion::toDouble(aTime);
     252           0 :     sal_Int32 nKey = xTypes->getStandardIndex(rParam.rLocale) + 41; // XXX hack
     253           0 :     return rParam.xFormatter->convertNumberToString(nKey, fTime);
     254             : }
     255             : 
     256             : 
     257        1931 : void OSQLParseNode::parseNodeToStr(OUString& rString,
     258             :                                    const Reference< XConnection >& _rxConnection,
     259             :                                    const IParseContext* pContext,
     260             :                                    bool _bIntl,
     261             :                                    bool _bQuote) const
     262             : {
     263             :     parseNodeToStr(
     264             :         rString, _rxConnection, NULL, NULL, OUString(),
     265        1931 :         pContext ? pContext->getPreferredLocale() : OParseContext::getDefaultLocale(),
     266        3862 :         pContext, _bIntl, _bQuote, '.', false, false );
     267        1931 : }
     268             : 
     269             : 
     270          36 : void OSQLParseNode::parseNodeToPredicateStr(OUString& rString,
     271             :                                               const Reference< XConnection >& _rxConnection,
     272             :                                               const Reference< XNumberFormatter > & xFormatter,
     273             :                                               const ::com::sun::star::lang::Locale& rIntl,
     274             :                                               sal_Char _cDec,
     275             :                                               const IParseContext* pContext ) const
     276             : {
     277             :     OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
     278             : 
     279          36 :     if (xFormatter.is())
     280          36 :         parseNodeToStr(rString, _rxConnection, xFormatter, NULL, OUString(), rIntl, pContext, true, true, _cDec, true, false);
     281          36 : }
     282             : 
     283             : 
     284           0 : void OSQLParseNode::parseNodeToPredicateStr(OUString& rString,
     285             :                                               const Reference< XConnection > & _rxConnection,
     286             :                                               const Reference< XNumberFormatter > & xFormatter,
     287             :                                               const Reference< XPropertySet > & _xField,
     288             :                                               const OUString &_sPredicateTableAlias,
     289             :                                               const ::com::sun::star::lang::Locale& rIntl,
     290             :                                               sal_Char _cDec,
     291             :                                               const IParseContext* pContext ) const
     292             : {
     293             :     OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
     294             : 
     295           0 :     if (xFormatter.is())
     296           0 :         parseNodeToStr( rString, _rxConnection, xFormatter, _xField, _sPredicateTableAlias, rIntl, pContext, true, true, _cDec, true, false );
     297           0 : }
     298             : 
     299             : 
     300        1967 : void OSQLParseNode::parseNodeToStr(OUString& rString,
     301             :                       const Reference< XConnection > & _rxConnection,
     302             :                       const Reference< XNumberFormatter > & xFormatter,
     303             :                       const Reference< XPropertySet > & _xField,
     304             :                       const OUString &_sPredicateTableAlias,
     305             :                       const ::com::sun::star::lang::Locale& rIntl,
     306             :                       const IParseContext* pContext,
     307             :                       bool _bIntl,
     308             :                       bool _bQuote,
     309             :                       sal_Char _cDecSep,
     310             :                       bool _bPredicate,
     311             :                       bool _bSubstitute) const
     312             : {
     313             :     OSL_ENSURE( _rxConnection.is(), "OSQLParseNode::parseNodeToStr: invalid connection!" );
     314             : 
     315        1967 :     if ( _rxConnection.is() )
     316             :     {
     317        1967 :         OUStringBuffer sBuffer = rString;
     318             :         try
     319             :         {
     320             :             OSQLParseNode::impl_parseNodeToString_throw( sBuffer,
     321             :                 SQLParseNodeParameter(
     322             :                      _rxConnection, xFormatter, _xField, _sPredicateTableAlias, rIntl, pContext,
     323             :                     _bIntl, _bQuote, _cDecSep, _bPredicate, _bSubstitute
     324        1967 :                 ) );
     325             :         }
     326           0 :         catch( const SQLException& )
     327             :         {
     328             :             SAL_WARN( "connectivity.parse", "OSQLParseNode::parseNodeToStr: this should not throw!" );
     329             :             // our callers don't expect this method to throw anything. The only known situation
     330             :             // where impl_parseNodeToString_throw can throw is when there is a cyclic reference
     331             :             // in the sub queries, but this cannot be the case here, as we do not parse to
     332             :             // SDBC level.
     333             :         }
     334        1967 :         rString = sBuffer.makeStringAndClear();
     335             :     }
     336        1967 : }
     337             : 
     338         104 : bool OSQLParseNode::parseNodeToExecutableStatement( OUString& _out_rString, const Reference< XConnection >& _rxConnection,
     339             :     OSQLParser& _rParser, ::com::sun::star::sdbc::SQLException* _pErrorHolder ) const
     340             : {
     341             :     OSL_PRECOND( _rxConnection.is(), "OSQLParseNode::parseNodeToExecutableStatement: invalid connection!" );
     342             :     SQLParseNodeParameter aParseParam( _rxConnection,
     343         104 :         NULL, NULL, OUString(), OParseContext::getDefaultLocale(), NULL, false, true, '.', false, true );
     344             : 
     345         104 :     if ( aParseParam.aMetaData.supportsSubqueriesInFrom() )
     346             :     {
     347          35 :         Reference< XQueriesSupplier > xSuppQueries( _rxConnection, UNO_QUERY );
     348             :         OSL_ENSURE( xSuppQueries.is(), "OSQLParseNode::parseNodeToExecutableStatement: cannot substitute everything without a QueriesSupplier!" );
     349          35 :         if ( xSuppQueries.is() )
     350          35 :             aParseParam.xQueries = xSuppQueries->getQueries();
     351             :     }
     352             : 
     353         104 :     aParseParam.pParser = &_rParser;
     354             : 
     355         104 :     _out_rString.clear();
     356         208 :     OUStringBuffer sBuffer;
     357         104 :     bool bSuccess = false;
     358             :     try
     359             :     {
     360         104 :         impl_parseNodeToString_throw( sBuffer, aParseParam );
     361         104 :         bSuccess = true;
     362             :     }
     363           0 :     catch( const SQLException& e )
     364             :     {
     365           0 :         if ( _pErrorHolder )
     366           0 :             *_pErrorHolder = e;
     367             :     }
     368         104 :     _out_rString = sBuffer.makeStringAndClear();
     369         208 :     return bSuccess;
     370             : }
     371             : 
     372             : 
     373             : namespace
     374             : {
     375           0 :     bool lcl_isAliasNamePresent( const OSQLParseNode& _rTableNameNode )
     376             :     {
     377           0 :         return !OSQLParseNode::getTableRange(_rTableNameNode.getParent()).isEmpty();
     378             :     }
     379             : }
     380             : 
     381             : 
     382       15440 : void OSQLParseNode::impl_parseNodeToString_throw(OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple) const
     383             : {
     384       15440 :     if ( isToken() )
     385             :     {
     386        7218 :         parseLeaf(rString,rParam);
     387       22658 :         return;
     388             :     }
     389             : 
     390             :     // Lets see how many nodes this subtree has
     391        8222 :     sal_uInt32 nCount = count();
     392             : 
     393        8222 :     bool bHandled = false;
     394        8222 :     switch ( getKnownRuleID() )
     395             :     {
     396             :     // special handling for parameters
     397             :     case parameter:
     398             :     {
     399         115 :         bSimple=false;
     400         115 :         if(!rString.isEmpty())
     401          63 :             rString.appendAscii(" ");
     402         115 :         if (nCount == 1)    // ?
     403          30 :             m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
     404          85 :         else if (nCount == 2)   // :Name
     405             :         {
     406          85 :             m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
     407          85 :             rString.append(m_aChildren[1]->m_aNodeValue);
     408             :         }                   // [Name]
     409             :         else
     410             :         {
     411           0 :             m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
     412           0 :             rString.append(m_aChildren[1]->m_aNodeValue);
     413           0 :             rString.append(m_aChildren[2]->m_aNodeValue);
     414             :         }
     415         115 :         bHandled = true;
     416             :     }
     417         115 :     break;
     418             : 
     419             :     // table refs
     420             :     case table_ref:
     421         340 :         bSimple=false;
     422         340 :         if (  ( nCount == 2 ) || ( nCount == 3 ) || ( nCount == 5 ) )
     423             :         {
     424         340 :             impl_parseTableRangeNodeToString_throw( rString, rParam );
     425         340 :             bHandled = true;
     426             :         }
     427         340 :         break;
     428             : 
     429             :     // table name - might be a query name
     430             :     case table_name:
     431         340 :         bSimple=false;
     432         340 :         bHandled = impl_parseTableNameNodeToString_throw( rString, rParam );
     433         340 :         break;
     434             : 
     435             :     case as_clause:
     436         133 :         bSimple=false;
     437             :         assert(nCount == 0 || nCount == 2);
     438         133 :         if (nCount == 2)
     439             :         {
     440           0 :             if ( rParam.aMetaData.generateASBeforeCorrelationName() )
     441           0 :                 rString.append(" AS ");
     442           0 :             m_aChildren[1]->impl_parseNodeToString_throw( rString, rParam, false );
     443             :         }
     444         133 :         bHandled = true;
     445         133 :         break;
     446             : 
     447             :     case opt_as:
     448             :         assert(nCount == 0);
     449          26 :         bHandled = true;
     450          26 :         break;
     451             : 
     452             :     case like_predicate:
     453             :         // Depending on whether international is given, LIKE is treated differently
     454             :         // international: *, ? are placeholders
     455             :         // else SQL92 conform: %, _
     456          17 :         impl_parseLikeNodeToString_throw( rString, rParam, bSimple );
     457          17 :         bHandled = true;
     458          17 :         break;
     459             : 
     460             :     case general_set_fct:
     461             :     case set_fct_spec:
     462             :     case position_exp:
     463             :     case extract_exp:
     464             :     case length_exp:
     465             :     case char_value_fct:
     466             :     {
     467           0 :         bSimple=false;
     468           0 :         if (!addDateValue(rString, rParam))
     469             :         {
     470             :             // Do not quote function name
     471           0 :             SQLParseNodeParameter aNewParam(rParam);
     472           0 :             aNewParam.bQuote = ( SQL_ISRULE(this,length_exp)    || SQL_ISRULE(this,char_value_fct) );
     473             : 
     474           0 :             m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam, false );
     475           0 :             aNewParam.bQuote = rParam.bQuote;
     476             :             //aNewParam.bPredicate = sal_False; // disable [ ] around names // look at i73215
     477           0 :             OUStringBuffer aStringPara;
     478           0 :             for (sal_uInt32 i=1; i<nCount; i++)
     479             :             {
     480           0 :                 const OSQLParseNode * pSubTree = m_aChildren[i];
     481           0 :                 if (pSubTree)
     482             :                 {
     483           0 :                     pSubTree->impl_parseNodeToString_throw( aStringPara, aNewParam, false );
     484             : 
     485             :                     // In the comma lists, put commas in-between all subtrees
     486           0 :                     if ((m_eNodeType == SQL_NODE_COMMALISTRULE)     && (i < (nCount - 1)))
     487           0 :                         aStringPara.appendAscii(",");
     488             :                 }
     489             :                 else
     490           0 :                     i++;
     491             :             }
     492           0 :             rString.append(aStringPara.makeStringAndClear());
     493             :         }
     494           0 :         bHandled = true;
     495             :     }
     496             :     //fall-through
     497             :     case odbc_call_spec:
     498             :     case subquery:
     499             :     case term:
     500             :     case factor:
     501             :     case window_function:
     502             :     case cast_spec:
     503             :     case num_value_exp:
     504           0 :         bSimple = false;
     505             : 
     506           0 :     break;
     507             :     default:
     508        7251 :         break;
     509             :     }   // switch ( getKnownRuleID() )
     510             : 
     511        8222 :     if ( !bHandled )
     512             :     {
     513       47751 :         for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
     514       40160 :             i != m_aChildren.end();)
     515             :         {
     516       12489 :             const OSQLParseNode* pSubTree = *i;
     517       12489 :             if ( !pSubTree )
     518             :             {
     519           0 :                 ++i;
     520           0 :                 continue;
     521             :             }
     522             : 
     523       12489 :             SQLParseNodeParameter aNewParam(rParam);
     524             : 
     525             :             // don't replace the field for subqueries
     526       12489 :             if (rParam.xField.is() && SQL_ISRULE(pSubTree,subquery))
     527           0 :                 aNewParam.xField = NULL;
     528             : 
     529             :             // When we are building a criterion inside a query view,
     530             :             // simplify criterion display by removing:
     531             :             //   "currentFieldName"
     532             :             //   "currentFieldName" =
     533             :             // but only in simple expressions.
     534             :             // This means anything that is made of:
     535             :             // (see the rules conditionalised by inPredicateCheck() in sqlbison.y).
     536             :             //  - parentheses
     537             :             //  - logical operators (and, or, not)
     538             :             //  - comparison operators (IS, =, >, <, BETWEEN, LIKE, ...)
     539             :             // but *not* e.g. in function arguments
     540       12489 :             if (bSimple && rParam.bPredicate && rParam.xField.is() && SQL_ISRULE(pSubTree,column_ref))
     541             :             {
     542           0 :                 if (columnMatchP(pSubTree, rParam))
     543             :                 {
     544             :                     // skip field
     545           0 :                     ++i;
     546             :                     // if the following node is the comparison operator'=',
     547             :                     // we filter it as well
     548           0 :                     if (SQL_ISRULE(this, comparison_predicate))
     549             :                     {
     550           0 :                         if(i != m_aChildren.end())
     551             :                         {
     552           0 :                             pSubTree = *i;
     553           0 :                             if (pSubTree && pSubTree->getNodeType() == SQL_NODE_EQUAL)
     554           0 :                                 ++i;
     555             :                         }
     556             :                     }
     557             :                 }
     558             :                 else
     559             :                 {
     560           0 :                     pSubTree->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
     561           0 :                     ++i;
     562             : 
     563             :                     // In the comma lists, put commas in-between all subtrees
     564           0 :                     if ((m_eNodeType == SQL_NODE_COMMALISTRULE)     && (i != m_aChildren.end()))
     565           0 :                         rString.appendAscii(",");
     566             :                 }
     567             :             }
     568             :             else
     569             :             {
     570       12489 :                 pSubTree->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
     571       12489 :                 ++i;
     572             : 
     573             :                 // In the comma lists, put commas in-between all subtrees
     574       12489 :                 if ((m_eNodeType == SQL_NODE_COMMALISTRULE)     && (i != m_aChildren.end()))
     575             :                 {
     576          52 :                     if (SQL_ISRULE(this,value_exp_commalist) && rParam.bPredicate)
     577           0 :                         rString.appendAscii(";");
     578             :                     else
     579          52 :                         rString.appendAscii(",");
     580             :                 }
     581             :             }
     582             :             // The right hand-side of these operators is not simple
     583       12489 :             switch ( getKnownRuleID() )
     584             :             {
     585             :             case general_set_fct:
     586             :             case set_fct_spec:
     587             :             case position_exp:
     588             :             case extract_exp:
     589             :             case length_exp:
     590             :             case char_value_fct:
     591             :             case odbc_call_spec:
     592             :             case subquery:
     593             :             case comparison_predicate:
     594             :             case between_predicate:
     595             :             case like_predicate:
     596             :             case test_for_null:
     597             :             case in_predicate:
     598             :             case existence_test:
     599             :             case unique_test:
     600             :             case all_or_any_predicate:
     601             :             case join_condition:
     602             :             case comparison_predicate_part_2:
     603             :             case parenthesized_boolean_value_expression:
     604             :             case other_like_predicate_part_2:
     605             :             case between_predicate_part_2:
     606        2514 :                 bSimple=false;
     607        2514 :                 break;
     608             :             default:
     609        9975 :                 break;
     610             :             }
     611       12489 :         }
     612             :     }
     613             : }
     614             : 
     615             : 
     616         340 : bool OSQLParseNode::impl_parseTableNameNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const
     617             : {
     618             :     // is the table_name part of a table_ref?
     619             :     OSL_ENSURE( getParent(), "OSQLParseNode::impl_parseTableNameNodeToString_throw: table_name without parent?" );
     620         340 :     if ( !getParent() || ( getParent()->getKnownRuleID() != table_ref ) )
     621           0 :         return false;
     622             : 
     623             :     // if it's a query, maybe we need to substitute the SQL statement ...
     624         340 :     if ( !rParam.bParseToSDBCLevel )
     625         236 :         return false;
     626             : 
     627         104 :     if ( !rParam.xQueries.is() )
     628             :         // connection does not support queries in queries, or was no query supplier
     629          69 :         return false;
     630             : 
     631             :     try
     632             :     {
     633          35 :         OUString sTableOrQueryName( getChild(0)->getTokenValue() );
     634          35 :         bool bIsQuery = rParam.xQueries->hasByName( sTableOrQueryName );
     635          35 :         if ( !bIsQuery )
     636          35 :             return false;
     637             : 
     638             :         // avoid recursion (e.g. "foo" defined as "SELECT * FROM bar" and "bar" defined as "SELECT * FROM foo".
     639           0 :         if ( rParam.pSubQueryHistory->find( sTableOrQueryName ) != rParam.pSubQueryHistory->end() )
     640             :         {
     641             :             OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: no parser?" );
     642           0 :             if ( rParam.pParser )
     643             :             {
     644           0 :                 const SQLError& rErrors( rParam.pParser->getErrorHelper() );
     645           0 :                 rErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
     646             :             }
     647             :             else
     648             :             {
     649           0 :                 SQLError aErrors( ::comphelper::getProcessComponentContext() );
     650           0 :                 aErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
     651             :             }
     652             :         }
     653           0 :         rParam.pSubQueryHistory->insert( sTableOrQueryName );
     654             : 
     655           0 :         Reference< XPropertySet > xQuery( rParam.xQueries->getByName( sTableOrQueryName ), UNO_QUERY_THROW );
     656             : 
     657             :         // substitute the query name with the constituting command
     658           0 :         OUString sCommand;
     659           0 :         OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND ) ) >>= sCommand );
     660             : 
     661           0 :         bool bEscapeProcessing = false;
     662           0 :         OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ) ) >>= bEscapeProcessing );
     663             : 
     664             :         // the query we found here might itself be based on another query, so parse it recursively
     665             :         OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: cannot analyze sub queries without a parser!" );
     666           0 :         if ( bEscapeProcessing && rParam.pParser )
     667             :         {
     668           0 :             OUString sError;
     669           0 :             boost::scoped_ptr< OSQLParseNode > pSubQueryNode( rParam.pParser->parseTree( sError, sCommand, false ) );
     670           0 :             if ( pSubQueryNode.get() )
     671             :             {
     672             :                 // parse the sub-select to SDBC level, too
     673           0 :                 OUStringBuffer sSubSelect;
     674           0 :                 pSubQueryNode->impl_parseNodeToString_throw( sSubSelect, rParam, false );
     675           0 :                 if ( !sSubSelect.isEmpty() )
     676           0 :                     sCommand = sSubSelect.makeStringAndClear();
     677           0 :             }
     678             :         }
     679             : 
     680           0 :         rString.appendAscii( " ( " );
     681           0 :         rString.append(sCommand);
     682           0 :         rString.appendAscii( " )" );
     683             : 
     684             :         // append the query name as table alias, since it might be referenced in other
     685             :         // parts of the statement - but only if there's no other alias name present
     686           0 :         if ( !lcl_isAliasNamePresent( *this ) )
     687             :         {
     688           0 :             rString.appendAscii( " AS " );
     689           0 :             if ( rParam.bQuote )
     690             :                 rString.append(SetQuotation( sTableOrQueryName,
     691           0 :                     rParam.aMetaData.getIdentifierQuoteString(), rParam.aMetaData.getIdentifierQuoteString() ));
     692             :         }
     693             : 
     694             :         // don't forget to remove the query name from the history, else multiple inclusions
     695             :         // won't work
     696             :         // #i69227# / 2006-10-10 / frank.schoenheit@sun.com
     697           0 :         rParam.pSubQueryHistory->erase( sTableOrQueryName );
     698             : 
     699          35 :         return true;
     700             :     }
     701           0 :     catch( const SQLException& )
     702             :     {
     703           0 :         throw;
     704             :     }
     705           0 :     catch( const Exception& )
     706             :     {
     707             :         DBG_UNHANDLED_EXCEPTION();
     708             :     }
     709           0 :     return false;
     710             : }
     711             : 
     712             : 
     713         340 : void OSQLParseNode::impl_parseTableRangeNodeToString_throw(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
     714             : {
     715             :     OSL_PRECOND(  ( count() == 2 ) || ( count() == 3 ) || ( count() == 5 ) ,"Illegal count");
     716             : 
     717             :     // rString += " ";
     718             :     ::std::for_each(m_aChildren.begin(),m_aChildren.end(),
     719         340 :         boost::bind( &OSQLParseNode::impl_parseNodeToString_throw, _1, boost::ref( rString ), boost::cref( rParam ), false ));
     720         340 : }
     721             : 
     722             : 
     723          17 : void OSQLParseNode::impl_parseLikeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple ) const
     724             : {
     725             :     assert(SQL_ISRULE(this,like_predicate));
     726             :     OSL_ENSURE(count() == 2,"count != 2: Prepare for GPF");
     727             : 
     728          17 :     const OSQLParseNode* pEscNode = NULL;
     729          17 :     const OSQLParseNode* pParaNode = NULL;
     730             : 
     731          17 :     SQLParseNodeParameter aNewParam(rParam);
     732             :     //aNewParam.bQuote = sal_True; // why setting this to true? @see http://www.openoffice.org/issues/show_bug.cgi?id=75557
     733             : 
     734          17 :     if ( !(bSimple && rParam.bPredicate && rParam.xField.is() && SQL_ISRULE(m_aChildren[0],column_ref) && columnMatchP(m_aChildren[0], rParam)) )
     735          17 :         m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
     736             : 
     737          17 :     const OSQLParseNode* pPart2 = m_aChildren[1];
     738          17 :     pPart2->getChild(0)->impl_parseNodeToString_throw( rString, aNewParam, false );
     739          17 :     pPart2->getChild(1)->impl_parseNodeToString_throw( rString, aNewParam, false );
     740          17 :     pParaNode = pPart2->getChild(2);
     741          17 :     pEscNode  = pPart2->getChild(3);
     742             : 
     743          17 :     if (pParaNode->isToken())
     744             :     {
     745           0 :         OUString aStr = ConvertLikeToken(pParaNode, pEscNode, rParam.bInternational);
     746           0 :         rString.appendAscii(" ");
     747           0 :         rString.append(SetQuotation(aStr,OUString("\'"),OUString("\'\'")));
     748             :     }
     749             :     else
     750          17 :         pParaNode->impl_parseNodeToString_throw( rString, aNewParam, false );
     751             : 
     752          17 :     pEscNode->impl_parseNodeToString_throw( rString, aNewParam, false );
     753          17 : }
     754             : 
     755             : 
     756             : 
     757         414 : bool OSQLParseNode::getTableComponents(const OSQLParseNode* _pTableNode,
     758             :                                             ::com::sun::star::uno::Any &_rCatalog,
     759             :                                             OUString &_rSchema,
     760             :                                             OUString &_rTable,
     761             :                                             const Reference< XDatabaseMetaData >& _xMetaData)
     762             : {
     763             :     OSL_ENSURE(_pTableNode,"Wrong use of getTableComponents! _pTableNode is not allowed to be null!");
     764         414 :     if(_pTableNode)
     765             :     {
     766         414 :         const bool bSupportsCatalog = _xMetaData.is() && _xMetaData->supportsCatalogsInDataManipulation();
     767         414 :         const bool bSupportsSchema = _xMetaData.is() && _xMetaData->supportsSchemasInDataManipulation();
     768         414 :         const OSQLParseNode* pTableNode = _pTableNode;
     769             :         // clear the parameter given
     770         414 :         _rCatalog = Any();
     771         414 :         _rSchema.clear();
     772         414 :         _rTable.clear();
     773             :         // see rule catalog_name: in sqlbison.y
     774         414 :         if (SQL_ISRULE(pTableNode,catalog_name))
     775             :         {
     776             :             OSL_ENSURE(pTableNode->getChild(0) && pTableNode->getChild(0)->isToken(),"Invalid parsenode!");
     777           0 :             _rCatalog <<= pTableNode->getChild(0)->getTokenValue();
     778           0 :             pTableNode = pTableNode->getChild(2);
     779             :         }
     780             :         // check if we have schema_name rule
     781         414 :         if(SQL_ISRULE(pTableNode,schema_name))
     782             :         {
     783           0 :             if ( bSupportsCatalog && !bSupportsSchema )
     784           0 :                 _rCatalog <<= pTableNode->getChild(0)->getTokenValue();
     785             :             else
     786           0 :                 _rSchema = pTableNode->getChild(0)->getTokenValue();
     787           0 :             pTableNode = pTableNode->getChild(2);
     788             :         }
     789             :         // check if we have table_name rule
     790         414 :         if(SQL_ISRULE(pTableNode,table_name))
     791             :         {
     792         414 :             _rTable = pTableNode->getChild(0)->getTokenValue();
     793             :         }
     794             :         else
     795             :         {
     796             :             SAL_WARN( "connectivity.parse","Error in parse tree!");
     797             :         }
     798             :     }
     799         414 :     return !_rTable.isEmpty();
     800             : }
     801             : 
     802           0 : void OSQLParser::killThousandSeparator(OSQLParseNode* pLiteral)
     803             : {
     804           0 :     if ( pLiteral )
     805             :     {
     806           0 :         if ( s_xLocaleData->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
     807             :         {
     808           0 :             pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace('.', sal_Unicode());
     809             :             // and replace decimal
     810           0 :             pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', '.');
     811             :         }
     812             :         else
     813           0 :             pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', sal_Unicode());
     814             :         }
     815           0 : }
     816             : 
     817          72 : OSQLParseNode* OSQLParser::convertNode(sal_Int32 nType, OSQLParseNode* pLiteral)
     818             : {
     819          72 :     if ( !pLiteral )
     820          36 :         return NULL;
     821             : 
     822          36 :     OSQLParseNode* pReturn = pLiteral;
     823             : 
     824          36 :     if ( ( pLiteral->isRule() && !SQL_ISRULE(pLiteral,value_exp) ) || SQL_ISTOKEN(pLiteral,FALSE) || SQL_ISTOKEN(pLiteral,TRUE) )
     825             :     {
     826           0 :         switch(nType)
     827             :         {
     828             :             case DataType::CHAR:
     829             :             case DataType::VARCHAR:
     830             :             case DataType::LONGVARCHAR:
     831             :             case DataType::CLOB:
     832           0 :                 if ( !SQL_ISRULE(pReturn,char_value_exp) && !buildStringNodes(pReturn) )
     833           0 :                     pReturn = NULL;
     834             :             default:
     835           0 :                 break;
     836             :         }
     837             :     }
     838             :     else
     839             :     {
     840          36 :         switch(pLiteral->getNodeType())
     841             :         {
     842             :         case SQL_NODE_STRING:
     843          36 :             switch(nType)
     844             :             {
     845             :                 case DataType::CHAR:
     846             :                 case DataType::VARCHAR:
     847             :                 case DataType::LONGVARCHAR:
     848             :                 case DataType::CLOB:
     849          36 :                     break;
     850             :                 case DataType::DATE:
     851             :                 case DataType::TIME:
     852             :                 case DataType::TIMESTAMP:
     853           0 :                     if (m_xFormatter.is())
     854           0 :                         pReturn = buildDate( nType, pReturn);
     855           0 :                     break;
     856             :                 default:
     857           0 :                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE);
     858           0 :                     break;
     859             :             }
     860          36 :             break;
     861             :         case SQL_NODE_ACCESS_DATE:
     862           0 :             switch(nType)
     863             :             {
     864             :                 case DataType::DATE:
     865             :                 case DataType::TIME:
     866             :                 case DataType::TIMESTAMP:
     867           0 :                 if ( m_xFormatter.is() )
     868           0 :                     pReturn = buildDate( nType, pReturn);
     869             :                     else
     870           0 :                         m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE);
     871           0 :                     break;
     872             :                 default:
     873           0 :                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_COMPARE);
     874           0 :                     break;
     875             :             }
     876           0 :             break;
     877             :         case SQL_NODE_INTNUM:
     878           0 :             switch(nType)
     879             :             {
     880             :                 case DataType::BIT:
     881             :                 case DataType::BOOLEAN:
     882             :                 case DataType::DECIMAL:
     883             :                 case DataType::NUMERIC:
     884             :                 case DataType::TINYINT:
     885             :                 case DataType::SMALLINT:
     886             :                 case DataType::INTEGER:
     887             :                 case DataType::BIGINT:
     888             :                 case DataType::FLOAT:
     889             :                 case DataType::REAL:
     890             :                 case DataType::DOUBLE:
     891             :                     // kill thousand separators if any
     892           0 :                     killThousandSeparator(pReturn);
     893           0 :                     break;
     894             :                 case DataType::CHAR:
     895             :                 case DataType::VARCHAR:
     896             :                 case DataType::LONGVARCHAR:
     897             :                 case DataType::CLOB:
     898           0 :                     pReturn = buildNode_STR_NUM(pReturn);
     899           0 :                     break;
     900             :                 default:
     901           0 :                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_INT_COMPARE);
     902           0 :                     break;
     903             :             }
     904           0 :             break;
     905             :         case SQL_NODE_APPROXNUM:
     906           0 :             switch(nType)
     907             :             {
     908             :                 case DataType::DECIMAL:
     909             :                 case DataType::NUMERIC:
     910             :                 case DataType::FLOAT:
     911             :                 case DataType::REAL:
     912             :                 case DataType::DOUBLE:
     913             :                     // kill thousand separators if any
     914           0 :                     killThousandSeparator(pReturn);
     915           0 :                     break;
     916             :                 case DataType::CHAR:
     917             :                 case DataType::VARCHAR:
     918             :                 case DataType::LONGVARCHAR:
     919             :                 case DataType::CLOB:
     920           0 :                     pReturn = buildNode_STR_NUM(pReturn);
     921           0 :                     break;
     922             :                 case DataType::INTEGER:
     923             :                 default:
     924           0 :                     m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_REAL_COMPARE);
     925           0 :                     break;
     926             :             }
     927           0 :             break;
     928             :         default:
     929             :             ;
     930             :         }
     931             :     }
     932          36 :     return pReturn;
     933             : }
     934             : 
     935          36 : sal_Int16 OSQLParser::buildPredicateRule(OSQLParseNode*& pAppend, OSQLParseNode* pLiteral, OSQLParseNode* pCompare, OSQLParseNode* pLiteral2)
     936             : {
     937             :     OSL_ENSURE(inPredicateCheck(),"Only in predicate check allowed!");
     938          36 :     sal_Int16 nErg = 0;
     939          36 :     if ( m_xField.is() )
     940             :     {
     941          36 :         sal_Int32 nType = 0;
     942             :         try
     943             :         {
     944          36 :             m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
     945             :         }
     946           0 :         catch( Exception& )
     947             :         {
     948           0 :             return nErg;
     949             :         }
     950             : 
     951          36 :         OSQLParseNode* pNode1 = convertNode(nType,pLiteral);
     952          36 :         if ( pNode1 )
     953             :         {
     954          36 :             OSQLParseNode* pNode2 = convertNode(nType,pLiteral2);
     955          36 :             if ( m_sErrorMessage.isEmpty() )
     956          36 :                 nErg = buildNode(pAppend,pCompare,pNode1,pNode2);
     957             :         }
     958             :     }
     959          36 :     if (!pCompare->getParent()) // I have no parent so I was not used and I must die :-)
     960           0 :         delete pCompare;
     961          36 :     return nErg;
     962             : }
     963             : 
     964           0 : sal_Int16 OSQLParser::buildLikeRule(OSQLParseNode* pAppend, OSQLParseNode*& pLiteral, const OSQLParseNode* pEscape)
     965             : {
     966           0 :     sal_Int16 nErg = 0;
     967           0 :     sal_Int32 nType = 0;
     968             : 
     969           0 :     if (!m_xField.is())
     970           0 :         return nErg;
     971             :     try
     972             :     {
     973           0 :         Any aValue;
     974             :         {
     975           0 :             aValue = m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE));
     976           0 :             aValue >>= nType;
     977           0 :         }
     978             :     }
     979           0 :     catch( Exception& )
     980             :     {
     981           0 :         return nErg;
     982             :     }
     983             : 
     984           0 :     switch (nType)
     985             :     {
     986             :         case DataType::CHAR:
     987             :         case DataType::VARCHAR:
     988             :         case DataType::LONGVARCHAR:
     989             :         case DataType::CLOB:
     990           0 :             if(pLiteral->isRule())
     991             :             {
     992           0 :                 pAppend->append(pLiteral);
     993           0 :                 nErg = 1;
     994             :             }
     995             :             else
     996             :             {
     997           0 :                 switch(pLiteral->getNodeType())
     998             :                 {
     999             :                     case SQL_NODE_STRING:
    1000           0 :                         pLiteral->m_aNodeValue = ConvertLikeToken(pLiteral, pEscape, false);
    1001           0 :                         pAppend->append(pLiteral);
    1002           0 :                         nErg = 1;
    1003           0 :                         break;
    1004             :                     case SQL_NODE_APPROXNUM:
    1005           0 :                         if (m_xFormatter.is() && m_nFormatKey)
    1006             :                         {
    1007           0 :                             sal_Int16 nScale = 0;
    1008             :                             try
    1009             :                             {
    1010           0 :                                 Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, OUString("Decimals") );
    1011           0 :                                 aValue >>= nScale;
    1012             :                             }
    1013           0 :                             catch( Exception& )
    1014             :                             {
    1015             :                             }
    1016             : 
    1017           0 :                             pAppend->append(new OSQLInternalNode(stringToDouble(pLiteral->getTokenValue(),nScale),SQL_NODE_STRING));
    1018             :                         }
    1019             :                         else
    1020           0 :                             pAppend->append(new OSQLInternalNode(pLiteral->getTokenValue(),SQL_NODE_STRING));
    1021             : 
    1022           0 :                         delete pLiteral;
    1023           0 :                         nErg = 1;
    1024           0 :                         break;
    1025             :                     default:
    1026           0 :                         m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_VALUE_NO_LIKE);
    1027           0 :                         m_sErrorMessage = m_sErrorMessage.replaceAt(m_sErrorMessage.indexOf("#1"),2,pLiteral->getTokenValue());
    1028           0 :                         break;
    1029             :                 }
    1030             :             }
    1031           0 :             break;
    1032             :         default:
    1033           0 :             m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_FIELD_NO_LIKE);
    1034           0 :             break;
    1035             :     }
    1036           0 :     return nErg;
    1037             : }
    1038             : 
    1039           0 : OSQLParseNode* OSQLParser::buildNode_Date(const double& fValue, sal_Int32 nType)
    1040             : {
    1041           0 :     OUString aEmptyString;
    1042           0 :     OSQLParseNode* pNewNode = new OSQLInternalNode(aEmptyString, SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::set_fct_spec));
    1043           0 :     pNewNode->append(new OSQLInternalNode(OUString("{"), SQL_NODE_PUNCTUATION));
    1044           0 :     OSQLParseNode* pDateNode = new OSQLInternalNode(aEmptyString, SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::odbc_fct_spec));
    1045           0 :     pNewNode->append(pDateNode);
    1046           0 :     pNewNode->append(new OSQLInternalNode(OUString("}"), SQL_NODE_PUNCTUATION));
    1047             : 
    1048           0 :     switch (nType)
    1049             :     {
    1050             :         case DataType::DATE:
    1051             :         {
    1052           0 :             Date aDate = DBTypeConversion::toDate(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
    1053           0 :             OUString aString = DBTypeConversion::toDateString(aDate);
    1054           0 :             pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_D));
    1055           0 :             pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
    1056           0 :             break;
    1057             :         }
    1058             :         case DataType::TIME:
    1059             :         {
    1060           0 :             css::util::Time aTime = DBTypeConversion::toTime(fValue);
    1061           0 :             OUString aString = DBTypeConversion::toTimeString(aTime);
    1062           0 :             pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_T));
    1063           0 :             pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
    1064           0 :             break;
    1065             :         }
    1066             :         case DataType::TIMESTAMP:
    1067             :         {
    1068           0 :             DateTime aDateTime = DBTypeConversion::toDateTime(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
    1069           0 :             if (aDateTime.Seconds || aDateTime.Minutes || aDateTime.Hours)
    1070             :             {
    1071           0 :                 OUString aString = DBTypeConversion::toDateTimeString(aDateTime);
    1072           0 :                 pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_TS));
    1073           0 :                 pDateNode->append(new OSQLInternalNode(aString, SQL_NODE_STRING));
    1074             :             }
    1075             :             else
    1076             :             {
    1077           0 :                 Date aDate(aDateTime.Day,aDateTime.Month,aDateTime.Year);
    1078           0 :                 pDateNode->append(new OSQLInternalNode(aEmptyString, SQL_NODE_KEYWORD, SQL_TOKEN_D));
    1079           0 :                 pDateNode->append(new OSQLInternalNode(DBTypeConversion::toDateString(aDate), SQL_NODE_STRING));
    1080             :             }
    1081           0 :             break;
    1082             :         }
    1083             :     }
    1084             : 
    1085           0 :     return pNewNode;
    1086             : }
    1087             : 
    1088           0 : OSQLParseNode* OSQLParser::buildNode_STR_NUM(OSQLParseNode*& _pLiteral)
    1089             : {
    1090           0 :     OSQLParseNode* pReturn = NULL;
    1091           0 :     if ( _pLiteral )
    1092             :     {
    1093           0 :         if (m_nFormatKey)
    1094             :         {
    1095           0 :             sal_Int16 nScale = 0;
    1096             :             try
    1097             :             {
    1098           0 :                 Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, OUString("Decimals") );
    1099           0 :                 aValue >>= nScale;
    1100             :             }
    1101           0 :             catch( Exception& )
    1102             :             {
    1103             :             }
    1104             : 
    1105           0 :             pReturn = new OSQLInternalNode(stringToDouble(_pLiteral->getTokenValue(),nScale),SQL_NODE_STRING);
    1106             :         }
    1107             :         else
    1108           0 :             pReturn = new OSQLInternalNode(_pLiteral->getTokenValue(),SQL_NODE_STRING);
    1109             : 
    1110           0 :         delete _pLiteral;
    1111           0 :         _pLiteral = NULL;
    1112             :     }
    1113           0 :     return pReturn;
    1114             : }
    1115             : 
    1116           0 : OUString OSQLParser::stringToDouble(const OUString& _rValue,sal_Int16 _nScale)
    1117             : {
    1118           0 :     OUString aValue;
    1119           0 :     if(!m_xCharClass.is())
    1120           0 :         m_xCharClass  = CharacterClassification::create( m_xContext );
    1121           0 :     if( s_xLocaleData.is() )
    1122             :     {
    1123             :         try
    1124             :         {
    1125           0 :             ParseResult aResult = m_xCharClass->parsePredefinedToken(KParseType::ANY_NUMBER,_rValue,0,m_pData->aLocale,0,OUString(),KParseType::ANY_NUMBER,OUString());
    1126           0 :             if((aResult.TokenType & KParseType::IDENTNAME) && aResult.EndPos == _rValue.getLength())
    1127             :             {
    1128           0 :                 aValue = OUString::number(aResult.Value);
    1129           0 :                 sal_Int32 nPos = aValue.lastIndexOf('.');
    1130           0 :                 if((nPos+_nScale) < aValue.getLength())
    1131           0 :                     aValue = aValue.replaceAt(nPos+_nScale,aValue.getLength()-nPos-_nScale,OUString());
    1132           0 :                 aValue = aValue.replaceAt(aValue.lastIndexOf('.'),1,s_xLocaleData->getLocaleItem(m_pData->aLocale).decimalSeparator);
    1133           0 :                 return aValue;
    1134           0 :             }
    1135             :         }
    1136           0 :         catch(Exception&)
    1137             :         {
    1138             :         }
    1139             :     }
    1140           0 :     return aValue;
    1141             : }
    1142             : 
    1143             : 
    1144        1945 : ::osl::Mutex& OSQLParser::getMutex()
    1145             : {
    1146        1945 :     static ::osl::Mutex aMutex;
    1147        1945 :     return aMutex;
    1148             : }
    1149             : 
    1150             : 
    1151          36 : OSQLParseNode* OSQLParser::predicateTree(OUString& rErrorMessage, const OUString& rStatement,
    1152             :                                          const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter,
    1153             :                                          const Reference< XPropertySet > & xField,
    1154             :                                          bool bUseRealName)
    1155             : {
    1156             :     // Guard the parsing
    1157          36 :     ::osl::MutexGuard aGuard(getMutex());
    1158             :     // must be reset
    1159          36 :     setParser(this);
    1160             : 
    1161             : 
    1162             :     // reset the parser
    1163          36 :     m_xField        = xField;
    1164          36 :     m_xFormatter    = xFormatter;
    1165             : 
    1166          36 :     if (m_xField.is())
    1167             :     {
    1168          36 :         sal_Int32 nType=0;
    1169             :         try
    1170             :         {
    1171             :             // get the field name
    1172          36 :             OUString aString;
    1173             : 
    1174             :             // retrieve the fields name
    1175             :             // #75243# use the RealName of the column if there is any otherwise the name which could be the alias
    1176             :             // of the field
    1177          72 :             Reference< XPropertySetInfo> xInfo = m_xField->getPropertySetInfo();
    1178          36 :             if ( bUseRealName && xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)))
    1179          36 :                 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)) >>= aString;
    1180             :             else
    1181           0 :                 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aString;
    1182             : 
    1183          36 :             m_sFieldName = aString;
    1184             : 
    1185             :             // get the field format key
    1186          36 :             if ( xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)))
    1187           0 :                 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= m_nFormatKey;
    1188             :             else
    1189          36 :                 m_nFormatKey = 0;
    1190             : 
    1191             :             // get the field type
    1192          72 :             m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
    1193             :         }
    1194           0 :         catch ( Exception& )
    1195             :         {
    1196             :             OSL_ASSERT(false);
    1197             :         }
    1198             : 
    1199          36 :         if (m_nFormatKey && m_xFormatter.is())
    1200             :         {
    1201           0 :             Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_LOCALE) );
    1202             :             OSL_ENSURE(aValue.getValueType() == cppu::UnoType<com::sun::star::lang::Locale>::get(), "OSQLParser::PredicateTree : invalid language property !");
    1203             : 
    1204           0 :             if (aValue.getValueType() == cppu::UnoType<com::sun::star::lang::Locale>::get())
    1205           0 :                 aValue >>= m_pData->aLocale;
    1206             :         }
    1207             :         else
    1208          36 :             m_pData->aLocale = m_pContext->getPreferredLocale();
    1209             : 
    1210          36 :         if ( m_xFormatter.is() )
    1211             :         {
    1212             :             try
    1213             :             {
    1214          36 :                 Reference< ::com::sun::star::util::XNumberFormatsSupplier >  xFormatSup = m_xFormatter->getNumberFormatsSupplier();
    1215          36 :                 if ( xFormatSup.is() )
    1216             :                 {
    1217          36 :                     Reference< ::com::sun::star::util::XNumberFormats >  xFormats = xFormatSup->getNumberFormats();
    1218          36 :                     if ( xFormats.is() )
    1219             :                     {
    1220          36 :                         ::com::sun::star::lang::Locale aLocale;
    1221          36 :                         aLocale.Language = "en";
    1222          36 :                         aLocale.Country = "US";
    1223          72 :                         OUString sFormat("YYYY-MM-DD");
    1224          36 :                         m_nDateFormatKey = xFormats->queryKey(sFormat,aLocale,sal_False);
    1225          36 :                         if ( m_nDateFormatKey == sal_Int32(-1) )
    1226          36 :                             m_nDateFormatKey = xFormats->addNew(sFormat, aLocale);
    1227          36 :                     }
    1228          36 :                 }
    1229             :             }
    1230           0 :             catch ( Exception& )
    1231             :             {
    1232             :                 SAL_WARN( "connectivity.parse","DateFormatKey");
    1233             :             }
    1234             :         }
    1235             : 
    1236          36 :         switch (nType)
    1237             :         {
    1238             :             case DataType::DATE:
    1239             :             case DataType::TIME:
    1240             :             case DataType::TIMESTAMP:
    1241           0 :                 s_pScanner->SetRule(OSQLScanner::GetDATERule());
    1242           0 :                 break;
    1243             :             case DataType::CHAR:
    1244             :             case DataType::VARCHAR:
    1245             :             case DataType::LONGVARCHAR:
    1246             :             case DataType::CLOB:
    1247          36 :                 s_pScanner->SetRule(OSQLScanner::GetSTRINGRule());
    1248          36 :                 break;
    1249             :             default:
    1250           0 :                 if ( s_xLocaleData->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
    1251           0 :                     s_pScanner->SetRule(OSQLScanner::GetGERRule());
    1252             :                 else
    1253           0 :                     s_pScanner->SetRule(OSQLScanner::GetENGRule());
    1254             :         }
    1255             : 
    1256             :     }
    1257             :     else
    1258           0 :         s_pScanner->SetRule(OSQLScanner::GetSQLRule());
    1259             : 
    1260          36 :     s_pScanner->prepareScan(rStatement, m_pContext, true);
    1261             : 
    1262          36 :     SQLyylval.pParseNode = NULL;
    1263             :     //  SQLyypvt = NULL;
    1264          36 :     m_pParseTree = NULL;
    1265          36 :     m_sErrorMessage.clear();
    1266             : 
    1267             :     // Start the parser
    1268          36 :     if (SQLyyparse() != 0)
    1269             :     {
    1270           0 :         m_sFieldName.clear();
    1271           0 :         m_xField.clear();
    1272           0 :         m_xFormatter.clear();
    1273           0 :         m_nFormatKey = 0;
    1274           0 :         m_nDateFormatKey = 0;
    1275             : 
    1276           0 :         if (m_sErrorMessage.isEmpty())
    1277           0 :             m_sErrorMessage = s_pScanner->getErrorMessage();
    1278           0 :         if (m_sErrorMessage.isEmpty())
    1279           0 :             m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_GENERAL);
    1280             : 
    1281           0 :         rErrorMessage = m_sErrorMessage;
    1282             : 
    1283             :         // clear the garbage collector
    1284           0 :         (*s_pGarbageCollector)->clearAndDelete();
    1285           0 :         return NULL;
    1286             :     }
    1287             :     else
    1288             :     {
    1289          36 :         (*s_pGarbageCollector)->clear();
    1290             : 
    1291          36 :         m_sFieldName.clear();
    1292          36 :         m_xField.clear();
    1293          36 :         m_xFormatter.clear();
    1294          36 :         m_nFormatKey = 0;
    1295          36 :         m_nDateFormatKey = 0;
    1296             : 
    1297             :         // Return the result (the root parse node):
    1298             : 
    1299             :         // Instead, the parse method sets the member pParseTree and simply returns that
    1300             :         OSL_ENSURE(m_pParseTree != NULL,"OSQLParser: Parser did not return a ParseTree!");
    1301          36 :         return m_pParseTree;
    1302          36 :     }
    1303             : }
    1304             : 
    1305             : 
    1306             : 
    1307         431 : OSQLParser::OSQLParser(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext, const IParseContext* _pContext)
    1308             :     :m_pContext(_pContext)
    1309             :     ,m_pParseTree(NULL)
    1310         431 :     ,m_pData( new OSQLParser_Data( rxContext ) )
    1311             :     ,m_nFormatKey(0)
    1312             :     ,m_nDateFormatKey(0)
    1313         862 :     ,m_xContext(rxContext)
    1314             : {
    1315             : 
    1316             : 
    1317         431 :     setParser(this);
    1318             : 
    1319             : #ifdef SQLYYDEBUG
    1320             : #ifdef SQLYYDEBUG_ON
    1321             :     SQLyydebug = 1;
    1322             : #endif
    1323             : #endif
    1324             : 
    1325         431 :     ::osl::MutexGuard aGuard(getMutex());
    1326             :     // Do we have to initialize the data?
    1327         431 :     if (s_nRefCount == 0)
    1328             :     {
    1329         132 :         s_pScanner = new OSQLScanner();
    1330         132 :         s_pScanner->setScanner();
    1331         132 :         s_pGarbageCollector = new OSQLParseNodesGarbageCollector();
    1332             : 
    1333         132 :         if(!s_xLocaleData.is())
    1334         132 :             s_xLocaleData = LocaleData::create(m_xContext);
    1335             : 
    1336             :         // reset to UNKNOWN_RULE
    1337             :         static_assert(OSQLParseNode::UNKNOWN_RULE==0, "UNKNOWN_RULE must be 0 for memset to 0 to work");
    1338         132 :         memset(OSQLParser::s_nRuleIDs,0,sizeof(OSQLParser::s_nRuleIDs));
    1339             : 
    1340             :         struct
    1341       13332 :         {
    1342             :             OSQLParseNode::Rule eRule;      // the parse node's ID for the rule
    1343             :             OString      sRuleName;  // the name of the rule ("select_statement")
    1344             :         }   aRuleDescriptions[] =
    1345             :         {
    1346             :             { OSQLParseNode::select_statement, "select_statement" },
    1347             :             { OSQLParseNode::table_exp, "table_exp" },
    1348             :             { OSQLParseNode::table_ref_commalist, "table_ref_commalist" },
    1349             :             { OSQLParseNode::table_ref, "table_ref" },
    1350             :             { OSQLParseNode::catalog_name, "catalog_name" },
    1351             :             { OSQLParseNode::schema_name, "schema_name" },
    1352             :             { OSQLParseNode::table_name, "table_name" },
    1353             :             { OSQLParseNode::opt_column_commalist, "opt_column_commalist" },
    1354             :             { OSQLParseNode::column_commalist, "column_commalist" },
    1355             :             { OSQLParseNode::column_ref_commalist, "column_ref_commalist" },
    1356             :             { OSQLParseNode::column_ref, "column_ref" },
    1357             :             { OSQLParseNode::opt_order_by_clause, "opt_order_by_clause" },
    1358             :             { OSQLParseNode::ordering_spec_commalist, "ordering_spec_commalist" },
    1359             :             { OSQLParseNode::ordering_spec, "ordering_spec" },
    1360             :             { OSQLParseNode::opt_asc_desc, "opt_asc_desc" },
    1361             :             { OSQLParseNode::where_clause, "where_clause" },
    1362             :             { OSQLParseNode::opt_where_clause, "opt_where_clause" },
    1363             :             { OSQLParseNode::search_condition, "search_condition" },
    1364             :             { OSQLParseNode::comparison, "comparison" },
    1365             :             { OSQLParseNode::comparison_predicate, "comparison_predicate" },
    1366             :             { OSQLParseNode::between_predicate, "between_predicate" },
    1367             :             { OSQLParseNode::like_predicate, "like_predicate" },
    1368             :             { OSQLParseNode::opt_escape, "opt_escape" },
    1369             :             { OSQLParseNode::test_for_null, "test_for_null" },
    1370             :             { OSQLParseNode::scalar_exp_commalist, "scalar_exp_commalist" },
    1371             :             { OSQLParseNode::scalar_exp, "scalar_exp" },
    1372             :             { OSQLParseNode::parameter_ref, "parameter_ref" },
    1373             :             { OSQLParseNode::parameter, "parameter" },
    1374             :             { OSQLParseNode::general_set_fct, "general_set_fct" },
    1375             :             { OSQLParseNode::range_variable, "range_variable" },
    1376             :             { OSQLParseNode::column, "column" },
    1377             :             { OSQLParseNode::delete_statement_positioned, "delete_statement_positioned" },
    1378             :             { OSQLParseNode::delete_statement_searched, "delete_statement_searched" },
    1379             :             { OSQLParseNode::update_statement_positioned, "update_statement_positioned" },
    1380             :             { OSQLParseNode::update_statement_searched, "update_statement_searched" },
    1381             :             { OSQLParseNode::assignment_commalist, "assignment_commalist" },
    1382             :             { OSQLParseNode::assignment, "assignment" },
    1383             :             { OSQLParseNode::values_or_query_spec, "values_or_query_spec" },
    1384             :             { OSQLParseNode::insert_statement, "insert_statement" },
    1385             :             { OSQLParseNode::insert_atom_commalist, "insert_atom_commalist" },
    1386             :             { OSQLParseNode::insert_atom, "insert_atom" },
    1387             :             { OSQLParseNode::from_clause, "from_clause" },
    1388             :             { OSQLParseNode::qualified_join, "qualified_join" },
    1389             :             { OSQLParseNode::cross_union, "cross_union" },
    1390             :             { OSQLParseNode::select_sublist, "select_sublist" },
    1391             :             { OSQLParseNode::derived_column, "derived_column" },
    1392             :             { OSQLParseNode::column_val, "column_val" },
    1393             :             { OSQLParseNode::set_fct_spec, "set_fct_spec" },
    1394             :             { OSQLParseNode::boolean_term, "boolean_term" },
    1395             :             { OSQLParseNode::boolean_primary, "boolean_primary" },
    1396             :             { OSQLParseNode::num_value_exp, "num_value_exp" },
    1397             :             { OSQLParseNode::join_type, "join_type" },
    1398             :             { OSQLParseNode::position_exp, "position_exp" },
    1399             :             { OSQLParseNode::extract_exp, "extract_exp" },
    1400             :             { OSQLParseNode::length_exp, "length_exp" },
    1401             :             { OSQLParseNode::char_value_fct, "char_value_fct" },
    1402             :             { OSQLParseNode::odbc_call_spec, "odbc_call_spec" },
    1403             :             { OSQLParseNode::in_predicate, "in_predicate" },
    1404             :             { OSQLParseNode::existence_test, "existence_test" },
    1405             :             { OSQLParseNode::unique_test, "unique_test" },
    1406             :             { OSQLParseNode::all_or_any_predicate, "all_or_any_predicate" },
    1407             :             { OSQLParseNode::named_columns_join, "named_columns_join" },
    1408             :             { OSQLParseNode::join_condition, "join_condition" },
    1409             :             { OSQLParseNode::joined_table, "joined_table" },
    1410             :             { OSQLParseNode::boolean_factor, "boolean_factor" },
    1411             :             { OSQLParseNode::sql_not, "sql_not" },
    1412             :             { OSQLParseNode::manipulative_statement, "manipulative_statement" },
    1413             :             { OSQLParseNode::subquery, "subquery" },
    1414             :             { OSQLParseNode::value_exp_commalist, "value_exp_commalist" },
    1415             :             { OSQLParseNode::odbc_fct_spec, "odbc_fct_spec" },
    1416             :             { OSQLParseNode::union_statement, "union_statement" },
    1417             :             { OSQLParseNode::outer_join_type, "outer_join_type" },
    1418             :             { OSQLParseNode::char_value_exp, "char_value_exp" },
    1419             :             { OSQLParseNode::term, "term" },
    1420             :             { OSQLParseNode::value_exp_primary, "value_exp_primary" },
    1421             :             { OSQLParseNode::value_exp, "value_exp" },
    1422             :             { OSQLParseNode::selection, "selection" },
    1423             :             { OSQLParseNode::fold, "fold" },
    1424             :             { OSQLParseNode::char_substring_fct, "char_substring_fct" },
    1425             :             { OSQLParseNode::factor, "factor" },
    1426             :             { OSQLParseNode::base_table_def, "base_table_def" },
    1427             :             { OSQLParseNode::base_table_element_commalist, "base_table_element_commalist" },
    1428             :             { OSQLParseNode::data_type, "data_type" },
    1429             :             { OSQLParseNode::column_def, "column_def" },
    1430             :             { OSQLParseNode::table_node, "table_node" },
    1431             :             { OSQLParseNode::as_clause, "as_clause" },
    1432             :             { OSQLParseNode::opt_as, "opt_as" },
    1433             :             { OSQLParseNode::op_column_commalist, "op_column_commalist" },
    1434             :             { OSQLParseNode::table_primary_as_range_column, "table_primary_as_range_column" },
    1435             :             { OSQLParseNode::datetime_primary, "datetime_primary" },
    1436             :             { OSQLParseNode::concatenation, "concatenation" },
    1437             :             { OSQLParseNode::char_factor, "char_factor" },
    1438             :             { OSQLParseNode::bit_value_fct, "bit_value_fct" },
    1439             :             { OSQLParseNode::comparison_predicate_part_2, "comparison_predicate_part_2" },
    1440             :             { OSQLParseNode::parenthesized_boolean_value_expression, "parenthesized_boolean_value_expression" },
    1441             :             { OSQLParseNode::character_string_type, "character_string_type" },
    1442             :             { OSQLParseNode::other_like_predicate_part_2, "other_like_predicate_part_2" },
    1443             :             { OSQLParseNode::between_predicate_part_2, "between_predicate_part_2" },
    1444             :             { OSQLParseNode::null_predicate_part_2, "null_predicate_part_2" },
    1445             :             { OSQLParseNode::cast_spec, "cast_spec" },
    1446             :             { OSQLParseNode::window_function, "window_function" }
    1447       13464 :         };
    1448         132 :         const size_t nRuleMapCount = sizeof( aRuleDescriptions ) / sizeof( aRuleDescriptions[0] );
    1449             :         // added a new rule? Adjust this map!
    1450             :         // +1 for UNKNOWN_RULE
    1451             :         static_assert(nRuleMapCount + 1 == static_cast<size_t>(OSQLParseNode::rule_count), "must be equal");
    1452             : 
    1453       13464 :         for ( size_t mapEntry = 0; mapEntry < nRuleMapCount; ++mapEntry )
    1454             :         {
    1455             :             // look up the rule description in the our identifier map
    1456       13332 :             sal_uInt32 nParserRuleID = StrToRuleID( aRuleDescriptions[ mapEntry ].sRuleName );
    1457             :             // map the parser's rule ID to the OSQLParseNode::Rule
    1458       13332 :             s_aReverseRuleIDLookup[ nParserRuleID ] = aRuleDescriptions[ mapEntry ].eRule;
    1459             :             // and map the OSQLParseNode::Rule to the parser's rule ID
    1460       13332 :             s_nRuleIDs[ aRuleDescriptions[ mapEntry ].eRule ] = nParserRuleID;
    1461       13464 :         }
    1462             :     }
    1463         431 :     ++s_nRefCount;
    1464             : 
    1465         431 :     if (m_pContext == NULL)
    1466             :         // take the default context
    1467          85 :         m_pContext = &s_aDefaultContext;
    1468             : 
    1469         431 :     m_pData->aLocale = m_pContext->getPreferredLocale();
    1470         431 : }
    1471             : 
    1472             : 
    1473         862 : OSQLParser::~OSQLParser()
    1474             : {
    1475             :     {
    1476         431 :         ::osl::MutexGuard aGuard(getMutex());
    1477             :         OSL_ENSURE(s_nRefCount > 0, "OSQLParser::~OSQLParser() : suspicious call : has a refcount of 0 !");
    1478         431 :         if (!--s_nRefCount)
    1479             :         {
    1480         132 :             s_pScanner->setScanner(true);
    1481         132 :             delete s_pScanner;
    1482         132 :             s_pScanner = NULL;
    1483             : 
    1484         132 :             delete s_pGarbageCollector;
    1485         132 :             s_pGarbageCollector = NULL;
    1486             :             // Is only set the first time, so we should delete it only when there are no more instances
    1487         132 :             s_xLocaleData = NULL;
    1488             : 
    1489         132 :             RuleIDMap aEmpty;
    1490         132 :             s_aReverseRuleIDLookup.swap( aEmpty );
    1491             :         }
    1492         431 :         m_pParseTree = NULL;
    1493             :     }
    1494         431 : }
    1495             : 
    1496           0 : void OSQLParseNode::substituteParameterNames(OSQLParseNode* _pNode)
    1497             : {
    1498           0 :     sal_Int32 nCount = _pNode->count();
    1499           0 :     for(sal_Int32 i=0;i < nCount;++i)
    1500             :     {
    1501           0 :         OSQLParseNode* pChildNode = _pNode->getChild(i);
    1502           0 :         if(SQL_ISRULE(pChildNode,parameter) && pChildNode->count() > 1)
    1503             :         {
    1504           0 :             OSQLParseNode* pNewNode = new OSQLParseNode(OUString("?") ,SQL_NODE_PUNCTUATION,0);
    1505           0 :             delete pChildNode->replace(pChildNode->getChild(0),pNewNode);
    1506           0 :             sal_Int32 nChildCount = pChildNode->count();
    1507           0 :             for(sal_Int32 j=1;j < nChildCount;++j)
    1508           0 :                 delete pChildNode->removeAt(1);
    1509             :         }
    1510             :         else
    1511           0 :             substituteParameterNames(pChildNode);
    1512             : 
    1513             :     }
    1514           0 : }
    1515             : 
    1516           0 : bool OSQLParser::extractDate(OSQLParseNode* pLiteral,double& _rfValue)
    1517             : {
    1518           0 :     Reference< XNumberFormatsSupplier > xFormatSup = m_xFormatter->getNumberFormatsSupplier();
    1519           0 :     Reference< XNumberFormatTypes > xFormatTypes;
    1520           0 :     if ( xFormatSup.is() )
    1521           0 :         xFormatTypes.set(xFormatSup->getNumberFormats(), css::uno::UNO_QUERY);
    1522             : 
    1523             :     // if there is no format key, yet, make sure we have a feasible one for our locale
    1524             :     try
    1525             :     {
    1526           0 :         if ( !m_nFormatKey && xFormatTypes.is() )
    1527           0 :             m_nFormatKey = ::dbtools::getDefaultNumberFormat( m_xField, xFormatTypes, m_pData->aLocale );
    1528             :     }
    1529           0 :     catch( Exception& ) { }
    1530           0 :     OUString sValue = pLiteral->getTokenValue();
    1531           0 :     sal_Int32 nTryFormat = m_nFormatKey;
    1532           0 :     bool bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
    1533             : 
    1534             :     // If our format key didn't do, try the default date format for our locale.
    1535           0 :     if ( !bSuccess && xFormatTypes.is() )
    1536             :     {
    1537             :         try
    1538             :         {
    1539           0 :             nTryFormat = xFormatTypes->getStandardFormat( NumberFormat::DATE, m_pData->aLocale );
    1540             :         }
    1541           0 :         catch( Exception& ) { }
    1542           0 :         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
    1543             :     }
    1544             : 
    1545             :     // if this also didn't do, try ISO format
    1546           0 :     if ( !bSuccess && xFormatTypes.is() )
    1547             :     {
    1548             :         try
    1549             :         {
    1550           0 :             nTryFormat = xFormatTypes->getFormatIndex( NumberFormatIndex::DATE_DIN_YYYYMMDD, m_pData->aLocale );
    1551             :         }
    1552           0 :         catch( Exception& ) { }
    1553           0 :         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
    1554             :     }
    1555             : 
    1556             :     // if this also didn't do, try fallback date format (en-US)
    1557           0 :     if ( !bSuccess )
    1558             :     {
    1559           0 :         nTryFormat = m_nDateFormatKey;
    1560           0 :         bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
    1561             :     }
    1562           0 :     return bSuccess;
    1563             : }
    1564             : 
    1565           0 : OSQLParseNode* OSQLParser::buildDate(sal_Int32 _nType,OSQLParseNode*& pLiteral)
    1566             : {
    1567             :     // try converting the string into a date, according to our format key
    1568           0 :     double fValue = 0.0;
    1569           0 :     OSQLParseNode* pFCTNode = NULL;
    1570             : 
    1571           0 :     if ( extractDate(pLiteral,fValue) )
    1572           0 :         pFCTNode = buildNode_Date( fValue, _nType);
    1573             : 
    1574           0 :     delete pLiteral;
    1575           0 :     pLiteral = NULL;
    1576             : 
    1577           0 :     if ( !pFCTNode )
    1578           0 :         m_sErrorMessage = m_pContext->getErrorMessage(IParseContext::ERROR_INVALID_DATE_COMPARE);
    1579             : 
    1580           0 :     return pFCTNode;
    1581             : }
    1582             : 
    1583             : 
    1584       14032 : OSQLParseNode::OSQLParseNode(const sal_Char * pNewValue,
    1585             :                              SQLNodeType eNewNodeType,
    1586             :                              sal_uInt32 nNewNodeID)
    1587             :         :m_pParent(NULL)
    1588       14032 :         ,m_aNodeValue(pNewValue,strlen(pNewValue),RTL_TEXTENCODING_UTF8)
    1589             :         ,m_eNodeType(eNewNodeType)
    1590       28064 :         ,m_nNodeID(nNewNodeID)
    1591             : {
    1592             :     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: created with invalid NodeType");
    1593       14032 : }
    1594             : 
    1595           0 : OSQLParseNode::OSQLParseNode(const OString &_rNewValue,
    1596             :                              SQLNodeType eNewNodeType,
    1597             :                              sal_uInt32 nNewNodeID)
    1598             :         :m_pParent(NULL)
    1599             :         ,m_aNodeValue(OStringToOUString(_rNewValue,RTL_TEXTENCODING_UTF8))
    1600             :         ,m_eNodeType(eNewNodeType)
    1601           0 :         ,m_nNodeID(nNewNodeID)
    1602             : {
    1603             :     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: created with invalid NodeType");
    1604           0 : }
    1605             : 
    1606        3539 : OSQLParseNode::OSQLParseNode(const OUString &_rNewValue,
    1607             :                                  SQLNodeType eNewNodeType,
    1608             :                                  sal_uInt32 nNewNodeID)
    1609             :         :m_pParent(NULL)
    1610             :         ,m_aNodeValue(_rNewValue)
    1611             :         ,m_eNodeType(eNewNodeType)
    1612        3539 :         ,m_nNodeID(nNewNodeID)
    1613             : {
    1614             :     OSL_ENSURE(m_eNodeType >= SQL_NODE_RULE && m_eNodeType <= SQL_NODE_CONCAT,"OSQLParseNode: created with invalid NodeType");
    1615        3539 : }
    1616             : 
    1617           0 : OSQLParseNode::OSQLParseNode(const OSQLParseNode& rParseNode)
    1618             : {
    1619             :     // Set the getParent to NULL
    1620           0 :     m_pParent = NULL;
    1621             : 
    1622             :     // Copy the members
    1623           0 :     m_aNodeValue = rParseNode.m_aNodeValue;
    1624           0 :     m_eNodeType  = rParseNode.m_eNodeType;
    1625           0 :     m_nNodeID    = rParseNode.m_nNodeID;
    1626             : 
    1627             : 
    1628             :     // Remember that we derived from Container. According to SV-Help the Container's
    1629             :     // copy ctor creates a new Container with the same pointers for content.
    1630             :     // This means after copying the Container, for all non-NULL pointers a copy is
    1631             :     // created and reattached instead of the old pointer.
    1632             : 
    1633             :     // If not a leaf, then process SubTrees
    1634           0 :     for (OSQLParseNodes::const_iterator i = rParseNode.m_aChildren.begin();
    1635           0 :          i != rParseNode.m_aChildren.end(); ++i)
    1636           0 :         append(new OSQLParseNode(**i));
    1637           0 : }
    1638             : 
    1639             : 
    1640           0 : OSQLParseNode& OSQLParseNode::operator=(const OSQLParseNode& rParseNode)
    1641             : {
    1642           0 :     if (this != &rParseNode)
    1643             :     {
    1644             :         // Copy the members - pParent remains the same
    1645           0 :         m_aNodeValue = rParseNode.m_aNodeValue;
    1646           0 :         m_eNodeType  = rParseNode.m_eNodeType;
    1647           0 :         m_nNodeID    = rParseNode.m_nNodeID;
    1648             : 
    1649           0 :         for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
    1650           0 :             i != m_aChildren.end(); ++i)
    1651           0 :             delete *i;
    1652             : 
    1653           0 :         m_aChildren.clear();
    1654             : 
    1655           0 :         for (OSQLParseNodes::const_iterator j = rParseNode.m_aChildren.begin();
    1656           0 :              j != rParseNode.m_aChildren.end(); ++j)
    1657           0 :             append(new OSQLParseNode(**j));
    1658             :     }
    1659           0 :     return *this;
    1660             : }
    1661             : 
    1662             : 
    1663         504 : bool OSQLParseNode::operator==(OSQLParseNode& rParseNode) const
    1664             : {
    1665             :     // The members must be equal
    1666         876 :     bool bResult = (m_nNodeID  == rParseNode.m_nNodeID) &&
    1667         744 :                    (m_eNodeType == rParseNode.m_eNodeType) &&
    1668        1144 :                    (m_aNodeValue == rParseNode.m_aNodeValue) &&
    1669         772 :                     count() == rParseNode.count();
    1670             : 
    1671             :     // Parameters are not equal!
    1672         504 :     bResult = bResult && !SQL_ISRULE(this, parameter);
    1673             : 
    1674             :     // compare children
    1675         772 :     for (sal_uInt32 i=0; bResult && i < count(); i++)
    1676         268 :         bResult = *getChild(i) == *rParseNode.getChild(i);
    1677             : 
    1678         504 :     return bResult;
    1679             : }
    1680             : 
    1681             : 
    1682       31410 : OSQLParseNode::~OSQLParseNode()
    1683             : {
    1684       92460 :     for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
    1685       61640 :          i != m_aChildren.end(); ++i)
    1686       15115 :         delete *i;
    1687       15705 :     m_aChildren.clear();
    1688       15705 : }
    1689             : 
    1690             : 
    1691       16929 : void OSQLParseNode::append(OSQLParseNode* pNewNode)
    1692             : {
    1693             :     OSL_ENSURE(pNewNode != NULL, "OSQLParseNode: invalid NewSubTree");
    1694             :     OSL_ENSURE(pNewNode->getParent() == NULL, "OSQLParseNode: Node is not an orphan");
    1695             :     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pNewNode) == m_aChildren.end(),
    1696             :             "OSQLParseNode::append() Node already element of parent");
    1697             : 
    1698             :     // Create connection to getParent
    1699       16929 :     pNewNode->setParent( this );
    1700             :     // and attach the SubTree at the end
    1701       16929 :     m_aChildren.push_back(pNewNode);
    1702       16929 : }
    1703             : 
    1704           0 : bool OSQLParseNode::addDateValue(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
    1705             : {
    1706             :     // special display for date/time values
    1707           0 :     if (SQL_ISRULE(this,set_fct_spec) && SQL_ISPUNCTUATION(m_aChildren[0],"{"))
    1708             :     {
    1709           0 :         const OSQLParseNode* pODBCNode = m_aChildren[1];
    1710           0 :         const OSQLParseNode* pODBCNodeChild = pODBCNode->m_aChildren[0];
    1711             : 
    1712           0 :         if (pODBCNodeChild->getNodeType() == SQL_NODE_KEYWORD && (
    1713           0 :             SQL_ISTOKEN(pODBCNodeChild, D) ||
    1714           0 :             SQL_ISTOKEN(pODBCNodeChild, T) ||
    1715           0 :             SQL_ISTOKEN(pODBCNodeChild, TS) ))
    1716             :         {
    1717           0 :             OUString suQuote("'");
    1718           0 :             if (rParam.bPredicate)
    1719             :             {
    1720           0 :                  if (rParam.aMetaData.shouldEscapeDateTime())
    1721             :                  {
    1722           0 :                      suQuote = "#";
    1723             :                  }
    1724             :             }
    1725             :             else
    1726             :             {
    1727           0 :                  if (rParam.aMetaData.shouldEscapeDateTime())
    1728             :                  {
    1729             :                      // suQuote = "'";
    1730           0 :                      return false;
    1731             :                  }
    1732             :             }
    1733             : 
    1734           0 :             if (!rString.isEmpty())
    1735           0 :                 rString.appendAscii(" ");
    1736           0 :             rString.append(suQuote);
    1737           0 :             const OUString sTokenValue = pODBCNode->m_aChildren[1]->getTokenValue();
    1738           0 :             if (SQL_ISTOKEN(pODBCNodeChild, D))
    1739             :             {
    1740           0 :                 rString.append(rParam.bPredicate ? convertDateString(rParam, sTokenValue) : sTokenValue);
    1741             :             }
    1742           0 :             else if (SQL_ISTOKEN(pODBCNodeChild, T))
    1743             :             {
    1744           0 :                 rString.append(rParam.bPredicate ? convertTimeString(rParam, sTokenValue) : sTokenValue);
    1745             :             }
    1746             :             else
    1747             :             {
    1748           0 :                 rString.append(rParam.bPredicate ? convertDateTimeString(rParam, sTokenValue) : sTokenValue);
    1749             :             }
    1750           0 :             rString.append(suQuote);
    1751           0 :             return true;
    1752             :         }
    1753             :     }
    1754           0 :     return false;
    1755             : }
    1756             : 
    1757           0 : void OSQLParseNode::replaceNodeValue(const OUString& rTableAlias, const OUString& rColumnName)
    1758             : {
    1759           0 :     for (size_t i=0;i<count();++i)
    1760             :     {
    1761           0 :         if (SQL_ISRULE(this,column_ref) && count() == 1 && getChild(0)->getTokenValue() == rColumnName)
    1762             :         {
    1763           0 :             OSQLParseNode * pCol = removeAt((sal_uInt32)0);
    1764           0 :             append(new OSQLParseNode(rTableAlias,SQL_NODE_NAME));
    1765           0 :             append(new OSQLParseNode(OUString("."),SQL_NODE_PUNCTUATION));
    1766           0 :             append(pCol);
    1767             :         }
    1768             :         else
    1769           0 :             getChild(i)->replaceNodeValue(rTableAlias,rColumnName);
    1770             :     }
    1771           0 : }
    1772             : 
    1773         180 : OSQLParseNode* OSQLParseNode::getByRule(OSQLParseNode::Rule eRule) const
    1774             : {
    1775         180 :     OSQLParseNode* pRetNode = 0;
    1776         180 :     if (isRule() && OSQLParser::RuleID(eRule) == getRuleID())
    1777           0 :         pRetNode = const_cast<OSQLParseNode*>(this);
    1778             :     else
    1779             :     {
    1780         972 :         for (OSQLParseNodes::const_iterator i = m_aChildren.begin();
    1781         972 :             !pRetNode && i != m_aChildren.end(); ++i)
    1782         144 :             pRetNode = (*i)->getByRule(eRule);
    1783             :     }
    1784         180 :     return pRetNode;
    1785             : }
    1786             : 
    1787           0 : OSQLParseNode* MakeANDNode(OSQLParseNode *pLeftLeaf,OSQLParseNode *pRightLeaf)
    1788             : {
    1789           0 :     OSQLParseNode* pNewNode = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_term));
    1790           0 :     pNewNode->append(pLeftLeaf);
    1791           0 :     pNewNode->append(new OSQLParseNode(OUString("AND"),SQL_NODE_KEYWORD,SQL_TOKEN_AND));
    1792           0 :     pNewNode->append(pRightLeaf);
    1793           0 :     return pNewNode;
    1794             : }
    1795             : 
    1796           0 : OSQLParseNode* MakeORNode(OSQLParseNode *pLeftLeaf,OSQLParseNode *pRightLeaf)
    1797             : {
    1798           0 :     OSQLParseNode* pNewNode = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::search_condition));
    1799           0 :     pNewNode->append(pLeftLeaf);
    1800           0 :     pNewNode->append(new OSQLParseNode(OUString("OR"),SQL_NODE_KEYWORD,SQL_TOKEN_OR));
    1801           0 :     pNewNode->append(pRightLeaf);
    1802           0 :     return pNewNode;
    1803             : }
    1804             : 
    1805          68 : void OSQLParseNode::disjunctiveNormalForm(OSQLParseNode*& pSearchCondition)
    1806             : {
    1807          68 :     if(!pSearchCondition) // no where condition at entry point
    1808          68 :         return;
    1809             : 
    1810          68 :     OSQLParseNode::absorptions(pSearchCondition);
    1811             :     // '(' search_condition ')'
    1812          68 :     if (SQL_ISRULE(pSearchCondition,boolean_primary))
    1813             :     {
    1814           0 :         OSQLParseNode* pLeft    = pSearchCondition->getChild(1);
    1815           0 :         disjunctiveNormalForm(pLeft);
    1816             :     }
    1817             :     // search_condition SQL_TOKEN_OR boolean_term
    1818          68 :     else if (SQL_ISRULE(pSearchCondition,search_condition))
    1819             :     {
    1820          16 :         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
    1821          16 :         disjunctiveNormalForm(pLeft);
    1822             : 
    1823          16 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    1824          16 :         disjunctiveNormalForm(pRight);
    1825             :     }
    1826             :     // boolean_term SQL_TOKEN_AND boolean_factor
    1827          52 :     else if (SQL_ISRULE(pSearchCondition,boolean_term))
    1828             :     {
    1829          16 :         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
    1830          16 :         disjunctiveNormalForm(pLeft);
    1831             : 
    1832          16 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    1833          16 :         disjunctiveNormalForm(pRight);
    1834             : 
    1835          16 :         OSQLParseNode* pNewNode = NULL;
    1836             :         // '(' search_condition ')' on left side
    1837          16 :         if(pLeft->count() == 3 && SQL_ISRULE(pLeft,boolean_primary) && SQL_ISRULE(pLeft->getChild(1),search_condition))
    1838             :         {
    1839             :             // and-or tree  on left side
    1840           0 :             OSQLParseNode* pOr = pLeft->getChild(1);
    1841           0 :             OSQLParseNode* pNewLeft = NULL;
    1842           0 :             OSQLParseNode* pNewRight = NULL;
    1843             : 
    1844             :             // cut right from parent
    1845           0 :             pSearchCondition->removeAt(2);
    1846             : 
    1847           0 :             pNewRight   = MakeANDNode(pOr->removeAt(2)      ,pRight);
    1848           0 :             pNewLeft    = MakeANDNode(pOr->removeAt((sal_uInt32)0)  ,new OSQLParseNode(*pRight));
    1849           0 :             pNewNode    = MakeORNode(pNewLeft,pNewRight);
    1850             :             // and append new Node
    1851           0 :             replaceAndReset(pSearchCondition,pNewNode);
    1852             : 
    1853           0 :             disjunctiveNormalForm(pSearchCondition);
    1854             :         }
    1855          16 :         else if(pRight->count() == 3 && SQL_ISRULE(pRight,boolean_primary) && SQL_ISRULE(pRight->getChild(1),search_condition))
    1856             :         {   // '(' search_condition ')' on right side
    1857             :             // and-or tree  on right side
    1858             :             // a and (b or c)
    1859           0 :             OSQLParseNode* pOr = pRight->getChild(1);
    1860           0 :             OSQLParseNode* pNewLeft = NULL;
    1861           0 :             OSQLParseNode* pNewRight = NULL;
    1862             : 
    1863             :             // cut left from parent
    1864           0 :             pSearchCondition->removeAt((sal_uInt32)0);
    1865             : 
    1866           0 :             pNewRight   = MakeANDNode(pLeft,pOr->removeAt(2));
    1867           0 :             pNewLeft    = MakeANDNode(new OSQLParseNode(*pLeft),pOr->removeAt((sal_uInt32)0));
    1868           0 :             pNewNode    = MakeORNode(pNewLeft,pNewRight);
    1869             : 
    1870             :             // and append new Node
    1871           0 :             replaceAndReset(pSearchCondition,pNewNode);
    1872           0 :             disjunctiveNormalForm(pSearchCondition);
    1873             :         }
    1874          16 :         else if(SQL_ISRULE(pLeft,boolean_primary) && (!SQL_ISRULE(pLeft->getChild(1),search_condition) || !SQL_ISRULE(pLeft->getChild(1),boolean_term)))
    1875           0 :             pSearchCondition->replace(pLeft, pLeft->removeAt(1));
    1876          16 :         else if(SQL_ISRULE(pRight,boolean_primary) && (!SQL_ISRULE(pRight->getChild(1),search_condition) || !SQL_ISRULE(pRight->getChild(1),boolean_term)))
    1877           0 :             pSearchCondition->replace(pRight, pRight->removeAt(1));
    1878             :     }
    1879             : }
    1880             : 
    1881          88 : void OSQLParseNode::negateSearchCondition(OSQLParseNode*& pSearchCondition, bool bNegate)
    1882             : {
    1883          88 :     if(!pSearchCondition) // no where condition at entry point
    1884          88 :         return;
    1885             :     // '(' search_condition ')'
    1886          88 :     if (pSearchCondition->count() == 3 && SQL_ISRULE(pSearchCondition,boolean_primary))
    1887             :     {
    1888          20 :         OSQLParseNode* pRight = pSearchCondition->getChild(1);
    1889          20 :         negateSearchCondition(pRight,bNegate);
    1890             :     }
    1891             :     // search_condition SQL_TOKEN_OR boolean_term
    1892          68 :     else if (SQL_ISRULE(pSearchCondition,search_condition))
    1893             :     {
    1894          16 :         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
    1895          16 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    1896          16 :         if(bNegate)
    1897             :         {
    1898           0 :             OSQLParseNode* pNewNode = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_term));
    1899           0 :             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)0));
    1900           0 :             pNewNode->append(new OSQLParseNode(OUString("AND"),SQL_NODE_KEYWORD,SQL_TOKEN_AND));
    1901           0 :             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)1));
    1902           0 :             replaceAndReset(pSearchCondition,pNewNode);
    1903             : 
    1904           0 :             pLeft   = pNewNode->getChild(0);
    1905           0 :             pRight  = pNewNode->getChild(2);
    1906             :         }
    1907             : 
    1908          16 :         negateSearchCondition(pLeft,bNegate);
    1909          16 :         negateSearchCondition(pRight,bNegate);
    1910             :     }
    1911             :     // boolean_term SQL_TOKEN_AND boolean_factor
    1912          52 :     else if (SQL_ISRULE(pSearchCondition,boolean_term))
    1913             :     {
    1914          16 :         OSQLParseNode* pLeft    = pSearchCondition->getChild(0);
    1915          16 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    1916          16 :         if(bNegate)
    1917             :         {
    1918           0 :             OSQLParseNode* pNewNode = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::search_condition));
    1919           0 :             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)0));
    1920           0 :             pNewNode->append(new OSQLParseNode(OUString("OR"),SQL_NODE_KEYWORD,SQL_TOKEN_OR));
    1921           0 :             pNewNode->append(pSearchCondition->removeAt((sal_uInt32)1));
    1922           0 :             replaceAndReset(pSearchCondition,pNewNode);
    1923             : 
    1924           0 :             pLeft   = pNewNode->getChild(0);
    1925           0 :             pRight  = pNewNode->getChild(2);
    1926             :         }
    1927             : 
    1928          16 :         negateSearchCondition(pLeft,bNegate);
    1929          16 :         negateSearchCondition(pRight,bNegate);
    1930             :     }
    1931             :     // SQL_TOKEN_NOT ( boolean_primary )
    1932          36 :     else if (SQL_ISRULE(pSearchCondition,boolean_factor))
    1933             :     {
    1934           0 :         OSQLParseNode *pNot = pSearchCondition->removeAt((sal_uInt32)0);
    1935           0 :         delete pNot;
    1936           0 :         OSQLParseNode *pBooleanTest = pSearchCondition->removeAt((sal_uInt32)0);
    1937             :         // TODO is this needed // pBooleanTest->setParent(NULL);
    1938           0 :         replaceAndReset(pSearchCondition,pBooleanTest);
    1939             : 
    1940           0 :         if (!bNegate)
    1941           0 :             negateSearchCondition(pSearchCondition, true); // negate all deeper values
    1942             :     }
    1943             :     // row_value_constructor comparison row_value_constructor
    1944             :     // row_value_constructor comparison any_all_some subquery
    1945          36 :     else if(bNegate && (SQL_ISRULE(pSearchCondition,comparison_predicate) || SQL_ISRULE(pSearchCondition,all_or_any_predicate)))
    1946             :     {
    1947             :         assert(pSearchCondition->count() == 3);
    1948           0 :         OSQLParseNode* pComparison = pSearchCondition->getChild(1);
    1949           0 :         if(SQL_ISRULE(pComparison, comparison))
    1950             :         {
    1951             :             assert(pComparison->count() == 2 ||
    1952             :                    pComparison->count() == 4);
    1953             :             assert(SQL_ISTOKEN(pComparison->getChild(0), IS));
    1954             : 
    1955           0 :             OSQLParseNode* pNot = pComparison->getChild(1);
    1956           0 :             OSQLParseNode* pNotNot = NULL;
    1957           0 :             if(pNot->isRule()) // no NOT token (empty rule)
    1958           0 :                 pNotNot = new OSQLParseNode(OUString("NOT"),SQL_NODE_KEYWORD,SQL_TOKEN_NOT);
    1959             :             else
    1960             :             {
    1961             :                 assert(SQL_ISTOKEN(pNot,NOT));
    1962           0 :                 pNotNot = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::sql_not));
    1963             :             }
    1964           0 :             pComparison->replace(pNot, pNotNot);
    1965           0 :             delete pNot;
    1966             :         }
    1967             :         else
    1968             :         {
    1969             :             OSQLParseNode* pNewComparison;
    1970           0 :             switch(pComparison->getNodeType())
    1971             :             {
    1972             :             default:
    1973             :                 assert(false && "OSQLParseNode::negateSearchCondition: unexpected node type!");
    1974             :                 // fall-through
    1975             :             case SQL_NODE_EQUAL:
    1976           0 :                 pNewComparison = new OSQLParseNode(OUString("<>"),SQL_NODE_NOTEQUAL,SQL_NOTEQUAL);
    1977           0 :                 break;
    1978             :             case SQL_NODE_LESS:
    1979           0 :                 pNewComparison = new OSQLParseNode(OUString(">="),SQL_NODE_GREATEQ,SQL_GREATEQ);
    1980           0 :                 break;
    1981             :             case SQL_NODE_GREAT:
    1982           0 :                 pNewComparison = new OSQLParseNode(OUString("<="),SQL_NODE_LESSEQ,SQL_LESSEQ);
    1983           0 :                 break;
    1984             :             case SQL_NODE_LESSEQ:
    1985           0 :                 pNewComparison = new OSQLParseNode(OUString(">"),SQL_NODE_GREAT,SQL_GREAT);
    1986           0 :                 break;
    1987             :             case SQL_NODE_GREATEQ:
    1988           0 :                 pNewComparison = new OSQLParseNode(OUString("<"),SQL_NODE_LESS,SQL_LESS);
    1989           0 :                 break;
    1990             :             case SQL_NODE_NOTEQUAL:
    1991           0 :                 pNewComparison = new OSQLParseNode(OUString("="),SQL_NODE_EQUAL,SQL_EQUAL);
    1992           0 :                 break;
    1993             :             }
    1994           0 :             pSearchCondition->replace(pComparison, pNewComparison);
    1995           0 :             delete pComparison;
    1996             :         }
    1997             :     }
    1998             : 
    1999          36 :     else if(bNegate && (SQL_ISRULE(pSearchCondition,test_for_null) ||
    2000           0 :                         SQL_ISRULE(pSearchCondition,in_predicate)  ||
    2001           0 :                         SQL_ISRULE(pSearchCondition,between_predicate) ))
    2002             :     {
    2003           0 :         OSQLParseNode* pPart2 = pSearchCondition->getChild(1);
    2004           0 :         sal_uInt32 nNotPos = 0;
    2005           0 :         if  ( SQL_ISRULE( pSearchCondition, test_for_null ) )
    2006           0 :             nNotPos = 1;
    2007             : 
    2008           0 :         OSQLParseNode* pNot = pPart2->getChild(nNotPos);
    2009           0 :         OSQLParseNode* pNotNot = NULL;
    2010           0 :         if(pNot->isRule()) // no NOT token (empty rule)
    2011           0 :             pNotNot = new OSQLParseNode(OUString("NOT"),SQL_NODE_KEYWORD,SQL_TOKEN_NOT);
    2012             :         else
    2013             :         {
    2014             :             assert(SQL_ISTOKEN(pNot,NOT));
    2015           0 :             pNotNot = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::sql_not));
    2016             :         }
    2017           0 :         pPart2->replace(pNot, pNotNot);
    2018           0 :         delete pNot;
    2019             :     }
    2020          36 :     else if(bNegate && (SQL_ISRULE(pSearchCondition,like_predicate)))
    2021             :     {
    2022           0 :         OSQLParseNode* pNot = pSearchCondition->getChild( 1 )->getChild( 0 );
    2023           0 :         OSQLParseNode* pNotNot = NULL;
    2024           0 :         if(pNot->isRule())
    2025           0 :             pNotNot = new OSQLParseNode(OUString("NOT"),SQL_NODE_KEYWORD,SQL_TOKEN_NOT);
    2026             :         else
    2027           0 :             pNotNot = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::sql_not));
    2028           0 :         pSearchCondition->getChild( 1 )->replace(pNot, pNotNot);
    2029           0 :         delete pNot;
    2030             :     }
    2031             : }
    2032             : 
    2033         840 : void OSQLParseNode::eraseBraces(OSQLParseNode*& pSearchCondition)
    2034             : {
    2035         860 :     if (pSearchCondition && (SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
    2036           0 :          SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")"))))
    2037             :     {
    2038          20 :         OSQLParseNode* pRight = pSearchCondition->getChild(1);
    2039          20 :         absorptions(pRight);
    2040             :         // if child is not an or and tree then delete () around child
    2041          72 :         if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
    2042          64 :             SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || // and can always stand without ()
    2043           0 :             (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
    2044             :         {
    2045          20 :             OSQLParseNode* pNode = pSearchCondition->removeAt(1);
    2046          20 :             replaceAndReset(pSearchCondition,pNode);
    2047             :         }
    2048             :     }
    2049         840 : }
    2050             : 
    2051         420 : void OSQLParseNode::absorptions(OSQLParseNode*& pSearchCondition)
    2052             : {
    2053         420 :     if(!pSearchCondition) // no where condition at entry point
    2054         420 :         return;
    2055             : 
    2056         420 :     eraseBraces(pSearchCondition);
    2057             : 
    2058         420 :     if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
    2059             :     {
    2060         164 :         OSQLParseNode* pLeft = pSearchCondition->getChild(0);
    2061         164 :         absorptions(pLeft);
    2062         164 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    2063         164 :         absorptions(pRight);
    2064             :     }
    2065             : 
    2066         420 :     sal_uInt32 nPos = 0;
    2067             :     // a and a || a or a
    2068         420 :     OSQLParseNode* pNewNode = NULL;
    2069        1260 :     if(( SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
    2070         584 :         && *pSearchCondition->getChild(0) == *pSearchCondition->getChild(2))
    2071             :     {
    2072           0 :         pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
    2073           0 :         replaceAndReset(pSearchCondition,pNewNode);
    2074             :     }
    2075             :     // (a or b) and a || ( b or c ) and a
    2076             :     // a and ( a or b) || a and ( b or c )
    2077        1260 :     else if (   SQL_ISRULE(pSearchCondition,boolean_term)
    2078         528 :             &&  (
    2079         216 :                     (       SQL_ISRULE(pSearchCondition->getChild(nPos = 0),boolean_primary)
    2080         108 :                         ||  SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
    2081             :                     )
    2082         108 :                 ||  (       SQL_ISRULE(pSearchCondition->getChild(nPos = 2),boolean_primary)
    2083         108 :                         ||  SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
    2084             :                     )
    2085             :                 )
    2086             :             )
    2087             :     {
    2088           0 :         OSQLParseNode* p2ndSearch = pSearchCondition->getChild(nPos);
    2089           0 :         if ( SQL_ISRULE(p2ndSearch,boolean_primary) )
    2090           0 :             p2ndSearch = p2ndSearch->getChild(1);
    2091             : 
    2092           0 :         if ( *p2ndSearch->getChild(0) == *pSearchCondition->getChild(2-nPos) ) // a and ( a or b) -> a or b
    2093             :         {
    2094           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
    2095           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2096             : 
    2097             :         }
    2098           0 :         else if ( *p2ndSearch->getChild(2) == *pSearchCondition->getChild(2-nPos) ) // a and ( b or a) -> a or b
    2099             :         {
    2100           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
    2101           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2102             :         }
    2103           0 :         else if ( p2ndSearch->getByRule(OSQLParseNode::search_condition) )
    2104             :         {
    2105             :             // a and ( b or c ) -> ( a and b ) or ( a and c )
    2106             :             // ( b or c ) and a -> ( a and b ) or ( a and c )
    2107           0 :             OSQLParseNode* pC = p2ndSearch->removeAt((sal_uInt32)2);
    2108           0 :             OSQLParseNode* pB = p2ndSearch->removeAt((sal_uInt32)0);
    2109           0 :             OSQLParseNode* pA = pSearchCondition->removeAt((sal_uInt32)2-nPos);
    2110             : 
    2111           0 :             OSQLParseNode* p1stAnd = MakeANDNode(pA,pB);
    2112           0 :             OSQLParseNode* p2ndAnd = MakeANDNode(new OSQLParseNode(*pA),pC);
    2113           0 :             pNewNode = MakeORNode(p1stAnd,p2ndAnd);
    2114           0 :             OSQLParseNode* pNode = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
    2115           0 :             pNode->append(new OSQLParseNode(OUString("("),SQL_NODE_PUNCTUATION));
    2116           0 :             pNode->append(pNewNode);
    2117           0 :             pNode->append(new OSQLParseNode(OUString(")"),SQL_NODE_PUNCTUATION));
    2118           0 :             OSQLParseNode::eraseBraces(p1stAnd);
    2119           0 :             OSQLParseNode::eraseBraces(p2ndAnd);
    2120           0 :             replaceAndReset(pSearchCondition,pNode);
    2121             :         }
    2122             :     }
    2123             :     // a or a and b || a or b and a
    2124         420 :     else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term))
    2125             :     {
    2126          36 :         if(*pSearchCondition->getChild(2)->getChild(0) == *pSearchCondition->getChild(0))
    2127             :         {
    2128           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
    2129           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2130             :         }
    2131          36 :         else if(*pSearchCondition->getChild(2)->getChild(2) == *pSearchCondition->getChild(0))
    2132             :         {
    2133           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)0);
    2134           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2135             :         }
    2136             :     }
    2137             :     // a and b or a || b and a or a
    2138         384 :     else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term))
    2139             :     {
    2140           0 :         if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2))
    2141             :         {
    2142           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
    2143           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2144             :         }
    2145           0 :         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2))
    2146             :         {
    2147           0 :             pNewNode = pSearchCondition->removeAt((sal_uInt32)2);
    2148           0 :             replaceAndReset(pSearchCondition,pNewNode);
    2149             :         }
    2150             :     }
    2151         420 :     eraseBraces(pSearchCondition);
    2152             : }
    2153             : 
    2154           0 : void OSQLParseNode::compress(OSQLParseNode *&pSearchCondition)
    2155             : {
    2156           0 :     if(!pSearchCondition) // no WHERE condition at entry point
    2157           0 :         return;
    2158             : 
    2159           0 :     OSQLParseNode::eraseBraces(pSearchCondition);
    2160             : 
    2161           0 :     if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
    2162             :     {
    2163           0 :         OSQLParseNode* pLeft = pSearchCondition->getChild(0);
    2164           0 :         compress(pLeft);
    2165             : 
    2166           0 :         OSQLParseNode* pRight = pSearchCondition->getChild(2);
    2167           0 :         compress(pRight);
    2168             :     }
    2169           0 :     else if( SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
    2170           0 :              SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")")))
    2171             :     {
    2172           0 :         OSQLParseNode* pRight = pSearchCondition->getChild(1);
    2173           0 :         compress(pRight);
    2174             :         // if child is not an or and tree then delete () around child
    2175           0 :         if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
    2176           0 :             (SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) && SQL_ISRULE(pSearchCondition->getParent(),boolean_term)) ||
    2177           0 :             (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
    2178             :         {
    2179           0 :             OSQLParseNode* pNode = pSearchCondition->removeAt(1);
    2180           0 :             replaceAndReset(pSearchCondition,pNode);
    2181             :         }
    2182             :     }
    2183             : 
    2184             :     // or with two and trees where one element of the and trees are equal
    2185           0 :     if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term))
    2186             :     {
    2187           0 :         if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(0))
    2188             :         {
    2189           0 :             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt(2);
    2190           0 :             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
    2191           0 :             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
    2192             : 
    2193           0 :             OSQLParseNode* pNewRule = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
    2194           0 :             pNewRule->append(new OSQLParseNode(OUString("("),SQL_NODE_PUNCTUATION));
    2195           0 :             pNewRule->append(pNode);
    2196           0 :             pNewRule->append(new OSQLParseNode(OUString(")"),SQL_NODE_PUNCTUATION));
    2197             : 
    2198           0 :             OSQLParseNode::eraseBraces(pLeft);
    2199           0 :             OSQLParseNode::eraseBraces(pRight);
    2200             : 
    2201           0 :             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt((sal_uInt32)0),pNewRule);
    2202           0 :             replaceAndReset(pSearchCondition,pNode);
    2203             :         }
    2204           0 :         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(0))
    2205             :         {
    2206           0 :             OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt((sal_uInt32)0);
    2207           0 :             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
    2208           0 :             OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
    2209             : 
    2210           0 :             OSQLParseNode* pNewRule = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
    2211           0 :             pNewRule->append(new OSQLParseNode(OUString("("),SQL_NODE_PUNCTUATION));
    2212           0 :             pNewRule->append(pNode);
    2213           0 :             pNewRule->append(new OSQLParseNode(OUString(")"),SQL_NODE_PUNCTUATION));
    2214             : 
    2215           0 :             OSQLParseNode::eraseBraces(pLeft);
    2216           0 :             OSQLParseNode::eraseBraces(pRight);
    2217             : 
    2218           0 :             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
    2219           0 :             replaceAndReset(pSearchCondition,pNode);
    2220             :         }
    2221           0 :         else if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(2))
    2222             :         {
    2223           0 :             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt(2);
    2224           0 :             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt((sal_uInt32)0);
    2225           0 :             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
    2226             : 
    2227           0 :             OSQLParseNode* pNewRule = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
    2228           0 :             pNewRule->append(new OSQLParseNode(OUString("("),SQL_NODE_PUNCTUATION));
    2229           0 :             pNewRule->append(pNode);
    2230           0 :             pNewRule->append(new OSQLParseNode(OUString(")"),SQL_NODE_PUNCTUATION));
    2231             : 
    2232           0 :             OSQLParseNode::eraseBraces(pLeft);
    2233           0 :             OSQLParseNode::eraseBraces(pRight);
    2234             : 
    2235           0 :             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt((sal_uInt32)0),pNewRule);
    2236           0 :             replaceAndReset(pSearchCondition,pNode);
    2237             :         }
    2238           0 :         else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(2))
    2239             :         {
    2240           0 :             OSQLParseNode* pLeft    = pSearchCondition->getChild(0)->removeAt((sal_uInt32)0);
    2241           0 :             OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt((sal_uInt32)0);
    2242           0 :             OSQLParseNode* pNode    = MakeORNode(pLeft,pRight);
    2243             : 
    2244           0 :             OSQLParseNode* pNewRule = new OSQLParseNode(OUString(),SQL_NODE_RULE,OSQLParser::RuleID(OSQLParseNode::boolean_primary));
    2245           0 :             pNewRule->append(new OSQLParseNode(OUString("("),SQL_NODE_PUNCTUATION));
    2246           0 :             pNewRule->append(pNode);
    2247           0 :             pNewRule->append(new OSQLParseNode(OUString(")"),SQL_NODE_PUNCTUATION));
    2248             : 
    2249           0 :             OSQLParseNode::eraseBraces(pLeft);
    2250           0 :             OSQLParseNode::eraseBraces(pRight);
    2251             : 
    2252           0 :             pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
    2253           0 :             replaceAndReset(pSearchCondition,pNode);
    2254             :         }
    2255             :     }
    2256             : }
    2257             : #if OSL_DEBUG_LEVEL > 1
    2258             : 
    2259             : void OSQLParseNode::showParseTree( OUString& rString ) const
    2260             : {
    2261             :     OUStringBuffer aBuf;
    2262             :     showParseTree( aBuf, 0 );
    2263             :     rString = aBuf.makeStringAndClear();
    2264             : }
    2265             : 
    2266             : 
    2267             : void OSQLParseNode::showParseTree( OUStringBuffer& _inout_rBuffer, sal_uInt32 nLevel ) const
    2268             : {
    2269             :     for ( sal_uInt32 j=0; j<nLevel; ++j)
    2270             :         _inout_rBuffer.appendAscii( "  " );
    2271             : 
    2272             :     if ( !isToken() )
    2273             :     {
    2274             :         // Rule name as rule
    2275             :         _inout_rBuffer.appendAscii( "RULE_ID: " );
    2276             :         _inout_rBuffer.append( (sal_Int32)getRuleID() );
    2277             :         _inout_rBuffer.append( '(' );
    2278             :         _inout_rBuffer.append( OSQLParser::RuleIDToStr( getRuleID() ) );
    2279             :         _inout_rBuffer.append( ')' );
    2280             :         _inout_rBuffer.append( '\n' );
    2281             : 
    2282             :         // Get the first sub tree
    2283             :         for (   OSQLParseNodes::const_iterator i = m_aChildren.begin();
    2284             :                 i != m_aChildren.end();
    2285             :                 ++i
    2286             :              )
    2287             :             (*i)->showParseTree( _inout_rBuffer, nLevel+1 );
    2288             :     }
    2289             :     else
    2290             :     {
    2291             :         // Found a token
    2292             :         switch (m_eNodeType)
    2293             :         {
    2294             : 
    2295             :         case SQL_NODE_KEYWORD:
    2296             :             _inout_rBuffer.appendAscii( "SQL_KEYWORD: " );
    2297             :             _inout_rBuffer.append( OStringToOUString( OSQLParser::TokenIDToStr( getTokenID() ), RTL_TEXTENCODING_UTF8 ) );
    2298             :             _inout_rBuffer.append( '\n' );
    2299             :             break;
    2300             : 
    2301             :         case SQL_NODE_COMPARISON:
    2302             :             _inout_rBuffer.appendAscii( "SQL_COMPARISON: " );
    2303             :             _inout_rBuffer.append( m_aNodeValue );
    2304             :             _inout_rBuffer.append( '\n' );
    2305             :             break;
    2306             : 
    2307             :         case SQL_NODE_NAME:
    2308             :             _inout_rBuffer.appendAscii( "SQL_NAME: " );
    2309             :             _inout_rBuffer.append( '"' );
    2310             :             _inout_rBuffer.append( m_aNodeValue );
    2311             :             _inout_rBuffer.append( '"' );
    2312             :             _inout_rBuffer.append( '\n' );
    2313             :              break;
    2314             : 
    2315             :         case SQL_NODE_STRING:
    2316             :             _inout_rBuffer.appendAscii( "SQL_STRING: " );
    2317             :             _inout_rBuffer.append( '\'' );
    2318             :             _inout_rBuffer.append( m_aNodeValue );
    2319             :             _inout_rBuffer.append( '\'' );
    2320             :             _inout_rBuffer.append( '\n' );
    2321             :             break;
    2322             : 
    2323             :         case SQL_NODE_INTNUM:
    2324             :             _inout_rBuffer.appendAscii( "SQL_INTNUM: " );
    2325             :             _inout_rBuffer.append( m_aNodeValue );
    2326             :             _inout_rBuffer.append( '\n' );
    2327             :             break;
    2328             : 
    2329             :         case SQL_NODE_APPROXNUM:
    2330             :             _inout_rBuffer.appendAscii( "SQL_APPROXNUM: " );
    2331             :             _inout_rBuffer.append( m_aNodeValue );
    2332             :             _inout_rBuffer.append( '\n' );
    2333             :              break;
    2334             : 
    2335             :         case SQL_NODE_PUNCTUATION:
    2336             :             _inout_rBuffer.appendAscii( "SQL_PUNCTUATION: " );
    2337             :             _inout_rBuffer.append( m_aNodeValue );
    2338             :             _inout_rBuffer.append( '\n' );
    2339             :             break;
    2340             : 
    2341             :         case SQL_NODE_AMMSC:
    2342             :             _inout_rBuffer.appendAscii( "SQL_AMMSC: " );
    2343             :             _inout_rBuffer.append( m_aNodeValue );
    2344             :             _inout_rBuffer.append( '\n' );
    2345             :             break;
    2346             : 
    2347             :         case SQL_NODE_EQUAL:
    2348             :         case SQL_NODE_LESS:
    2349             :         case SQL_NODE_GREAT:
    2350             :         case SQL_NODE_LESSEQ:
    2351             :         case SQL_NODE_GREATEQ:
    2352             :         case SQL_NODE_NOTEQUAL:
    2353             :             _inout_rBuffer.append( m_aNodeValue );
    2354             :             _inout_rBuffer.append( '\n' );
    2355             :             break;
    2356             : 
    2357             :         case SQL_NODE_ACCESS_DATE:
    2358             :             _inout_rBuffer.appendAscii( "SQL_ACCESS_DATE: " );
    2359             :             _inout_rBuffer.append( m_aNodeValue );
    2360             :             _inout_rBuffer.append( '\n' );
    2361             :             break;
    2362             : 
    2363             :         case SQL_NODE_DATE:
    2364             :             _inout_rBuffer.appendAscii( "SQL_DATE: " );
    2365             :             _inout_rBuffer.append( m_aNodeValue );
    2366             :             _inout_rBuffer.append( '\n' );
    2367             :             break;
    2368             : 
    2369             :         case SQL_NODE_CONCAT:
    2370             :             _inout_rBuffer.appendAscii( "||" );
    2371             :             _inout_rBuffer.append( '\n' );
    2372             :             break;
    2373             : 
    2374             :         default:
    2375             :             SAL_INFO( "connectivity.parse", "-- " << int( m_eNodeType ) );
    2376             :             SAL_WARN( "connectivity.parse", "OSQLParser::ShowParseTree: unzulaessiger NodeType" );
    2377             :         }
    2378             :     }
    2379             : }
    2380             : #endif // OSL_DEBUG_LEVEL > 0
    2381             : 
    2382             : // Insert methods
    2383             : 
    2384           0 : void OSQLParseNode::insert(sal_uInt32 nPos, OSQLParseNode* pNewSubTree)
    2385             : {
    2386             :     OSL_ENSURE(pNewSubTree != NULL, "OSQLParseNode: invalid NewSubTree");
    2387             :     OSL_ENSURE(pNewSubTree->getParent() == NULL, "OSQLParseNode: Node is not an orphan");
    2388             : 
    2389             :     // Create connection to getParent
    2390           0 :     pNewSubTree->setParent( this );
    2391           0 :     m_aChildren.insert(m_aChildren.begin() + nPos, pNewSubTree);
    2392           0 : }
    2393             : 
    2394             : // removeAt methods
    2395             : 
    2396          56 : OSQLParseNode* OSQLParseNode::removeAt(sal_uInt32 nPos)
    2397             : {
    2398             :     OSL_ENSURE(nPos < m_aChildren.size(),"Illegal position for removeAt");
    2399          56 :     OSQLParseNodes::iterator aPos(m_aChildren.begin() + nPos);
    2400          56 :     OSQLParseNode* pNode = *aPos;
    2401             : 
    2402             :     // Set the getParent of the removed node to NULL
    2403          56 :     pNode->setParent( NULL );
    2404             : 
    2405          56 :     m_aChildren.erase(aPos);
    2406          56 :     return pNode;
    2407             : }
    2408             : 
    2409             : // Replace methods
    2410             : 
    2411          20 : OSQLParseNode* OSQLParseNode::replace (OSQLParseNode* pOldSubNode, OSQLParseNode* pNewSubNode )
    2412             : {
    2413             :     OSL_ENSURE(pOldSubNode != NULL && pNewSubNode != NULL, "OSQLParseNode: invalid nodes");
    2414             :     OSL_ENSURE(pNewSubNode->getParent() == NULL, "OSQLParseNode: node already has getParent");
    2415             :     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pOldSubNode) != m_aChildren.end(),
    2416             :             "OSQLParseNode::Replace() Node not element of parent");
    2417             :     OSL_ENSURE(::std::find(m_aChildren.begin(), m_aChildren.end(), pNewSubNode) == m_aChildren.end(),
    2418             :             "OSQLParseNode::Replace() Node already element of parent");
    2419             : 
    2420          20 :     pOldSubNode->setParent( NULL );
    2421          20 :     pNewSubNode->setParent( this );
    2422          20 :     ::std::replace(m_aChildren.begin(), m_aChildren.end(), pOldSubNode, pNewSubNode);
    2423          20 :     return pOldSubNode;
    2424             : }
    2425             : 
    2426        7218 : void OSQLParseNode::parseLeaf(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
    2427             : {
    2428             :     // Found a leaf
    2429             :     // Append content to the output string
    2430        7218 :     switch (m_eNodeType)
    2431             :     {
    2432             :         case SQL_NODE_KEYWORD:
    2433             :         {
    2434        1241 :             if (!rString.isEmpty())
    2435        1013 :                 rString.appendAscii(" ");
    2436             : 
    2437        1241 :             const OString sT = OSQLParser::TokenIDToStr(m_nNodeID, rParam.bInternational ? &rParam.m_rContext :  NULL);
    2438        1241 :             rString.append(OStringToOUString(sT,RTL_TEXTENCODING_UTF8));
    2439        1241 :         }   break;
    2440             :         case SQL_NODE_STRING:
    2441        1756 :             if (!rString.isEmpty())
    2442         738 :                 rString.appendAscii(" ");
    2443        1756 :             rString.append(SetQuotation(m_aNodeValue,OUString("\'"),OUString("\'\'")));
    2444        1756 :             break;
    2445             :         case SQL_NODE_NAME:
    2446        1608 :             if (!rString.isEmpty())
    2447             :             {
    2448        1372 :                 switch(rString[rString.getLength()-1])
    2449             :                 {
    2450             :                     case ' ' :
    2451         234 :                     case '.' : break;
    2452             :                     default  :
    2453        2276 :                         if  (   rParam.aMetaData.getCatalogSeparator().isEmpty()
    2454        1138 :                                 || rString[rString.getLength() - 1] != rParam.aMetaData.getCatalogSeparator().toChar()
    2455             :                             )
    2456        1138 :                             rString.appendAscii(" "); break;
    2457             :                 }
    2458             :             }
    2459        1608 :             if (rParam.bQuote)
    2460             :             {
    2461        1430 :                 if (rParam.bPredicate)
    2462             :                 {
    2463           0 :                     rString.appendAscii("[");
    2464           0 :                     rString.append(m_aNodeValue);
    2465           0 :                     rString.appendAscii("]");
    2466             :                 }
    2467             :                 else
    2468             :                     rString.append(SetQuotation(m_aNodeValue,
    2469        1430 :                         rParam.aMetaData.getIdentifierQuoteString(), rParam.aMetaData.getIdentifierQuoteString() ));
    2470             :             }
    2471             :             else
    2472         178 :                 rString.append(m_aNodeValue);
    2473        1608 :             break;
    2474             :         case SQL_NODE_ACCESS_DATE:
    2475           0 :             if (!rString.isEmpty())
    2476           0 :                 rString.appendAscii(" ");
    2477           0 :             rString.appendAscii("#");
    2478           0 :             rString.append(m_aNodeValue);
    2479           0 :             rString.appendAscii("#");
    2480           0 :             break;
    2481             : 
    2482             :         case SQL_NODE_INTNUM:
    2483             :         case SQL_NODE_APPROXNUM:
    2484             :             {
    2485         228 :                 OUString aTmp = m_aNodeValue;
    2486         228 :                 if (rParam.bInternational && rParam.bPredicate && rParam.cDecSep != '.')
    2487           0 :                     aTmp = aTmp.replace('.', rParam.cDecSep);
    2488             : 
    2489         228 :                 if (!rString.isEmpty())
    2490         108 :                     rString.appendAscii(" ");
    2491         228 :                 rString.append(aTmp);
    2492             : 
    2493         228 :             }   break;
    2494             :         case SQL_NODE_PUNCTUATION:
    2495        1547 :             if ( getParent() && SQL_ISRULE(getParent(),cast_spec) && m_aNodeValue.toChar() == '(' ) // no spaces in front of '('
    2496             :             {
    2497           0 :                 rString.append(m_aNodeValue);
    2498           0 :                 break;
    2499             :             }
    2500             :             // fall through
    2501             :         default:
    2502        2385 :             if (!rString.isEmpty() && m_aNodeValue.toChar() != '.' && m_aNodeValue.toChar() != ':' )
    2503             :             {
    2504        2114 :                 switch( rString[rString.getLength() - 1] )
    2505             :                 {
    2506             :                     case ' ' :
    2507          85 :                     case '.' : break;
    2508             :                     default  :
    2509        4058 :                         if  (   rParam.aMetaData.getCatalogSeparator().isEmpty()
    2510        2029 :                             ||  rString[rString.getLength() - 1] != rParam.aMetaData.getCatalogSeparator().toChar()
    2511             :                             )
    2512        2029 :                             rString.appendAscii(" "); break;
    2513             :                 }
    2514             :             }
    2515        2385 :             rString.append(m_aNodeValue);
    2516             :     }
    2517        7218 : }
    2518             : 
    2519             : 
    2520           0 : sal_Int32 OSQLParser::getFunctionReturnType(const OUString& _sFunctionName, const IParseContext* pContext)
    2521             : {
    2522           0 :     sal_Int32 nType = DataType::VARCHAR;
    2523           0 :     OString sFunctionName(OUStringToOString(_sFunctionName,RTL_TEXTENCODING_UTF8));
    2524             : 
    2525           0 :     if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASCII,pContext)))                     nType = DataType::INTEGER;
    2526           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_BIT_LENGTH,pContext)))           nType = DataType::INTEGER;
    2527           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR,pContext)))                 nType = DataType::VARCHAR;
    2528           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR_LENGTH,pContext)))          nType = DataType::INTEGER;
    2529           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CONCAT,pContext)))               nType = DataType::VARCHAR;
    2530           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DIFFERENCE,pContext)))           nType = DataType::VARCHAR;
    2531           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_INSERT,pContext)))               nType = DataType::VARCHAR;
    2532           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LCASE,pContext)))                nType = DataType::VARCHAR;
    2533           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LEFT,pContext)))                 nType = DataType::VARCHAR;
    2534           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LENGTH,pContext)))               nType = DataType::INTEGER;
    2535           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE,pContext)))               nType = DataType::VARCHAR;
    2536           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE_2,pContext)))             nType = DataType::VARCHAR;
    2537           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LTRIM,pContext)))                nType = DataType::VARCHAR;
    2538           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_OCTET_LENGTH,pContext)))         nType = DataType::INTEGER;
    2539           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POSITION,pContext)))             nType = DataType::INTEGER;
    2540           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPEAT,pContext)))               nType = DataType::VARCHAR;
    2541           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPLACE,pContext)))              nType = DataType::VARCHAR;
    2542           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RIGHT,pContext)))                nType = DataType::VARCHAR;
    2543           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RTRIM,pContext)))                nType = DataType::VARCHAR;
    2544           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SOUNDEX,pContext)))              nType = DataType::VARCHAR;
    2545           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SPACE,pContext)))                nType = DataType::VARCHAR;
    2546           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUBSTRING,pContext)))            nType = DataType::VARCHAR;
    2547           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UCASE,pContext)))                nType = DataType::VARCHAR;
    2548           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_DATE,pContext)))         nType = DataType::DATE;
    2549           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIME,pContext)))         nType = DataType::TIME;
    2550           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIMESTAMP,pContext)))    nType = DataType::TIMESTAMP;
    2551           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURDATE,pContext)))              nType = DataType::DATE;
    2552           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEDIFF,pContext)))             nType = DataType::INTEGER;
    2553           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEVALUE,pContext)))            nType = DataType::DATE;
    2554           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURTIME,pContext)))              nType = DataType::TIME;
    2555           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYNAME,pContext)))              nType = DataType::VARCHAR;
    2556           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFMONTH,pContext)))           nType = DataType::INTEGER;
    2557           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFWEEK,pContext)))            nType = DataType::INTEGER;
    2558           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFYEAR,pContext)))            nType = DataType::INTEGER;
    2559           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXTRACT,pContext)))              nType = DataType::VARCHAR;
    2560           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_HOUR,pContext)))                 nType = DataType::INTEGER;
    2561           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MINUTE,pContext)))               nType = DataType::INTEGER;
    2562           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTH,pContext)))                nType = DataType::INTEGER;
    2563           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTHNAME,pContext)))            nType = DataType::VARCHAR;
    2564           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_NOW,pContext)))                  nType = DataType::TIMESTAMP;
    2565           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_QUARTER,pContext)))              nType = DataType::INTEGER;
    2566           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SECOND,pContext)))               nType = DataType::INTEGER;
    2567           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPADD,pContext)))         nType = DataType::TIMESTAMP;
    2568           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPDIFF,pContext)))        nType = DataType::TIMESTAMP;
    2569           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMEVALUE,pContext)))            nType = DataType::TIMESTAMP;
    2570           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_WEEK,pContext)))                 nType = DataType::INTEGER;
    2571           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_YEAR,pContext)))                 nType = DataType::INTEGER;
    2572           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ABS,pContext)))                  nType = DataType::DOUBLE;
    2573           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ACOS,pContext)))                 nType = DataType::DOUBLE;
    2574           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASIN,pContext)))                 nType = DataType::DOUBLE;
    2575           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN,pContext)))                 nType = DataType::DOUBLE;
    2576           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN2,pContext)))                nType = DataType::DOUBLE;
    2577           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CEILING,pContext)))              nType = DataType::DOUBLE;
    2578           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COS,pContext)))                  nType = DataType::DOUBLE;
    2579           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COT,pContext)))                  nType = DataType::DOUBLE;
    2580           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DEGREES,pContext)))              nType = DataType::DOUBLE;
    2581           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXP,pContext)))                  nType = DataType::DOUBLE;
    2582           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_FLOOR,pContext)))                nType = DataType::DOUBLE;
    2583           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOGF,pContext)))                 nType = DataType::DOUBLE;
    2584           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG,pContext)))                  nType = DataType::DOUBLE;
    2585           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG10,pContext)))                nType = DataType::DOUBLE;
    2586           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LN,pContext)))                   nType = DataType::DOUBLE;
    2587           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MOD,pContext)))                  nType = DataType::DOUBLE;
    2588           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_PI,pContext)))                   nType = DataType::DOUBLE;
    2589           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POWER,pContext)))                nType = DataType::DOUBLE;
    2590           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RADIANS,pContext)))              nType = DataType::DOUBLE;
    2591           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RAND,pContext)))                 nType = DataType::DOUBLE;
    2592           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUND,pContext)))                nType = DataType::DOUBLE;
    2593           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUNDMAGIC,pContext)))           nType = DataType::DOUBLE;
    2594           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIGN,pContext)))                 nType = DataType::DOUBLE;
    2595           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIN,pContext)))                  nType = DataType::DOUBLE;
    2596           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SQRT,pContext)))                 nType = DataType::DOUBLE;
    2597           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TAN,pContext)))                  nType = DataType::DOUBLE;
    2598           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TRUNCATE,pContext)))             nType = DataType::DOUBLE;
    2599           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COUNT,pContext)))                nType = DataType::INTEGER;
    2600           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MAX,pContext)))                  nType = DataType::DOUBLE;
    2601           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MIN,pContext)))                  nType = DataType::DOUBLE;
    2602           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_AVG,pContext)))                  nType = DataType::DOUBLE;
    2603           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUM,pContext)))                  nType = DataType::DOUBLE;
    2604           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOWER,pContext)))                nType = DataType::VARCHAR;
    2605           0 :     else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UPPER,pContext)))                nType = DataType::VARCHAR;
    2606             : 
    2607           0 :     return nType;
    2608             : }
    2609             : 
    2610           0 : sal_Int32 OSQLParser::getFunctionParameterType(sal_uInt32 _nTokenId, sal_uInt32 _nPos)
    2611             : {
    2612           0 :     sal_Int32 nType = DataType::VARCHAR;
    2613             : 
    2614           0 :     if(_nTokenId == SQL_TOKEN_CHAR)                 nType = DataType::INTEGER;
    2615           0 :     else if(_nTokenId == SQL_TOKEN_INSERT)
    2616             :     {
    2617           0 :         if ( _nPos == 2 || _nPos == 3 )
    2618           0 :             nType = DataType::INTEGER;
    2619             :     }
    2620           0 :     else if(_nTokenId == SQL_TOKEN_LEFT)
    2621             :     {
    2622           0 :         if ( _nPos == 2 )
    2623           0 :             nType = DataType::INTEGER;
    2624             :     }
    2625           0 :     else if(_nTokenId == SQL_TOKEN_LOCATE)
    2626             :     {
    2627           0 :         if ( _nPos == 3 )
    2628           0 :             nType = DataType::INTEGER;
    2629             :     }
    2630           0 :     else if(_nTokenId == SQL_TOKEN_LOCATE_2)
    2631             :     {
    2632           0 :         if ( _nPos == 3 )
    2633           0 :             nType = DataType::INTEGER;
    2634             :     }
    2635           0 :     else if( _nTokenId == SQL_TOKEN_REPEAT || _nTokenId == SQL_TOKEN_RIGHT )
    2636             :     {
    2637           0 :         if ( _nPos == 2 )
    2638           0 :             nType = DataType::INTEGER;
    2639             :     }
    2640           0 :     else if(_nTokenId == SQL_TOKEN_SPACE )
    2641             :     {
    2642           0 :         nType = DataType::INTEGER;
    2643             :     }
    2644           0 :     else if(_nTokenId == SQL_TOKEN_SUBSTRING)
    2645             :     {
    2646           0 :         if ( _nPos != 1 )
    2647           0 :             nType = DataType::INTEGER;
    2648             :     }
    2649           0 :     else if(_nTokenId == SQL_TOKEN_DATEDIFF)
    2650             :     {
    2651           0 :         if ( _nPos != 1 )
    2652           0 :             nType = DataType::TIMESTAMP;
    2653             :     }
    2654           0 :     else if(_nTokenId == SQL_TOKEN_DATEVALUE)
    2655           0 :         nType = DataType::DATE;
    2656           0 :     else if(_nTokenId == SQL_TOKEN_DAYNAME)
    2657           0 :         nType = DataType::DATE;
    2658           0 :     else if(_nTokenId == SQL_TOKEN_DAYOFMONTH)
    2659           0 :         nType = DataType::DATE;
    2660           0 :     else if(_nTokenId == SQL_TOKEN_DAYOFWEEK)
    2661           0 :         nType = DataType::DATE;
    2662           0 :     else if(_nTokenId == SQL_TOKEN_DAYOFYEAR)
    2663           0 :         nType = DataType::DATE;
    2664           0 :     else if(_nTokenId == SQL_TOKEN_EXTRACT)              nType = DataType::VARCHAR;
    2665           0 :     else if(_nTokenId == SQL_TOKEN_HOUR)                 nType = DataType::TIME;
    2666           0 :     else if(_nTokenId == SQL_TOKEN_MINUTE)               nType = DataType::TIME;
    2667           0 :     else if(_nTokenId == SQL_TOKEN_MONTH)                nType = DataType::DATE;
    2668           0 :     else if(_nTokenId == SQL_TOKEN_MONTHNAME)            nType = DataType::DATE;
    2669           0 :     else if(_nTokenId == SQL_TOKEN_NOW)                  nType = DataType::TIMESTAMP;
    2670           0 :     else if(_nTokenId == SQL_TOKEN_QUARTER)              nType = DataType::DATE;
    2671           0 :     else if(_nTokenId == SQL_TOKEN_SECOND)               nType = DataType::TIME;
    2672           0 :     else if(_nTokenId == SQL_TOKEN_TIMESTAMPADD)         nType = DataType::TIMESTAMP;
    2673           0 :     else if(_nTokenId == SQL_TOKEN_TIMESTAMPDIFF)        nType = DataType::TIMESTAMP;
    2674           0 :     else if(_nTokenId == SQL_TOKEN_TIMEVALUE)            nType = DataType::TIMESTAMP;
    2675           0 :     else if(_nTokenId == SQL_TOKEN_WEEK)                 nType = DataType::DATE;
    2676           0 :     else if(_nTokenId == SQL_TOKEN_YEAR)                 nType = DataType::DATE;
    2677             : 
    2678           0 :     else if(_nTokenId == SQL_TOKEN_ABS)                  nType = DataType::DOUBLE;
    2679           0 :     else if(_nTokenId == SQL_TOKEN_ACOS)                 nType = DataType::DOUBLE;
    2680           0 :     else if(_nTokenId == SQL_TOKEN_ASIN)                 nType = DataType::DOUBLE;
    2681           0 :     else if(_nTokenId == SQL_TOKEN_ATAN)                 nType = DataType::DOUBLE;
    2682           0 :     else if(_nTokenId == SQL_TOKEN_ATAN2)                nType = DataType::DOUBLE;
    2683           0 :     else if(_nTokenId == SQL_TOKEN_CEILING)              nType = DataType::DOUBLE;
    2684           0 :     else if(_nTokenId == SQL_TOKEN_COS)                  nType = DataType::DOUBLE;
    2685           0 :     else if(_nTokenId == SQL_TOKEN_COT)                  nType = DataType::DOUBLE;
    2686           0 :     else if(_nTokenId == SQL_TOKEN_DEGREES)              nType = DataType::DOUBLE;
    2687           0 :     else if(_nTokenId == SQL_TOKEN_EXP)                  nType = DataType::DOUBLE;
    2688           0 :     else if(_nTokenId == SQL_TOKEN_FLOOR)                nType = DataType::DOUBLE;
    2689           0 :     else if(_nTokenId == SQL_TOKEN_LOGF)                 nType = DataType::DOUBLE;
    2690           0 :     else if(_nTokenId == SQL_TOKEN_LOG)                  nType = DataType::DOUBLE;
    2691           0 :     else if(_nTokenId == SQL_TOKEN_LOG10)                nType = DataType::DOUBLE;
    2692           0 :     else if(_nTokenId == SQL_TOKEN_LN)                   nType = DataType::DOUBLE;
    2693           0 :     else if(_nTokenId == SQL_TOKEN_MOD)                  nType = DataType::DOUBLE;
    2694           0 :     else if(_nTokenId == SQL_TOKEN_PI)                   nType = DataType::DOUBLE;
    2695           0 :     else if(_nTokenId == SQL_TOKEN_POWER)                nType = DataType::DOUBLE;
    2696           0 :     else if(_nTokenId == SQL_TOKEN_RADIANS)              nType = DataType::DOUBLE;
    2697           0 :     else if(_nTokenId == SQL_TOKEN_RAND)                 nType = DataType::DOUBLE;
    2698           0 :     else if(_nTokenId == SQL_TOKEN_ROUND)                nType = DataType::DOUBLE;
    2699           0 :     else if(_nTokenId == SQL_TOKEN_ROUNDMAGIC)           nType = DataType::DOUBLE;
    2700           0 :     else if(_nTokenId == SQL_TOKEN_SIGN)                 nType = DataType::DOUBLE;
    2701           0 :     else if(_nTokenId == SQL_TOKEN_SIN)                  nType = DataType::DOUBLE;
    2702           0 :     else if(_nTokenId == SQL_TOKEN_SQRT)                 nType = DataType::DOUBLE;
    2703           0 :     else if(_nTokenId == SQL_TOKEN_TAN)                  nType = DataType::DOUBLE;
    2704           0 :     else if(_nTokenId == SQL_TOKEN_TRUNCATE)             nType = DataType::DOUBLE;
    2705           0 :     else if(_nTokenId == SQL_TOKEN_COUNT)                nType = DataType::INTEGER;
    2706           0 :     else if(_nTokenId == SQL_TOKEN_MAX)                  nType = DataType::DOUBLE;
    2707           0 :     else if(_nTokenId == SQL_TOKEN_MIN)                  nType = DataType::DOUBLE;
    2708           0 :     else if(_nTokenId == SQL_TOKEN_AVG)                  nType = DataType::DOUBLE;
    2709           0 :     else if(_nTokenId == SQL_TOKEN_SUM)                  nType = DataType::DOUBLE;
    2710             : 
    2711           0 :     else if(_nTokenId == SQL_TOKEN_LOWER)                nType = DataType::VARCHAR;
    2712           0 :     else if(_nTokenId == SQL_TOKEN_UPPER)                nType = DataType::VARCHAR;
    2713             : 
    2714           0 :     return nType;
    2715             : }
    2716             : 
    2717             : 
    2718           0 : const SQLError& OSQLParser::getErrorHelper() const
    2719             : {
    2720           0 :     return m_pData->aErrors;
    2721             : }
    2722             : 
    2723             : 
    2724       21279 : OSQLParseNode::Rule OSQLParseNode::getKnownRuleID() const
    2725             : {
    2726       21279 :     if ( !isRule() )
    2727           0 :         return UNKNOWN_RULE;
    2728       21279 :     return OSQLParser::RuleIDToRule( getRuleID() );
    2729             : }
    2730             : 
    2731         306 : OUString OSQLParseNode::getTableRange(const OSQLParseNode* _pTableRef)
    2732             : {
    2733             :     OSL_ENSURE(_pTableRef && _pTableRef->count() > 1 && _pTableRef->getKnownRuleID() == OSQLParseNode::table_ref,"Invalid node give, only table ref is allowed!");
    2734         306 :     const sal_uInt32 nCount = _pTableRef->count();
    2735         306 :     OUString sTableRange;
    2736         306 :     if ( nCount == 2 || (nCount == 3 && !_pTableRef->getChild(0)->isToken()) )
    2737             :     {
    2738         306 :         const OSQLParseNode* pNode = _pTableRef->getChild(nCount - (nCount == 2 ? 1 : 2));
    2739             :         OSL_ENSURE(pNode && (pNode->getKnownRuleID() == OSQLParseNode::table_primary_as_range_column
    2740             :                           || pNode->getKnownRuleID() == OSQLParseNode::range_variable)
    2741             :                          ,"SQL grammar changed!");
    2742         306 :         if ( !pNode->isLeaf() )
    2743          50 :             sTableRange = pNode->getChild(1)->getTokenValue();
    2744             :     } // if ( nCount == 2 || nCount == 3 )
    2745             : 
    2746         306 :     return sTableRange;
    2747             : }
    2748             : 
    2749         132 : OSQLParseNodesContainer::OSQLParseNodesContainer()
    2750             : {
    2751         132 : }
    2752             : 
    2753         132 : OSQLParseNodesContainer::~OSQLParseNodesContainer()
    2754             : {
    2755         132 : }
    2756             : 
    2757       17571 : void OSQLParseNodesContainer::push_back(OSQLParseNode* _pNode)
    2758             : {
    2759       17571 :     ::osl::MutexGuard aGuard(m_aMutex);
    2760       17571 :     m_aNodes.push_back(_pNode);
    2761       17571 : }
    2762             : 
    2763       15705 : void OSQLParseNodesContainer::erase(OSQLParseNode* _pNode)
    2764             : {
    2765       15705 :     ::osl::MutexGuard aGuard(m_aMutex);
    2766       15705 :     if ( !m_aNodes.empty() )
    2767             :     {
    2768         177 :         ::std::vector< OSQLParseNode* >::iterator aFind = ::std::find(m_aNodes.begin(), m_aNodes.end(),_pNode);
    2769         177 :         if ( aFind != m_aNodes.end() )
    2770         177 :             m_aNodes.erase(aFind);
    2771       15705 :     }
    2772       15705 : }
    2773             : 
    2774         508 : void OSQLParseNodesContainer::clear()
    2775             : {
    2776         508 :     ::osl::MutexGuard aGuard(m_aMutex);
    2777         508 :     m_aNodes.clear();
    2778         508 : }
    2779             : 
    2780          67 : void OSQLParseNodesContainer::clearAndDelete()
    2781             : {
    2782          67 :     ::osl::MutexGuard aGuard(m_aMutex);
    2783             :     // clear the garbage collector
    2784         268 :     while ( !m_aNodes.empty() )
    2785             :     {
    2786         134 :         OSQLParseNode* pNode = m_aNodes[0];
    2787         270 :         while ( pNode->getParent() )
    2788             :         {
    2789           2 :             pNode = pNode->getParent();
    2790             :         }
    2791         134 :         delete pNode;
    2792          67 :     }
    2793          67 : }
    2794         441 : }   // namespace connectivity
    2795             : 
    2796             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11