LCOV - code coverage report
Current view: top level - connectivity/source/commontools - statementcomposer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 63 99 63.6 %
Date: 2012-08-25 Functions: 11 11 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 35 179 19.6 %

           Branch data     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                 :         32 :     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                 :         32 :         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                 :         32 :             ,bDisposeComposer( true )
      80                 :            :         {
      81         [ -  + ]:         32 :             if ( !_rxConnection.is() )
      82         [ #  # ]:          0 :                 throw NullPointerException();
      83                 :         32 :         }
      84                 :            :     };
      85                 :            : 
      86                 :            :     //--------------------------------------------------------------------
      87                 :            :     namespace
      88                 :            :     {
      89                 :            :         //----------------------------------------------------------------
      90                 :         64 :         void    lcl_resetComposer( StatementComposer_Data& _rData )
      91                 :            :         {
      92 [ +  + ][ -  + ]:         64 :             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                 :         64 :             _rData.xComposer.clear();
     105                 :         64 :         }
     106                 :            : 
     107                 :            :         //----------------------------------------------------------------
     108                 :         64 :         bool    lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
     109                 :            :         {
     110         [ +  + ]:         64 :             if ( !_rData.bComposerDirty )
     111                 :         32 :                 return _rData.xComposer.is();
     112                 :         32 :             lcl_resetComposer( _rData );
     113                 :            : 
     114                 :            :             try
     115                 :            :             {
     116                 :         32 :                 ::rtl::OUString sStatement;
     117   [ +  +  -  - ]:         32 :                 switch ( _rData.nCommandType )
     118                 :            :                 {
     119                 :            :                     case CommandType::COMMAND:
     120         [ +  - ]:          6 :                         if ( _rData.bEscapeProcessing )
     121                 :          6 :                             sStatement = _rData.sCommand;
     122                 :            :                         // (in case of no escape processing  we assume a not parseable statement)
     123                 :          6 :                         break;
     124                 :            : 
     125                 :            :                     case CommandType::TABLE:
     126                 :            :                     {
     127         [ +  - ]:         26 :                         if ( _rData.sCommand.isEmpty() )
     128                 :            :                             break;
     129                 :            : 
     130         [ +  - ]:         26 :                         sStatement = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SELECT * FROM " ) );
     131                 :            : 
     132                 :         26 :                         ::rtl::OUString sCatalog, sSchema, sTable;
     133 [ +  - ][ +  - ]:         26 :                         qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, eInDataManipulation );
                 [ +  - ]
     134                 :            : 
     135         [ +  - ]:         26 :                         sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
     136                 :            :                     }
     137                 :         26 :                     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         [ +  - ]:         32 :                 if ( !sStatement.isEmpty() )
     207                 :            :                 {
     208                 :            :                     // create an composer
     209         [ +  - ]:         32 :                     Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
     210         [ +  - ]:         32 :                     Reference< XSingleSelectQueryComposer > xComposer( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
     211 [ +  - ][ +  - ]:         32 :                         UNO_QUERY_THROW );
                 [ +  - ]
     212 [ +  - ][ +  - ]:         32 :                     xComposer->setElementaryQuery( sStatement );
     213                 :            : 
     214                 :            :                     // append sort/filter
     215 [ +  - ][ +  - ]:         32 :                     xComposer->setOrder( _rData.sOrder );
     216 [ +  - ][ +  - ]:         32 :                     xComposer->setFilter( _rData.sFilter );
     217                 :            : 
     218 [ +  - ][ +  - ]:         32 :                     sStatement = xComposer->getQuery();
     219                 :            : 
     220         [ +  - ]:         32 :                     _rData.xComposer = xComposer;
     221                 :         32 :                     _rData.bComposerDirty = false;
     222      [ #  #  # ]:         32 :                 }
     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                 :         64 :             return _rData.xComposer.is();
     234                 :            :         }
     235                 :            :     }
     236                 :            : 
     237                 :            :     //====================================================================
     238                 :            :     //= StatementComposer
     239                 :            :     //====================================================================
     240                 :            :     //--------------------------------------------------------------------
     241                 :         32 :     StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
     242                 :            :         const ::rtl::OUString&  _rCommand, const sal_Int32 _nCommandType, const sal_Bool _bEscapeProcessing )
     243 [ +  - ][ +  - ]:         32 :         :m_pData( new StatementComposer_Data( _rxConnection ) )
     244                 :            :     {
     245                 :            :         OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
     246                 :         32 :         m_pData->sCommand = _rCommand;
     247                 :         32 :         m_pData->nCommandType = _nCommandType;
     248                 :         32 :         m_pData->bEscapeProcessing = _bEscapeProcessing;
     249                 :         32 :     }
     250                 :            : 
     251                 :            :     //--------------------------------------------------------------------
     252                 :         32 :     StatementComposer::~StatementComposer()
     253                 :            :     {
     254         [ +  - ]:         32 :         lcl_resetComposer( *m_pData );
     255                 :         32 :     }
     256                 :            : 
     257                 :            :     //--------------------------------------------------------------------
     258                 :         32 :     void StatementComposer::setDisposeComposer( bool _bDoDispose )
     259                 :            :     {
     260                 :         32 :         m_pData->bDisposeComposer = _bDoDispose;
     261                 :         32 :     }
     262                 :            : 
     263                 :            :     //--------------------------------------------------------------------
     264                 :         22 :     void StatementComposer::setFilter( const ::rtl::OUString& _rFilter )
     265                 :            :     {
     266                 :         22 :         m_pData->sFilter = _rFilter;
     267                 :         22 :         m_pData->bComposerDirty = true;
     268                 :         22 :     }
     269                 :            : 
     270                 :            :     //--------------------------------------------------------------------
     271                 :         32 :     void StatementComposer::setOrder( const ::rtl::OUString& _rOrder )
     272                 :            :     {
     273                 :         32 :         m_pData->sOrder = _rOrder;
     274                 :         32 :         m_pData->bComposerDirty = true;
     275                 :         32 :     }
     276                 :            : 
     277                 :            :     //--------------------------------------------------------------------
     278                 :         32 :     Reference< XSingleSelectQueryComposer > StatementComposer::getComposer()
     279                 :            :     {
     280                 :         32 :         lcl_ensureUpToDateComposer_nothrow( *m_pData );
     281                 :         32 :         return m_pData->xComposer;
     282                 :            :     }
     283                 :            : 
     284                 :            :     //--------------------------------------------------------------------
     285                 :         32 :     ::rtl::OUString StatementComposer::getQuery()
     286                 :            :     {
     287         [ +  - ]:         32 :         if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
     288                 :            :         {
     289                 :         32 :             return m_pData->xComposer->getQuery();
     290                 :            :         }
     291                 :            : 
     292                 :         32 :         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