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 .
20 #ifndef INCLUDED_CONNECTIVITY_SQLERROR_HXX
21 #define INCLUDED_CONNECTIVITY_SQLERROR_HXX
23 #include <com/sun/star/sdbc/SQLException.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <connectivity/dbtoolsdllapi.hxx>
26 #include <boost/optional.hpp>
29 namespace connectivity
36 /** the type of error codes to be used in SQLExceptions
38 @see com::sun::star::sdbc::SQLException::ErrorCode
40 typedef ::sal_Int32 ErrorCode
;
42 /** error condition values as defined in com::sun::star::sdb::ErrorCondition
44 typedef ::sal_Int32 ErrorCondition
;
51 /** a class which provides helpers for working with SQLErrors
53 In particular, this class provides vendor-specific error codes (where
54 the vendor is OpenOffice.org Base), which can be used in OOo's various
55 database drivers, and checked in application-level code, to properly
56 recognize highly specific error conditions.
58 @see ::com::sun::star::sdb::ErrorCondition
60 class OOO_DLLPUBLIC_DBTOOLS SQLError
66 /** convenience wrapper around boost::optional, allowing implicit construction
68 class ParamValue
: public ::boost::optional
< OUString
>
70 typedef ::boost::optional
< OUString
> base_type
;
73 ParamValue( ) : base_type( ) { }
74 ParamValue( OUString
const& val
) : base_type( val
) { }
75 ParamValue( ParamValue
const& rhs
) : base_type( (base_type
const&)rhs
) { }
77 bool is() const { return !base_type::operator!(); }
82 explicit SQLError( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> & _rxContext
);
85 /** returns the message associated with a given error condition, after (optionally) replacing
86 a placeholder with a given string
88 Some error messages need to contain references to runtime-dependent data (say, the
89 name of a concrete table in the database), which in the resource file's strings are
90 represented by a placeholder, namely $1$, $2, and so on. This method allows to
91 retrieve such an error message, and replace upo to 3 placeholders with their concrete
94 In a non-product build, assertions will fire if the number of placeholders in the
95 message's resource string does not match the number of passed parameter values.
97 As specified in the com::sun::star::sdb::ErrorCondition type,
98 error messages thrown by core components of OpenOffice.org Base will contain
99 a standardized prefix "[OOoBase]" in every message.
102 the value which the placeholder $1$ should be replaced with. If this value is
103 not present (see <code>::boost::optional::operator !</code>), then no replacement
104 will happen, and <code>_rParamValue2</code> and <code>_rParamValue3</code> will be
108 the value which the placeholder $2$ should be replaced with. If this value is
109 not present (see <code>::boost::optional::operator !</code>), then no replacement
110 will happen, and <code>_rParamValue3</code> will be ignored.
113 the value which the placeholder $1$ should be replaced with. If this value is
114 not present (see <code>::boost::optional::operator !</code>), then no replacement
117 @see ::com::sun::star::sdb::ErrorCondition
119 OUString
getErrorMessage(
120 const ErrorCondition _eCondition
,
121 const ParamValue
& _rParamValue1
= ParamValue(),
122 const ParamValue
& _rParamValue2
= ParamValue(),
123 const ParamValue
& _rParamValue3
= ParamValue()
126 /** returns the error code associated with a given error condition
129 @see ::com::sun::star::sdb::ErrorCondition
130 @see ::com::sun::star::sdbc::SQLException::ErrorCode
133 getErrorCode( const ErrorCondition _eCondition
);
135 /** returns the prefix which is used for OpenOffice.org Base's error messages
137 As specified in the com::sun::star::sdb::ErrorCondition type,
138 error messages thrown by core components of OpenOffice.org Base will
139 contain a standardized prefix in every message. <code>getBaseErrorMessagePrefix</code>
140 returns this prefix, so clients of such error messages might decide to strip this
141 prefix before presenting the message to the user, or use it to determine
142 whether a concrete error has been raised by a OpenOffice.org core component.
144 static const OUString
&
148 /** throws an SQLException describing the given error condition
150 The thrown SQLException will contain the OOo-specific error code which derives
151 from the given error condition, and the error message associated with that condition.
154 the ErrorCondition which hit you
157 the context in which the error occurred. This will be filled in as
158 <member scope="com::sun::star::uno">Exception::Context</member> member.
161 a runtime-dependent value which should be filled into the error message
162 which is associated with <arg>_eCondition</arg>, replacing the first placeholder
166 a runtime-dependent value which should be filled into the error message
167 which is associated with <arg>_eCondition</arg>, replacing the second placeholder
171 a runtime-dependent value which should be filled into the error message
172 which is associated with <arg>_eCondition</arg>, replacing the third placeholder
179 const ErrorCondition _eCondition
,
180 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
181 const ParamValue
& _rParamValue1
= ParamValue(),
182 const ParamValue
& _rParamValue2
= ParamValue(),
183 const ParamValue
& _rParamValue3
= ParamValue()
186 /** throws an SQLException describing the given error condition
188 The thrown SQLException will contain the OOo-specific error code which derives
189 from the given error condition, and the error message associated with that condition.
191 Note: You should prefer the version of raiseException which takes
192 an additional Context parameter, since this allows clients of your
193 exception to examine where the error occurred.
196 the ErrorCondition which hit you
199 a runtime-dependent value which should be filled into the error message
200 which is associated with <arg>_eCondition</arg>, replacing the first placeholder
204 a runtime-dependent value which should be filled into the error message
205 which is associated with <arg>_eCondition</arg>, replacing the second placeholder
209 a runtime-dependent value which should be filled into the error message
210 which is associated with <arg>_eCondition</arg>, replacing the third placeholder
217 const ErrorCondition _eCondition
,
218 const ParamValue
& _rParamValue1
= ParamValue(),
219 const ParamValue
& _rParamValue2
= ParamValue(),
220 const ParamValue
& _rParamValue3
= ParamValue()
223 /** raises a typed exception, that is, a UNO exception which is derived from
224 com::sun::star::sdbc::SQLException
227 the ErrorCondition which hit you
230 the context in which the error occurred. This will be filled in as
231 <member scope="com::sun::star::uno">Exception::Context</member> member.
233 @param _rExceptionType
234 the type of the exception to throw. This type <em>must</em> specify
235 an exception class derived from com::sun::star::sdbc::SQLException.
238 a runtime-dependent value which should be filled into the error message
239 which is associated with <arg>_eCondition</arg>, replacing the first placeholder
243 a runtime-dependent value which should be filled into the error message
244 which is associated with <arg>_eCondition</arg>, replacing the second placeholder
248 a runtime-dependent value which should be filled into the error message
249 which is associated with <arg>_eCondition</arg>, replacing the third placeholder
252 @throws ::std::bad_cast
253 if <arg>_rExceptionType</arg> does not specify an exception class derived from
254 com::sun::star::sdbc::SQLException.
259 void raiseTypedException(
260 const ErrorCondition _eCondition
,
261 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
262 const ::com::sun::star::uno::Type
& _rExceptionType
,
263 const ParamValue
& _rParamValue1
= ParamValue(),
264 const ParamValue
& _rParamValue2
= ParamValue(),
265 const ParamValue
& _rParamValue3
= ParamValue()
268 /** retrieves an <code>SQLException</code> object which contains information about
269 the given error condition
272 the ErrorCondition which hit you
275 the context in which the error occurred. This will be filled in as
276 <member scope="com::sun::star::uno">Exception::Context</member> member.
279 a runtime-dependent value which should be filled into the error message
280 which is associated with <arg>_eCondition</arg>, replacing the first placeholder
284 a runtime-dependent value which should be filled into the error message
285 which is associated with <arg>_eCondition</arg>, replacing the second placeholder
289 a runtime-dependent value which should be filled into the error message
290 which is associated with <arg>_eCondition</arg>, replacing the third placeholder
296 ::com::sun::star::sdbc::SQLException
298 const ErrorCondition _eCondition
,
299 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _rxContext
,
300 const ParamValue
& _rParamValue1
= ParamValue(),
301 const ParamValue
& _rParamValue2
= ParamValue(),
302 const ParamValue
& _rParamValue3
= ParamValue()
306 std::shared_ptr
< SQLError_Impl
> m_pImpl
;
310 } // namespace connectivity
313 #endif // INCLUDED_CONNECTIVITY_SQLERROR_HXX
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */