1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
32 namespace connectivity
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
;
48 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
51 typedef SQLError::ParamValue ParamValue
;
55 //= SQLError_Impl - declaration
60 SQLError_Impl( const Reference
<XComponentContext
> & _rxContext
);
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
);
74 /// returns the basic error message associated with the given error condition, without any parameter replacements
76 impl_getErrorMessage( const ErrorCondition
& _eCondition
);
78 /// returns the SQLState associated with the given error condition
80 impl_getSQLState( const ErrorCondition
& _eCondition
);
82 /// returns an SQLException describing the given error condition
84 impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
85 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
87 /// initializes our resource bundle
88 bool impl_initResources();
91 ::osl::Mutex m_aMutex
;
92 Reference
<XComponentContext
> m_aContext
;
93 ::std::auto_ptr
< ::comphelper::OfficeResourceBundle
> m_pResources
;
94 bool m_bAttemptedInit
;
98 //= SQLError_Impl - implementation
101 SQLError_Impl::SQLError_Impl( const Reference
<XComponentContext
> & _rxContext
)
102 :m_aContext( _rxContext
)
104 ,m_bAttemptedInit( false )
109 SQLError_Impl::~SQLError_Impl()
114 const OUString
& SQLError_Impl::getMessagePrefix()
116 static const OUString
s_sMessagePrefix( "[OOoBase]" );
117 return s_sMessagePrefix
;
124 /** substitutes a given placeholder in the given message with the given value
126 void lcl_substitutePlaceholder( OUString
& _rMessage
, const sal_Char
* _pPlaceholder
, ParamValue _rParamValue
)
128 size_t nPlaceholderLen( strlen( _pPlaceholder
) );
129 sal_Int32 nIndex
= _rMessage
.indexOfAsciiL( _pPlaceholder
, nPlaceholderLen
);
131 bool bHasPlaceholder
= ( nIndex
!= -1 );
132 bool bWantsPlaceholder
= _rParamValue
.is();
133 OSL_ENSURE( bHasPlaceholder
== bWantsPlaceholder
, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
135 if ( bHasPlaceholder
&& bWantsPlaceholder
)
136 _rMessage
= _rMessage
.replaceAt( nIndex
, nPlaceholderLen
, *_rParamValue
);
140 sal_Int32
lcl_getResourceID( const ErrorCondition _eCondition
, bool _bSQLState
)
143 + 2 * ::sal::static_int_cast
< sal_Int32
, ErrorCondition
>( _eCondition
)
144 + ( _bSQLState
? 1 : 0 );
149 OUString
SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
151 OUString
sErrorMessage( impl_getErrorMessage( _eCondition
) );
153 lcl_substitutePlaceholder( sErrorMessage
, "$1$", _rParamValue1
);
154 lcl_substitutePlaceholder( sErrorMessage
, "$2$", _rParamValue2
);
155 lcl_substitutePlaceholder( sErrorMessage
, "$3$", _rParamValue3
);
157 return sErrorMessage
;
161 OUString
SQLError_Impl::getSQLState( const ErrorCondition _eCondition
)
163 return impl_getSQLState( _eCondition
);
167 ErrorCode
SQLError_Impl::getErrorCode( const ErrorCondition _eCondition
)
169 return 0 - ::sal::static_int_cast
< ErrorCode
, ErrorCondition
>( _eCondition
);
173 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
178 ::cppu::UnoType
< SQLException
>::get(),
186 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
191 ::cppu::UnoType
< SQLException
>::get(),
199 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
200 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
202 if ( !::cppu::UnoType
< SQLException
>::get().isAssignableFrom( _rExceptionType
) )
203 throw ::std::bad_cast();
205 // default-construct an exception of the desired type
206 Any
aException( NULL
, _rExceptionType
);
209 SQLException
* pException
= static_cast< SQLException
* >( aException
.pData
);
210 *pException
= impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
213 ::cppu::throwException( aException
);
217 SQLException
SQLError_Impl::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
218 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
220 return impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
224 SQLException
SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
225 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
228 getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
),
230 getSQLState( _eCondition
),
231 getErrorCode( _eCondition
),
237 OUString
SQLError_Impl::impl_getErrorMessage( const ErrorCondition
& _eCondition
)
239 OUStringBuffer aMessage
;
241 if ( impl_initResources() )
243 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 aMessage
.append( getMessagePrefix() ).appendAscii( " " ).append( sResMessage
);
248 return aMessage
.makeStringAndClear();
252 OUString
SQLError_Impl::impl_getSQLState( const ErrorCondition
& _eCondition
)
256 if ( impl_initResources() )
258 sal_Int32
nResourceId( lcl_getResourceID( _eCondition
, true ) );
259 if ( m_pResources
->hasString( nResourceId
) )
260 sState
= m_pResources
->loadString( nResourceId
);
263 if ( sState
.isEmpty() )
264 sState
= OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
270 bool SQLError_Impl::impl_initResources()
272 if ( m_pResources
.get() )
274 if ( m_bAttemptedInit
)
277 ::osl::MutexGuard
aGuard( m_aMutex
);
278 m_bAttemptedInit
= true;
280 m_pResources
.reset( new ::comphelper::OfficeResourceBundle( m_aContext
, "sdberr" ) );
281 return m_pResources
.get() != NULL
;
288 SQLError::SQLError( const Reference
<XComponentContext
> & _rxContext
)
289 :m_pImpl( new SQLError_Impl( _rxContext
) )
294 SQLError::~SQLError()
299 const OUString
& SQLError::getMessagePrefix()
301 return SQLError_Impl::getMessagePrefix();
305 OUString
SQLError::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
307 return m_pImpl
->getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
311 ErrorCode
SQLError::getErrorCode( const ErrorCondition _eCondition
)
313 return SQLError_Impl::getErrorCode( _eCondition
);
317 void SQLError::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
319 m_pImpl
->raiseException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
323 void SQLError::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
325 m_pImpl
->raiseException( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
329 void SQLError::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
330 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
332 m_pImpl
->raiseTypedException( _eCondition
, _rxContext
, _rExceptionType
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
336 SQLException
SQLError::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
337 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
339 return m_pImpl
->getSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
343 } // namespace connectivity
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */