1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsqlerror.h"
5#include "qdebug.h"
6
7QT_BEGIN_NAMESPACE
8
9using namespace Qt::StringLiterals;
10
11#ifndef QT_NO_DEBUG_STREAM
12QDebug operator<<(QDebug dbg, const QSqlError &s)
13{
14 QDebugStateSaver saver(dbg);
15 dbg.nospace();
16 dbg << "QSqlError(" << s.nativeErrorCode() << ", " << s.driverText()
17 << ", " << s.databaseText() << ')';
18 return dbg;
19}
20#endif
21
22class QSqlErrorPrivate : public QSharedData
23{
24public:
25 QString driverError;
26 QString databaseError;
27 QSqlError::ErrorType errorType;
28 QString errorCode;
29};
30QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlErrorPrivate)
31
32/*!
33 \class QSqlError
34 \brief The QSqlError class provides SQL database error information.
35
36 \ingroup database
37 \inmodule QtSql
38
39 A QSqlError object can provide database-specific error data,
40 including the driverText() and databaseText() messages (or both
41 concatenated together as text()), and the nativeErrorCode() and
42 type().
43
44 \sa QSqlDatabase::lastError(), QSqlQuery::lastError()
45*/
46
47/*!
48 \enum QSqlError::ErrorType
49
50 This enum type describes the context in which the error occurred, e.g., a connection error, a statement error, etc.
51
52 \value NoError No error occurred.
53 \value ConnectionError Connection error.
54 \value StatementError SQL statement syntax error.
55 \value TransactionError Transaction failed error.
56 \value UnknownError Unknown error.
57*/
58
59/*! \fn QSqlError::QSqlError(QSqlError &&other)
60 Move-constructs a QSqlError instance, making it point at the same
61 object that \a other was pointing to.
62
63 \note The moved-from object \a other is placed in a
64 partially-formed state, in which the only valid operations are
65 destruction and assignment of a new value.
66
67 \since 5.10
68*/
69
70/*! \fn QSqlError::operator=(QSqlError &&other)
71 Move-assigns \a other to this QSqlError instance.
72
73 \note The moved-from object \a other is placed in a
74 partially-formed state, in which the only valid operations are
75 destruction and assignment of a new value.
76
77 \since 5.10
78*/
79
80/*! \fn QSqlError::swap(QSqlError &other)
81 Swaps error \a other with this error. This operation is very fast
82 and never fails.
83
84 \since 5.10
85*/
86
87/*!
88 Constructs an error containing the driver error text \a
89 driverText, the database-specific error text \a databaseText, the
90 type \a type and the error code \a code.
91*/
92QSqlError::QSqlError(const QString &driverText, const QString &databaseText,
93 ErrorType type, const QString &code)
94 : d(new QSqlErrorPrivate)
95{
96 d->driverError = driverText;
97 d->databaseError = databaseText;
98 d->errorType = type;
99 d->errorCode = code;
100}
101
102
103/*!
104 Creates a copy of \a other.
105*/
106QSqlError::QSqlError(const QSqlError &other)
107 = default;
108
109/*!
110 Assigns the \a other error's values to this error.
111*/
112
113QSqlError &QSqlError::operator=(const QSqlError &other)
114 = default;
115
116
117/*!
118 Compare the \a other error's type() and nativeErrorCode()
119 to this error and returns \c true, if it equal.
120*/
121
122bool QSqlError::operator==(const QSqlError &other) const
123{
124 return (d->errorType == other.d->errorType &&
125 d->errorCode == other.d->errorCode);
126}
127
128
129/*!
130 Compare the \a other error's type() and nativeErrorCode()
131 to this error and returns \c true if it is not equal.
132*/
133
134bool QSqlError::operator!=(const QSqlError &other) const
135{
136 return (d->errorType != other.d->errorType ||
137 d->errorCode != other.d->errorCode);
138}
139
140
141/*!
142 Destroys the object and frees any allocated resources.
143*/
144
145QSqlError::~QSqlError()
146 = default;
147
148/*!
149 Returns the text of the error as reported by the driver. This may
150 contain database-specific descriptions. It may also be empty.
151
152 \sa databaseText(), text()
153*/
154QString QSqlError::driverText() const
155{
156 return d->driverError;
157}
158
159/*!
160 Returns the text of the error as reported by the database. This
161 may contain database-specific descriptions; it may be empty.
162
163 \sa driverText(), text()
164*/
165
166QString QSqlError::databaseText() const
167{
168 return d->databaseError;
169}
170
171/*!
172 Returns the error type, or -1 if the type cannot be determined.
173*/
174
175QSqlError::ErrorType QSqlError::type() const
176{
177 return d->errorType;
178}
179
180/*!
181 Returns the database-specific error code, or an empty string if
182 it cannot be determined.
183 \note Some drivers (like DB2 or ODBC) may return more than one
184 error code. When this happens, \c ; is used as separator between
185 the error codes.
186*/
187
188QString QSqlError::nativeErrorCode() const
189{
190 return d->errorCode;
191}
192
193/*!
194 This is a convenience function that returns databaseText() and
195 driverText() concatenated into a single string.
196
197 \sa driverText(), databaseText()
198*/
199
200QString QSqlError::text() const
201{
202 QString result = d->databaseError;
203 if (!d->databaseError.isEmpty() && !d->driverError.isEmpty() && !d->databaseError.endsWith(c: u'\n'))
204 result += u' ';
205 result += d->driverError;
206 return result;
207}
208
209/*!
210 Returns \c true if an error is set, otherwise false.
211
212 Example:
213 \snippet code/src_sql_kernel_qsqlerror.cpp 0
214
215 \sa type()
216*/
217bool QSqlError::isValid() const
218{
219 return d->errorType != NoError;
220}
221
222QT_END_NAMESPACE
223

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