LCOV - code coverage report
Current view: top level - connectivity/source/commontools - sqlerror.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 10 96 10.4 %
Date: 2012-08-25 Functions: 4 25 16.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 108 3.7 %

           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                 :            : 
      21                 :            : #include "connectivity/sqlerror.hxx"
      22                 :            : 
      23                 :            : #include <com/sun/star/sdbc/SQLException.hpp>
      24                 :            : 
      25                 :            : #include <comphelper/officeresourcebundle.hxx>
      26                 :            : #include <cppuhelper/exc_hlp.hxx>
      27                 :            : #include <rtl/ustrbuf.hxx>
      28                 :            : 
      29                 :            : #include <string.h>
      30                 :            : 
      31                 :            : //........................................................................
      32                 :            : namespace connectivity
      33                 :            : {
      34                 :            : //........................................................................
      35                 :            : 
      36                 :            :     /** === begin UNO using === **/
      37                 :            :     using ::com::sun::star::uno::Reference;
      38                 :            :     using ::com::sun::star::uno::UNO_QUERY;
      39                 :            :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      40                 :            :     using ::com::sun::star::uno::Exception;
      41                 :            :     using ::com::sun::star::uno::RuntimeException;
      42                 :            :     using ::com::sun::star::uno::Any;
      43                 :            :     using ::com::sun::star::uno::makeAny;
      44                 :            :     using ::com::sun::star::uno::XInterface;
      45                 :            :     using ::com::sun::star::sdbc::SQLException;
      46                 :            :     using ::com::sun::star::uno::Type;
      47                 :            :     /** === end UNO using === **/
      48                 :            : 
      49                 :            :     //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
      50                 :            :     namespace
      51                 :            :     {
      52                 :            :         typedef SQLError::ParamValue ParamValue;
      53                 :            :     }
      54                 :            : 
      55                 :            :     //====================================================================
      56                 :            :     //= SQLError_Impl - declaration
      57                 :            :     //====================================================================
      58                 :            :     class SQLError_Impl
      59                 :            :     {
      60                 :            :     public:
      61                 :            :         SQLError_Impl( const ::comphelper::ComponentContext& _rContext );
      62                 :            :         ~SQLError_Impl();
      63                 :            : 
      64                 :            :         // versions of the public SQLError methods which are just delegated to this impl-class
      65                 :            :         static const ::rtl::OUString& getMessagePrefix();
      66                 :            :         ::rtl::OUString     getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      67                 :            :         ::rtl::OUString     getSQLState( const ErrorCondition _eCondition );
      68                 :            :         static ErrorCode    getErrorCode( const ErrorCondition _eCondition );
      69                 :            :         void                raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      70                 :            :         void                raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      71                 :            :         void                raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      72                 :            :         SQLException        getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      73                 :            : 
      74                 :            :     private:
      75                 :            :         /// returns the basic error message associated with the given error condition, without any parameter replacements
      76                 :            :         ::rtl::OUString
      77                 :            :                 impl_getErrorMessage( const ErrorCondition& _eCondition );
      78                 :            : 
      79                 :            :         /// returns the SQLState associated with the given error condition
      80                 :            :         ::rtl::OUString
      81                 :            :                 impl_getSQLState( const ErrorCondition& _eCondition );
      82                 :            : 
      83                 :            :         /// returns an SQLException describing the given error condition
      84                 :            :         SQLException
      85                 :            :                 impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
      86                 :            :                     const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
      87                 :            : 
      88                 :            :         /// initializes our resource bundle
      89                 :            :         bool    impl_initResources();
      90                 :            : 
      91                 :            :     private:
      92                 :            :         ::osl::Mutex                                            m_aMutex;
      93                 :            :         ::comphelper::ComponentContext                          m_aContext;
      94                 :            :         ::std::auto_ptr< ::comphelper::OfficeResourceBundle >   m_pResources;
      95                 :            :         bool                                                    m_bAttemptedInit;
      96                 :            :     };
      97                 :            : 
      98                 :            :     //====================================================================
      99                 :            :     //= SQLError_Impl - implementation
     100                 :            :     //====================================================================
     101                 :            :     //--------------------------------------------------------------------
     102                 :       2306 :     SQLError_Impl::SQLError_Impl( const ::comphelper::ComponentContext& _rContext )
     103                 :            :         :m_aContext( _rContext )
     104                 :            :         ,m_pResources( )
     105         [ +  - ]:       2306 :         ,m_bAttemptedInit( false )
     106                 :            :     {
     107                 :       2306 :     }
     108                 :            : 
     109                 :            :     //--------------------------------------------------------------------
     110 [ +  - ][ +  - ]:       2299 :     SQLError_Impl::~SQLError_Impl()
     111                 :            :     {
     112                 :       2299 :     }
     113                 :            : 
     114                 :            :     //--------------------------------------------------------------------
     115                 :          0 :     const ::rtl::OUString& SQLError_Impl::getMessagePrefix()
     116                 :            :     {
     117 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString s_sMessagePrefix( RTL_CONSTASCII_USTRINGPARAM( "[OOoBase]" ) );
         [ #  # ][ #  # ]
     118                 :          0 :         return s_sMessagePrefix;
     119                 :            :     }
     120                 :            : 
     121                 :            :     //--------------------------------------------------------------------
     122                 :            :     namespace
     123                 :            :     {
     124                 :            :         //................................................................
     125                 :            :         /** substitutes a given placeholder in the given message with the given value
     126                 :            :         */
     127                 :          0 :         void    lcl_substitutePlaceholder( ::rtl::OUString& _rMessage, const sal_Char* _pPlaceholder, ParamValue _rParamValue )
     128                 :            :         {
     129                 :          0 :             size_t nPlaceholderLen( strlen( _pPlaceholder ) );
     130                 :          0 :             sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
     131                 :            : 
     132                 :          0 :             bool bHasPlaceholder = ( nIndex != -1 );
     133                 :          0 :             bool bWantsPlaceholder = _rParamValue.is();
     134                 :            :             OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
     135                 :            : 
     136 [ #  # ][ #  # ]:          0 :             if ( bHasPlaceholder && bWantsPlaceholder )
     137                 :          0 :                 _rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *_rParamValue );
     138                 :          0 :         }
     139                 :            : 
     140                 :            :         //................................................................
     141                 :          0 :         sal_Int32   lcl_getResourceID( const ErrorCondition _eCondition, bool _bSQLState )
     142                 :            :         {
     143                 :            :             return  256
     144                 :          0 :                 +   2 * ::sal::static_int_cast< sal_Int32, ErrorCondition >( _eCondition )
     145         [ #  # ]:          0 :                 +   ( _bSQLState ? 1 : 0 );
     146                 :            :         }
     147                 :            :     }
     148                 :            : 
     149                 :            :     //--------------------------------------------------------------------
     150                 :          0 :     ::rtl::OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     151                 :            :     {
     152                 :          0 :         ::rtl::OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
     153                 :            : 
     154 [ #  # ][ #  # ]:          0 :         lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
                 [ #  # ]
     155 [ #  # ][ #  # ]:          0 :         lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
                 [ #  # ]
     156 [ #  # ][ #  # ]:          0 :         lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
                 [ #  # ]
     157                 :            : 
     158                 :          0 :         return sErrorMessage;
     159                 :            :     }
     160                 :            : 
     161                 :            :     //--------------------------------------------------------------------
     162                 :          0 :     ::rtl::OUString SQLError_Impl::getSQLState( const ErrorCondition _eCondition )
     163                 :            :     {
     164                 :          0 :         return impl_getSQLState( _eCondition );
     165                 :            :     }
     166                 :            : 
     167                 :            :     //--------------------------------------------------------------------
     168                 :          0 :     ErrorCode SQLError_Impl::getErrorCode( const ErrorCondition _eCondition )
     169                 :            :     {
     170                 :          0 :         return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
     171                 :            :     }
     172                 :            : 
     173                 :            :     //--------------------------------------------------------------------
     174                 :          0 :     void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     175                 :            :     {
     176                 :            :         raiseTypedException(
     177                 :            :             _eCondition,
     178                 :            :             _rxContext,
     179                 :          0 :             ::cppu::UnoType< SQLException >::get(),
     180                 :            :             _rParamValue1,
     181                 :            :             _rParamValue2,
     182                 :            :             _rParamValue3
     183                 :          0 :         );
     184                 :          0 :     }
     185                 :            : 
     186                 :            :     //--------------------------------------------------------------------
     187                 :          0 :     void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     188                 :            :     {
     189                 :            :         raiseTypedException(
     190                 :            :             _eCondition,
     191                 :            :             NULL,
     192                 :          0 :             ::cppu::UnoType< SQLException >::get(),
     193                 :            :             _rParamValue1,
     194                 :            :             _rParamValue2,
     195                 :            :             _rParamValue3
     196         [ #  # ]:          0 :         );
     197                 :          0 :     }
     198                 :            : 
     199                 :            :     //--------------------------------------------------------------------
     200                 :          0 :     void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
     201                 :            :         const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     202                 :            :     {
     203         [ #  # ]:          0 :         if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
     204                 :          0 :             throw ::std::bad_cast();
     205                 :            : 
     206                 :            :         // default-construct an exception of the desired type
     207                 :          0 :         Any aException( NULL, _rExceptionType );
     208                 :            : 
     209                 :            :         // fill it
     210                 :          0 :         SQLException* pException = static_cast< SQLException* >( aException.pData );
     211 [ #  # ][ #  # ]:          0 :         *pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
                 [ #  # ]
     212                 :            : 
     213                 :            :         // throw it
     214         [ #  # ]:          0 :         ::cppu::throwException( aException );
     215                 :          0 :     }
     216                 :            : 
     217                 :            :     //--------------------------------------------------------------------
     218                 :          0 :     SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
     219                 :            :         const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     220                 :            :     {
     221                 :          0 :         return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
     222                 :            :     }
     223                 :            : 
     224                 :            :     //--------------------------------------------------------------------
     225                 :          0 :     SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
     226                 :            :         const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
     227                 :            :     {
     228                 :            :         return SQLException(
     229                 :            :             getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
     230                 :            :             _rxContext,
     231                 :            :             getSQLState( _eCondition ),
     232         [ #  # ]:          0 :             getErrorCode( _eCondition ),
     233                 :            :             Any()
     234 [ #  # ][ #  # ]:          0 :         );
                 [ #  # ]
     235                 :            :     }
     236                 :            : 
     237                 :            :     //--------------------------------------------------------------------
     238                 :          0 :     ::rtl::OUString SQLError_Impl::impl_getErrorMessage( const ErrorCondition& _eCondition )
     239                 :            :     {
     240                 :          0 :         ::rtl::OUStringBuffer aMessage;
     241                 :            : 
     242 [ #  # ][ #  # ]:          0 :         if ( impl_initResources() )
     243                 :            :         {
     244 [ #  # ][ #  # ]:          0 :             ::rtl::OUString sResMessage( m_pResources->loadString( lcl_getResourceID( _eCondition, false ) ) );
     245                 :            :             OSL_ENSURE( !sResMessage.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
     246 [ #  # ][ #  # ]:          0 :             aMessage.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage );
         [ #  # ][ #  # ]
     247                 :            :         }
     248                 :            : 
     249         [ #  # ]:          0 :         return aMessage.makeStringAndClear();
     250                 :            :     }
     251                 :            : 
     252                 :            :     //--------------------------------------------------------------------
     253                 :          0 :     ::rtl::OUString SQLError_Impl::impl_getSQLState( const ErrorCondition& _eCondition )
     254                 :            :     {
     255                 :          0 :         ::rtl::OUString sState;
     256                 :            : 
     257 [ #  # ][ #  # ]:          0 :         if ( impl_initResources() )
     258                 :            :         {
     259         [ #  # ]:          0 :             sal_Int32 nResourceId( lcl_getResourceID( _eCondition, true ) );
     260 [ #  # ][ #  # ]:          0 :             if ( m_pResources->hasString( nResourceId ) )
     261         [ #  # ]:          0 :                 sState = m_pResources->loadString( nResourceId );
     262                 :            :         }
     263                 :            : 
     264         [ #  # ]:          0 :         if ( sState.isEmpty() )
     265         [ #  # ]:          0 :             sState = ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
     266                 :            : 
     267                 :          0 :         return sState;
     268                 :            :     }
     269                 :            : 
     270                 :            :     //--------------------------------------------------------------------
     271                 :          0 :     bool SQLError_Impl::impl_initResources()
     272                 :            :     {
     273         [ #  # ]:          0 :         if ( m_pResources.get() )
     274                 :          0 :             return true;
     275         [ #  # ]:          0 :         if ( m_bAttemptedInit )
     276                 :          0 :             return false;
     277                 :            : 
     278         [ #  # ]:          0 :         ::osl::MutexGuard aGuard( m_aMutex );
     279                 :          0 :         m_bAttemptedInit = true;
     280                 :            : 
     281 [ #  # ][ #  # ]:          0 :         m_pResources.reset( new ::comphelper::OfficeResourceBundle( m_aContext.getUNOContext(), "sdberr" ) );
                 [ #  # ]
     282         [ #  # ]:          0 :         return m_pResources.get() != NULL;
     283                 :            :     }
     284                 :            : 
     285                 :            :     //====================================================================
     286                 :            :     //= SQLError
     287                 :            :     //====================================================================
     288                 :            :     //--------------------------------------------------------------------
     289                 :       2306 :     SQLError::SQLError( const ::comphelper::ComponentContext& _rContext )
     290         [ +  - ]:       2306 :         :m_pImpl( new SQLError_Impl( _rContext ) )
     291                 :            :     {
     292                 :       2306 :     }
     293                 :            : 
     294                 :            :     //--------------------------------------------------------------------
     295                 :       2299 :     SQLError::~SQLError()
     296                 :            :     {
     297                 :       2299 :     }
     298                 :            : 
     299                 :            :     //--------------------------------------------------------------------
     300                 :          0 :     const ::rtl::OUString& SQLError::getMessagePrefix()
     301                 :            :     {
     302                 :          0 :         return SQLError_Impl::getMessagePrefix();
     303                 :            :     }
     304                 :            : 
     305                 :            :     //--------------------------------------------------------------------
     306                 :          0 :     ::rtl::OUString SQLError::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
     307                 :            :     {
     308                 :          0 :         return m_pImpl->getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
     309                 :            :     }
     310                 :            : 
     311                 :            :     //--------------------------------------------------------------------
     312                 :          0 :     ErrorCode SQLError::getErrorCode( const ErrorCondition _eCondition )
     313                 :            :     {
     314                 :          0 :         return SQLError_Impl::getErrorCode( _eCondition );
     315                 :            :     }
     316                 :            : 
     317                 :            :     //--------------------------------------------------------------------
     318                 :          0 :     void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
     319                 :            :     {
     320                 :          0 :         m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
     321                 :          0 :     }
     322                 :            : 
     323                 :            :     //--------------------------------------------------------------------
     324                 :          0 :     void SQLError::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
     325                 :            :     {
     326                 :          0 :         m_pImpl->raiseException( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
     327                 :          0 :     }
     328                 :            : 
     329                 :            :     //--------------------------------------------------------------------
     330                 :          0 :     void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
     331                 :            :         const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
     332                 :            :     {
     333                 :          0 :         m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, _rParamValue1, _rParamValue2, _rParamValue3 );
     334                 :          0 :     }
     335                 :            : 
     336                 :            :     //--------------------------------------------------------------------
     337                 :          0 :     SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
     338                 :            :         const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
     339                 :            :     {
     340                 :          0 :         return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
     341                 :            :     }
     342                 :            : 
     343                 :            : //........................................................................
     344                 :            : } // namespace connectivity
     345                 :            : //........................................................................
     346                 :            : 
     347                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10