1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <connectivity/sqlerror.hxx>
#include <com/sun/star/sdbc/SQLException.hpp>
#include <com/sun/star/sdb/ErrorCondition.hpp>
#include <cppuhelper/exc_hlp.hxx>
#include <rtl/ustrbuf.hxx>
#include <tools/resmgr.hxx>
#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
#include <osl/diagnose.h>
#include <strings.hrc>
#include <strings.hxx>
#include <string.h>
namespace connectivity
{
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::XInterface;
using ::com::sun::star::uno::XComponentContext;
using ::com::sun::star::sdbc::SQLException;
using ::com::sun::star::uno::Type;
//using SQLError::ParamValue; // GCC (unxlngi6) does not like this
namespace
{
typedef SQLError::ParamValue ParamValue;
}
class SQLError_Impl
{
public:
explicit SQLError_Impl( const Reference<XComponentContext> & _rxContext );
// versions of the public SQLError methods which are just delegated to this impl-class
static const OUString& getMessagePrefix();
OUString getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
static ErrorCode getErrorCode( const ErrorCondition _eCondition );
void raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
void raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
void raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
SQLException getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
private:
/// returns the basic error message associated with the given error condition, without any parameter replacements
OUString
impl_getErrorMessage( ErrorCondition _eCondition );
/// returns the SQLState associated with the given error condition
static OUString
impl_getSQLState( ErrorCondition _eCondition );
/// returns an SQLException describing the given error condition
SQLException
impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 );
/// initializes our resource bundle
bool impl_initResources();
private:
::osl::Mutex m_aMutex;
Reference<XComponentContext> m_aContext;
std::unique_ptr<std::locale> m_xResources;
bool m_bAttemptedInit;
};
SQLError_Impl::SQLError_Impl( const Reference<XComponentContext> & _rxContext )
:m_aContext( _rxContext )
,m_bAttemptedInit( false )
{
}
const OUString& SQLError_Impl::getMessagePrefix()
{
static const OUString s_sMessagePrefix( "[OOoBase]" );
return s_sMessagePrefix;
}
namespace
{
/** substitutes a given placeholder in the given message with the given value
*/
void lcl_substitutePlaceholder(OUString& _rMessage, const sal_Char* _pPlaceholder, const ParamValue& rParamValue)
{
size_t nPlaceholderLen( strlen( _pPlaceholder ) );
sal_Int32 nIndex = _rMessage.indexOfAsciiL( _pPlaceholder, nPlaceholderLen );
bool bHasPlaceholder = ( nIndex != -1 );
bool bWantsPlaceholder = rParamValue.is();
OSL_ENSURE( bHasPlaceholder == bWantsPlaceholder, "lcl_substitutePlaceholder: placeholder where none is expected, or no placeholder where one is needed!" );
if ( bHasPlaceholder && bWantsPlaceholder )
_rMessage = _rMessage.replaceAt( nIndex, nPlaceholderLen, *rParamValue );
}
const char* lcl_getResourceErrorID(const ErrorCondition _eCondition)
{
switch (_eCondition)
{
case css::sdb::ErrorCondition::ROW_SET_OPERATION_VETOED:
return STR_ROW_SET_OPERATION_VETOED;
case css::sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES:
return STR_PARSER_CYCLIC_SUB_QUERIES;
case css::sdb::ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES:
return STR_DB_OBJECT_NAME_WITH_SLASHES;
case css::sdb::ErrorCondition::DB_INVALID_SQL_NAME:
return STR_DB_INVALID_SQL_NAME;
case css::sdb::ErrorCondition::DB_QUERY_NAME_WITH_QUOTES:
return STR_DB_QUERY_NAME_WITH_QUOTES;
case css::sdb::ErrorCondition::DB_OBJECT_NAME_IS_USED:
return STR_DB_OBJECT_NAME_IS_USED;
case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
return STR_DB_NOT_CONNECTED;
case css::sdb::ErrorCondition::AB_ADDRESSBOOK_NOT_FOUND:
return STR_AB_ADDRESSBOOK_NOT_FOUND;
case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
return STR_DATA_CANNOT_SELECT_UNFILTERED;
}
return nullptr;
}
OUString lcl_getResourceState(const ErrorCondition _eCondition)
{
switch (_eCondition)
{
case css::sdb::ErrorCondition::DB_NOT_CONNECTED:
return OUString(STR_DB_NOT_CONNECTED_STATE);
case css::sdb::ErrorCondition::DATA_CANNOT_SELECT_UNFILTERED:
return OUString(STR_DATA_CANNOT_SELECT_UNFILTERED_STATE);
}
return OUString();
}
}
OUString SQLError_Impl::getErrorMessage( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
OUString sErrorMessage( impl_getErrorMessage( _eCondition ) );
lcl_substitutePlaceholder( sErrorMessage, "$1$", _rParamValue1 );
lcl_substitutePlaceholder( sErrorMessage, "$2$", _rParamValue2 );
lcl_substitutePlaceholder( sErrorMessage, "$3$", _rParamValue3 );
return sErrorMessage;
}
ErrorCode SQLError_Impl::getErrorCode( const ErrorCondition _eCondition )
{
return 0 - ::sal::static_int_cast< ErrorCode, ErrorCondition >( _eCondition );
}
void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
raiseTypedException(
_eCondition,
_rxContext,
::cppu::UnoType< SQLException >::get(),
_rParamValue1,
_rParamValue2,
_rParamValue3
);
}
void SQLError_Impl::raiseException( const ErrorCondition _eCondition, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
raiseTypedException(
_eCondition,
nullptr,
::cppu::UnoType< SQLException >::get(),
_rParamValue1,
_rParamValue2,
_rParamValue3
);
}
void SQLError_Impl::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const Type& _rExceptionType, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
if ( !::cppu::UnoType< SQLException >::get().isAssignableFrom( _rExceptionType ) )
throw std::bad_cast();
// default-construct an exception of the desired type
Any aException( nullptr, _rExceptionType );
// fill it
SQLException* pException = static_cast< SQLException* >( aException.pData );
*pException = impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
// throw it
::cppu::throwException( aException );
}
SQLException SQLError_Impl::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
return impl_buildSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
}
SQLException SQLError_Impl::impl_buildSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 )
{
return SQLException(
getErrorMessage( _eCondition, _rParamValue1, _rParamValue2, _rParamValue3 ),
_rxContext,
impl_getSQLState( _eCondition ),
getErrorCode( _eCondition ),
Any()
);
}
OUString SQLError_Impl::impl_getErrorMessage( ErrorCondition _eCondition )
{
OUStringBuffer aMessage;
if ( impl_initResources() )
{
OUString sResMessage(Translate::get(lcl_getResourceErrorID(_eCondition), *m_xResources));
OSL_ENSURE( !sResMessage.isEmpty(), "SQLError_Impl::impl_getErrorMessage: illegal error condition, or invalid resource!" );
aMessage.append( getMessagePrefix() ).append( " " ).append( sResMessage );
}
return aMessage.makeStringAndClear();
}
OUString SQLError_Impl::impl_getSQLState( ErrorCondition _eCondition )
{
OUString sState = lcl_getResourceState(_eCondition);
if (sState.isEmpty())
sState = OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "S1000" ) );
return sState;
}
bool SQLError_Impl::impl_initResources()
{
if (m_xResources.get())
return true;
if (m_bAttemptedInit)
return false;
::osl::MutexGuard aGuard( m_aMutex );
m_bAttemptedInit = true;
m_xResources.reset(new std::locale(Translate::Create("cnr", Application::GetSettings().GetUILanguageTag())));
return m_xResources.get() != nullptr;
}
SQLError::SQLError( const Reference<XComponentContext> & _rxContext )
:m_pImpl( new SQLError_Impl( _rxContext ) )
{
}
SQLError::~SQLError()
{
}
const OUString& SQLError::getMessagePrefix()
{
return SQLError_Impl::getMessagePrefix();
}
OUString SQLError::getErrorMessage( const ErrorCondition _eCondition ) const
{
return m_pImpl->getErrorMessage( _eCondition, ParamValue(), ParamValue(), ParamValue() );
}
ErrorCode SQLError::getErrorCode( const ErrorCondition _eCondition )
{
return SQLError_Impl::getErrorCode( _eCondition );
}
void SQLError::raiseException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext, const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
{
m_pImpl->raiseException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
}
void SQLError::raiseException( const ErrorCondition _eCondition ) const
{
m_pImpl->raiseException( _eCondition, ParamValue(), ParamValue(), ParamValue() );
}
void SQLError::raiseTypedException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const Type& _rExceptionType ) const
{
m_pImpl->raiseTypedException( _eCondition, _rxContext, _rExceptionType, ParamValue(), ParamValue(), ParamValue() );
}
SQLException SQLError::getSQLException( const ErrorCondition _eCondition, const Reference< XInterface >& _rxContext,
const ParamValue& _rParamValue1, const ParamValue& _rParamValue2, const ParamValue& _rParamValue3 ) const
{
return m_pImpl->getSQLException( _eCondition, _rxContext, _rParamValue1, _rParamValue2, _rParamValue3 );
}
} // namespace connectivity
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|