File indexing completed on 2022-11-29 19:02:28

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KDbResult.h"
0021 #include "KDbMessageHandler.h"
0022 #include "kdb_debug.h"
0023 #include "KDb_p.h"
0024 
0025 #if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class" because header is generated
0026 class KDbResult { Q_DECLARE_TR_FUNCTIONS(KDbResult) };
0027 class KDbResultable {};
0028 #endif
0029 
0030 #define ERRMSG(a) \
0031     { if (m_msgHandler) m_msgHandler->showErrorMessage(a); }
0032 
0033 #define STORE_PREV_ERR \
0034 
0035 KDbResult::KDbResult(int code, const QString& message)
0036     : d(new Data)
0037 {
0038     init(code, message);
0039 }
0040 
0041 KDbResult::KDbResult(const QString& message)
0042     : d(new Data)
0043 {
0044     init(ERR_OTHER, message);
0045 }
0046 
0047 KDbResult::~KDbResult()
0048 {
0049 }
0050 
0051 void KDbResult::init(int code, const QString& message)
0052 {
0053     d->code = code;
0054     d->errorSql = d->sql;
0055     if (d->code == ERR_OTHER && message.isEmpty())
0056         d->message = tr("Unspecified error encountered");
0057     else
0058         d->message = message;
0059 //! @todo
0060 /*    if (m_hasError)
0061         ERRMSG(this);*/
0062 }
0063 
0064 bool KDbResult::isError() const
0065 {
0066     return d->code != ERR_NONE
0067             || d->serverErrorCodeSet
0068             || !d->message.isEmpty()
0069             || !d->messageTitle.isEmpty()
0070             || !d->errorSql.isEmpty()
0071             || !d->serverMessage.isEmpty();
0072 }
0073 
0074 void KDbResult::setServerErrorCode(int errorCode)
0075 {
0076     d->serverErrorCode = errorCode;
0077     d->serverErrorCodeSet = true;
0078 }
0079 
0080 void KDbResult::prependMessage(int code, const QString& message)
0081 {
0082     if (d->code == ERR_NONE) {
0083         if (code == ERR_NONE)
0084             d->code = ERR_OTHER;
0085         else
0086             d->code = code;
0087     }
0088     if (!message.isEmpty()) {
0089         if (d->message.isEmpty())
0090             d->message = message;
0091         else
0092             d->message = message + QLatin1Char(' ') + d->message;
0093     }
0094 //    if (m_hasError)
0095 //! @todo IMPORTANT: ERRMSG(this);
0096 }
0097 
0098 void KDbResult::prependMessage(const QString& message)
0099 {
0100     prependMessage(ERR_NONE, message);
0101 }
0102 
0103 QDebug operator<<(QDebug dbg, const KDbResult& result)
0104 {
0105     if (result.isError()) {
0106         dbg.nospace() << "KDbResult:";
0107         dbg.nospace() << " CODE=" << result.code();
0108         if (!result.message().isEmpty())
0109             dbg.nospace() << " MESSAGE=" << result.message();
0110         if (!result.messageTitle().isEmpty())
0111             dbg.nospace() << " TITLE=" << result.messageTitle();
0112         if (!result.sql().isEmpty())
0113             dbg.nospace() << " SQL=" << result.sql();
0114         if (!result.errorSql().isEmpty())
0115             dbg.nospace() << " ERR_SQL=" << result.errorSql();
0116         dbg.nospace() << " SERVER_ERROR_CODE=" << result.serverErrorCode() /*<< "NAME:" << result.serverResultName()*/;
0117         if (!result.serverMessage().isEmpty())
0118             dbg.nospace() << " SERVER_MESSAGE=" << result.serverMessage();
0119     } else {
0120         dbg.nospace() << "KDbResult: OK";
0121     }
0122     return dbg;
0123 }
0124 /*
0125 KDbMessageHandler::ButtonCode KDbObject::askQuestion(
0126     KDbMessageHandler::QuestionType messageType,
0127     const QString& message,
0128     const QString &caption,
0129     KDbMessageHandler::ButtonCode defaultResult,
0130     const KDbGuiItem &buttonYes,
0131     const KDbGuiItem &buttonNo,
0132     const QString &dontShowAskAgainName,
0133     KDbMessageHandler::Options options,
0134     KDbMessageHandler* msgHandler)
0135 {
0136     if (msgHandler)
0137         return msgHandler->askQuestion(messageType, message, caption, defaultResult, buttonYes, buttonNo,
0138                                        dontShowAskAgainName, options);
0139 
0140     if (m_msgHandler)
0141         return m_msgHandler->askQuestionInternal(messageType, message, caption, defaultResult, buttonYes, buttonNo,
0142                                                  dontShowAskAgainName, options);
0143 
0144     return defaultResult;
0145 }*/
0146 
0147 /*
0148 void KDbResultable::storePreviousError()
0149 {
0150     m_previousServerResultCode = m_previousServerResultCode2;
0151     m_previousServerResultName = m_previousServerResultName2;
0152     m_previousServerResultCode2 = m_serverResultCode;
0153     m_previousServerResultName2 = m_serverResultName;
0154     kdbDebug() << "Object ERROR:" << m_previousServerResultCode2 << ":" << m_previousServerResultName2;
0155 }*/
0156 
0157 class Q_DECL_HIDDEN KDbResultable::Private
0158 {
0159 public:
0160     Private() : messageHandler(nullptr)
0161     {
0162     }
0163     Private(const Private &other) {
0164         copy(other);
0165     }
0166 #define KDbResultablePrivateArgs(o) std::tie(o.messageHandler)
0167     void copy(const Private &other) {
0168         KDbResultablePrivateArgs((*this)) = KDbResultablePrivateArgs(other);
0169     }
0170     bool operator==(const Private &other) const {
0171         return KDbResultablePrivateArgs((*this)) == KDbResultablePrivateArgs(other);
0172     }
0173     KDbMessageHandler* messageHandler;
0174 };
0175 
0176 KDbResultable::KDbResultable()
0177  : d(new Private)
0178 {
0179 }
0180 
0181 KDbResultable::KDbResultable(const KDbResultable &other)
0182     : m_result(other.m_result)
0183     , d(new Private(*other.d))
0184 {
0185 }
0186 
0187 KDbResultable::~KDbResultable()
0188 {
0189     delete d;
0190 }
0191 
0192 KDbResultable& KDbResultable::operator=(const KDbResultable &other)
0193 {
0194     d->messageHandler = other.messageHandler();
0195     m_result = other.m_result;
0196     return *this;
0197 }
0198 
0199 KDbResult KDbResultable::result() const
0200 {
0201     return m_result;
0202 }
0203 
0204 void KDbResultable::clearResult()
0205 {
0206     m_result = KDbResult();
0207 }
0208 
0209 QString KDbResultable::serverResultName() const
0210 {
0211     return QString();
0212 }
0213 
0214 void KDbResultable::setMessageHandler(KDbMessageHandler *handler)
0215 {
0216     d->messageHandler = handler;
0217 }
0218 
0219 KDbMessageHandler* KDbResultable::messageHandler() const
0220 {
0221     return d->messageHandler;
0222 }
0223 
0224 void KDbResultable::showMessage()
0225 {
0226     if (d->messageHandler && m_result.isError()) {
0227         d->messageHandler->showErrorMessage(m_result);
0228     }
0229 }
0230 
0231 QDebug operator<<(QDebug dbg, const KDbResultInfo &info)
0232 {
0233     dbg.nospace() << "ResultInfo(";
0234     dbg.space() << "success:" << info.success
0235                 << "allowToDiscardChanges:" << info.allowToDiscardChanges
0236                 << "message:" << info.message
0237                 << "description:" << info.description
0238                 << "column:" << info.column;
0239     dbg.nospace() << ")";
0240     return dbg.space();
0241 }
0242 
0243 void KDbResultInfo::clear()
0244 {
0245     success = true;
0246     allowToDiscardChanges = false;
0247     column = -1;
0248     message.clear();
0249     description.clear();
0250 }