LCOV - code coverage report
Current view: top level - libreoffice/connectivity/source/commontools - statementcomposer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 99 0.0 %
Date: 2012-12-27 Functions: 0 11 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 <connectivity/statementcomposer.hxx>
      21             : 
      22             : #include <connectivity/dbtools.hxx>
      23             : 
      24             : #include <com/sun/star/sdb/CommandType.hpp>
      25             : #include <com/sun/star/lang/NullPointerException.hpp>
      26             : #include <com/sun/star/lang/XComponent.hpp>
      27             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      28             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      29             : 
      30             : #include <unotools/sharedunocomponent.hxx>
      31             : #include <tools/diagnose_ex.h>
      32             : #include <comphelper/property.hxx>
      33             : 
      34             : //........................................................................
      35             : namespace dbtools
      36             : {
      37             : //........................................................................
      38             : 
      39             :     /** === begin UNO using === **/
      40             :     using ::com::sun::star::uno::Reference;
      41             :     using ::com::sun::star::sdbc::XConnection;
      42             :     using ::com::sun::star::sdb::XSingleSelectQueryComposer;
      43             :     using ::com::sun::star::lang::NullPointerException;
      44             :     using ::com::sun::star::uno::Exception;
      45             :     using ::com::sun::star::lang::XComponent;
      46             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      47             :     using ::com::sun::star::sdb::XQueriesSupplier;
      48             :     using ::com::sun::star::container::XNameAccess;
      49             :     using ::com::sun::star::uno::UNO_QUERY;
      50             :     using ::com::sun::star::beans::XPropertySet;
      51             :     using ::com::sun::star::lang::XMultiServiceFactory;
      52             :     using ::com::sun::star::sdbc::SQLException;
      53             :     /** === end UNO using === **/
      54             :     namespace CommandType = ::com::sun::star::sdb::CommandType;
      55             : 
      56             :     //====================================================================
      57             :     //= StatementComposer_Data
      58             :     //====================================================================
      59           0 :     struct StatementComposer_Data
      60             :     {
      61             :         const Reference< XConnection >          xConnection;
      62             :         Reference< XSingleSelectQueryComposer > xComposer;
      63             :         ::rtl::OUString                         sCommand;
      64             :         ::rtl::OUString                         sFilter;
      65             :         ::rtl::OUString                         sOrder;
      66             :         sal_Int32                               nCommandType;
      67             :         sal_Bool                                bEscapeProcessing;
      68             :         bool                                    bComposerDirty;
      69             :         bool                                    bDisposeComposer;
      70             : 
      71           0 :         StatementComposer_Data( const Reference< XConnection >& _rxConnection )
      72             :             :xConnection( _rxConnection )
      73             :             ,sCommand()
      74             :             ,sFilter()
      75             :             ,sOrder()
      76             :             ,nCommandType( CommandType::COMMAND )
      77             :             ,bEscapeProcessing( sal_True )
      78             :             ,bComposerDirty( true )
      79           0 :             ,bDisposeComposer( true )
      80             :         {
      81           0 :             if ( !_rxConnection.is() )
      82           0 :                 throw NullPointerException();
      83           0 :         }
      84             :     };
      85             : 
      86             :     //--------------------------------------------------------------------
      87             :     namespace
      88             :     {
      89             :         //----------------------------------------------------------------
      90           0 :         void    lcl_resetComposer( StatementComposer_Data& _rData )
      91             :         {
      92           0 :             if ( _rData.bDisposeComposer && _rData.xComposer.is() )
      93             :             {
      94             :                 try
      95             :                 {
      96           0 :                     Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
      97           0 :                     xComposerComponent->dispose();
      98             :                 }
      99           0 :                 catch( const Exception& )
     100             :                 {
     101             :                     DBG_UNHANDLED_EXCEPTION();
     102             :                 }
     103             :             }
     104           0 :             _rData.xComposer.clear();
     105           0 :         }
     106             : 
     107             :         //----------------------------------------------------------------
     108           0 :         bool    lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
     109             :         {
     110           0 :             if ( !_rData.bComposerDirty )
     111           0 :                 return _rData.xComposer.is();
     112           0 :             lcl_resetComposer( _rData );
     113             : 
     114             :             try
     115             :             {
     116           0 :                 ::rtl::OUString sStatement;
     117           0 :                 switch ( _rData.nCommandType )
     118             :                 {
     119             :                     case CommandType::COMMAND:
     120           0 :                         if ( _rData.bEscapeProcessing )
     121           0 :                             sStatement = _rData.sCommand;
     122             :                         // (in case of no escape processing  we assume a not parseable statement)
     123           0 :                         break;
     124             : 
     125             :                     case CommandType::TABLE:
     126             :                     {
     127           0 :                         if ( _rData.sCommand.isEmpty() )
     128             :                             break;
     129             : 
     130           0 :                         sStatement = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SELECT * FROM " ) );
     131             : 
     132           0 :                         ::rtl::OUString sCatalog, sSchema, sTable;
     133           0 :                         qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, eInDataManipulation );
     134             : 
     135           0 :                         sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
     136             :                     }
     137           0 :                     break;
     138             : 
     139             :                     case CommandType::QUERY:
     140             :                     {
     141             :                         // ask the connection for the query
     142           0 :                         Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
     143           0 :                         Reference< XNameAccess >      xQueries( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
     144             : 
     145           0 :                         if ( !xQueries->hasByName( _rData.sCommand ) )
     146             :                             break;
     147             : 
     148           0 :                         Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
     149             : 
     150             :                         //  a native query ?
     151           0 :                         sal_Bool bQueryEscapeProcessing = sal_False;
     152           0 :                         xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EscapeProcessing" ) ) ) >>= bQueryEscapeProcessing;
     153           0 :                         if ( !bQueryEscapeProcessing )
     154             :                             break;
     155             : 
     156             :                         // the command used by the query
     157           0 :                         xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Command" ) ) ) >>= sStatement;
     158           0 :                         if ( sStatement.isEmpty() )
     159             :                             break;
     160             : 
     161             :                         // use a composer to build a statement from the query filter/order props
     162           0 :                         Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
     163           0 :                         ::utl::SharedUNOComponent< XSingleSelectQueryComposer > xComposer;
     164             :                         xComposer.set(
     165           0 :                             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
     166             :                             UNO_QUERY_THROW
     167           0 :                         );
     168             : 
     169             :                         // the "basic" statement
     170           0 :                         xComposer->setElementaryQuery( sStatement );
     171             : 
     172             :                         // the sort order
     173           0 :                         const ::rtl::OUString sPropOrder( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Order" ) ) );
     174           0 :                         if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
     175             :                         {
     176           0 :                             ::rtl::OUString sOrder;
     177           0 :                             OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
     178           0 :                             xComposer->setOrder( sOrder );
     179             :                         }
     180             : 
     181             :                         // the filter
     182           0 :                         sal_Bool bApplyFilter = sal_True;
     183           0 :                         const ::rtl::OUString sPropApply( RTL_CONSTASCII_USTRINGPARAM( "ApplyFilter" ));
     184           0 :                         if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
     185             :                         {
     186           0 :                             OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
     187             :                         }
     188             : 
     189           0 :                         if ( bApplyFilter )
     190             :                         {
     191           0 :                             ::rtl::OUString sFilter;
     192           0 :                             OSL_VERIFY( xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Filter" ) ) ) >>= sFilter );
     193           0 :                             xComposer->setFilter( sFilter );
     194             :                         }
     195             : 
     196             :                         // the composed statement
     197           0 :                         sStatement = xComposer->getQuery();
     198             :                     }
     199           0 :                     break;
     200             : 
     201             :                     default:
     202             :                         OSL_FAIL("lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
     203           0 :                         break;
     204             :                 }
     205             : 
     206           0 :                 if ( !sStatement.isEmpty() )
     207             :                 {
     208             :                     // create an composer
     209           0 :                     Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
     210           0 :                     Reference< XSingleSelectQueryComposer > xComposer( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
     211           0 :                         UNO_QUERY_THROW );
     212           0 :                     xComposer->setElementaryQuery( sStatement );
     213             : 
     214             :                     // append sort/filter
     215           0 :                     xComposer->setOrder( _rData.sOrder );
     216           0 :                     xComposer->setFilter( _rData.sFilter );
     217             : 
     218           0 :                     sStatement = xComposer->getQuery();
     219             : 
     220           0 :                     _rData.xComposer = xComposer;
     221           0 :                     _rData.bComposerDirty = false;
     222           0 :                 }
     223             :             }
     224           0 :             catch( const SQLException& )
     225             :             {
     226             :                 // allowed to leave here
     227             :             }
     228           0 :             catch( const Exception& )
     229             :             {
     230             :                 DBG_UNHANDLED_EXCEPTION();
     231             :             }
     232             : 
     233           0 :             return _rData.xComposer.is();
     234             :         }
     235             :     }
     236             : 
     237             :     //====================================================================
     238             :     //= StatementComposer
     239             :     //====================================================================
     240             :     //--------------------------------------------------------------------
     241           0 :     StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
     242             :         const ::rtl::OUString&  _rCommand, const sal_Int32 _nCommandType, const sal_Bool _bEscapeProcessing )
     243           0 :         :m_pData( new StatementComposer_Data( _rxConnection ) )
     244             :     {
     245             :         OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
     246           0 :         m_pData->sCommand = _rCommand;
     247           0 :         m_pData->nCommandType = _nCommandType;
     248           0 :         m_pData->bEscapeProcessing = _bEscapeProcessing;
     249           0 :     }
     250             : 
     251             :     //--------------------------------------------------------------------
     252           0 :     StatementComposer::~StatementComposer()
     253             :     {
     254           0 :         lcl_resetComposer( *m_pData );
     255           0 :     }
     256             : 
     257             :     //--------------------------------------------------------------------
     258           0 :     void StatementComposer::setDisposeComposer( bool _bDoDispose )
     259             :     {
     260           0 :         m_pData->bDisposeComposer = _bDoDispose;
     261           0 :     }
     262             : 
     263             :     //--------------------------------------------------------------------
     264           0 :     void StatementComposer::setFilter( const ::rtl::OUString& _rFilter )
     265             :     {
     266           0 :         m_pData->sFilter = _rFilter;
     267           0 :         m_pData->bComposerDirty = true;
     268           0 :     }
     269             : 
     270             :     //--------------------------------------------------------------------
     271           0 :     void StatementComposer::setOrder( const ::rtl::OUString& _rOrder )
     272             :     {
     273           0 :         m_pData->sOrder = _rOrder;
     274           0 :         m_pData->bComposerDirty = true;
     275           0 :     }
     276             : 
     277             :     //--------------------------------------------------------------------
     278           0 :     Reference< XSingleSelectQueryComposer > StatementComposer::getComposer()
     279             :     {
     280           0 :         lcl_ensureUpToDateComposer_nothrow( *m_pData );
     281           0 :         return m_pData->xComposer;
     282             :     }
     283             : 
     284             :     //--------------------------------------------------------------------
     285           0 :     ::rtl::OUString StatementComposer::getQuery()
     286             :     {
     287           0 :         if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
     288             :         {
     289           0 :             return m_pData->xComposer->getQuery();
     290             :         }
     291             : 
     292           0 :         return ::rtl::OUString();
     293             :     }
     294             : 
     295             : //........................................................................
     296             : } // namespace dbtools
     297             : //........................................................................
     298             : 
     299             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10