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