update dev300-m58
[ooovba.git] / connectivity / source / commontools / sqlerror.cxx
blob0b8710c4954ea8a3ba21c89c6117f56c6dd199ed
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sqlerror.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "connectivity/sqlerror.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/sdbc/SQLException.hpp>
38 /** === end UNO includes === **/
40 #include <comphelper/officeresourcebundle.hxx>
41 #include <cppuhelper/exc_hlp.hxx>
42 #include <rtl/ustrbuf.hxx>
44 #include <string.h>
46 //........................................................................
47 namespace connectivity
49 //........................................................................
51 /** === begin UNO using === **/
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::uno::Exception;
56 using ::com::sun::star::uno::RuntimeException;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::uno::makeAny;
59 using ::com::sun::star::uno::XInterface;
60 using ::com::sun::star::sdbc::SQLException;
61 using ::com::sun::star::uno::Type;
62 /** === end UNO using === **/
64 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
65 namespace
67 typedef SQLError::ParamValue ParamValue;
70 //====================================================================
71 //= SQLError_Impl - declaration
72 //====================================================================
73 class SQLError_Impl
75 public:
76 SQLError_Impl( const ::comphelper::ComponentContext& _rContext );
77 ~SQLError_Impl();
79 // versions of the public SQLError methods which are just delegated to this impl-class
80 static const ::rtl::OUString& getMessagePrefix();
81 ::rtl::OUString getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
82 ::rtl::OUString getSQLState( const ErrorCondition _eCondition );
83 static ErrorCode getErrorCode( const ErrorCondition _eCondition );
84 void raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
85 void raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
86 void raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
87 SQLException getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
89 private:
90 /// returns the basic error message associated with the given error condition, without any parameter replacements
91 ::rtl::OUString
92 impl_getErrorMessage( const ErrorCondition& _eCondition );
94 /// returns the SQLState associated with the given error condition
95 ::rtl::OUString
96 impl_getSQLState( const ErrorCondition& _eCondition );
98 /// returns an SQLException describing the given error condition
99 SQLException
100 impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
101 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
103 /// initializes our resource bundle
104 bool impl_initResources();
106 private:
107 ::osl::Mutex m_aMutex;
108 ::comphelper::ComponentContext m_aContext;
109 ::std::auto_ptr< ::comphelper::OfficeResourceBundle > m_pResources;
110 bool m_bAttemptedInit;
113 //====================================================================
114 //= SQLError_Impl - implementation
115 //====================================================================
116 //--------------------------------------------------------------------
117 SQLError_Impl::SQLError_Impl( const ::comphelper::ComponentContext& _rContext )
118 :m_aContext( _rContext )
119 ,m_pResources( )
120 ,m_bAttemptedInit( false )
124 //--------------------------------------------------------------------
125 SQLError_Impl::~SQLError_Impl()
129 //--------------------------------------------------------------------
130 const ::rtl::OUString& SQLError_Impl::getMessagePrefix()
132 static ::rtl::OUString s_sMessagePrefix( RTL_CONSTASCII_USTRINGPARAM( "[OOoBase]" ) );
133 return s_sMessagePrefix;
136 //--------------------------------------------------------------------
137 namespace
139 //................................................................
140 /** substitutes a given placeholder in the given message with the given value
142 void lcl_substitutePlaceholder( ::rtl::OUString& _rMessage, const sal_Char* _pPlaceholder, ParamValue _rParamValue )
144 size_t nPlaceholderLen( strlen( _pPlaceholder ) );
145 sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
147 bool bHasPlaceholder = ( nIndex != -1 );
148 bool bWantsPlaceholder = _rParamValue.is();
149 OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
151 if ( bHasPlaceholder && bWantsPlaceholder )
152 _rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *_rParamValue );
155 //................................................................
156 sal_Int32 lcl_getResourceID( const ErrorCondition _eCondition, bool _bSQLState )
158 return 256
159 + 2 * ::sal::static_int_cast< sal_Int32, ErrorCondition >( _eCondition )
160 + ( _bSQLState ? 1 : 0 );
164 //--------------------------------------------------------------------
165 ::rtl::OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
167 ::rtl::OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
169 lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
170 lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
171 lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
173 return sErrorMessage;
176 //--------------------------------------------------------------------
177 ::rtl::OUString SQLError_Impl::getSQLState( const ErrorCondition _eCondition )
179 return impl_getSQLState( _eCondition );
182 //--------------------------------------------------------------------
183 ErrorCode SQLError_Impl::getErrorCode( const ErrorCondition _eCondition )
185 return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
188 //--------------------------------------------------------------------
189 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
191 raiseTypedException(
192 _eCondition,
193 _rxContext,
194 ::cppu::UnoType< SQLException >::get(),
195 _rParamValue1,
196 _rParamValue2,
197 _rParamValue3
201 //--------------------------------------------------------------------
202 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
204 raiseTypedException(
205 _eCondition,
206 NULL,
207 ::cppu::UnoType< SQLException >::get(),
208 _rParamValue1,
209 _rParamValue2,
210 _rParamValue3
214 //--------------------------------------------------------------------
215 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
216 const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
218 if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
219 throw ::std::bad_cast();
221 // default-construct an exception of the desired type
222 Any aException( NULL, _rExceptionType );
224 // fill it
225 SQLException* pException = static_cast< SQLException* >( aException.pData );
226 *pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
228 // throw it
229 ::cppu::throwException( aException );
232 //--------------------------------------------------------------------
233 SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
234 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
236 return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
239 //--------------------------------------------------------------------
240 SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
241 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
243 return SQLException(
244 getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
245 _rxContext,
246 getSQLState( _eCondition ),
247 getErrorCode( _eCondition ),
248 Any()
252 //--------------------------------------------------------------------
253 ::rtl::OUString SQLError_Impl::impl_getErrorMessage( const ErrorCondition& _eCondition )
255 ::rtl::OUStringBuffer aMessage;
257 if ( impl_initResources() )
259 ::rtl::OUString sResMessage( m_pResources->loadString( lcl_getResourceID( _eCondition, false ) ) );
260 OSL_ENSURE( sResMessage.getLength(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
261 aMessage.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage );
264 return aMessage.makeStringAndClear();
267 //--------------------------------------------------------------------
268 ::rtl::OUString SQLError_Impl::impl_getSQLState( const ErrorCondition& _eCondition )
270 ::rtl::OUString sState;
272 if ( impl_initResources() )
274 sal_Int32 nResourceId( lcl_getResourceID( _eCondition, true ) );
275 if ( m_pResources->hasString( nResourceId ) )
276 sState = m_pResources->loadString( nResourceId );
279 if ( !sState.getLength() )
280 sState = ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
282 return sState;
285 //--------------------------------------------------------------------
286 bool SQLError_Impl::impl_initResources()
288 if ( m_pResources.get() )
289 return true;
290 if ( m_bAttemptedInit )
291 return false;
293 ::osl::MutexGuard aGuard( m_aMutex );
294 m_bAttemptedInit = true;
296 m_pResources.reset( new ::comphelper::OfficeResourceBundle( m_aContext.getUNOContext(), "sdberr" ) );
297 return m_pResources.get() != NULL;
300 //====================================================================
301 //= SQLError
302 //====================================================================
303 //--------------------------------------------------------------------
304 SQLError::SQLError( const ::comphelper::ComponentContext& _rContext )
305 :m_pImpl( new SQLError_Impl( _rContext ) )
309 //--------------------------------------------------------------------
310 SQLError::~SQLError()
314 //--------------------------------------------------------------------
315 const ::rtl::OUString& SQLError::getMessagePrefix()
317 return SQLError_Impl::getMessagePrefix();
320 //--------------------------------------------------------------------
321 ::rtl::OUString SQLError::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
323 return m_pImpl->getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
326 //--------------------------------------------------------------------
327 ::rtl::OUString SQLError::getSQLState( const ErrorCondition _eCondition ) const
329 return m_pImpl->getSQLState( _eCondition );
332 //--------------------------------------------------------------------
333 ErrorCode SQLError::getErrorCode( const ErrorCondition _eCondition )
335 return SQLError_Impl::getErrorCode( _eCondition );
338 //--------------------------------------------------------------------
339 void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
341 m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
344 //--------------------------------------------------------------------
345 void SQLError::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
347 m_pImpl->raiseException( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
350 //--------------------------------------------------------------------
351 void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
352 const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
354 m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, _rParamValue1, _rParamValue2, _rParamValue3 );
357 //--------------------------------------------------------------------
358 SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
359 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
361 return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
364 //........................................................................
365 } // namespace connectivity
366 //........................................................................