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

Generated by: LCOV version 1.10