LibreOffice Module connectivity (master) 1
sqlerror.cxx
Go to the documentation of this file.
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 <memory>
23
24#include <com/sun/star/sdbc/SQLException.hpp>
25#include <com/sun/star/sdb/ErrorCondition.hpp>
26
28#include <unotools/resmgr.hxx>
29#include <osl/diagnose.h>
30
31#include <strings.hrc>
32#include <strings.hxx>
33#include <string.h>
34
35namespace connectivity
36{
37
38
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::uno::Any;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::sdbc::SQLException;
43 using ::com::sun::star::uno::Type;
44
46 {
47 public:
48 explicit SQLError_Impl();
49
50 // versions of the public SQLError methods which are just delegated to this impl-class
51 static const OUString& getMessagePrefix();
52 OUString getErrorMessage( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const;
53 static ErrorCode getErrorCode( const ErrorCondition _eCondition );
54 void raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
55 void raiseException( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
56 void raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
57 SQLException getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
58
59 private:
61 OUString impl_getErrorMessage( ErrorCondition _eCondition ) const;
62
64 static OUString
65 impl_getSQLState( ErrorCondition _eCondition );
66
68 SQLException
69 impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
70 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 );
71 private:
72 std::locale m_aResources;
73 };
74
76 : m_aResources(Translate::Create("cnr"))
77 {
78 }
79
81 {
82 static const OUString s_sMessagePrefix( "[OOoBase]" );
83 return s_sMessagePrefix;
84 }
85
86 namespace
87 {
88
91 void lcl_substitutePlaceholder(OUString& _rMessage, const char* _pPlaceholder, const std::optional<OUString>& rParamValue)
92 {
93 size_t nPlaceholderLen( strlen( _pPlaceholder ) );
94 sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
95
96 bool bHasPlaceholder = ( nIndex != -1 );
97 bool bWantsPlaceholder = rParamValue.has_value();
98 OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
99
100 if ( bHasPlaceholder && bWantsPlaceholder )
101 _rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *rParamValue );
102 }
103
104 TranslateId lcl_getResourceErrorID(const ErrorCondition _eCondition)
105 {
106 switch (_eCondition)
107 {
108 case css::sdb::ErrorCondition::ROW_SET_OPERATION_VETOED:
109 return STR_ROW_SET_OPERATION_VETOED;
110 case css::sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES:
111 return STR_PARSER_CYCLIC_SUB_QUERIES;
112 case css::sdb::ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES:
113 return STR_DB_OBJECT_NAME_WITH_SLASHES;
114 case css::sdb::ErrorCondition::DB_INVALID_SQL_NAME:
115 return STR_DB_INVALID_SQL_NAME;
116 case css::sdb::ErrorCondition::DB_QUERY_NAME_WITH_QUOTES:
117 return STR_DB_QUERY_NAME_WITH_QUOTES;
118 case css::sdb::ErrorCondition::DB_OBJECT_NAME_IS_USED:
119 return STR_DB_OBJECT_NAME_IS_USED;
120 case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
121 return STR_DB_NOT_CONNECTED;
122 case css::sdb::ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND:
123 return STR_AB_ADDRESSBOOK_NOT_FOUND;
124 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
125 return STR_DATA_CANNOT_SELECT_UNFILTERED;
126 }
127 return {};
128 }
129
130 OUString lcl_getResourceState(const ErrorCondition _eCondition)
131 {
132 switch (_eCondition)
133 {
134 case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
136 case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
138 }
139 return OUString();
140 }
141 }
142
143 OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
144 {
145 OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
146
147 lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
148 lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
149 lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
150
151 return sErrorMessage;
152 }
153
154
156 {
157 return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
158 }
159
160
161 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
162 {
164 _eCondition,
165 _rxContext,
167 _rParamValue1,
168 _rParamValue2,
169 _rParamValue3
170 );
171 }
172
173
174 void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
175 {
177 _eCondition,
178 nullptr,
180 _rParamValue1,
181 _rParamValue2,
182 _rParamValue3
183 );
184 }
185
186 void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
187 const Type& _rExceptionType, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
188 {
189 if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
190 throw std::bad_cast();
191
192 // default-construct an exception of the desired type
193 Any aException( nullptr, _rExceptionType );
194
195 // fill it
196 SQLException* pException = static_cast< SQLException* >( aException.pData );
197 *pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
198
199 // throw it
200 ::cppu::throwException( aException );
201 }
202
203 SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
204 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
205 {
206 return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
207 }
208
209 SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
210 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 )
211 {
212 return SQLException(
213 getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
214 _rxContext,
215 impl_getSQLState( _eCondition ),
216 getErrorCode( _eCondition ),
217 Any()
218 );
219 }
220
222 {
223 OUString sResMessage(Translate::get(lcl_getResourceErrorID(_eCondition), m_aResources));
224 OSL_ENSURE( !sResMessage.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
225 return getMessagePrefix() + " " + sResMessage;
226 }
227
229 {
230 static constexpr OUStringLiteral DEFAULT_STATE = u"S1000";
231 OUString sState = lcl_getResourceState(_eCondition);
232 if (sState.isEmpty())
233 sState = DEFAULT_STATE;
234 return sState;
235 }
236
239 {
240 }
241
242
244 {
245 }
246
247
249 {
251 }
252
253
254 OUString SQLError::getErrorMessage( const ErrorCondition _eCondition ) const
255 {
256 return m_pImpl->getErrorMessage( _eCondition, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
257 }
258
259
261 {
262 return SQLError_Impl::getErrorCode( _eCondition );
263 }
264
265
266 void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
267 {
268 m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
269 }
270
271
272 void SQLError::raiseException( const ErrorCondition _eCondition ) const
273 {
274 m_pImpl->raiseException( _eCondition, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
275 }
276
277
278 void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
279 const Type& _rExceptionType ) const
280 {
281 m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, std::optional<OUString>(), std::optional<OUString>(), std::optional<OUString>() );
282 }
283
284
285 SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
286 const std::optional<OUString>& _rParamValue1, const std::optional<OUString>& _rParamValue2, const std::optional<OUString>& _rParamValue3 ) const
287 {
288 return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
289 }
290
291
292} // namespace connectivity
293
294
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
OUString impl_getErrorMessage(ErrorCondition _eCondition) const
returns the basic error message associated with the given error condition, without any parameter repl...
Definition: sqlerror.cxx:221
static OUString impl_getSQLState(ErrorCondition _eCondition)
returns the SQLState associated with the given error condition
Definition: sqlerror.cxx:228
void raiseException(const ErrorCondition _eCondition, const Reference< XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1, const std::optional< OUString > &_rParamValue2, const std::optional< OUString > &_rParamValue3)
Definition: sqlerror.cxx:161
SQLException getSQLException(const ErrorCondition _eCondition, const Reference< XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1, const std::optional< OUString > &_rParamValue2, const std::optional< OUString > &_rParamValue3)
Definition: sqlerror.cxx:203
SQLException impl_buildSQLException(const ErrorCondition _eCondition, const Reference< XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1, const std::optional< OUString > &_rParamValue2, const std::optional< OUString > &_rParamValue3)
returns an SQLException describing the given error condition
Definition: sqlerror.cxx:209
OUString getErrorMessage(const ErrorCondition _eCondition, const std::optional< OUString > &_rParamValue1, const std::optional< OUString > &_rParamValue2, const std::optional< OUString > &_rParamValue3) const
Definition: sqlerror.cxx:143
void raiseTypedException(const ErrorCondition _eCondition, const Reference< XInterface > &_rxContext, const Type &_rExceptionType, const std::optional< OUString > &_rParamValue1, const std::optional< OUString > &_rParamValue2, const std::optional< OUString > &_rParamValue3)
Definition: sqlerror.cxx:186
static ErrorCode getErrorCode(const ErrorCondition _eCondition)
Definition: sqlerror.cxx:155
static const OUString & getMessagePrefix()
Definition: sqlerror.cxx:80
void raiseTypedException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const css::uno::Type &_rExceptionType) const
raises a typed exception, that is, a UNO exception which is derived from css::sdbc::SQLException
Definition: sqlerror.cxx:278
static const OUString & getMessagePrefix()
returns the prefix which is used for OpenOffice.org Base's error messages
Definition: sqlerror.cxx:248
std::shared_ptr< SQLError_Impl > m_pImpl
Definition: sqlerror.hxx:230
void raiseException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1=std::nullopt, const std::optional< OUString > &_rParamValue2=std::nullopt, const std::optional< OUString > &_rParamValue3=std::nullopt) const
throws an SQLException describing the given error condition
static ErrorCode getErrorCode(const ErrorCondition _eCondition)
returns the error code associated with a given error condition
Definition: sqlerror.cxx:260
OUString getErrorMessage(const ErrorCondition _eCondition) const
returns the message associated with a given error condition, after (optionally) replacing a placehold...
Definition: sqlerror.cxx:254
css::sdbc::SQLException getSQLException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1=std::nullopt, const std::optional< OUString > &_rParamValue2=std::nullopt, const std::optional< OUString > &_rParamValue3=std::nullopt) const
retrieves an SQLException object which contains information about the given error condition
Definition: sqlerror.cxx:285
float u
sal_Int32 nIndex
OUString get(TranslateId sContextAndId, const std::locale &loc)
bool isAssignableFrom(const Type &_rAssignable, const Type &_rFrom)
Type
::sal_Int32 ErrorCondition
error condition values as defined in css::sdb::ErrorCondition
Definition: sqlerror.hxx:42
::sal_Int32 ErrorCode
the type of error codes to be used in SQLExceptions
Definition: sqlerror.hxx:38
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
std::shared_ptr< T > make_shared(Args &&... args)
constexpr OUStringLiteral STR_DATA_CANNOT_SELECT_UNFILTERED_STATE
Definition: strings.hxx:74
constexpr OUStringLiteral STR_DB_NOT_CONNECTED_STATE
Definition: strings.hxx:73