bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / commontools / sqlerror.cxx
blobf5318f80cba2ec0286b7283ce19e2daf605623ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <connectivity/sqlerror.hxx>
23 #include <com/sun/star/sdbc/SQLException.hpp>
25 #include <comphelper/officeresourcebundle.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <osl/diagnose.h>
30 #include <string.h>
33 namespace connectivity
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::uno::XComponentContext;
46 using ::com::sun::star::sdbc::SQLException;
47 using ::com::sun::star::uno::Type;
49 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
50 namespace
52 typedef SQLError::ParamValue ParamValue;
56 class SQLError_Impl
58 public:
59 SQLError_Impl( const Reference<XComponentContext> & _rxContext );
60 ~SQLError_Impl();
62 // versions of the public SQLError methods which are just delegated to this impl-class
63 static const OUString& getMessagePrefix();
64 OUString getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
65 OUString getSQLState( const ErrorCondition _eCondition );
66 static ErrorCode getErrorCode( const ErrorCondition _eCondition );
67 void raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
68 void raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
69 void raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
70 SQLException getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
72 private:
73 /// returns the basic error message associated with the given error condition, without any parameter replacements
74 OUString
75 impl_getErrorMessage( const ErrorCondition& _eCondition );
77 /// returns the SQLState associated with the given error condition
78 OUString
79 impl_getSQLState( const ErrorCondition& _eCondition );
81 /// returns an SQLException describing the given error condition
82 SQLException
83 impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
84 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
86 /// initializes our resource bundle
87 bool impl_initResources();
89 private:
90 ::osl::Mutex m_aMutex;
91 Reference<XComponentContext> m_aContext;
92 ::std::unique_ptr< ::comphelper::OfficeResourceBundle > m_pResources;
93 bool m_bAttemptedInit;
96 SQLError_Impl::SQLError_Impl( const Reference<XComponentContext> & _rxContext )
97 :m_aContext( _rxContext )
98 ,m_pResources( )
99 ,m_bAttemptedInit( false )
104 SQLError_Impl::~SQLError_Impl()
109 const OUString& SQLError_Impl::getMessagePrefix()
111 static const OUString s_sMessagePrefix( "[OOoBase]" );
112 return s_sMessagePrefix;
116 namespace
119 /** substitutes a given placeholder in the given message with the given value
121 void lcl_substitutePlaceholder(OUString& _rMessage, const sal_Char* _pPlaceholder, const ParamValue& rParamValue)
123 size_t nPlaceholderLen( strlen( _pPlaceholder ) );
124 sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
126 bool bHasPlaceholder = ( nIndex != -1 );
127 bool bWantsPlaceholder = rParamValue.is();
128 OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
130 if ( bHasPlaceholder && bWantsPlaceholder )
131 _rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *rParamValue );
135 sal_Int32 lcl_getResourceID( const ErrorCondition _eCondition, bool _bSQLState )
137 return 256
138 + 2 * ::sal::static_int_cast< sal_Int32, ErrorCondition >( _eCondition )
139 + ( _bSQLState ? 1 : 0 );
144 OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
146 OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
148 lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
149 lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
150 lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
152 return sErrorMessage;
156 OUString SQLError_Impl::getSQLState( const ErrorCondition _eCondition )
158 return impl_getSQLState( _eCondition );
162 ErrorCode SQLError_Impl::getErrorCode( const ErrorCondition _eCondition )
164 return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
168 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
170 raiseTypedException(
171 _eCondition,
172 _rxContext,
173 ::cppu::UnoType< SQLException >::get(),
174 _rParamValue1,
175 _rParamValue2,
176 _rParamValue3
181 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
183 raiseTypedException(
184 _eCondition,
185 NULL,
186 ::cppu::UnoType< SQLException >::get(),
187 _rParamValue1,
188 _rParamValue2,
189 _rParamValue3
194 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
195 const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
197 if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
198 throw ::std::bad_cast();
200 // default-construct an exception of the desired type
201 Any aException( NULL, _rExceptionType );
203 // fill it
204 SQLException* pException = static_cast< SQLException* >( aException.pData );
205 *pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
207 // throw it
208 ::cppu::throwException( aException );
212 SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
213 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
215 return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
219 SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
220 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
222 return SQLException(
223 getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
224 _rxContext,
225 getSQLState( _eCondition ),
226 getErrorCode( _eCondition ),
227 Any()
232 OUString SQLError_Impl::impl_getErrorMessage( const ErrorCondition& _eCondition )
234 OUStringBuffer aMessage;
236 if ( impl_initResources() )
238 OUString sResMessage( m_pResources->loadString( lcl_getResourceID( _eCondition, false ) ) );
239 OSL_ENSURE( !sResMessage.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
240 aMessage.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage );
243 return aMessage.makeStringAndClear();
247 OUString SQLError_Impl::impl_getSQLState( const ErrorCondition& _eCondition )
249 OUString sState;
251 if ( impl_initResources() )
253 sal_Int32 nResourceId( lcl_getResourceID( _eCondition, true ) );
254 if ( m_pResources->hasString( nResourceId ) )
255 sState = m_pResources->loadString( nResourceId );
258 if ( sState.isEmpty() )
259 sState = OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
261 return sState;
265 bool SQLError_Impl::impl_initResources()
267 if ( m_pResources.get() )
268 return true;
269 if ( m_bAttemptedInit )
270 return false;
272 ::osl::MutexGuard aGuard( m_aMutex );
273 m_bAttemptedInit = true;
275 m_pResources.reset( new ::comphelper::OfficeResourceBundle( m_aContext, "sdberr" ) );
276 return m_pResources.get() != NULL;
279 SQLError::SQLError( const Reference<XComponentContext> & _rxContext )
280 :m_pImpl( new SQLError_Impl( _rxContext ) )
285 SQLError::~SQLError()
290 const OUString& SQLError::getMessagePrefix()
292 return SQLError_Impl::getMessagePrefix();
296 OUString SQLError::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
298 return m_pImpl->getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
302 ErrorCode SQLError::getErrorCode( const ErrorCondition _eCondition )
304 return SQLError_Impl::getErrorCode( _eCondition );
308 void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
310 m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
314 void SQLError::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
316 m_pImpl->raiseException( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 );
320 void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
321 const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
323 m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, _rParamValue1, _rParamValue2, _rParamValue3 );
327 SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
328 const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
330 return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
334 } // namespace connectivity
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */