xref: /trunk/main/connectivity/inc/connectivity/dbexception.hxx (revision cea84f56765a2ac06f0f5a2718f45f6430edc781)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _DBHELPER_DBEXCEPTION_HXX_
25 #define _DBHELPER_DBEXCEPTION_HXX_
26 
27 #include <com/sun/star/sdbc/SQLException.hpp>
28 #include "connectivity/standardsqlstate.hxx"
29 #include "connectivity/dbtoolsdllapi.hxx"
30 
31 namespace com
32 {
33     namespace sun
34     {
35         namespace star
36         {
37             namespace sdb
38             {
39                 class SQLContext;
40                 struct SQLErrorEvent;
41             }
42             namespace sdbc
43             {
44                 class SQLWarning;
45             }
46         }
47     }
48 }
49 //.........................................................................
50 namespace dbtools
51 {
52 //.........................................................................
53 
54 //==============================================================================
55 //= Special exception if cancel is pressed in DBA UI
56 //==============================================================================
57 enum OOoBaseErrorCode
58 {
59     ParameterInteractionCancelled = 1
60 };
61 
62 //==============================================================================
63 //= SQLExceptionInfo - encapsulating the type info of an SQLException-derived class
64 //==============================================================================
65 
66 class OOO_DLLPUBLIC_DBTOOLS SQLExceptionInfo
67 {
68 public:
69     enum TYPE { SQL_EXCEPTION, SQL_WARNING, SQL_CONTEXT, UNDEFINED };
70 
71 private:
72     ::com::sun::star::uno::Any  m_aContent;
73     TYPE            m_eType;    // redundant (could be derived from m_aContent.getValueType())
74 
75 public:
76     SQLExceptionInfo();
77     SQLExceptionInfo(const ::com::sun::star::sdbc::SQLException& _rError);
78     SQLExceptionInfo(const ::com::sun::star::sdbc::SQLWarning& _rError);
79     SQLExceptionInfo(const ::com::sun::star::sdb::SQLContext& _rError);
80 
81     /** convenience constructor
82 
83     If your error processing relies on SQLExceptions, and SQLExceptionInfos, you still may
84     need to display an error which consists of a simple message string only.
85     In those cases, you can use this constructor, which behaves as if you would have used
86     an SQLException containing exactly the given error message.
87     */
88     SQLExceptionInfo( const ::rtl::OUString& _rSimpleErrorMessage );
89 
90     SQLExceptionInfo(const SQLExceptionInfo& _rCopySource);
91     SQLExceptionInfo& operator=(const SQLExceptionInfo&) = default;
92 
93     SQLExceptionInfo(const ::com::sun::star::sdb::SQLErrorEvent& _rError);
94             // use for events got via XSQLErrorListener::errorOccured
95     SQLExceptionInfo(const ::com::sun::star::uno::Any& _rError);
96             // use with the Reason member of an SQLErrorEvent or with NextElement of an SQLException
97 
98     /** prepends a plain error message to the chain of exceptions
99         @param  _rSimpleErrorMessage
100             the error message to prepend
101         @param  _pAsciiSQLState
102             the SQLState of the to-be-constructed SQLException, or NULL if this should be defaulted to HY000
103         @param  _nErrorCode
104             the ErrorCode of the to-be-constructed SQLException
105     */
106     void    prepend( const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState = NULL, const sal_Int32 _nErrorCode = 0 );
107 
108     /** appends a plain message to the chain of exceptions
109         @param  _eType
110             the type of exception to append. Must be SQL_EXCEPTION, SQL_WARNING, SQL_CONTEXT, for all other
111             values, the behavior is undefined.
112         @param  _rErrorMessage
113             the message to append
114         @param  _pAsciiSQLState
115             the SQLState of the exception to append
116         @param  _nErrorCode
117             the error code of the exception to append
118     */
119     void    append( TYPE _eType, const ::rtl::OUString& _rErrorMessage, const sal_Char* _pAsciiSQLState = NULL, const sal_Int32 _nErrorCode = 0 );
120 
121     /** throws (properly typed) the exception contained in the object
122         @precond
123             isValid() returns <TRUE/>
124         @throws SQLException
125         @throws RuntimeException
126             if the instance does not contain an SQLException
127     */
128     void    doThrow();
129 
130     const SQLExceptionInfo& operator=(const ::com::sun::star::sdbc::SQLException& _rError);
131     const SQLExceptionInfo& operator=(const ::com::sun::star::sdbc::SQLWarning& _rError);
132     const SQLExceptionInfo& operator=(const ::com::sun::star::sdb::SQLContext& _rError);
133     const SQLExceptionInfo& operator=(const ::com::sun::star::sdb::SQLErrorEvent& _rErrorEvent);
134     const SQLExceptionInfo& operator=(const ::com::sun::star::uno::Any& _rCaughtSQLException);
135 
136     sal_Bool    isKindOf(TYPE _eType) const;
137         // not just a simple comparisation ! e.g. getType() == SQL_CONTEXT implies isKindOf(SQL_EXCEPTION) == sal_True !
isValid() const138     sal_Bool    isValid() const { return m_eType != UNDEFINED; }
getType() const139     TYPE        getType() const { return m_eType; }
140 
141     operator const ::com::sun::star::sdbc::SQLException*    () const;
142     operator const ::com::sun::star::sdbc::SQLWarning*      () const;
143     operator const ::com::sun::star::sdb::SQLContext*       () const;
144 
get() const145     const ::com::sun::star::uno::Any& get() const { return m_aContent; }
146 
clear()147     void    clear()
148     {
149         m_aContent.clear();
150         m_eType = UNDEFINED;
151     }
152 
153 protected:
154     void implDetermineType();
155 };
156 
157 //==============================================================================
158 //= SQLExceptionIteratorHelper - iterating through an SQLException chain
159 //==============================================================================
160 
161 class OOO_DLLPUBLIC_DBTOOLS SQLExceptionIteratorHelper
162 {
163 protected:
164     const ::com::sun::star::sdbc::SQLException* m_pCurrent;
165     SQLExceptionInfo::TYPE                      m_eCurrentType;
166 
167 public:
168     /** constructs an iterator instance from an SQLException
169 
170         @param _rChainStart
171             the start of the exception chain to iterate. Must live as long as the iterator
172             instances lives, at least.
173     */
174     SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLException& _rChainStart );
175 
176     /** constructs an iterator instance from an SQLWarning
177 
178         @param _rChainStart
179             the start of the exception chain to iterate. Must live as long as the iterator
180             instances lives, at least.
181     */
182     SQLExceptionIteratorHelper( const ::com::sun::star::sdbc::SQLWarning& _rChainStart );
183 
184     /** constructs an iterator instance from an SQLContext
185 
186         @param _rChainStart
187             the start of the exception chain to iterate. Must live as long as the iterator
188             instances lives, at least.
189     */
190     SQLExceptionIteratorHelper( const ::com::sun::star::sdb::SQLContext& _rChainStart );
191 
192     /** constructs an iterator instance from an SQLExceptionInfo
193 
194         @param _rErrorInfo
195             the start of the exception chain to iterate. Must live as long as the iterator
196             instances lives, at least.
197     */
198     SQLExceptionIteratorHelper( const SQLExceptionInfo& _rErrorInfo );
199 
200     /** determines whether there are more elements in the exception chain
201     */
hasMoreElements() const202     sal_Bool                                    hasMoreElements() const { return ( m_pCurrent != NULL ); }
203 
204     /** returns the type of the current element in the exception chain
205     */
currentType() const206     SQLExceptionInfo::TYPE                      currentType() const { return m_eCurrentType; }
207 
208     /** retrieves the current element in the chain, or <NULL/> if the chain has been completely
209         traveled.
210     */
current() const211     const ::com::sun::star::sdbc::SQLException* current() const { return m_pCurrent; }
212 
213     /** retrieves the current element in the chain, or <NULL/> if the chain has been completely
214         traveled.
215 
216         In opposite to the second <member>current</member>, this version allows typed access to
217         the respective SQLException.
218     */
219     void                                        current( SQLExceptionInfo& _out_rInfo ) const;
220 
221     /** proceeds to the next element in the chain
222 
223         @return the current element in the chain, as <b>before</em> the chain move.
224     */
225     const ::com::sun::star::sdbc::SQLException* next();
226 
227     /** proceeds to the next element in the chain
228 
229         In opposite to the second <member>current</member>, this version allows typed access to
230         the respective SQLException.
231     */
232     void                                        next( SQLExceptionInfo& _out_rInfo );
233 };
234 
235 //==================================================================================
236 //= StandardExceptions
237 //==================================================================================
238 //----------------------------------------------------------------------------------
239 /** returns a standard error string for a given SQLState
240 
241     @param _eState
242         describes the state whose description is to retrieve. Must not be SQL_ERROR_UNSPECIFIED.
243     @raises RuntimeException
244         in case of an internal error
245 */
246 OOO_DLLPUBLIC_DBTOOLS ::rtl::OUString getStandardSQLState( StandardSQLState _eState );
247 
248 //----------------------------------------------------------------------------------
249 /** returns a standard ASCII string for a given SQLState
250 
251     @param _eState
252         describes the state whose description is to retrieve. Must not be SQL_ERROR_UNSPECIFIED.
253     @return
254         a non-<NULL/> pointer to an ASCII character string denoting the requested SQLState
255     @raises RuntimeException
256         in case of an internal error
257 */
258 OOO_DLLPUBLIC_DBTOOLS const sal_Char* getStandardSQLStateAscii( StandardSQLState _eState );
259 
260 //----------------------------------------------------------------------------------
261 OOO_DLLPUBLIC_DBTOOLS void throwFunctionNotSupportedException(
262         const ::rtl::OUString& _rMsg,
263         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
264         const ::com::sun::star::uno::Any& _Next = ::com::sun::star::uno::Any()
265     );
266 
267 //----------------------------------------------------------------------------------
268 /** throws an exception with SQL state IM001, saying that a certain function is not supported
269 */
270 OOO_DLLPUBLIC_DBTOOLS void throwFunctionNotSupportedException(
271         const sal_Char* _pAsciiFunctionName,
272         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
273         const ::com::sun::star::uno::Any* _pNextException = NULL
274     );
275 
276 //----------------------------------------------------------------------------------
277 /** throws a function sequence (HY010) exception
278 */
279 OOO_DLLPUBLIC_DBTOOLS void throwFunctionSequenceException(
280         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
281         const ::com::sun::star::uno::Any& _Next = ::com::sun::star::uno::Any()
282     );
283 
284 //----------------------------------------------------------------------------------
285 /** throw a invalid index sqlexception
286 */
287 OOO_DLLPUBLIC_DBTOOLS void throwInvalidIndexException(
288         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _Context,
289         const ::com::sun::star::uno::Any& _Next = ::com::sun::star::uno::Any()
290     );
291 
292 //----------------------------------------------------------------------------------
293 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
294 */
295 OOO_DLLPUBLIC_DBTOOLS void throwGenericSQLException(
296         const ::rtl::OUString& _rMsg,
297         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource
298     );
299 
300 //----------------------------------------------------------------------------------
301 /** throw a generic SQLException, i.e. one with an SQLState of HY000, an ErrorCode of 0 and no NextException
302 */
303 OOO_DLLPUBLIC_DBTOOLS void throwGenericSQLException(
304         const ::rtl::OUString& _rMsg,
305         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxSource,
306         const ::com::sun::star::uno::Any& _rNextException
307     );
308 
309 //----------------------------------------------------------------------------------
310 /** throw a SQLException with SQLState HYC00 (Optional feature not implemented)
311     @param _pAsciiFeatureName
312         an ASCII description of the feature which is not implemented. It's recommended that the feature
313         name is built from the name of the interface plus its method, for instance "XParameters::updateBinaryStream"
314     @param _rxContext
315         the context of the exception
316     @param _pNextException
317         the next exception to chain into the thrown exception, if any
318 */
319 OOO_DLLPUBLIC_DBTOOLS void throwFeatureNotImplementedException(
320         const sal_Char* _pAsciiFeatureName,
321         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
322         const ::com::sun::star::uno::Any* _pNextException = NULL
323     );
324 
325 //----------------------------------------------------------------------------------
326 /** throws an SQLException
327 */
328 OOO_DLLPUBLIC_DBTOOLS void throwSQLException(
329         const sal_Char* _pAsciiMessage,
330         const sal_Char* _pAsciiState,
331         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
332         const sal_Int32 _nErrorCode = 0,
333         const ::com::sun::star::uno::Any* _pNextException = NULL
334     );
335 
336 //----------------------------------------------------------------------------------
337 /** throws an SQLException
338 */
339 OOO_DLLPUBLIC_DBTOOLS void throwSQLException(
340         const sal_Char* _pAsciiMessage,
341         StandardSQLState _eSQLState,
342         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
343         const sal_Int32 _nErrorCode = 0,
344         const ::com::sun::star::uno::Any* _pNextException = NULL
345     );
346 
347 //----------------------------------------------------------------------------------
348 /** throws an SQLException
349 */
350 OOO_DLLPUBLIC_DBTOOLS void throwSQLException(
351         const ::rtl::OUString& _rMessage,
352         StandardSQLState _eSQLState,
353         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext,
354         const sal_Int32 _nErrorCode = 0,
355         const ::com::sun::star::uno::Any* _pNextException = NULL
356     );
357 
358 //.........................................................................
359 }   // namespace dbtools
360 //.........................................................................
361 
362 #endif // _DBHELPER_DBEXCEPTION_HXX_
363