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 .
22 #include <connectivity/sqlerror.hxx>
24 #include <com/sun/star/sdbc/SQLException.hpp>
25 #include <com/sun/star/sdb/ErrorCondition.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <cppuhelper/exc_hlp.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <unotools/resmgr.hxx>
31 #include <osl/diagnose.h>
33 #include <strings.hrc>
34 #include <strings.hxx>
37 namespace connectivity
41 using ::com::sun::star::uno::Reference
;
42 using ::com::sun::star::uno::Any
;
43 using ::com::sun::star::uno::XInterface
;
44 using ::com::sun::star::sdbc::SQLException
;
45 using ::com::sun::star::uno::Type
;
47 //using SQLError::ParamValue; // GCC (unxlngi6) does not like this
50 typedef SQLError::ParamValue ParamValue
;
57 explicit SQLError_Impl();
59 // versions of the public SQLError methods which are just delegated to this impl-class
60 static const OUString
& getMessagePrefix();
61 OUString
getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
62 static ErrorCode
getErrorCode( const ErrorCondition _eCondition
);
63 void raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
64 void raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
65 void raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
66 SQLException
getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
69 /// returns the basic error message associated with the given error condition, without any parameter replacements
71 impl_getErrorMessage( ErrorCondition _eCondition
);
73 /// returns the SQLState associated with the given error condition
75 impl_getSQLState( ErrorCondition _eCondition
);
77 /// returns an SQLException describing the given error condition
79 impl_buildSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
80 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
);
82 std::locale m_aResources
;
85 SQLError_Impl::SQLError_Impl()
86 : m_aResources(Translate::Create("cnr"))
90 const OUString
& SQLError_Impl::getMessagePrefix()
92 static const OUString
s_sMessagePrefix( "[OOoBase]" );
93 return s_sMessagePrefix
;
99 /** substitutes a given placeholder in the given message with the given value
101 void lcl_substitutePlaceholder(OUString
& _rMessage
, const sal_Char
* _pPlaceholder
, const ParamValue
& rParamValue
)
103 size_t nPlaceholderLen( strlen( _pPlaceholder
) );
104 sal_Int32 nIndex
= _rMessage
.indexOfAsciiL( _pPlaceholder
, nPlaceholderLen
);
106 bool bHasPlaceholder
= ( nIndex
!= -1 );
107 bool bWantsPlaceholder
= rParamValue
.is();
108 OSL_ENSURE( bHasPlaceholder
== bWantsPlaceholder
, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
110 if ( bHasPlaceholder
&& bWantsPlaceholder
)
111 _rMessage
= _rMessage
.replaceAt( nIndex
, nPlaceholderLen
, *rParamValue
);
114 const char* lcl_getResourceErrorID(const ErrorCondition _eCondition
)
118 case css::sdb::ErrorCondition::ROW_SET_OPERATION_VETOED
:
119 return STR_ROW_SET_OPERATION_VETOED
;
120 case css::sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES
:
121 return STR_PARSER_CYCLIC_SUB_QUERIES
;
122 case css::sdb::ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES
:
123 return STR_DB_OBJECT_NAME_WITH_SLASHES
;
124 case css::sdb::ErrorCondition::DB_INVALID_SQL_NAME
:
125 return STR_DB_INVALID_SQL_NAME
;
126 case css::sdb::ErrorCondition::DB_QUERY_NAME_WITH_QUOTES
:
127 return STR_DB_QUERY_NAME_WITH_QUOTES
;
128 case css::sdb::ErrorCondition::DB_OBJECT_NAME_IS_USED
:
129 return STR_DB_OBJECT_NAME_IS_USED
;
130 case css::sdb::ErrorCondition::DB_NOT_CONNECTED
:
131 return STR_DB_NOT_CONNECTED
;
132 case css::sdb::ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND
:
133 return STR_AB_ADDRESSBOOK_NOT_FOUND
;
134 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED
:
135 return STR_DATA_CANNOT_SELECT_UNFILTERED
;
140 OUString
lcl_getResourceState(const ErrorCondition _eCondition
)
144 case css::sdb::ErrorCondition::DB_NOT_CONNECTED
:
145 return OUString(STR_DB_NOT_CONNECTED_STATE
);
146 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED
:
147 return OUString(STR_DATA_CANNOT_SELECT_UNFILTERED_STATE
);
153 OUString
SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
155 OUString
sErrorMessage( impl_getErrorMessage( _eCondition
) );
157 lcl_substitutePlaceholder( sErrorMessage
, "$1$", _rParamValue1
);
158 lcl_substitutePlaceholder( sErrorMessage
, "$2$", _rParamValue2
);
159 lcl_substitutePlaceholder( sErrorMessage
, "$3$", _rParamValue3
);
161 return sErrorMessage
;
165 ErrorCode
SQLError_Impl::getErrorCode( const ErrorCondition _eCondition
)
167 return 0 - ::sal::static_int_cast
< ErrorCode
, ErrorCondition
>( _eCondition
);
171 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
176 ::cppu::UnoType
< SQLException
>::get(),
184 void SQLError_Impl::raiseException( const ErrorCondition _eCondition
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
189 ::cppu::UnoType
< SQLException
>::get(),
196 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
197 const Type
& _rExceptionType
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
199 if ( !::cppu::UnoType
< SQLException
>::get().isAssignableFrom( _rExceptionType
) )
200 throw std::bad_cast();
202 // default-construct an exception of the desired type
203 Any
aException( nullptr, _rExceptionType
);
206 SQLException
* pException
= static_cast< SQLException
* >( aException
.pData
);
207 *pException
= impl_buildSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
210 ::cppu::throwException( aException
);
213 SQLException
SQLError_Impl::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
214 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
)
216 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
)
223 getErrorMessage( _eCondition
, _rParamValue1
, _rParamValue2
, _rParamValue3
),
225 impl_getSQLState( _eCondition
),
226 getErrorCode( _eCondition
),
231 OUString
SQLError_Impl::impl_getErrorMessage( ErrorCondition _eCondition
)
233 OUStringBuffer aMessage
;
235 OUString
sResMessage(Translate::get(lcl_getResourceErrorID(_eCondition
), m_aResources
));
236 OSL_ENSURE( !sResMessage
.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
237 aMessage
.append( getMessagePrefix() ).append( " " ).append( sResMessage
);
239 return aMessage
.makeStringAndClear();
242 OUString
SQLError_Impl::impl_getSQLState( ErrorCondition _eCondition
)
244 OUString sState
= lcl_getResourceState(_eCondition
);
245 if (sState
.isEmpty())
246 sState
= OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
251 :m_pImpl( new SQLError_Impl
)
256 SQLError::~SQLError()
261 const OUString
& SQLError::getMessagePrefix()
263 return SQLError_Impl::getMessagePrefix();
267 OUString
SQLError::getErrorMessage( const ErrorCondition _eCondition
) const
269 return m_pImpl
->getErrorMessage( _eCondition
, ParamValue(), ParamValue(), ParamValue() );
273 ErrorCode
SQLError::getErrorCode( const ErrorCondition _eCondition
)
275 return SQLError_Impl::getErrorCode( _eCondition
);
279 void SQLError::raiseException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
, const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
281 m_pImpl
->raiseException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
285 void SQLError::raiseException( const ErrorCondition _eCondition
) const
287 m_pImpl
->raiseException( _eCondition
, ParamValue(), ParamValue(), ParamValue() );
291 void SQLError::raiseTypedException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
292 const Type
& _rExceptionType
) const
294 m_pImpl
->raiseTypedException( _eCondition
, _rxContext
, _rExceptionType
, ParamValue(), ParamValue(), ParamValue() );
298 SQLException
SQLError::getSQLException( const ErrorCondition _eCondition
, const Reference
< XInterface
>& _rxContext
,
299 const ParamValue
& _rParamValue1
, const ParamValue
& _rParamValue2
, const ParamValue
& _rParamValue3
) const
301 return m_pImpl
->getSQLException( _eCondition
, _rxContext
, _rParamValue1
, _rParamValue2
, _rParamValue3
);
305 } // namespace connectivity
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */