LCOV - code coverage report
Current view: top level - dbaccess/source/core/api - SingleSelectQueryComposer.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 925 0.0 %
Date: 2014-04-14 Functions: 0 69 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <string.h>
      21             : #include "composertools.hxx"
      22             : #include "core_resource.hrc"
      23             : #include "core_resource.hxx"
      24             : #include "dbastrings.hrc"
      25             : #include "HelperCollections.hxx"
      26             : #include "SingleSelectQueryComposer.hxx"
      27             : #include "sdbcoretools.hxx"
      28             : 
      29             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      30             : #include <com/sun/star/container/XChild.hpp>
      31             : #include <com/sun/star/i18n/LocaleData.hpp>
      32             : #include <com/sun/star/lang/DisposedException.hpp>
      33             : #include <com/sun/star/script/Converter.hpp>
      34             : #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
      35             : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
      36             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      37             : #include <com/sun/star/sdb/CommandType.hpp>
      38             : #include <com/sun/star/sdbc/ColumnSearch.hpp>
      39             : #include <com/sun/star/sdbc/DataType.hpp>
      40             : #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
      41             : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
      42             : #include <com/sun/star/sdbc/XParameters.hpp>
      43             : #include <com/sun/star/uno/XAggregation.hpp>
      44             : #include <com/sun/star/util/NumberFormatter.hpp>
      45             : 
      46             : #include <comphelper/processfactory.hxx>
      47             : #include <comphelper/sequence.hxx>
      48             : #include <comphelper/types.hxx>
      49             : #include <cppuhelper/typeprovider.hxx>
      50             : #include <connectivity/predicateinput.hxx>
      51             : #include <unotools/syslocale.hxx>
      52             : #include <tools/debug.hxx>
      53             : #include <tools/diagnose_ex.h>
      54             : #include <osl/diagnose.h>
      55             : #include <unotools/configmgr.hxx>
      56             : #include <unotools/sharedunocomponent.hxx>
      57             : 
      58             : #include <boost/scoped_ptr.hpp>
      59             : 
      60             : using namespace ::dbaccess;
      61             : using namespace ::dbtools;
      62             : using namespace ::comphelper;
      63             : using namespace ::connectivity;
      64             : using namespace ::com::sun::star::uno;
      65             : using namespace ::com::sun::star::beans;
      66             : using namespace ::com::sun::star::sdbc;
      67             : using namespace ::com::sun::star::sdb;
      68             : using namespace ::com::sun::star::sdbcx;
      69             : using namespace ::com::sun::star::container;
      70             : using namespace ::com::sun::star::i18n;
      71             : using namespace ::com::sun::star::lang;
      72             : using namespace ::com::sun::star::script;
      73             : using namespace ::com::sun::star::util;
      74             : using namespace ::cppu;
      75             : using namespace ::osl;
      76             : using namespace ::utl;
      77             : 
      78             : namespace dbaccess {
      79             : namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
      80             : }
      81             : 
      82             : #define STR_SELECT      "SELECT "
      83             : #define STR_FROM        " FROM "
      84             : #define STR_WHERE       " WHERE "
      85             : #define STR_GROUP_BY    " GROUP BY "
      86             : #define STR_HAVING      " HAVING "
      87             : #define STR_ORDER_BY    " ORDER BY "
      88             : #define STR_AND         " AND "
      89             : #define STR_OR          " OR "
      90             : #define STR_LIKE        OUString(" LIKE ")
      91             : #define L_BRACKET       "("
      92             : #define R_BRACKET       ")"
      93             : #define COMMA           ","
      94             : 
      95             : namespace
      96             : {
      97             :     /** parses the given statement, using the given parser, returns a parse node representing
      98             :         the statement
      99             : 
     100             :         If the statement cannot be parsed, an error is thrown.
     101             :     */
     102           0 :     const OSQLParseNode* parseStatement_throwError( OSQLParser& _rParser, const OUString& _rStatement, const Reference< XInterface >& _rxContext )
     103             :     {
     104             :         SAL_INFO("dbaccess", "SingleSelectQueryComposer.cxx::parseStatement_throwError" );
     105           0 :         OUString aErrorMsg;
     106           0 :         const OSQLParseNode* pNewSqlParseNode = _rParser.parseTree( aErrorMsg, _rStatement );
     107           0 :         if ( !pNewSqlParseNode )
     108             :         {
     109           0 :             OUString sSQLStateGeneralError( getStandardSQLState( SQL_GENERAL_ERROR ) );
     110           0 :             SQLException aError2( aErrorMsg, _rxContext, sSQLStateGeneralError, 1000, Any() );
     111           0 :             SQLException aError1( _rStatement, _rxContext, sSQLStateGeneralError, 1000, makeAny( aError2 ) );
     112           0 :             throw SQLException(_rParser.getContext().getErrorMessage(OParseContext::ERROR_GENERAL),_rxContext,sSQLStateGeneralError,1000,makeAny(aError1));
     113             :         }
     114           0 :         return pNewSqlParseNode;
     115             :     }
     116             : 
     117             :     /** checks whether the given parse node describes a valid single select statement, throws
     118             :         an error if not
     119             :     */
     120           0 :     void checkForSingleSelect_throwError( const OSQLParseNode* pStatementNode, OSQLParseTreeIterator& _rIterator,
     121             :         const Reference< XInterface >& _rxContext, const OUString& _rOriginatingCommand )
     122             :     {
     123           0 :         const OSQLParseNode* pOldNode = _rIterator.getParseTree();
     124             : 
     125             :         // determine the statement type
     126           0 :         _rIterator.setParseTree( pStatementNode );
     127           0 :         _rIterator.traverseAll();
     128           0 :         bool bIsSingleSelect = ( _rIterator.getStatementType() == SQL_STATEMENT_SELECT );
     129             : 
     130             :         // throw the error, if necessary
     131           0 :         if ( !bIsSingleSelect || SQL_ISRULE( pStatementNode, union_statement ) ) // #i4229# OJ
     132             :         {
     133             :             // restore the old node before throwing the exception
     134           0 :             _rIterator.setParseTree( pOldNode );
     135             :             // and now really ...
     136           0 :             SQLException aError1( _rOriginatingCommand, _rxContext, getStandardSQLState( SQL_GENERAL_ERROR ), 1000, Any() );
     137             :             throw SQLException( DBACORE_RESSTRING( RID_STR_ONLY_QUERY ), _rxContext,
     138           0 :                 getStandardSQLState( SQL_GENERAL_ERROR ), 1000, makeAny( aError1 ) );
     139             :         }
     140             : 
     141           0 :         delete pOldNode;
     142           0 :     }
     143             : 
     144             :     /** combines parseStatement_throwError and checkForSingleSelect_throwError
     145             :     */
     146           0 :     void parseAndCheck_throwError( OSQLParser& _rParser, const OUString& _rStatement,
     147             :         OSQLParseTreeIterator& _rIterator, const Reference< XInterface >& _rxContext )
     148             :     {
     149             :         SAL_INFO("dbaccess", "SingleSelectQueryComposer.cxx::parseAndCheck_throwError" );
     150           0 :         const OSQLParseNode* pNode = parseStatement_throwError( _rParser, _rStatement, _rxContext );
     151           0 :         checkForSingleSelect_throwError( pNode, _rIterator, _rxContext, _rStatement );
     152           0 :     }
     153             : 
     154             :     /** transforms a parse node describing a complete statement into a pure select
     155             :         statement, without any filter/order/groupby/having clauses
     156             :     */
     157           0 :     OUString getPureSelectStatement( const OSQLParseNode* _pRootNode, Reference< XConnection > _rxConnection )
     158             :     {
     159           0 :         OUString sSQL = STR_SELECT;
     160           0 :         _pRootNode->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
     161           0 :         _pRootNode->getChild(2)->parseNodeToStr( sSQL, _rxConnection );
     162           0 :         sSQL += STR_FROM;
     163           0 :         _pRootNode->getChild(3)->getChild(0)->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
     164           0 :         return sSQL;
     165             :     }
     166             : 
     167             :     /** resets an SQL iterator, including deletion of the parse tree, and disposal if desired
     168             :     */
     169           0 :     void resetIterator( OSQLParseTreeIterator& _rIterator, bool _bDispose )
     170             :     {
     171           0 :         const OSQLParseNode* pSqlParseNode = _rIterator.getParseTree();
     172           0 :         _rIterator.setParseTree(NULL);
     173           0 :         delete pSqlParseNode;
     174           0 :         if ( _bDispose )
     175           0 :             _rIterator.dispose();
     176           0 :     }
     177           0 :     void lcl_addFilterCriteria_throw(sal_Int32 i_nFilterOperator,const OUString& i_sValue,OUStringBuffer& o_sRet)
     178             :     {
     179           0 :         switch( i_nFilterOperator )
     180             :         {
     181             :             case SQLFilterOperator::EQUAL:
     182           0 :                 o_sRet.append(" = " + i_sValue);
     183           0 :                 break;
     184             :             case SQLFilterOperator::NOT_EQUAL:
     185           0 :                 o_sRet.append(" <> " + i_sValue);
     186           0 :                 break;
     187             :             case SQLFilterOperator::LESS:
     188           0 :                 o_sRet.append(" < " + i_sValue);
     189           0 :                 break;
     190             :             case SQLFilterOperator::GREATER:
     191           0 :                 o_sRet.append(" > " + i_sValue);
     192           0 :                 break;
     193             :             case SQLFilterOperator::LESS_EQUAL:
     194           0 :                 o_sRet.append(" <= " + i_sValue);
     195           0 :                 break;
     196             :             case SQLFilterOperator::GREATER_EQUAL:
     197           0 :                 o_sRet.append(" >= " + i_sValue);
     198           0 :                 break;
     199             :             case SQLFilterOperator::LIKE:
     200           0 :                 o_sRet.append(" LIKE " + i_sValue);
     201           0 :                 break;
     202             :             case SQLFilterOperator::NOT_LIKE:
     203           0 :                 o_sRet.append(" NOT LIKE " + i_sValue);
     204           0 :                 break;
     205             :             case SQLFilterOperator::SQLNULL:
     206           0 :                 o_sRet.append(" IS NULL");
     207           0 :                 break;
     208             :             case SQLFilterOperator::NOT_SQLNULL:
     209           0 :                 o_sRet.append(" IS NOT NULL");
     210           0 :                 break;
     211             :             default:
     212           0 :                 throw SQLException();
     213             :         }
     214           0 :     }
     215             : 
     216             : }
     217             : 
     218             : 
     219           0 : OSingleSelectQueryComposer::OSingleSelectQueryComposer(const Reference< XNameAccess>& _rxTables,
     220             :                                const Reference< XConnection>& _xConnection,
     221             :                                const Reference<XComponentContext>& _rContext )
     222             :     :OSubComponent(m_aMutex,_xConnection)
     223             :     ,OPropertyContainer(m_aBHelper)
     224             :     ,m_aSqlParser( _rContext, &m_aParseContext )
     225             :     ,m_aSqlIterator( _xConnection, _rxTables, m_aSqlParser, NULL )
     226             :     ,m_aAdditiveIterator( _xConnection, _rxTables, m_aSqlParser, NULL )
     227             :     ,m_aElementaryParts( (size_t)SQLPartCount )
     228             :     ,m_xConnection(_xConnection)
     229           0 :     ,m_xMetaData(_xConnection->getMetaData())
     230             :     ,m_xConnectionTables( _rxTables )
     231             :     ,m_aContext( _rContext )
     232             :     ,m_pTables(NULL)
     233             :     ,m_nBoolCompareMode( BooleanComparisonMode::EQUAL_INTEGER )
     234           0 :     ,m_nCommandType(CommandType::COMMAND)
     235             : {
     236             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::OSingleSelectQueryComposer" );
     237             : 
     238           0 :     if ( !m_aContext.is() || !m_xConnection.is() || !m_xConnectionTables.is() )
     239           0 :         throw IllegalArgumentException();
     240             : 
     241           0 :     registerProperty(PROPERTY_ORIGINAL,PROPERTY_ID_ORIGINAL,PropertyAttribute::BOUND|PropertyAttribute::READONLY,&m_sOrignal,::getCppuType(&m_sOrignal));
     242             : 
     243           0 :     m_aCurrentColumns.resize(4);
     244             : 
     245           0 :     m_aLocale = m_aParseContext.getPreferredLocale();
     246           0 :     m_xNumberFormatsSupplier = dbtools::getNumberFormats( m_xConnection, true, m_aContext );
     247           0 :     Reference< XLocaleData4 > xLocaleData( LocaleData::create(m_aContext) );
     248           0 :     LocaleDataItem aData = xLocaleData->getLocaleItem(m_aLocale);
     249           0 :     m_sDecimalSep = aData.decimalSeparator;
     250             :     OSL_ENSURE(m_sDecimalSep.getLength() == 1,"OSingleSelectQueryComposer::OSingleSelectQueryComposer decimal separator is not 1 length");
     251             :     try
     252             :     {
     253           0 :         Any aValue;
     254           0 :         Reference<XInterface> xDs = dbaccess::getDataSource(_xConnection);
     255           0 :         if ( dbtools::getDataSourceSetting(xDs,static_cast <OUString> (PROPERTY_BOOLEANCOMPARISONMODE),aValue) )
     256             :         {
     257           0 :             OSL_VERIFY( aValue >>= m_nBoolCompareMode );
     258             :         }
     259           0 :         Reference< XQueriesSupplier >  xQueriesAccess(m_xConnection, UNO_QUERY);
     260           0 :         if (xQueriesAccess.is())
     261           0 :             m_xConnectionQueries = xQueriesAccess->getQueries();
     262             :     }
     263           0 :     catch(Exception&)
     264             :     {
     265           0 :     }
     266           0 : }
     267             : 
     268           0 : OSingleSelectQueryComposer::~OSingleSelectQueryComposer()
     269             : {
     270           0 :     ::std::vector<OPrivateColumns*>::iterator aColIter = m_aColumnsCollection.begin();
     271           0 :     ::std::vector<OPrivateColumns*>::iterator aEnd = m_aColumnsCollection.end();
     272           0 :     for(;aColIter != aEnd;++aColIter)
     273           0 :         delete *aColIter;
     274             : 
     275           0 :     ::std::vector<OPrivateTables*>::iterator aTabIter = m_aTablesCollection.begin();
     276           0 :     ::std::vector<OPrivateTables*>::iterator aTabEnd = m_aTablesCollection.end();
     277           0 :     for(;aTabIter != aTabEnd;++aTabIter)
     278           0 :         delete *aTabIter;
     279           0 : }
     280             : 
     281             : // OComponentHelper
     282           0 : void SAL_CALL OSingleSelectQueryComposer::disposing(void)
     283             : {
     284             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::disposing" );
     285           0 :     OSubComponent::disposing();
     286             : 
     287           0 :     MutexGuard aGuard(m_aMutex);
     288             : 
     289           0 :     resetIterator( m_aSqlIterator, true );
     290           0 :     resetIterator( m_aAdditiveIterator, true );
     291             : 
     292           0 :     m_xConnectionTables = NULL;
     293           0 :     m_xConnection       = NULL;
     294             : 
     295           0 :     clearCurrentCollections();
     296           0 : }
     297             : 
     298           0 : IMPLEMENT_FORWARD_XINTERFACE3(OSingleSelectQueryComposer,OSubComponent,OSingleSelectQueryComposer_BASE,OPropertyContainer)
     299           0 : IMPLEMENT_SERVICE_INFO1(OSingleSelectQueryComposer,"org.openoffice.comp.dba.OSingleSelectQueryComposer",SERVICE_NAME_SINGLESELECTQUERYCOMPOSER)
     300             : 
     301           0 : css::uno::Sequence<sal_Int8> OSingleSelectQueryComposer::getImplementationId()
     302             :     throw (css::uno::RuntimeException, std::exception)
     303             : {
     304           0 :     return css::uno::Sequence<sal_Int8>();
     305             : }
     306             : 
     307           0 : IMPLEMENT_GETTYPES3(OSingleSelectQueryComposer,OSubComponent,OSingleSelectQueryComposer_BASE,OPropertyContainer)
     308           0 : IMPLEMENT_PROPERTYCONTAINER_DEFAULTS(OSingleSelectQueryComposer)
     309             : 
     310             : // XSingleSelectQueryAnalyzer
     311           0 : OUString SAL_CALL OSingleSelectQueryComposer::getQuery(  ) throw(RuntimeException, std::exception)
     312             : {
     313             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getQuery" );
     314           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     315           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     316             : 
     317           0 :     TGetParseNode F_tmp(&OSQLParseTreeIterator::getParseTree);
     318           0 :     return getStatementPart(F_tmp,m_aSqlIterator);
     319             : }
     320             : 
     321           0 : void SAL_CALL OSingleSelectQueryComposer::setQuery( const OUString& command ) throw(SQLException, RuntimeException, std::exception)
     322             : {
     323             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setQuery" );
     324           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     325             : 
     326           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     327           0 :     m_nCommandType = CommandType::COMMAND;
     328             :     // first clear the tables and columns
     329           0 :     clearCurrentCollections();
     330             :     // now set the new one
     331           0 :     setQuery_Impl(command);
     332           0 :     m_sOrignal = command;
     333             : 
     334             :     // reset the additive iterator to the same statement
     335           0 :     parseAndCheck_throwError( m_aSqlParser, m_sOrignal, m_aAdditiveIterator, *this );
     336             : 
     337             :     // we have no "elementary" parts anymore (means filter/groupby/having/order clauses)
     338           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     339           0 :         m_aElementaryParts[ eLoopParts ] = OUString();
     340           0 : }
     341             : 
     342           0 : void SAL_CALL OSingleSelectQueryComposer::setCommand( const OUString& Command,sal_Int32 _nCommandType ) throw(SQLException, RuntimeException, std::exception)
     343             : {
     344           0 :     OUStringBuffer sSQL;
     345           0 :     switch(_nCommandType)
     346             :     {
     347             :         case CommandType::COMMAND:
     348           0 :             setElementaryQuery(Command);
     349           0 :             return;
     350             :         case CommandType::TABLE:
     351           0 :             if ( m_xConnectionTables->hasByName(Command) )
     352             :             {
     353           0 :                 sSQL.appendAscii("SELECT * FROM ");
     354           0 :                 Reference< XPropertySet > xTable;
     355             :                 try
     356             :                 {
     357           0 :                     m_xConnectionTables->getByName( Command ) >>= xTable;
     358             :                 }
     359           0 :                 catch(const WrappedTargetException& e)
     360             :                 {
     361           0 :                     SQLException e2;
     362           0 :                     if ( e.TargetException >>= e2 )
     363           0 :                         throw e2;
     364             :                 }
     365           0 :                 catch(Exception&)
     366             :                 {
     367             :                     DBG_UNHANDLED_EXCEPTION();
     368             :                 }
     369             : 
     370           0 :                 sSQL.append(dbtools::composeTableNameForSelect(m_xConnection,xTable));
     371             :             }
     372             :             else
     373             :             {
     374           0 :                 OUString sMessage( DBACORE_RESSTRING( RID_STR_TABLE_DOES_NOT_EXIST ) );
     375           0 :                 throwGenericSQLException(sMessage.replaceAll( "$table$", Command ),*this);
     376             :             }
     377           0 :             break;
     378             :         case CommandType::QUERY:
     379           0 :             if ( m_xConnectionQueries->hasByName(Command) )
     380             :             {
     381             : 
     382           0 :                 Reference<XPropertySet> xQuery(m_xConnectionQueries->getByName(Command),UNO_QUERY);
     383           0 :                 OUString sCommand;
     384           0 :                 xQuery->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
     385           0 :                 sSQL.append(sCommand);
     386             :             }
     387             :             else
     388             :             {
     389           0 :                 OUString sMessage( DBACORE_RESSTRING( RID_STR_QUERY_DOES_NOT_EXIST ) );
     390           0 :                 throwGenericSQLException(sMessage.replaceAll( "$table$", Command ),*this);
     391             :             }
     392             : 
     393           0 :             break;
     394             :         default:
     395           0 :             break;
     396             :     }
     397           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     398             : 
     399           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     400           0 :     m_nCommandType = _nCommandType;
     401           0 :     m_sCommand = Command;
     402             :     // first clear the tables and columns
     403           0 :     clearCurrentCollections();
     404             :     // now set the new one
     405           0 :     OUString sCommand = sSQL.makeStringAndClear();
     406           0 :     setElementaryQuery(sCommand);
     407           0 :     m_sOrignal = sCommand;
     408             : }
     409             : 
     410           0 : void OSingleSelectQueryComposer::setQuery_Impl( const OUString& command )
     411             : {
     412             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setQuery_Impl" );
     413             :     // parse this
     414           0 :     parseAndCheck_throwError( m_aSqlParser, command, m_aSqlIterator, *this );
     415             : 
     416             :     // strip it from all clauses, to have the pure SELECT statement
     417           0 :     m_aPureSelectSQL = getPureSelectStatement( m_aSqlIterator.getParseTree(), m_xConnection );
     418             : 
     419             :     // update tables
     420           0 :     getTables();
     421           0 : }
     422             : 
     423           0 : Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredHavingClause(  ) throw (RuntimeException, std::exception)
     424             : {
     425             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getStructuredHavingClause" );
     426           0 :     TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleHavingTree);
     427           0 :     return getStructuredCondition(F_tmp);
     428             : }
     429             : 
     430           0 : Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredFilter(  ) throw(RuntimeException, std::exception)
     431             : {
     432             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getStructuredFilter" );
     433           0 :     TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleWhereTree);
     434           0 :     return getStructuredCondition(F_tmp);
     435             : }
     436             : 
     437           0 : void SAL_CALL OSingleSelectQueryComposer::appendHavingClauseByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator ) throw (SQLException, RuntimeException, std::exception)
     438             : {
     439             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::appendHavingClauseByColumn" );
     440           0 :     ::std::mem_fun1_t<bool,OSingleSelectQueryComposer,const OUString&> F_tmp(&OSingleSelectQueryComposer::implSetHavingClause);
     441           0 :     setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
     442           0 : }
     443             : 
     444           0 : void SAL_CALL OSingleSelectQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator ) throw(SQLException, RuntimeException, std::exception)
     445             : {
     446             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::appendFilterByColumn" );
     447           0 :     ::std::mem_fun1_t<bool,OSingleSelectQueryComposer,const OUString&> F_tmp(&OSingleSelectQueryComposer::implSetFilter);
     448           0 :     setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
     449           0 : }
     450             : 
     451           0 : OUString OSingleSelectQueryComposer::impl_getColumnRealName_throw(const Reference< XPropertySet >& column, bool bGroupBy)
     452             : {
     453           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     454             : 
     455           0 :     getColumns();
     456           0 :     if ( !column.is()
     457           0 :         || !m_aCurrentColumns[SelectColumns]
     458           0 :         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
     459             :         )
     460             :         {
     461           0 :             OUString sError(DBACORE_RESSTRING(RID_STR_COLUMN_UNKNOWN_PROP));
     462           0 :             SQLException aErr(sError.replaceAll("%value", OUString(PROPERTY_NAME)),*this,SQLSTATE_GENERAL,1000,Any() );
     463           0 :             throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,makeAny(aErr) );
     464             :         }
     465             : 
     466           0 :     OUString aName, aNewName;
     467           0 :     column->getPropertyValue(PROPERTY_NAME)         >>= aName;
     468             : 
     469           0 :     if ( bGroupBy &&
     470           0 :          !m_xMetaData->supportsGroupByUnrelated() &&
     471           0 :          m_aCurrentColumns[SelectColumns] &&
     472           0 :          !m_aCurrentColumns[SelectColumns]->hasByName(aName) )
     473             :     {
     474           0 :         OUString sError(DBACORE_RESSTRING(RID_STR_COLUMN_MUST_VISIBLE));
     475           0 :         throw SQLException(sError.replaceAll("%name", aName),*this,SQLSTATE_GENERAL,1000,Any() );
     476             :     }
     477             : 
     478           0 :     OUString aQuote  = m_xMetaData->getIdentifierQuoteString();
     479           0 :     if ( m_aCurrentColumns[SelectColumns]->hasByName(aName) )
     480             :     {
     481           0 :         Reference<XPropertySet> xColumn;
     482           0 :         m_aCurrentColumns[SelectColumns]->getByName(aName) >>= xColumn;
     483             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
     484             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
     485             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName("Function"),"Property FUNCTION not available!");
     486             : 
     487           0 :         OUString sRealName, sTableName;
     488           0 :         xColumn->getPropertyValue(PROPERTY_REALNAME)    >>= sRealName;
     489           0 :         xColumn->getPropertyValue(PROPERTY_TABLENAME)   >>= sTableName;
     490           0 :         sal_Bool bFunction = sal_False;
     491           0 :         xColumn->getPropertyValue("Function") >>= bFunction;
     492           0 :         if ( sRealName == aName )
     493             :         {
     494           0 :             if ( bFunction )
     495           0 :                 aNewName = aName;
     496             :             else
     497             :             {
     498           0 :                 if(sTableName.indexOf('.',0) != -1)
     499             :                 {
     500           0 :                     OUString aCatlog,aSchema,aTable;
     501           0 :                     ::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatlog,aSchema,aTable,::dbtools::eInDataManipulation);
     502           0 :                     sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, true, ::dbtools::eInDataManipulation );
     503             :                 }
     504           0 :                 else if (!sTableName.isEmpty())
     505           0 :                     sTableName = ::dbtools::quoteName(aQuote,sTableName);
     506             : 
     507           0 :                 if(sTableName.isEmpty())
     508           0 :                     aNewName =  ::dbtools::quoteName(aQuote,sRealName);
     509             :                 else
     510           0 :                     aNewName =  sTableName + "." + ::dbtools::quoteName(aQuote,sRealName);
     511             :             }
     512             :         }
     513             :         else
     514           0 :             aNewName = ::dbtools::quoteName(aQuote,aName);
     515             :     }
     516             :     else
     517           0 :         aNewName = getTableAlias(column) + ::dbtools::quoteName(aQuote,aName);
     518           0 :     return aNewName;
     519             : }
     520             : 
     521           0 : OUString OSingleSelectQueryComposer::impl_getColumnName_throw(const Reference< XPropertySet >& column, bool bOrderBy)
     522             : {
     523           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     524             : 
     525           0 :     getColumns();
     526           0 :     if ( !column.is()
     527           0 :         || !m_aCurrentColumns[SelectColumns]
     528           0 :         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
     529             :         )
     530             :         {
     531           0 :             OUString sError(DBACORE_RESSTRING(RID_STR_COLUMN_UNKNOWN_PROP));
     532           0 :             SQLException aErr(sError.replaceAll("%value", OUString(PROPERTY_NAME)),*this,SQLSTATE_GENERAL,1000,Any() );
     533           0 :             throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,makeAny(aErr) );
     534             :         }
     535             : 
     536           0 :     OUString aName;
     537           0 :     column->getPropertyValue(PROPERTY_NAME)         >>= aName;
     538             : 
     539           0 :     const OUString aQuote  = m_xMetaData->getIdentifierQuoteString();
     540             : 
     541           0 :     if ( m_aCurrentColumns[SelectColumns] &&
     542           0 :          m_aCurrentColumns[SelectColumns]->hasByName(aName) )
     543             :     {
     544             :         // It is a column from the SELECT list, use it as such.
     545           0 :         return ::dbtools::quoteName(aQuote,aName);
     546             :     }
     547             : 
     548             :     // Nope, it is an unrelated column.
     549             :     // Is that supported?
     550           0 :     if ( bOrderBy &&
     551           0 :          !m_xMetaData->supportsOrderByUnrelated() )
     552             :     {
     553           0 :         OUString sError(DBACORE_RESSTRING(RID_STR_COLUMN_MUST_VISIBLE));
     554           0 :         throw SQLException(sError.replaceAll("%name", aName),*this,SQLSTATE_GENERAL,1000,Any() );
     555             :     }
     556             : 
     557             :     // We need to refer to it by its "real" name, that is by schemaName.tableName.columnNameInTable
     558           0 :     return impl_getColumnRealName_throw(column, false);
     559             : }
     560             : 
     561           0 : void SAL_CALL OSingleSelectQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending ) throw(SQLException, RuntimeException, std::exception)
     562             : {
     563             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::appendOrderByColumn" );
     564           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     565           0 :     OUString sColumnName( impl_getColumnName_throw(column, true) );
     566           0 :     OUString sOrder = getOrder();
     567           0 :     if ( !(sOrder.isEmpty() || sColumnName.isEmpty()) )
     568           0 :         sOrder += COMMA;
     569           0 :     sOrder += sColumnName;
     570           0 :     if ( !(ascending || sColumnName.isEmpty()) )
     571           0 :         sOrder += " DESC ";
     572             : 
     573           0 :     setOrder(sOrder);
     574           0 : }
     575             : 
     576           0 : void SAL_CALL OSingleSelectQueryComposer::appendGroupByColumn( const Reference< XPropertySet >& column) throw(SQLException, RuntimeException, std::exception)
     577             : {
     578             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::appendGroupByColumn" );
     579           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     580           0 :     OUString sColumnName( impl_getColumnRealName_throw(column, true) );
     581           0 :     OrderCreator aComposer;
     582           0 :     aComposer.append( getGroup() );
     583           0 :     aComposer.append( sColumnName );
     584           0 :     setGroup( aComposer.getComposedAndClear() );
     585           0 : }
     586             : 
     587           0 : OUString OSingleSelectQueryComposer::composeStatementFromParts( const ::std::vector< OUString >& _rParts )
     588             : {
     589             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::composeStatementFromParts" );
     590             :     OSL_ENSURE( _rParts.size() == (size_t)SQLPartCount, "OSingleSelectQueryComposer::composeStatementFromParts: invalid parts array!" );
     591             : 
     592           0 :     OUStringBuffer aSql( m_aPureSelectSQL );
     593           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     594           0 :         if ( !_rParts[ eLoopParts ].isEmpty() )
     595             :         {
     596           0 :             aSql.append( getKeyword( eLoopParts ) );
     597           0 :             aSql.append( _rParts[ eLoopParts ] );
     598             :         }
     599             : 
     600           0 :     return aSql.makeStringAndClear();
     601             : }
     602             : 
     603           0 : OUString SAL_CALL OSingleSelectQueryComposer::getElementaryQuery() throw (::com::sun::star::uno::RuntimeException, std::exception)
     604             : {
     605             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getElementaryQuery" );
     606           0 :     return composeStatementFromParts( m_aElementaryParts );
     607             : }
     608             : 
     609           0 : void SAL_CALL OSingleSelectQueryComposer::setElementaryQuery( const OUString& _rElementary ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception)
     610             : {
     611             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setElementaryQuery" );
     612           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     613           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     614             : 
     615             :     // remember the 4 current "additive" clauses
     616           0 :     ::std::vector< OUString > aAdditiveClauses( SQLPartCount );
     617           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     618           0 :         aAdditiveClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, sal_False );
     619             : 
     620             :     // clear the tables and columns
     621           0 :     clearCurrentCollections();
     622             :     // set and parse the new query
     623           0 :     setQuery_Impl( _rElementary );
     624             : 
     625             :     // get the 4 elementary parts of the statement
     626           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     627           0 :         m_aElementaryParts[ eLoopParts ] = getSQLPart( eLoopParts, m_aSqlIterator, sal_False );
     628             : 
     629             :     // reset the AdditiveIterator: m_aPureSelectSQL may have changed
     630             :     try
     631             :     {
     632           0 :         parseAndCheck_throwError( m_aSqlParser, composeStatementFromParts( aAdditiveClauses ), m_aAdditiveIterator, *this );
     633             :     }
     634           0 :     catch( const Exception& e )
     635             :     {
     636             :         (void)e;
     637             :         SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setElementaryQuery: there should be no error anymore for the additive statement!" );
     638             :         DBG_UNHANDLED_EXCEPTION();
     639             :         // every part of the additive statement should have passed other tests already, and should not
     640             :         // be able to cause any errors ... me thinks
     641           0 :     }
     642           0 : }
     643             : 
     644             : namespace
     645             : {
     646           0 :     OUString getComposedClause( const OUString& _rElementaryClause, const OUString& _rAdditionalClause,
     647             :         TokenComposer& _rComposer, const OUString& _rKeyword )
     648             :     {
     649           0 :         _rComposer.clear();
     650           0 :         _rComposer.append( _rElementaryClause );
     651           0 :         _rComposer.append( _rAdditionalClause );
     652           0 :         OUString sComposed = _rComposer.getComposedAndClear();
     653           0 :         if ( !sComposed.isEmpty() )
     654           0 :             sComposed = _rKeyword + sComposed;
     655           0 :         return sComposed;
     656             :     }
     657             : }
     658             : 
     659           0 : void OSingleSelectQueryComposer::setSingleAdditiveClause( SQLPart _ePart, const OUString& _rClause )
     660             : {
     661             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setSingleAdditiveClause" );
     662           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     663           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     664             : 
     665             :     // if nothing is changed, do nothing
     666           0 :     if ( getSQLPart( _ePart, m_aAdditiveIterator, sal_False ) == _rClause )
     667           0 :         return;
     668             : 
     669             :     // collect the 4 single parts as they're currently set
     670           0 :     ::std::vector< OUString > aClauses;
     671           0 :     aClauses.reserve( (size_t)SQLPartCount );
     672           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     673           0 :         aClauses.push_back( getSQLPart( eLoopParts, m_aSqlIterator, sal_True ) );
     674             : 
     675             :     // overwrite the one part in question here
     676           0 :     boost::scoped_ptr< TokenComposer > pComposer;
     677           0 :     if ( ( _ePart == Where ) || ( _ePart == Having ) )
     678           0 :         pComposer.reset( new FilterCreator );
     679             :     else
     680           0 :         pComposer.reset( new OrderCreator );
     681           0 :     aClauses[ _ePart ] = getComposedClause( m_aElementaryParts[ _ePart ], _rClause,
     682           0 :         *pComposer, getKeyword( _ePart ) );
     683             : 
     684             :     // construct the complete statement
     685           0 :     OUStringBuffer aSql(m_aPureSelectSQL);
     686           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     687           0 :         aSql.append(aClauses[ eLoopParts ]);
     688             : 
     689             :     // set the query
     690           0 :     setQuery_Impl(aSql.makeStringAndClear());
     691             : 
     692             :     // clear column collections which (might) have changed
     693           0 :     clearColumns( ParameterColumns );
     694           0 :     if ( _ePart == Order )
     695           0 :         clearColumns( OrderColumns );
     696           0 :     else if ( _ePart == Group )
     697           0 :         clearColumns( GroupByColumns );
     698             : 
     699             :     // also, since the "additive filter" change, we need to rebuild our "additive" statement
     700           0 :     aSql = m_aPureSelectSQL;
     701             :     // again, first get all the old additive parts
     702           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     703           0 :         aClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, sal_True );
     704             :     // then overwrite the one in question
     705           0 :     aClauses[ _ePart ] = getComposedClause( OUString(), _rClause, *pComposer, getKeyword( _ePart ) );
     706             :     // and parse it, so that m_aAdditiveIterator is up to date
     707           0 :     for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
     708           0 :         aSql.append(aClauses[ eLoopParts ]);
     709             :     try
     710             :     {
     711           0 :         parseAndCheck_throwError( m_aSqlParser, aSql.makeStringAndClear(), m_aAdditiveIterator, *this );
     712             :     }
     713           0 :     catch( const Exception& e )
     714             :     {
     715             :         (void)e;
     716             :         SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setSingleAdditiveClause: there should be no error anymore for the additive statement!" );
     717             :         // every part of the additive statement should have passed other tests already, and should not
     718             :         // be able to cause any errors ... me thinks
     719           0 :     }
     720             : }
     721             : 
     722           0 : void SAL_CALL OSingleSelectQueryComposer::setFilter( const OUString& filter ) throw(SQLException, RuntimeException, std::exception)
     723             : {
     724             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setFilter" );
     725           0 :     setSingleAdditiveClause( Where, filter );
     726           0 : }
     727             : 
     728           0 : void SAL_CALL OSingleSelectQueryComposer::setOrder( const OUString& order ) throw(SQLException, RuntimeException, std::exception)
     729             : {
     730             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setOrder" );
     731           0 :     setSingleAdditiveClause( Order, order );
     732           0 : }
     733             : 
     734           0 : void SAL_CALL OSingleSelectQueryComposer::setGroup( const OUString& group ) throw (SQLException, RuntimeException, std::exception)
     735             : {
     736             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setGroup" );
     737           0 :     setSingleAdditiveClause( Group, group );
     738           0 : }
     739             : 
     740           0 : void SAL_CALL OSingleSelectQueryComposer::setHavingClause( const OUString& filter ) throw(SQLException, RuntimeException, std::exception)
     741             : {
     742             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setHavingClause" );
     743           0 :     setSingleAdditiveClause( Having, filter );
     744           0 : }
     745             : 
     746             : // XTablesSupplier
     747           0 : Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getTables(  ) throw(RuntimeException, std::exception)
     748             : {
     749             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getTables" );
     750           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     751             : 
     752           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     753           0 :     if ( !m_pTables )
     754             :     {
     755           0 :         const OSQLTables& aTables = m_aSqlIterator.getTables();
     756           0 :         ::std::vector< OUString> aNames;
     757           0 :         OSQLTables::const_iterator aEnd = aTables.end();
     758           0 :         for(OSQLTables::const_iterator aIter = aTables.begin(); aIter != aEnd;++aIter)
     759           0 :             aNames.push_back(aIter->first);
     760             : 
     761           0 :         m_pTables = new OPrivateTables(aTables,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames);
     762             :     }
     763             : 
     764           0 :     return m_pTables;
     765             : }
     766             : 
     767             : // XColumnsSupplier
     768           0 : Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getColumns(  ) throw(RuntimeException, std::exception)
     769             : {
     770             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getColumns" );
     771           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
     772           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     773           0 :     if ( !!m_aCurrentColumns[SelectColumns] )
     774           0 :         return m_aCurrentColumns[SelectColumns];
     775             : 
     776           0 :     ::std::vector< OUString> aNames;
     777           0 :     ::rtl::Reference< OSQLColumns> aSelectColumns;
     778           0 :     sal_Bool bCase = sal_True;
     779           0 :     Reference< XNameAccess> xQueryColumns;
     780           0 :     if ( m_nCommandType == CommandType::QUERY )
     781             :     {
     782           0 :         Reference<XColumnsSupplier> xSup(m_xConnectionQueries->getByName(m_sCommand),UNO_QUERY);
     783           0 :         if(xSup.is())
     784           0 :             xQueryColumns = xSup->getColumns();
     785             :     }
     786             : 
     787             :     do {
     788             : 
     789             :     try
     790             :     {
     791           0 :         SharedUNOComponent< XStatement, DisposableComponent > xStatement;
     792           0 :         SharedUNOComponent< XPreparedStatement, DisposableComponent > xPreparedStatement;
     793             : 
     794           0 :         bCase = m_xMetaData->supportsMixedCaseQuotedIdentifiers();
     795           0 :         aSelectColumns = m_aSqlIterator.getSelectColumns();
     796             : 
     797           0 :         OUStringBuffer aSQL( m_aPureSelectSQL + STR_WHERE + " ( 0 = 1 )");
     798             : 
     799             :         // preserve the original WHERE clause
     800             :         // #i102234#
     801           0 :         OUString sOriginalWhereClause = getSQLPart( Where, m_aSqlIterator, sal_False );
     802           0 :         if ( !sOriginalWhereClause.isEmpty() )
     803             :         {
     804           0 :             aSQL.append( " AND ( " + sOriginalWhereClause + " ) " );
     805             :         }
     806             : 
     807           0 :         OUString sGroupBy = getSQLPart( Group, m_aSqlIterator, sal_True );
     808           0 :         if ( !sGroupBy.isEmpty() )
     809           0 :             aSQL.append( sGroupBy );
     810             : 
     811           0 :         OUString sSQL( aSQL.makeStringAndClear() );
     812             :         // normalize the statement so that it doesn't contain any application-level features anymore
     813           0 :         OUString sError;
     814           0 :         const boost::scoped_ptr< OSQLParseNode > pStatementTree( m_aSqlParser.parseTree( sError, sSQL, false ) );
     815             :         OSL_ENSURE( pStatementTree.get(), "OSingleSelectQueryComposer::getColumns: could not parse the column retrieval statement!" );
     816           0 :         if ( pStatementTree.get() )
     817           0 :             if ( !pStatementTree->parseNodeToExecutableStatement( sSQL, m_xConnection, m_aSqlParser, NULL ) )
     818           0 :                 break;
     819             : 
     820           0 :         Reference< XResultSetMetaData > xResultSetMeta;
     821           0 :         Reference< XResultSetMetaDataSupplier > xResMetaDataSup;
     822             :         try
     823             :         {
     824           0 :             xPreparedStatement.set( m_xConnection->prepareStatement( sSQL ), UNO_QUERY_THROW );
     825           0 :             xResMetaDataSup.set( xPreparedStatement, UNO_QUERY_THROW );
     826           0 :             xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
     827             :         }
     828           0 :         catch( const Exception& ) { }
     829             : 
     830             :         try
     831             :         {
     832           0 :             if ( !xResultSetMeta.is() )
     833             :             {
     834           0 :                 xStatement.reset( Reference< XStatement >( m_xConnection->createStatement(), UNO_QUERY_THROW ) );
     835           0 :                 Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
     836           0 :                 try { xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, makeAny( sal_False ) ); }
     837           0 :                 catch ( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
     838           0 :                 xResMetaDataSup.set( xStatement->executeQuery( sSQL ), UNO_QUERY_THROW );
     839           0 :                 xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
     840             :             }
     841             :         }
     842           0 :         catch( const Exception& )
     843             :         {
     844             :             //@see issue http://qa.openoffice.org/issues/show_bug.cgi?id=110111
     845             :             // access returns a different order of column names when executing select * from
     846             :             // and asking the columns from the metadata.
     847           0 :             Reference< XParameters > xParameters( xPreparedStatement, UNO_QUERY_THROW );
     848           0 :             Reference< XIndexAccess > xPara = getParameters();
     849           0 :             for(sal_Int32 i = 1;i <= xPara->getCount();++i)
     850           0 :                 xParameters->setNull(i,DataType::VARCHAR);
     851           0 :             xResMetaDataSup.set(xPreparedStatement->executeQuery(), UNO_QUERY_THROW );
     852           0 :             xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_QUERY_THROW );
     853             :         }
     854             : 
     855           0 :         if ( aSelectColumns->get().empty() )
     856             :         {
     857             :             // This is a valid case. If we can syntactically parse the query, but not semantically
     858             :             // (e.g. because it is based on a table we do not know), then there will be no SelectColumns
     859           0 :             aSelectColumns = ::connectivity::parse::OParseColumn::createColumnsForResultSet( xResultSetMeta, m_xMetaData ,xQueryColumns);
     860           0 :             break;
     861             :         }
     862             : 
     863           0 :         const ::comphelper::UStringMixEqual aCaseCompare( bCase );
     864             :         typedef ::std::set< size_t > SizeTSet;
     865           0 :         SizeTSet aUsedSelectColumns;
     866           0 :         ::connectivity::parse::OParseColumn::StringMap aColumnNames;
     867             : 
     868           0 :         sal_Int32 nCount = xResultSetMeta->getColumnCount();
     869             :         OSL_ENSURE( (size_t) nCount == aSelectColumns->get().size(), "OSingleSelectQueryComposer::getColumns: inconsistent column counts, this might result in wrong columns!" );
     870           0 :         for(sal_Int32 i=1;i<=nCount;++i)
     871             :         {
     872           0 :             OUString sColumnName = xResultSetMeta->getColumnName(i);
     873           0 :             OUString sColumnLabel;
     874           0 :             if ( xQueryColumns.is() && xQueryColumns->hasByName(sColumnName) )
     875             :             {
     876           0 :                 Reference<XPropertySet> xQueryColumn(xQueryColumns->getByName(sColumnName),UNO_QUERY_THROW);
     877           0 :                 xQueryColumn->getPropertyValue(PROPERTY_LABEL) >>= sColumnLabel;
     878             :             }
     879             :             else
     880           0 :                 sColumnLabel = xResultSetMeta->getColumnLabel(i);
     881           0 :             sal_Bool bFound = sal_False;
     882           0 :             OSQLColumns::Vector::const_iterator aFind = ::connectivity::find(aSelectColumns->get().begin(),aSelectColumns->get().end(),sColumnLabel,aCaseCompare);
     883           0 :             size_t nFoundSelectColumnPos = aFind - aSelectColumns->get().begin();
     884           0 :             if ( aFind != aSelectColumns->get().end() )
     885             :             {
     886           0 :                 if ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
     887             :                 {   // we found a column name which exists twice
     888             :                     // so we start after the first found
     889           0 :                     do
     890             :                     {
     891           0 :                         aFind = ::connectivity::findRealName(++aFind,aSelectColumns->get().end(),sColumnName,aCaseCompare);
     892           0 :                         nFoundSelectColumnPos = aFind - aSelectColumns->get().begin();
     893             :                     }
     894           0 :                     while   (   ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
     895           0 :                                 &&  ( aFind != aSelectColumns->get().end() )
     896             :                             );
     897             :                 }
     898           0 :                 if ( aFind != aSelectColumns->get().end() )
     899             :                 {
     900           0 :                     (*aFind)->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
     901           0 :                     aUsedSelectColumns.insert( nFoundSelectColumnPos );
     902           0 :                     aNames.push_back(sColumnName);
     903           0 :                     bFound = sal_True;
     904             :                 }
     905             :             }
     906             : 
     907           0 :             if ( bFound )
     908           0 :                 continue;
     909             : 
     910             :             OSQLColumns::Vector::const_iterator aRealFind = ::connectivity::findRealName(
     911           0 :                 aSelectColumns->get().begin(), aSelectColumns->get().end(), sColumnName, aCaseCompare );
     912             : 
     913           0 :             if ( i > static_cast< sal_Int32>( aSelectColumns->get().size() ) )
     914             :             {
     915           0 :                 aSelectColumns->get().push_back(
     916           0 :                     ::connectivity::parse::OParseColumn::createColumnForResultSet( xResultSetMeta, m_xMetaData, i ,aColumnNames)
     917           0 :                 );
     918             :                 OSL_ENSURE( aSelectColumns->get().size() == (size_t)i, "OSingleSelectQueryComposer::getColumns: inconsistency!" );
     919             :             }
     920           0 :             else if ( aRealFind == aSelectColumns->get().end() )
     921             :             {
     922             :                 // we can now only look if we found it under the realname propertery
     923             :                 // here we have to make the assumption that the position is correct
     924           0 :                 OSQLColumns::Vector::iterator aFind2 = aSelectColumns->get().begin() + i-1;
     925           0 :                 Reference<XPropertySet> xProp(*aFind2,UNO_QUERY);
     926           0 :                 if ( !xProp.is() || !xProp->getPropertySetInfo()->hasPropertyByName( PROPERTY_REALNAME ) )
     927           0 :                     continue;
     928             : 
     929           0 :                 ::connectivity::parse::OParseColumn* pColumn = new ::connectivity::parse::OParseColumn(xProp,bCase);
     930           0 :                 pColumn->setFunction(::comphelper::getBOOL(xProp->getPropertyValue("Function")));
     931           0 :                 pColumn->setAggregateFunction(::comphelper::getBOOL(xProp->getPropertyValue("AggregateFunction")));
     932             : 
     933           0 :                 OUString sRealName;
     934           0 :                 xProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
     935           0 :                 ::std::vector< OUString>::iterator aFindName;
     936           0 :                 if ( sColumnName.isEmpty() )
     937           0 :                     xProp->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
     938             : 
     939           0 :                 aFindName = ::std::find_if(aNames.begin(),aNames.end(),::std::bind2nd(aCaseCompare,sColumnName));
     940           0 :                 sal_Int32 j = 0;
     941           0 :                 while ( aFindName != aNames.end() )
     942             :                 {
     943           0 :                     sColumnName += OUString::number(++j);
     944           0 :                     aFindName = ::std::find_if(aNames.begin(),aNames.end(),::std::bind2nd(aCaseCompare,sColumnName));
     945             :                 }
     946             : 
     947           0 :                 pColumn->setName(sColumnName);
     948           0 :                 pColumn->setRealName(sRealName);
     949           0 :                 pColumn->setTableName(::comphelper::getString(xProp->getPropertyValue(PROPERTY_TABLENAME)));
     950             : 
     951           0 :                 (aSelectColumns->get())[i-1] = pColumn;
     952             :             }
     953             :             else
     954           0 :                 continue;
     955             : 
     956           0 :             aUsedSelectColumns.insert( (size_t)(i - 1) );
     957           0 :             aNames.push_back( sColumnName );
     958           0 :         }
     959             :     }
     960           0 :     catch(const Exception&)
     961             :     {
     962             :     }
     963             : 
     964             :     } while ( false );
     965             : 
     966           0 :     bool bMissingSomeColumnLabels = !aNames.empty() && aNames.size() != aSelectColumns->get().size();
     967             :     SAL_WARN_IF(bMissingSomeColumnLabels, "dbaccess", "We have column labels for *some* columns but not all");
     968             :     //^^this happens in the evolution address book where we have real column names of e.g.
     969             :     //first_name, second_name and city. On parsing via
     970             :     //OSQLParseTreeIterator::appendColumns it creates some labels using those real names
     971             :     //but the evo address book gives them proper labels of First Name, Second Name and City
     972             :     //the munge means that here we have e.g. just "City" as a label because it matches
     973             : 
     974             :     //This is all a horrible mess
     975           0 :     if (bMissingSomeColumnLabels)
     976           0 :         aNames.clear();
     977             : 
     978           0 :     if ( aNames.empty() )
     979           0 :         m_aCurrentColumns[ SelectColumns ] = OPrivateColumns::createWithIntrinsicNames( aSelectColumns, bCase, *this, m_aMutex );
     980             :     else
     981           0 :         m_aCurrentColumns[ SelectColumns ] = new OPrivateColumns( aSelectColumns, bCase, *this, m_aMutex, aNames );
     982             : 
     983           0 :     return m_aCurrentColumns[SelectColumns];
     984             : }
     985             : 
     986           0 : sal_Bool OSingleSelectQueryComposer::setORCriteria(OSQLParseNode* pCondition, OSQLParseTreeIterator& _rIterator,
     987             :                                     ::std::vector< ::std::vector < PropertyValue > >& rFilters, const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const
     988             : {
     989             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setORCriteria" );
     990             :     // Round brackets around the expression
     991           0 :     if (pCondition->count() == 3 &&
     992           0 :         SQL_ISPUNCTUATION(pCondition->getChild(0),"(") &&
     993           0 :         SQL_ISPUNCTUATION(pCondition->getChild(2),")"))
     994             :     {
     995           0 :         return setORCriteria(pCondition->getChild(1), _rIterator, rFilters, xFormatter);
     996             :     }
     997             :     // OR logic expression
     998             :     // a searchcondition can only look like this: search_condition SQL_TOKEN_OR boolean_term
     999           0 :     else if (SQL_ISRULE(pCondition,search_condition))
    1000             :     {
    1001           0 :         sal_Bool bResult = sal_True;
    1002           0 :         for (int i = 0; bResult && i < 3; i+=2)
    1003             :         {
    1004             :             // Is the first element a OR logic expression again?
    1005             :             // Then descend recursively ...
    1006           0 :             if (SQL_ISRULE(pCondition->getChild(i),search_condition))
    1007           0 :                 bResult = setORCriteria(pCondition->getChild(i), _rIterator, rFilters, xFormatter);
    1008             :             else
    1009             :             {
    1010           0 :                 rFilters.push_back( ::std::vector < PropertyValue >());
    1011           0 :                 bResult = setANDCriteria(pCondition->getChild(i), _rIterator, rFilters[rFilters.size() - 1], xFormatter);
    1012             :             }
    1013             :         }
    1014           0 :         return bResult;
    1015             :     }
    1016             :     else
    1017             :     {
    1018           0 :         rFilters.push_back(::std::vector < PropertyValue >());
    1019           0 :         return setANDCriteria(pCondition, _rIterator, rFilters[rFilters.size() - 1], xFormatter);
    1020             :     }
    1021             : }
    1022             : 
    1023           0 : sal_Bool OSingleSelectQueryComposer::setANDCriteria( OSQLParseNode * pCondition,
    1024             :     OSQLParseTreeIterator& _rIterator, ::std::vector < PropertyValue >& rFilter, const Reference< XNumberFormatter > & xFormatter) const
    1025             : {
    1026             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setANDCriteria" );
    1027             :     // Round brackets
    1028           0 :     if (SQL_ISRULE(pCondition,boolean_primary))
    1029             :     {
    1030             :         // this should not occur
    1031             :         SAL_WARN("dbaccess","boolean_primary in And-Criteria");
    1032           0 :         return sal_False;
    1033             :     }
    1034             :     // The first element is an AND logical expression again
    1035           0 :     else if ( SQL_ISRULE(pCondition,boolean_term) && pCondition->count() == 3 )
    1036             :     {
    1037           0 :         return setANDCriteria(pCondition->getChild(0), _rIterator, rFilter, xFormatter) &&
    1038           0 :                setANDCriteria(pCondition->getChild(2), _rIterator, rFilter, xFormatter);
    1039             :     }
    1040           0 :     else if (SQL_ISRULE(pCondition, comparison_predicate))
    1041             :     {
    1042           0 :         return setComparsionPredicate(pCondition,_rIterator,rFilter,xFormatter);
    1043             :     }
    1044           0 :     else if (SQL_ISRULE(pCondition,like_predicate) ||
    1045           0 :              SQL_ISRULE(pCondition,test_for_null) ||
    1046           0 :              SQL_ISRULE(pCondition,in_predicate) ||
    1047           0 :              SQL_ISRULE(pCondition,all_or_any_predicate) ||
    1048           0 :              SQL_ISRULE(pCondition,between_predicate))
    1049             :     {
    1050           0 :         if (SQL_ISRULE(pCondition->getChild(0), column_ref))
    1051             :         {
    1052           0 :             PropertyValue aItem;
    1053           0 :             OUString aValue;
    1054           0 :             OUString aColumnName;
    1055             : 
    1056           0 :             pCondition->parseNodeToStr( aValue, m_xConnection, NULL );
    1057           0 :             pCondition->getChild(0)->parseNodeToStr( aColumnName, m_xConnection, NULL );
    1058             : 
    1059             :             // don't display the column name
    1060           0 :             aValue = aValue.copy(aColumnName.getLength());
    1061           0 :             aValue = aValue.trim();
    1062             : 
    1063           0 :             aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
    1064           0 :             aItem.Value <<= aValue;
    1065           0 :             aItem.Handle = 0; // just to know that this is not one the known ones
    1066           0 :             if ( SQL_ISRULE(pCondition,like_predicate) )
    1067             :             {
    1068           0 :                 if ( SQL_ISTOKEN(pCondition->getChild(1)->getChild(0),NOT) )
    1069           0 :                     aItem.Handle = SQLFilterOperator::NOT_LIKE;
    1070             :                 else
    1071           0 :                     aItem.Handle = SQLFilterOperator::LIKE;
    1072             :             }
    1073           0 :             else if (SQL_ISRULE(pCondition,test_for_null))
    1074             :             {
    1075           0 :                 if (SQL_ISTOKEN(pCondition->getChild(1)->getChild(1),NOT) )
    1076           0 :                     aItem.Handle = SQLFilterOperator::NOT_SQLNULL;
    1077             :                 else
    1078           0 :                     aItem.Handle = SQLFilterOperator::SQLNULL;
    1079             :             }
    1080           0 :             else if (SQL_ISRULE(pCondition,in_predicate))
    1081             :             {
    1082             :                 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: in_predicate not implemented!" );
    1083             :             }
    1084           0 :             else if (SQL_ISRULE(pCondition,all_or_any_predicate))
    1085             :             {
    1086             :                 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: all_or_any_predicate not implemented!" );
    1087             :             }
    1088           0 :             else if (SQL_ISRULE(pCondition,between_predicate))
    1089             :             {
    1090             :                 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: between_predicate not implemented!" );
    1091             :             }
    1092             : 
    1093           0 :             rFilter.push_back(aItem);
    1094             :         }
    1095             :         else
    1096           0 :             return sal_False;
    1097             :     }
    1098           0 :     else if (SQL_ISRULE(pCondition,existence_test) ||
    1099           0 :              SQL_ISRULE(pCondition,unique_test))
    1100             :     {
    1101             :         // this couldn't be handled here, too complex
    1102             :         // as we need a field name
    1103           0 :         return sal_False;
    1104             :     }
    1105             :     else
    1106           0 :         return sal_False;
    1107             : 
    1108           0 :     return sal_True;
    1109             : }
    1110             : 
    1111           0 : sal_Int32 OSingleSelectQueryComposer::getPredicateType(OSQLParseNode * _pPredicate) const
    1112             : {
    1113             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getPredicateType" );
    1114           0 :     sal_Int32 nPredicate = SQLFilterOperator::EQUAL;
    1115           0 :     switch (_pPredicate->getNodeType())
    1116             :     {
    1117             :         case SQL_NODE_EQUAL:
    1118           0 :             nPredicate = SQLFilterOperator::EQUAL;
    1119           0 :             break;
    1120             :         case SQL_NODE_NOTEQUAL:
    1121           0 :             nPredicate = SQLFilterOperator::NOT_EQUAL;
    1122           0 :             break;
    1123             :         case SQL_NODE_LESS:
    1124           0 :             nPredicate = SQLFilterOperator::LESS;
    1125           0 :             break;
    1126             :         case SQL_NODE_LESSEQ:
    1127           0 :             nPredicate = SQLFilterOperator::LESS_EQUAL;
    1128           0 :             break;
    1129             :         case SQL_NODE_GREAT:
    1130           0 :             nPredicate = SQLFilterOperator::GREATER;
    1131           0 :             break;
    1132             :         case SQL_NODE_GREATEQ:
    1133           0 :             nPredicate = SQLFilterOperator::GREATER_EQUAL;
    1134           0 :             break;
    1135             :         default:
    1136             :             SAL_WARN("dbaccess","Wrong NodeType!");
    1137             :     }
    1138           0 :     return nPredicate;
    1139             : }
    1140             : 
    1141           0 : sal_Bool OSingleSelectQueryComposer::setComparsionPredicate(OSQLParseNode * pCondition, OSQLParseTreeIterator& _rIterator,
    1142             :                                             ::std::vector < PropertyValue >& rFilter, const Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const
    1143             : {
    1144             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setComparsionPredicate" );
    1145             :     OSL_ENSURE(SQL_ISRULE(pCondition, comparison_predicate),"setComparsionPredicate: pCondition ist kein ComparsionPredicate");
    1146           0 :     if (SQL_ISRULE(pCondition->getChild(0), column_ref) ||
    1147           0 :         SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
    1148             :     {
    1149           0 :         PropertyValue aItem;
    1150           0 :         OUString aValue;
    1151             :         sal_uInt32 nPos;
    1152           0 :         if (SQL_ISRULE(pCondition->getChild(0), column_ref))
    1153             :         {
    1154           0 :             nPos = 0;
    1155           0 :             sal_uInt32 i=1;
    1156             : 
    1157           0 :             aItem.Handle = getPredicateType(pCondition->getChild(i));
    1158             :             // don't display the equal
    1159           0 :             if (pCondition->getChild(i)->getNodeType() == SQL_NODE_EQUAL)
    1160           0 :                 i++;
    1161             : 
    1162             :             // go forward
    1163           0 :             for (;i < pCondition->count();i++)
    1164             :                 pCondition->getChild(i)->parseNodeToPredicateStr(
    1165           0 :                     aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>(m_sDecimalSep.toChar() ) );
    1166             :         }
    1167           0 :         else if (SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
    1168             :         {
    1169           0 :             nPos = pCondition->count()-1;
    1170             : 
    1171           0 :             sal_Int32 i = pCondition->count() - 2;
    1172           0 :             switch (pCondition->getChild(i)->getNodeType())
    1173             :             {
    1174             :                 case SQL_NODE_EQUAL:
    1175             :                     // don't display the equal
    1176           0 :                     i--;
    1177           0 :                     aItem.Handle = SQLFilterOperator::EQUAL;
    1178           0 :                     break;
    1179             :                 case SQL_NODE_NOTEQUAL:
    1180           0 :                     i--;
    1181           0 :                     aItem.Handle = SQLFilterOperator::NOT_EQUAL;
    1182           0 :                     break;
    1183             :                 case SQL_NODE_LESS:
    1184             :                     // take the opposite as we change the order
    1185           0 :                     i--;
    1186           0 :                     aValue = ">=";
    1187           0 :                     aItem.Handle = SQLFilterOperator::GREATER_EQUAL;
    1188           0 :                     break;
    1189             :                 case SQL_NODE_LESSEQ:
    1190             :                     // take the opposite as we change the order
    1191           0 :                     i--;
    1192           0 :                     aValue = ">";
    1193           0 :                     aItem.Handle = SQLFilterOperator::GREATER;
    1194           0 :                     break;
    1195             :                 case SQL_NODE_GREAT:
    1196             :                     // take the opposite as we change the order
    1197           0 :                     i--;
    1198           0 :                     aValue = "<=";
    1199           0 :                     aItem.Handle = SQLFilterOperator::LESS_EQUAL;
    1200           0 :                     break;
    1201             :                 case SQL_NODE_GREATEQ:
    1202             :                     // take the opposite as we change the order
    1203           0 :                     i--;
    1204           0 :                     aValue = "<";
    1205           0 :                     aItem.Handle = SQLFilterOperator::LESS;
    1206           0 :                     break;
    1207             :                 default:
    1208           0 :                     break;
    1209             :             }
    1210             : 
    1211             :             // go backward
    1212           0 :             for (; i >= 0; i--)
    1213             :                 pCondition->getChild(i)->parseNodeToPredicateStr(
    1214           0 :                     aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
    1215             :         }
    1216             :         else
    1217           0 :             return sal_False;
    1218             : 
    1219           0 :         aItem.Name = getColumnName(pCondition->getChild(nPos),_rIterator);
    1220           0 :         aItem.Value <<= aValue;
    1221           0 :         rFilter.push_back(aItem);
    1222             :     }
    1223           0 :     else if (SQL_ISRULE(pCondition->getChild(0), set_fct_spec ) ||
    1224           0 :              SQL_ISRULE(pCondition->getChild(0), general_set_fct))
    1225             :     {
    1226           0 :         PropertyValue aItem;
    1227           0 :         OUString aValue;
    1228           0 :         OUString aColumnName;
    1229             : 
    1230           0 :         pCondition->getChild(2)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
    1231           0 :         pCondition->getChild(0)->parseNodeToPredicateStr( aColumnName, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep .toChar() ) );
    1232             : 
    1233           0 :         aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
    1234           0 :         aItem.Value <<= aValue;
    1235           0 :         aItem.Handle = getPredicateType(pCondition->getChild(1));
    1236           0 :         rFilter.push_back(aItem);
    1237             :     }
    1238             :     else // Can only be an expression
    1239             :     {
    1240           0 :         PropertyValue aItem;
    1241           0 :         OUString aName, aValue;
    1242             : 
    1243           0 :         OSQLParseNode *pLhs = pCondition->getChild(0);
    1244           0 :         OSQLParseNode *pRhs = pCondition->getChild(2);
    1245             : 
    1246             :         // Field names
    1247             :         sal_uInt16 i;
    1248           0 :         for (i=0;i< pLhs->count();i++)
    1249           0 :              pLhs->getChild(i)->parseNodeToPredicateStr( aName, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
    1250             : 
    1251             :         // Criterion
    1252           0 :         aItem.Handle = getPredicateType(pCondition->getChild(1));
    1253           0 :         aValue       = pCondition->getChild(1)->getTokenValue();
    1254           0 :         for(i=0;i< pRhs->count();i++)
    1255           0 :             pRhs->getChild(i)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, static_cast<sal_Char>( m_sDecimalSep.toChar() ) );
    1256             : 
    1257           0 :         aItem.Name = aName;
    1258           0 :         aItem.Value <<= aValue;
    1259           0 :         rFilter.push_back(aItem);
    1260             :     }
    1261           0 :     return sal_True;
    1262             : }
    1263             : 
    1264             : // Functions for analysing SQL
    1265           0 : OUString OSingleSelectQueryComposer::getColumnName( ::connectivity::OSQLParseNode* pColumnRef, OSQLParseTreeIterator& _rIterator ) const
    1266             : {
    1267             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getColumnName" );
    1268           0 :     OUString aTableRange, aColumnName;
    1269           0 :     _rIterator.getColumnRange(pColumnRef,aColumnName,aTableRange);
    1270           0 :     return aColumnName;
    1271             : }
    1272             : 
    1273           0 : OUString SAL_CALL OSingleSelectQueryComposer::getFilter(  ) throw(RuntimeException, std::exception)
    1274             : {
    1275             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getFilter" );
    1276           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1277           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1278           0 :     return getSQLPart(Where,m_aAdditiveIterator,sal_False);
    1279             : }
    1280             : 
    1281           0 : OUString SAL_CALL OSingleSelectQueryComposer::getOrder(  ) throw(RuntimeException, std::exception)
    1282             : {
    1283             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getOrder" );
    1284           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1285           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1286           0 :     return getSQLPart(Order,m_aAdditiveIterator,sal_False);
    1287             : }
    1288             : 
    1289           0 : OUString SAL_CALL OSingleSelectQueryComposer::getGroup(  ) throw (RuntimeException, std::exception)
    1290             : {
    1291             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getGroup" );
    1292           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1293           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1294           0 :     return getSQLPart(Group,m_aAdditiveIterator,sal_False);
    1295             : }
    1296             : 
    1297           0 : OUString OSingleSelectQueryComposer::getHavingClause() throw (RuntimeException, std::exception)
    1298             : {
    1299             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getHavingClause" );
    1300           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1301           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1302           0 :     return getSQLPart(Having,m_aAdditiveIterator,sal_False);
    1303             : }
    1304             : 
    1305           0 : OUString OSingleSelectQueryComposer::getTableAlias(const Reference< XPropertySet >& column) const
    1306             : {
    1307             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getTableAlias" );
    1308           0 :     OUString sReturn;
    1309           0 :     if(m_pTables && m_pTables->getCount() > 1)
    1310             :     {
    1311           0 :         OUString aCatalog,aSchema,aTable,aComposedName,aColumnName;
    1312           0 :         if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_CATALOGNAME))
    1313           0 :             column->getPropertyValue(PROPERTY_CATALOGNAME)  >>= aCatalog;
    1314           0 :         if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_SCHEMANAME))
    1315           0 :             column->getPropertyValue(PROPERTY_SCHEMANAME)   >>= aSchema;
    1316           0 :         if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME))
    1317           0 :             column->getPropertyValue(PROPERTY_TABLENAME)    >>= aTable;
    1318           0 :         column->getPropertyValue(PROPERTY_NAME)         >>= aColumnName;
    1319             : 
    1320           0 :         Sequence< OUString> aNames(m_pTables->getElementNames());
    1321           0 :         const OUString* pBegin     = aNames.getConstArray();
    1322           0 :         const OUString* const pEnd = pBegin + aNames.getLength();
    1323             : 
    1324           0 :         if(aTable.isEmpty())
    1325             :         { // we haven't found a table name, now we must search every table for this column
    1326           0 :             for(;pBegin != pEnd;++pBegin)
    1327             :             {
    1328           0 :                 Reference<XColumnsSupplier> xColumnsSupp;
    1329           0 :                 m_pTables->getByName(*pBegin) >>= xColumnsSupp;
    1330             : 
    1331           0 :                 if(xColumnsSupp.is() && xColumnsSupp->getColumns()->hasByName(aColumnName))
    1332             :                 {
    1333           0 :                     aTable = *pBegin;
    1334           0 :                     break;
    1335             :                 }
    1336           0 :             }
    1337             :         }
    1338             :         else
    1339             :         {
    1340           0 :             aComposedName = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, false, ::dbtools::eInDataManipulation );
    1341             : 
    1342             :             // Is this the right case for the table name?
    1343             :             // Else, look for it with different case, if applicable.
    1344             : 
    1345           0 :             if(!m_pTables->hasByName(aComposedName))
    1346             :             {
    1347           0 :                 ::comphelper::UStringMixLess aTmp(m_aAdditiveIterator.getTables().key_comp());
    1348           0 :                 ::comphelper::UStringMixEqual aComp(static_cast< ::comphelper::UStringMixLess*>(&aTmp)->isCaseSensitive());
    1349           0 :                 for(;pBegin != pEnd;++pBegin)
    1350             :                 {
    1351           0 :                     Reference<XPropertySet> xTableProp;
    1352           0 :                     m_pTables->getByName(*pBegin) >>= xTableProp;
    1353             :                     OSL_ENSURE(xTableProp.is(),"Table isn't a propertyset!");
    1354           0 :                     if(xTableProp.is())
    1355             :                     {
    1356           0 :                         OUString aCatalog2,aSchema2,aTable2;
    1357           0 :                         xTableProp->getPropertyValue(PROPERTY_CATALOGNAME)  >>= aCatalog2;
    1358           0 :                         xTableProp->getPropertyValue(PROPERTY_SCHEMANAME)   >>= aSchema2;
    1359           0 :                         xTableProp->getPropertyValue(PROPERTY_NAME)         >>= aTable2;
    1360           0 :                         if(aComp(aCatalog,aCatalog2) && aComp(aSchema,aSchema2) && aComp(aTable,aTable2))
    1361             :                         {
    1362           0 :                             aCatalog    = aCatalog2;
    1363           0 :                             aSchema     = aSchema2;
    1364           0 :                             aTable      = aTable2;
    1365           0 :                             break;
    1366           0 :                         }
    1367             :                     }
    1368           0 :                 }
    1369             :             }
    1370             :         }
    1371           0 :         if(pBegin != pEnd)
    1372             :         {
    1373           0 :             sReturn = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, true, ::dbtools::eInDataManipulation ) + ".";
    1374           0 :         }
    1375             :     }
    1376           0 :     return sReturn;
    1377             : }
    1378             : 
    1379           0 : Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getParameters(  ) throw(RuntimeException, std::exception)
    1380             : {
    1381             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getParameters" );
    1382             :     // now set the Parameters
    1383           0 :     if ( !m_aCurrentColumns[ParameterColumns] )
    1384             :     {
    1385           0 :         ::rtl::Reference< OSQLColumns> aCols = m_aSqlIterator.getParameters();
    1386           0 :         ::std::vector< OUString> aNames;
    1387           0 :         OSQLColumns::Vector::const_iterator aEnd = aCols->get().end();
    1388           0 :         for(OSQLColumns::Vector::const_iterator aIter = aCols->get().begin(); aIter != aEnd;++aIter)
    1389           0 :             aNames.push_back(getString((*aIter)->getPropertyValue(PROPERTY_NAME)));
    1390           0 :         m_aCurrentColumns[ParameterColumns] = new OPrivateColumns(aCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,sal_True);
    1391             :     }
    1392             : 
    1393           0 :     return m_aCurrentColumns[ParameterColumns];
    1394             : }
    1395             : 
    1396           0 : void OSingleSelectQueryComposer::clearColumns( const EColumnType _eType )
    1397             : {
    1398             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::clearColumns" );
    1399           0 :     OPrivateColumns* pColumns = m_aCurrentColumns[ _eType ];
    1400           0 :     if ( pColumns != NULL )
    1401             :     {
    1402           0 :         pColumns->disposing();
    1403           0 :         m_aColumnsCollection.push_back( pColumns );
    1404           0 :         m_aCurrentColumns[ _eType ] = NULL;
    1405             :     }
    1406           0 : }
    1407             : 
    1408           0 : void OSingleSelectQueryComposer::clearCurrentCollections()
    1409             : {
    1410             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::clearCurrentCollections" );
    1411           0 :     ::std::vector<OPrivateColumns*>::iterator aIter = m_aCurrentColumns.begin();
    1412           0 :     ::std::vector<OPrivateColumns*>::iterator aEnd = m_aCurrentColumns.end();
    1413           0 :     for (;aIter != aEnd;++aIter)
    1414             :     {
    1415           0 :         if ( *aIter )
    1416             :         {
    1417           0 :             (*aIter)->disposing();
    1418           0 :             m_aColumnsCollection.push_back(*aIter);
    1419           0 :             *aIter = NULL;
    1420             :         }
    1421             :     }
    1422             : 
    1423           0 :     if(m_pTables)
    1424             :     {
    1425           0 :         m_pTables->disposing();
    1426           0 :         m_aTablesCollection.push_back(m_pTables);
    1427           0 :         m_pTables = NULL;
    1428             :     }
    1429           0 : }
    1430             : 
    1431           0 : Reference< XIndexAccess > OSingleSelectQueryComposer::setCurrentColumns( EColumnType _eType,
    1432             :     const ::rtl::Reference< OSQLColumns >& _rCols )
    1433             : {
    1434             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setCurrentColumns" );
    1435           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1436             : 
    1437           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1438             :     // now set the group columns
    1439           0 :     if ( !m_aCurrentColumns[_eType] )
    1440             :     {
    1441           0 :         ::std::vector< OUString> aNames;
    1442           0 :         OSQLColumns::Vector::const_iterator aEnd = _rCols->get().end();
    1443           0 :         for(OSQLColumns::Vector::const_iterator aIter = _rCols->get().begin(); aIter != aEnd;++aIter)
    1444           0 :             aNames.push_back(getString((*aIter)->getPropertyValue(PROPERTY_NAME)));
    1445           0 :         m_aCurrentColumns[_eType] = new OPrivateColumns(_rCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,sal_True);
    1446             :     }
    1447             : 
    1448           0 :     return m_aCurrentColumns[_eType];
    1449             : }
    1450             : 
    1451           0 : Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getGroupColumns(  ) throw(RuntimeException, std::exception)
    1452             : {
    1453             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getGroupColumns" );
    1454           0 :     return setCurrentColumns( GroupByColumns, m_aAdditiveIterator.getGroupColumns() );
    1455             : }
    1456             : 
    1457           0 : Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getOrderColumns(  ) throw(RuntimeException, std::exception)
    1458             : {
    1459             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getOrderColumns" );
    1460           0 :     return setCurrentColumns( OrderColumns, m_aAdditiveIterator.getOrderColumns() );
    1461             : }
    1462             : 
    1463           0 : OUString SAL_CALL OSingleSelectQueryComposer::getQueryWithSubstitution(  ) throw (SQLException, RuntimeException, std::exception)
    1464             : {
    1465             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getQueryWithSubstitution" );
    1466           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1467           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1468             : 
    1469           0 :     OUString sSqlStatement( getQuery() );
    1470             : 
    1471           0 :     const OSQLParseNode* pStatementNode = m_aSqlIterator.getParseTree();
    1472           0 :     if ( pStatementNode )
    1473             :     {
    1474           0 :         SQLException aError;
    1475           0 :         if ( !pStatementNode->parseNodeToExecutableStatement( sSqlStatement, m_xConnection, m_aSqlParser, &aError ) )
    1476           0 :             throw SQLException( aError );
    1477             :     }
    1478             : 
    1479           0 :     return sSqlStatement;
    1480             : }
    1481             : 
    1482           0 : OUString OSingleSelectQueryComposer::getStatementPart( TGetParseNode& _aGetFunctor, OSQLParseTreeIterator& _rIterator )
    1483             : {
    1484             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getStatementPart" );
    1485           0 :     OUString sResult;
    1486             : 
    1487           0 :     const OSQLParseNode* pNode = _aGetFunctor( &_rIterator );
    1488           0 :     if ( pNode )
    1489           0 :         pNode->parseNodeToStr( sResult, m_xConnection );
    1490             : 
    1491           0 :     return sResult;
    1492             : }
    1493             : 
    1494             : namespace
    1495             : {
    1496           0 :     OUString lcl_getCondition(const Sequence< Sequence< PropertyValue > >& filter,const OPredicateInputController& i_aPredicateInputController,const Reference< XNameAccess >& i_xSelectColumns)
    1497             :     {
    1498           0 :         OUStringBuffer sRet;
    1499           0 :         const Sequence< PropertyValue >* pOrIter = filter.getConstArray();
    1500           0 :         const Sequence< PropertyValue >* pOrEnd = pOrIter + filter.getLength();
    1501           0 :         while ( pOrIter != pOrEnd )
    1502             :         {
    1503           0 :             if ( pOrIter->getLength() )
    1504             :             {
    1505           0 :                 sRet.append(L_BRACKET);
    1506           0 :                 const PropertyValue* pAndIter = pOrIter->getConstArray();
    1507           0 :                 const PropertyValue* pAndEnd = pAndIter + pOrIter->getLength();
    1508           0 :                 while ( pAndIter != pAndEnd )
    1509             :                 {
    1510           0 :                     sRet.append(pAndIter->Name);
    1511           0 :                     OUString sValue;
    1512           0 :                     pAndIter->Value >>= sValue;
    1513           0 :                     if ( i_xSelectColumns.is() && i_xSelectColumns->hasByName(pAndIter->Name) )
    1514             :                     {
    1515           0 :                         Reference<XPropertySet> xColumn(i_xSelectColumns->getByName(pAndIter->Name),UNO_QUERY);
    1516           0 :                         sValue = i_aPredicateInputController.getPredicateValue(sValue,xColumn,true);
    1517             :                     }
    1518             :                     else
    1519             :                     {
    1520           0 :                         sValue = i_aPredicateInputController.getPredicateValue(pAndIter->Name,sValue,true);
    1521             :                     }
    1522           0 :                     lcl_addFilterCriteria_throw(pAndIter->Handle,sValue,sRet);
    1523           0 :                     ++pAndIter;
    1524           0 :                     if ( pAndIter != pAndEnd )
    1525           0 :                         sRet.append(STR_AND);
    1526           0 :                 }
    1527           0 :                 sRet.append(R_BRACKET);
    1528             :             }
    1529           0 :             ++pOrIter;
    1530           0 :             if ( pOrIter != pOrEnd && !sRet.isEmpty() )
    1531           0 :                 sRet.append(STR_OR);
    1532             :         }
    1533           0 :         return sRet.makeStringAndClear();
    1534             :     }
    1535             : }
    1536             : 
    1537           0 : void SAL_CALL OSingleSelectQueryComposer::setStructuredFilter( const Sequence< Sequence< PropertyValue > >& filter ) throw (SQLException, ::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
    1538             : {
    1539             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setStructuredFilter" );
    1540           0 :     OPredicateInputController aPredicateInput(m_aContext, m_xConnection, &m_aParseContext);
    1541           0 :     setFilter(lcl_getCondition(filter,aPredicateInput,getColumns()));
    1542           0 : }
    1543             : 
    1544           0 : void SAL_CALL OSingleSelectQueryComposer::setStructuredHavingClause( const Sequence< Sequence< PropertyValue > >& filter ) throw (SQLException, RuntimeException, std::exception)
    1545             : {
    1546             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setStructuredHavingClause" );
    1547           0 :     OPredicateInputController aPredicateInput(m_aContext, m_xConnection);
    1548           0 :     setHavingClause(lcl_getCondition(filter,aPredicateInput,getColumns()));
    1549           0 : }
    1550             : 
    1551           0 : void OSingleSelectQueryComposer::setConditionByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria ,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,const OUString& >& _aSetFunctor,sal_Int32 filterOperator)
    1552             : {
    1553             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::setConditionByColumn" );
    1554           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1555             : 
    1556           0 :     if ( !column.is()
    1557           0 :         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_VALUE)
    1558           0 :         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
    1559           0 :         || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_TYPE))
    1560           0 :         throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
    1561             : 
    1562           0 :     sal_Int32 nType = 0;
    1563           0 :     column->getPropertyValue(PROPERTY_TYPE) >>= nType;
    1564           0 :     sal_Int32 nSearchable = dbtools::getSearchColumnFlag(m_xConnection,nType);
    1565           0 :     if(nSearchable == ColumnSearch::NONE)
    1566           0 :         throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_SEARCHABLE),*this,SQLSTATE_GENERAL,1000,Any() );
    1567             : 
    1568           0 :     ::osl::MutexGuard aGuard( m_aMutex );
    1569             : 
    1570           0 :     OUString aName;
    1571           0 :     column->getPropertyValue(PROPERTY_NAME) >>= aName;
    1572             : 
    1573           0 :     Any aValue;
    1574           0 :     column->getPropertyValue(PROPERTY_VALUE) >>= aValue;
    1575             : 
    1576           0 :     OUStringBuffer aSQL;
    1577           0 :     const OUString aQuote    = m_xMetaData->getIdentifierQuoteString();
    1578           0 :     getColumns();
    1579             : 
    1580             :     // TODO: if this is called for HAVING, check that the column is a GROUP BY column
    1581             :     //       or that it is an aggregate function
    1582             : 
    1583           0 :     if ( m_aCurrentColumns[SelectColumns] && m_aCurrentColumns[SelectColumns]->hasByName(aName) )
    1584             :     {
    1585           0 :         Reference<XPropertySet> xColumn;
    1586           0 :         m_aCurrentColumns[SelectColumns]->getByName(aName) >>= xColumn;
    1587             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
    1588             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
    1589             :         OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName("AggregateFunction"),"Property AggregateFunction not available!");
    1590             : 
    1591           0 :         OUString sRealName,sTableName;
    1592           0 :         xColumn->getPropertyValue(PROPERTY_REALNAME)    >>= sRealName;
    1593           0 :         xColumn->getPropertyValue(PROPERTY_TABLENAME)   >>= sTableName;
    1594           0 :         if(sTableName.indexOf('.',0) != -1)
    1595             :         {
    1596           0 :             OUString aCatlog,aSchema,aTable;
    1597           0 :             ::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatlog,aSchema,aTable,::dbtools::eInDataManipulation);
    1598           0 :             sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, true, ::dbtools::eInDataManipulation );
    1599             :         }
    1600             :         else
    1601           0 :             sTableName = ::dbtools::quoteName(aQuote,sTableName);
    1602             : 
    1603           0 :         if ( !::comphelper::getBOOL(xColumn->getPropertyValue("Function")) )
    1604             :         {
    1605           0 :             aSQL =  sTableName + "." + ::dbtools::quoteName( aQuote, sRealName );
    1606             :         }
    1607             :         else
    1608           0 :             aSQL = sRealName;
    1609             : 
    1610             :     }
    1611             :     else
    1612             :     {
    1613           0 :         aSQL = getTableAlias( column ) + ::dbtools::quoteName( aQuote, aName );
    1614             :     }
    1615             : 
    1616           0 :     if ( aValue.hasValue() )
    1617             :     {
    1618           0 :         if(  !m_xTypeConverter.is() )
    1619           0 :             m_xTypeConverter.set( Converter::create(m_aContext) );
    1620             :         OSL_ENSURE(m_xTypeConverter.is(),"NO typeconverter!");
    1621             : 
    1622           0 :         if ( nType != DataType::BOOLEAN && DataType::BIT != nType )
    1623             :         {
    1624           0 :             OUString sEmpty;
    1625           0 :             lcl_addFilterCriteria_throw(filterOperator,sEmpty,aSQL);
    1626             :         }
    1627             : 
    1628           0 :         switch(nType)
    1629             :         {
    1630             :             case DataType::VARCHAR:
    1631             :             case DataType::CHAR:
    1632             :             case DataType::LONGVARCHAR:
    1633           0 :                 aSQL.append( DBTypeConversion::toSQLString( nType, aValue, true, m_xTypeConverter ) );
    1634           0 :                 break;
    1635             :             case DataType::CLOB:
    1636             :                 {
    1637           0 :                     Reference< XClob > xClob(aValue,UNO_QUERY);
    1638           0 :                     if ( xClob.is() )
    1639             :                     {
    1640           0 :                         const ::sal_Int64 nLength = xClob->length();
    1641           0 :                         if ( sal_Int64(nLength + aSQL.getLength() + STR_LIKE.getLength() ) < sal_Int64(SAL_MAX_INT32) )
    1642             :                         {
    1643           0 :                             aSQL.append("'" + xClob->getSubString(1,(sal_Int32)nLength) + "'");
    1644             :                         }
    1645             :                     }
    1646             :                     else
    1647             :                     {
    1648           0 :                         aSQL.append( DBTypeConversion::toSQLString( nType, aValue, true, m_xTypeConverter ) );
    1649           0 :                     }
    1650             :                 }
    1651           0 :                 break;
    1652             :             case DataType::VARBINARY:
    1653             :             case DataType::BINARY:
    1654             :             case DataType::LONGVARBINARY:
    1655             :                 {
    1656           0 :                     Sequence<sal_Int8> aSeq;
    1657           0 :                     if(aValue >>= aSeq)
    1658             :                     {
    1659           0 :                         if(nSearchable == ColumnSearch::CHAR)
    1660             :                         {
    1661           0 :                             aSQL.append( "\'" );
    1662             :                         }
    1663           0 :                         aSQL.appendAscii( "0x" );
    1664           0 :                         const sal_Int8* pBegin  = aSeq.getConstArray();
    1665           0 :                         const sal_Int8* pEnd    = pBegin + aSeq.getLength();
    1666           0 :                         for(;pBegin != pEnd;++pBegin)
    1667             :                         {
    1668           0 :                             aSQL.append( (sal_Int32)*pBegin, 16 ).getStr();
    1669             :                         }
    1670           0 :                         if(nSearchable == ColumnSearch::CHAR)
    1671           0 :                             aSQL.append( "\'" );
    1672             :                     }
    1673             :                     else
    1674           0 :                         throw SQLException(DBACORE_RESSTRING(RID_STR_NOT_SEQUENCE_INT8),*this,SQLSTATE_GENERAL,1000,Any() );
    1675             :                 }
    1676           0 :                 break;
    1677             :             case DataType::BIT:
    1678             :             case DataType::BOOLEAN:
    1679             :                 {
    1680           0 :                     sal_Bool bValue = sal_False;
    1681           0 :                     m_xTypeConverter->convertToSimpleType(aValue, TypeClass_BOOLEAN) >>= bValue;
    1682             : 
    1683           0 :                     OUString sColumnExp = aSQL.makeStringAndClear();
    1684           0 :                     getBooleanComparisonPredicate( sColumnExp, bValue, m_nBoolCompareMode, aSQL );
    1685             :                 }
    1686           0 :                 break;
    1687             :             default:
    1688           0 :                 aSQL.append( DBTypeConversion::toSQLString( nType, aValue, true, m_xTypeConverter ) );
    1689           0 :                 break;
    1690             :         }
    1691             :     }
    1692             :     else
    1693             :     {
    1694           0 :         sal_Int32 nFilterOp = filterOperator;
    1695           0 :         if ( filterOperator != SQLFilterOperator::SQLNULL && filterOperator != SQLFilterOperator::NOT_SQLNULL )
    1696           0 :             nFilterOp = SQLFilterOperator::SQLNULL;
    1697           0 :         OUString sEmpty;
    1698           0 :         lcl_addFilterCriteria_throw(nFilterOp,sEmpty,aSQL);
    1699             :     }
    1700             : 
    1701             :     // Attach filter
    1702             :     // Construct SELECT without WHERE and ORDER BY
    1703           0 :     OUString sFilter = getFilter();
    1704             : 
    1705           0 :     if ( !sFilter.isEmpty() && !aSQL.isEmpty() )
    1706             :     {
    1707           0 :         OUString sTemp(L_BRACKET + sFilter + R_BRACKET);
    1708           0 :         sTemp += andCriteria ? OUString(STR_AND) : OUString(STR_OR);
    1709           0 :         sFilter = sTemp;
    1710             :     }
    1711           0 :     sFilter += aSQL.makeStringAndClear();
    1712             : 
    1713             :     // add the filter and the sort order
    1714           0 :     _aSetFunctor(this,sFilter);
    1715           0 : }
    1716             : 
    1717           0 : Sequence< Sequence< PropertyValue > > OSingleSelectQueryComposer::getStructuredCondition( TGetParseNode& _aGetFunctor )
    1718             : {
    1719             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getStructuredCondition" );
    1720           0 :     ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
    1721             : 
    1722           0 :     MutexGuard aGuard(m_aMutex);
    1723             : 
    1724           0 :     Sequence< Sequence< PropertyValue > > aFilterSeq;
    1725           0 :     OUString sFilter = getStatementPart( _aGetFunctor, m_aAdditiveIterator );
    1726             : 
    1727           0 :     if ( !sFilter.isEmpty() )
    1728             :     {
    1729           0 :         OUString aSql(m_aPureSelectSQL + STR_WHERE + sFilter);
    1730             :         // build a temporary parse node
    1731           0 :         const OSQLParseNode* pTempNode = m_aAdditiveIterator.getParseTree();
    1732             : 
    1733           0 :         OUString aErrorMsg;
    1734           0 :         boost::scoped_ptr<OSQLParseNode> pSqlParseNode( m_aSqlParser.parseTree(aErrorMsg,aSql));
    1735           0 :         if ( pSqlParseNode.get() )
    1736             :         {
    1737           0 :             m_aAdditiveIterator.setParseTree(pSqlParseNode.get());
    1738             :             // normalize the filter
    1739           0 :             OSQLParseNode* pWhereNode = const_cast<OSQLParseNode*>(m_aAdditiveIterator.getWhereTree());
    1740             : 
    1741           0 :             OSQLParseNode* pCondition = pWhereNode->getChild(1);
    1742             :         #if OSL_DEBUG_LEVEL > 0
    1743             :             OUString sCondition;
    1744             :             pCondition->parseNodeToStr( sCondition, m_xConnection );
    1745             :         #endif
    1746           0 :             OSQLParseNode::negateSearchCondition(pCondition);
    1747             : 
    1748           0 :             pCondition = pWhereNode->getChild(1);
    1749             :         #if OSL_DEBUG_LEVEL > 0
    1750             :             sCondition = OUString();
    1751             :             pCondition->parseNodeToStr( sCondition, m_xConnection );
    1752             :         #endif
    1753           0 :             OSQLParseNode::disjunctiveNormalForm(pCondition);
    1754             : 
    1755           0 :             pCondition = pWhereNode->getChild(1);
    1756             :         #if OSL_DEBUG_LEVEL > 0
    1757             :             sCondition = OUString();
    1758             :             pCondition->parseNodeToStr( sCondition, m_xConnection );
    1759             :         #endif
    1760           0 :             OSQLParseNode::absorptions(pCondition);
    1761             : 
    1762           0 :             pCondition = pWhereNode->getChild(1);
    1763             :         #if OSL_DEBUG_LEVEL > 0
    1764             :             sCondition = OUString();
    1765             :             pCondition->parseNodeToStr( sCondition, m_xConnection );
    1766             :         #endif
    1767           0 :             if ( pCondition )
    1768             :             {
    1769           0 :                 ::std::vector< ::std::vector < PropertyValue > > aFilters;
    1770           0 :                 Reference< XNumberFormatter > xFormatter( NumberFormatter::create(m_aContext), UNO_QUERY_THROW );
    1771           0 :                 xFormatter->attachNumberFormatsSupplier( m_xNumberFormatsSupplier );
    1772             : 
    1773           0 :                 if (setORCriteria(pCondition, m_aAdditiveIterator, aFilters, xFormatter))
    1774             :                 {
    1775           0 :                     aFilterSeq.realloc(aFilters.size());
    1776           0 :                     Sequence<PropertyValue>* pFilters = aFilterSeq.getArray();
    1777           0 :                     ::std::vector< ::std::vector < PropertyValue > >::const_iterator aEnd = aFilters.end();
    1778           0 :                     ::std::vector< ::std::vector < PropertyValue > >::const_iterator i = aFilters.begin();
    1779           0 :                     for ( ; i != aEnd ; ++i)
    1780             :                     {
    1781           0 :                         const ::std::vector < PropertyValue >& rProperties = *i;
    1782           0 :                         pFilters->realloc(rProperties.size());
    1783           0 :                         PropertyValue* pFilter = pFilters->getArray();
    1784           0 :                         ::std::vector < PropertyValue >::const_iterator j = rProperties.begin();
    1785           0 :                         ::std::vector < PropertyValue >::const_iterator aEnd2 = rProperties.end();
    1786           0 :                         for ( ; j != aEnd2 ; ++j)
    1787             :                         {
    1788           0 :                             *pFilter = *j;
    1789           0 :                             ++pFilter;
    1790             :                         }
    1791           0 :                         ++pFilters;
    1792             :                     }
    1793           0 :                 }
    1794             :             }
    1795             :             // restore
    1796           0 :             m_aAdditiveIterator.setParseTree(pTempNode);
    1797           0 :         }
    1798             :     }
    1799           0 :     return aFilterSeq;
    1800             : }
    1801             : 
    1802           0 : OUString OSingleSelectQueryComposer::getKeyword( SQLPart _ePart ) const
    1803             : {
    1804             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getKeyword" );
    1805           0 :     OUString sKeyword;
    1806           0 :     switch(_ePart)
    1807             :     {
    1808             :         default:
    1809             :             SAL_WARN("dbaccess", "OSingleSelectQueryComposer::getKeyWord: Invalid enum value!" );
    1810             :             // no break, fallback to WHERE
    1811             :         case Where:
    1812           0 :             sKeyword = STR_WHERE;
    1813           0 :             break;
    1814             :         case Group:
    1815           0 :             sKeyword = STR_GROUP_BY;
    1816           0 :             break;
    1817             :         case Having:
    1818           0 :             sKeyword = STR_HAVING;
    1819           0 :             break;
    1820             :         case Order:
    1821           0 :             sKeyword = STR_ORDER_BY;
    1822           0 :             break;
    1823             :     }
    1824           0 :     return sKeyword;
    1825             : }
    1826             : 
    1827           0 : OUString OSingleSelectQueryComposer::getSQLPart( SQLPart _ePart, OSQLParseTreeIterator& _rIterator, sal_Bool _bWithKeyword )
    1828             : {
    1829             :     SAL_INFO("dbaccess", "OSingleSelectQueryComposer::getSQLPart" );
    1830           0 :     TGetParseNode F_tmp(&OSQLParseTreeIterator::getSimpleWhereTree);
    1831           0 :     OUString sKeyword( getKeyword( _ePart ) );
    1832           0 :     switch(_ePart)
    1833             :     {
    1834             :         case Where:
    1835           0 :             F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleWhereTree);
    1836           0 :             break;
    1837             :         case Group:
    1838           0 :             F_tmp = TGetParseNode (&OSQLParseTreeIterator::getSimpleGroupByTree);
    1839           0 :             break;
    1840             :         case Having:
    1841           0 :             F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleHavingTree);
    1842           0 :             break;
    1843             :         case Order:
    1844           0 :             F_tmp = TGetParseNode(&OSQLParseTreeIterator::getSimpleOrderTree);
    1845           0 :             break;
    1846             :         default:
    1847             :             SAL_WARN("dbaccess","Invalid enum value!");
    1848             :     }
    1849             : 
    1850           0 :     OUString sRet = getStatementPart( F_tmp, _rIterator );
    1851           0 :     if ( _bWithKeyword && !sRet.isEmpty() )
    1852           0 :         sRet = sKeyword + sRet;
    1853           0 :     return sRet;
    1854             : }
    1855             : 
    1856             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10