1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
Copyright (C) 2006-2010 Tuomas Suutari <thsuut@utu.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see the file COPYING); if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301 USA.
*/
#ifndef SQLDB_QUERYERRORS_H
#define SQLDB_QUERYERRORS_H
#include <qstring.h>
#include <QSqlError>
#include <QSqlQuery>
#define ERROR_CLASS(X,Y) \
struct X: public Y\
{\
X(const QString& message=QString()): Y(message) {}\
QString name() const { return QString::fromLatin1(#X); }\
}
#define ERROR_CLASS2(X,Y) \
struct X: public Y\
{\
X(const QString& queryLine=QString(),\
const QString& message=QString()): Y(queryLine, message) {}\
QString name() const { return QString::fromLatin1(#X); }\
}
namespace SQLDB
{
class Error: public std::exception
{
public:
Error(const QString& message=QString()): _message(message) {}
virtual ~Error() throw() {}
const QString& message() const { return _message; }
virtual QString name() const { return QString::fromLatin1("Error"); }
virtual const char* what() const throw()
{
if (_whatString.isNull()) {
_whatString = whatAsQString();
}
return _whatString.toLocal8Bit().constData();
}
virtual QString whatAsQString() const throw()
{
return name() + QLatin1String(": ") + message();
}
private:
mutable QString _whatString;
QString _message;
};
ERROR_CLASS(NotFoundError, Error);
ERROR_CLASS(DriverNotFoundError, NotFoundError);
ERROR_CLASS(DataNotFoundError, NotFoundError);
ERROR_CLASS(RowNotFoundError, DataNotFoundError);
ERROR_CLASS(EntryNotFoundError, DataNotFoundError);
ERROR_CLASS(OperationNotPossible, Error);
ERROR_CLASS(InitializationError, Error);
ERROR_CLASS(DriverLoadError, InitializationError);
ERROR_CLASS(ConnectionError, Error);
ERROR_CLASS(ConnectionCreateError, ConnectionError);
ERROR_CLASS(ConnectionOpenError, ConnectionError);
ERROR_CLASS(ConnectionCloseError, ConnectionError);
ERROR_CLASS(DatabaseError, Error);
ERROR_CLASS(DatabaseCreateError, DatabaseError);
ERROR_CLASS(DatabaseOpenError, DatabaseError);
ERROR_CLASS(DatabaseSchemaError, DatabaseOpenError);
ERROR_CLASS(TableCreateError, Error);
ERROR_CLASS(TransactionError, Error);
ERROR_CLASS(TransactionCreateError, TransactionError);
ERROR_CLASS(TransactionCommitError, TransactionError);
class SQLError: public Error
{
public:
explicit SQLError(const QString& queryLine=QString(),
const QString& message=QString()):
Error(message), _queryLine(queryLine) {}
virtual ~SQLError() throw() {}
const QString& queryLine() const { return _queryLine; }
QString name() const { return QString::fromLatin1("SQLError"); }
virtual QString whatAsQString() const throw()
{
return
Error::whatAsQString() + QLatin1String(". The query was \"") +
this->queryLine() + QLatin1String("\"");
}
private:
QString _queryLine;
};
ERROR_CLASS2(QueryError, SQLError);
ERROR_CLASS2(StatementError, SQLError);
class QtSQLError: public SQLError
{
public:
QtSQLError(const QSqlError& qtError,
const QString& message):
SQLError(QLatin1String("Unknown"), message),
_qtError(qtError)
{
Q_ASSERT(_qtError.isValid());
}
explicit QtSQLError(const QSqlQuery& executedQuery,
const QString& message=QString()):
SQLError(executedQuery.lastQuery(), message),
_qtError(executedQuery.lastError()),
_bindings(executedQuery.boundValues().values())
{
Q_ASSERT(_qtError.isValid());
}
virtual ~QtSQLError() throw()
{
}
virtual QString name() const { return QLatin1String("QtSQLError"); }
virtual QString whatAsQString() const throw()
{
QStringList bindingsAsStrings;
Q_FOREACH(QVariant x, _bindings)
bindingsAsStrings.push_back(x.toString());
return
SQLError::whatAsQString() +
QLatin1String(". Bindinds are (") +
bindingsAsStrings.join(QLatin1String(",")) + QLatin1String(")") +
QLatin1String(". The text of QSqlError is \"") +
_qtError.text() + QLatin1String("\"");
}
private:
QSqlError _qtError;
QList<QVariant> _bindings;
};
}
#undef ERROR_CLASS
#undef ERROR_CLASS2
#endif /* SQLDB_QUERYERRORS_H */
|