1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSql module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsqlerror.h"
41#include "qdebug.h"
42
43QT_BEGIN_NAMESPACE
44
45#ifndef QT_NO_DEBUG_STREAM
46QDebug operator<<(QDebug dbg, const QSqlError &s)
47{
48 QDebugStateSaver saver(dbg);
49 dbg.nospace();
50 dbg << "QSqlError(" << s.nativeErrorCode() << ", " << s.driverText()
51 << ", " << s.databaseText() << ')';
52 return dbg;
53}
54#endif
55
56class QSqlErrorPrivate
57{
58public:
59 QString driverError;
60 QString databaseError;
61 QSqlError::ErrorType errorType;
62 QString errorCode;
63};
64
65
66/*!
67 \class QSqlError
68 \brief The QSqlError class provides SQL database error information.
69
70 \ingroup database
71 \inmodule QtSql
72
73 A QSqlError object can provide database-specific error data,
74 including the driverText() and databaseText() messages (or both
75 concatenated together as text()), and the nativeErrorCode() and
76 type().
77
78 \sa QSqlDatabase::lastError(), QSqlQuery::lastError()
79*/
80
81/*!
82 \enum QSqlError::ErrorType
83
84 This enum type describes the context in which the error occurred, e.g., a connection error, a statement error, etc.
85
86 \value NoError No error occurred.
87 \value ConnectionError Connection error.
88 \value StatementError SQL statement syntax error.
89 \value TransactionError Transaction failed error.
90 \value UnknownError Unknown error.
91*/
92
93/*!
94 \fn QSqlError::QSqlError(const QString &driverText, const QString &databaseText, ErrorType type, int number)
95 \obsolete
96
97 Constructs an error containing the driver error text \a
98 driverText, the database-specific error text \a databaseText, the
99 type \a type and the optional error number \a number.
100*/
101
102/*! \fn QSqlError::QSqlError(QSqlError &&other)
103 Move-constructs a QSqlError instance, making it point at the same
104 object that \a other was pointing to.
105
106 \note The moved-from object \a other is placed in a
107 partially-formed state, in which the only valid operations are
108 destruction and assignment of a new value.
109
110 \since 5.10
111*/
112
113/*! \fn QSqlError::operator=(QSqlError &&other)
114 Move-assigns \a other to this QSqlError instance.
115
116 \note The moved-from object \a other is placed in a
117 partially-formed state, in which the only valid operations are
118 destruction and assignment of a new value.
119
120 \since 5.10
121*/
122
123/*! \fn QSqlError::swap(QSqlError &other)
124 Swaps error \a other with this error. This operation is very fast
125 and never fails.
126
127 \since 5.10
128*/
129
130#if QT_DEPRECATED_SINCE(5, 3)
131QSqlError::QSqlError(const QString& driverText, const QString& databaseText, ErrorType type,
132 int number)
133{
134 d = new QSqlErrorPrivate;
135
136 d->driverError = driverText;
137 d->databaseError = databaseText;
138 d->errorType = type;
139 if (number != -1)
140 d->errorCode = QString::number(number);
141}
142#endif
143
144/*!
145 Constructs an error containing the driver error text \a
146 driverText, the database-specific error text \a databaseText, the
147 type \a type and the error code \a code.
148
149 \note DB2: It is possible for DB2 to report more than one error code.
150 When this happens, \c ; is used as separator between the error codes.
151*/
152QSqlError::QSqlError(const QString &driverText, const QString &databaseText,
153 ErrorType type, const QString &code)
154{
155 d = new QSqlErrorPrivate;
156
157 d->driverError = driverText;
158 d->databaseError = databaseText;
159 d->errorType = type;
160 d->errorCode = code;
161}
162
163
164/*!
165 Creates a copy of \a other.
166*/
167QSqlError::QSqlError(const QSqlError& other)
168{
169 d = new QSqlErrorPrivate;
170
171 *d = *other.d;
172}
173
174/*!
175 Assigns the \a other error's values to this error.
176*/
177
178QSqlError& QSqlError::operator=(const QSqlError& other)
179{
180 if (d)
181 *d = *other.d;
182 else
183 d = new QSqlErrorPrivate(*other.d);
184 return *this;
185}
186
187/*!
188 Compare the \a other error's values to this error and returns \c true, if it equal.
189*/
190
191bool QSqlError::operator==(const QSqlError& other) const
192{
193 return (d->errorType == other.d->errorType);
194}
195
196
197/*!
198 Compare the \a other error's values to this error and returns \c true if it is not equal.
199*/
200
201bool QSqlError::operator!=(const QSqlError& other) const
202{
203 return (d->errorType != other.d->errorType);
204}
205
206
207/*!
208 Destroys the object and frees any allocated resources.
209*/
210
211QSqlError::~QSqlError()
212{
213 delete d;
214}
215
216/*!
217 Returns the text of the error as reported by the driver. This may
218 contain database-specific descriptions. It may also be empty.
219
220 \sa databaseText(), text()
221*/
222QString QSqlError::driverText() const
223{
224 return d->driverError;
225}
226
227/*!
228 \fn void QSqlError::setDriverText(const QString &driverText)
229 \obsolete
230
231 Sets the driver error text to the value of \a driverText.
232
233 Use QSqlError(const QString &driverText, const QString &databaseText,
234 ErrorType type, int number) instead
235
236 \sa driverText(), setDatabaseText(), text()
237*/
238
239#if QT_DEPRECATED_SINCE(5, 1)
240void QSqlError::setDriverText(const QString& driverText)
241{
242 d->driverError = driverText;
243}
244#endif
245
246/*!
247 Returns the text of the error as reported by the database. This
248 may contain database-specific descriptions; it may be empty.
249
250 \sa driverText(), text()
251*/
252
253QString QSqlError::databaseText() const
254{
255 return d->databaseError;
256}
257
258/*!
259 \fn void QSqlError::setDatabaseText(const QString &databaseText)
260 \obsolete
261
262 Sets the database error text to the value of \a databaseText.
263
264 Use QSqlError(const QString &driverText, const QString &databaseText,
265 ErrorType type, int number) instead
266
267 \sa databaseText(), setDriverText(), text()
268*/
269
270#if QT_DEPRECATED_SINCE(5, 1)
271void QSqlError::setDatabaseText(const QString& databaseText)
272{
273 d->databaseError = databaseText;
274}
275#endif
276
277/*!
278 Returns the error type, or -1 if the type cannot be determined.
279*/
280
281QSqlError::ErrorType QSqlError::type() const
282{
283 return d->errorType;
284}
285
286/*!
287 \fn void QSqlError::setType(ErrorType type)
288 \obsolete
289
290 Sets the error type to the value of \a type.
291
292 Use QSqlError(const QString &driverText, const QString &databaseText,
293 ErrorType type, int number) instead
294
295 \sa type()
296*/
297
298#if QT_DEPRECATED_SINCE(5, 1)
299void QSqlError::setType(ErrorType type)
300{
301 d->errorType = type;
302}
303#endif
304
305/*!
306 \fn int QSqlError::number() const
307 \obsolete
308
309 Returns the database-specific error number, or -1 if it cannot be
310 determined.
311
312 Returns 0 if the error code is not an integer.
313
314 \warning Some databases use alphanumeric error codes, which makes
315 number() unreliable if such a database is used.
316
317 Use nativeErrorCode() instead
318
319 \sa nativeErrorCode()
320*/
321
322#if QT_DEPRECATED_SINCE(5, 3)
323int QSqlError::number() const
324{
325 return d->errorCode.isEmpty() ? -1 : d->errorCode.toInt();
326}
327#endif
328
329/*!
330 \fn void QSqlError::setNumber(int number)
331 \obsolete
332
333 Sets the database-specific error number to \a number.
334
335 Use QSqlError(const QString &driverText, const QString &databaseText,
336 ErrorType type, int number) instead
337
338 \sa number()
339*/
340
341#if QT_DEPRECATED_SINCE(5, 1)
342void QSqlError::setNumber(int number)
343{
344 d->errorCode = QString::number(number);
345}
346#endif
347
348/*!
349 Returns the database-specific error code, or an empty string if
350 it cannot be determined.
351*/
352
353QString QSqlError::nativeErrorCode() const
354{
355 return d->errorCode;
356}
357
358/*!
359 This is a convenience function that returns databaseText() and
360 driverText() concatenated into a single string.
361
362 \sa driverText(), databaseText()
363*/
364
365QString QSqlError::text() const
366{
367 QString result = d->databaseError;
368 if (!d->databaseError.isEmpty() && !d->driverError.isEmpty() && !d->databaseError.endsWith(s: QLatin1String("\n")))
369 result += QLatin1Char(' ');
370 result += d->driverError;
371 return result;
372}
373
374/*!
375 Returns \c true if an error is set, otherwise false.
376
377 Example:
378 \snippet code/src_sql_kernel_qsqlerror.cpp 0
379
380 \sa type()
381*/
382bool QSqlError::isValid() const
383{
384 return d->errorType != NoError;
385}
386
387QT_END_NAMESPACE
388

source code of qtbase/src/sql/kernel/qsqlerror.cpp