0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtSql module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "qsqlerror.h"
|
|
43 |
#include "qdebug.h"
|
|
44 |
|
|
45 |
QT_BEGIN_NAMESPACE
|
|
46 |
|
|
47 |
#ifndef QT_NO_DEBUG_STREAM
|
|
48 |
QDebug operator<<(QDebug dbg, const QSqlError &s)
|
|
49 |
{
|
|
50 |
dbg.nospace() << "QSqlError(" << s.number() << ", " << s.driverText() <<
|
|
51 |
", " << s.databaseText() << ')';
|
|
52 |
return dbg.space();
|
|
53 |
}
|
|
54 |
#endif
|
|
55 |
|
|
56 |
/*!
|
|
57 |
\class QSqlError
|
|
58 |
\brief The QSqlError class provides SQL database error information.
|
|
59 |
|
|
60 |
\ingroup database
|
|
61 |
\inmodule QtSql
|
|
62 |
|
|
63 |
A QSqlError object can provide database-specific error data,
|
|
64 |
including the driverText() and databaseText() messages (or both
|
|
65 |
concatenated together as text()), and the error number() and
|
|
66 |
type(). The functions all have setters so that you can create and
|
|
67 |
return QSqlError objects from your own classes, for example from
|
|
68 |
your own SQL drivers.
|
|
69 |
|
|
70 |
\sa QSqlDatabase::lastError(), QSqlQuery::lastError()
|
|
71 |
*/
|
|
72 |
|
|
73 |
/*!
|
|
74 |
\enum QSqlError::ErrorType
|
|
75 |
|
|
76 |
This enum type describes the context in which the error occurred, e.g., a connection error, a statement error, etc.
|
|
77 |
|
|
78 |
\value NoError No error occurred.
|
|
79 |
\value ConnectionError Connection error.
|
|
80 |
\value StatementError SQL statement syntax error.
|
|
81 |
\value TransactionError Transaction failed error.
|
|
82 |
\value UnknownError Unknown error.
|
|
83 |
|
|
84 |
\omitvalue None
|
|
85 |
\omitvalue Connection
|
|
86 |
\omitvalue Statement
|
|
87 |
\omitvalue Transaction
|
|
88 |
\omitvalue Unknown
|
|
89 |
*/
|
|
90 |
|
|
91 |
/*!
|
|
92 |
Constructs an error containing the driver error text \a
|
|
93 |
driverText, the database-specific error text \a databaseText, the
|
|
94 |
type \a type and the optional error number \a number.
|
|
95 |
*/
|
|
96 |
|
|
97 |
QSqlError::QSqlError(const QString& driverText, const QString& databaseText, ErrorType type,
|
|
98 |
int number)
|
|
99 |
: driverError(driverText), databaseError(databaseText), errorType(type), errorNumber(number)
|
|
100 |
{
|
|
101 |
}
|
|
102 |
|
|
103 |
/*!
|
|
104 |
Creates a copy of \a other.
|
|
105 |
*/
|
|
106 |
QSqlError::QSqlError(const QSqlError& other)
|
|
107 |
: driverError(other.driverError), databaseError(other.databaseError),
|
|
108 |
errorType(other.errorType),
|
|
109 |
errorNumber(other.errorNumber)
|
|
110 |
{
|
|
111 |
}
|
|
112 |
|
|
113 |
/*!
|
|
114 |
Assigns the \a other error's values to this error.
|
|
115 |
*/
|
|
116 |
|
|
117 |
QSqlError& QSqlError::operator=(const QSqlError& other)
|
|
118 |
{
|
|
119 |
driverError = other.driverError;
|
|
120 |
databaseError = other.databaseError;
|
|
121 |
errorType = other.errorType;
|
|
122 |
errorNumber = other.errorNumber;
|
|
123 |
return *this;
|
|
124 |
}
|
|
125 |
|
|
126 |
/*!
|
|
127 |
Destroys the object and frees any allocated resources.
|
|
128 |
*/
|
|
129 |
|
|
130 |
QSqlError::~QSqlError()
|
|
131 |
{
|
|
132 |
}
|
|
133 |
|
|
134 |
/*!
|
|
135 |
Returns the text of the error as reported by the driver. This may
|
|
136 |
contain database-specific descriptions. It may also be empty.
|
|
137 |
|
|
138 |
\sa setDriverText() databaseText() text()
|
|
139 |
*/
|
|
140 |
QString QSqlError::driverText() const
|
|
141 |
{
|
|
142 |
return driverError;
|
|
143 |
}
|
|
144 |
|
|
145 |
/*!
|
|
146 |
Sets the driver error text to the value of \a driverText.
|
|
147 |
|
|
148 |
\sa driverText() setDatabaseText() text()
|
|
149 |
*/
|
|
150 |
|
|
151 |
void QSqlError::setDriverText(const QString& driverText)
|
|
152 |
{
|
|
153 |
driverError = driverText;
|
|
154 |
}
|
|
155 |
|
|
156 |
/*!
|
|
157 |
Returns the text of the error as reported by the database. This
|
|
158 |
may contain database-specific descriptions; it may be empty.
|
|
159 |
|
|
160 |
\sa setDatabaseText() driverText() text()
|
|
161 |
*/
|
|
162 |
|
|
163 |
QString QSqlError::databaseText() const
|
|
164 |
{
|
|
165 |
return databaseError;
|
|
166 |
}
|
|
167 |
|
|
168 |
/*!
|
|
169 |
Sets the database error text to the value of \a databaseText.
|
|
170 |
|
|
171 |
\sa databaseText() setDriverText() text()
|
|
172 |
*/
|
|
173 |
|
|
174 |
void QSqlError::setDatabaseText(const QString& databaseText)
|
|
175 |
{
|
|
176 |
databaseError = databaseText;
|
|
177 |
}
|
|
178 |
|
|
179 |
/*!
|
|
180 |
Returns the error type, or -1 if the type cannot be determined.
|
|
181 |
|
|
182 |
\sa setType()
|
|
183 |
*/
|
|
184 |
|
|
185 |
QSqlError::ErrorType QSqlError::type() const
|
|
186 |
{
|
|
187 |
return errorType;
|
|
188 |
}
|
|
189 |
|
|
190 |
/*!
|
|
191 |
Sets the error type to the value of \a type.
|
|
192 |
|
|
193 |
\sa type()
|
|
194 |
*/
|
|
195 |
|
|
196 |
void QSqlError::setType(ErrorType type)
|
|
197 |
{
|
|
198 |
errorType = type;
|
|
199 |
}
|
|
200 |
|
|
201 |
/*!
|
|
202 |
Returns the database-specific error number, or -1 if it cannot be
|
|
203 |
determined.
|
|
204 |
|
|
205 |
\sa setNumber()
|
|
206 |
*/
|
|
207 |
|
|
208 |
int QSqlError::number() const
|
|
209 |
{
|
|
210 |
return errorNumber;
|
|
211 |
}
|
|
212 |
|
|
213 |
/*!
|
|
214 |
Sets the database-specific error number to \a number.
|
|
215 |
|
|
216 |
\sa number()
|
|
217 |
*/
|
|
218 |
|
|
219 |
void QSqlError::setNumber(int number)
|
|
220 |
{
|
|
221 |
errorNumber = number;
|
|
222 |
}
|
|
223 |
|
|
224 |
/*!
|
|
225 |
This is a convenience function that returns databaseText() and
|
|
226 |
driverText() concatenated into a single string.
|
|
227 |
|
|
228 |
\sa driverText() databaseText()
|
|
229 |
*/
|
|
230 |
|
|
231 |
QString QSqlError::text() const
|
|
232 |
{
|
|
233 |
QString result = databaseError;
|
|
234 |
if (!databaseError.endsWith(QLatin1String("\n")))
|
|
235 |
result += QLatin1Char(' ');
|
|
236 |
result += driverError;
|
|
237 |
return result;
|
|
238 |
}
|
|
239 |
|
|
240 |
/*!
|
|
241 |
Returns true if an error is set, otherwise false.
|
|
242 |
|
|
243 |
Example:
|
|
244 |
\snippet doc/src/snippets/code/src_sql_kernel_qsqlerror.cpp 0
|
|
245 |
|
|
246 |
\sa type()
|
|
247 |
*/
|
|
248 |
bool QSqlError::isValid() const
|
|
249 |
{
|
|
250 |
return errorType != NoError;
|
|
251 |
}
|
|
252 |
|
|
253 |
QT_END_NAMESPACE
|