KDb

KDbResult.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbResult.h"
21#include "KDbMessageHandler.h"
22#include "kdb_debug.h"
23#include "KDb_p.h"
24
25#if 0 // needed by lupdate to avoid "Qualifying with unknown namespace/class" because header is generated
26class KDbResult { Q_DECLARE_TR_FUNCTIONS(KDbResult) };
27class KDbResultable {};
28#endif
29
30#define ERRMSG(a) \
31 { if (m_msgHandler) m_msgHandler->showErrorMessage(a); }
32
33#define STORE_PREV_ERR \
34
35KDbResult::KDbResult(int code, const QString& message)
36 : d(new Data)
37{
38 init(code, message);
39}
40
41KDbResult::KDbResult(const QString& message)
42 : d(new Data)
43{
44 init(ERR_OTHER, message);
45}
46
47KDbResult::~KDbResult()
48{
49}
50
51void KDbResult::init(int code, const QString& message)
52{
53 d->code = code;
54 d->errorSql = d->sql;
55 if (d->code == ERR_OTHER && message.isEmpty())
56 d->message = tr("Unspecified error encountered");
57 else
58 d->message = message;
59//! @todo
60/* if (m_hasError)
61 ERRMSG(this);*/
62}
63
65{
66 return d->code != ERR_NONE
67 || d->serverErrorCodeSet
68 || !d->message.isEmpty()
69 || !d->messageTitle.isEmpty()
70 || !d->errorSql.isEmpty()
71 || !d->serverMessage.isEmpty();
72}
73
75{
76 d->serverErrorCode = errorCode;
77 d->serverErrorCodeSet = true;
78}
79
80void KDbResult::prependMessage(int code, const QString& message)
81{
82 if (d->code == ERR_NONE) {
83 if (code == ERR_NONE)
84 d->code = ERR_OTHER;
85 else
86 d->code = code;
87 }
88 if (!message.isEmpty()) {
89 if (d->message.isEmpty())
90 d->message = message;
91 else
92 d->message = message + QLatin1Char(' ') + d->message;
93 }
94// if (m_hasError)
95//! @todo IMPORTANT: ERRMSG(this);
96}
97
99{
100 prependMessage(ERR_NONE, message);
101}
102
103QDebug operator<<(QDebug dbg, const KDbResult& result)
104{
105 if (result.isError()) {
106 dbg.nospace() << "KDbResult:";
107 dbg.nospace() << " CODE=" << result.code();
108 if (!result.message().isEmpty())
109 dbg.nospace() << " MESSAGE=" << result.message();
110 if (!result.messageTitle().isEmpty())
111 dbg.nospace() << " TITLE=" << result.messageTitle();
112 if (!result.sql().isEmpty())
113 dbg.nospace() << " SQL=" << result.sql();
114 if (!result.errorSql().isEmpty())
115 dbg.nospace() << " ERR_SQL=" << result.errorSql();
116 dbg.nospace() << " SERVER_ERROR_CODE=" << result.serverErrorCode() /*<< "NAME:" << result.serverResultName()*/;
117 if (!result.serverMessage().isEmpty())
118 dbg.nospace() << " SERVER_MESSAGE=" << result.serverMessage();
119 } else {
120 dbg.nospace() << "KDbResult: OK";
121 }
122 return dbg;
123}
124/*
125KDbMessageHandler::ButtonCode KDbObject::askQuestion(
126 KDbMessageHandler::QuestionType messageType,
127 const QString& message,
128 const QString &caption,
129 KDbMessageHandler::ButtonCode defaultResult,
130 const KDbGuiItem &buttonYes,
131 const KDbGuiItem &buttonNo,
132 const QString &dontShowAskAgainName,
133 KDbMessageHandler::Options options,
134 KDbMessageHandler* msgHandler)
135{
136 if (msgHandler)
137 return msgHandler->askQuestion(messageType, message, caption, defaultResult, buttonYes, buttonNo,
138 dontShowAskAgainName, options);
139
140 if (m_msgHandler)
141 return m_msgHandler->askQuestionInternal(messageType, message, caption, defaultResult, buttonYes, buttonNo,
142 dontShowAskAgainName, options);
143
144 return defaultResult;
145}*/
146
147/*
148void KDbResultable::storePreviousError()
149{
150 m_previousServerResultCode = m_previousServerResultCode2;
151 m_previousServerResultName = m_previousServerResultName2;
152 m_previousServerResultCode2 = m_serverResultCode;
153 m_previousServerResultName2 = m_serverResultName;
154 kdbDebug() << "Object ERROR:" << m_previousServerResultCode2 << ":" << m_previousServerResultName2;
155}*/
156
157class Q_DECL_HIDDEN KDbResultable::Private
158{
159public:
160 Private() : messageHandler(nullptr)
161 {
162 }
163 Private(const Private &other) {
164 copy(other);
165 }
166#define KDbResultablePrivateArgs(o) std::tie(o.messageHandler)
167 void copy(const Private &other) {
168 KDbResultablePrivateArgs((*this)) = KDbResultablePrivateArgs(other);
169 }
170 bool operator==(const Private &other) const {
171 return KDbResultablePrivateArgs((*this)) == KDbResultablePrivateArgs(other);
172 }
173 KDbMessageHandler* messageHandler;
174};
175
176KDbResultable::KDbResultable()
177 : d(new Private)
178{
179}
180
181KDbResultable::KDbResultable(const KDbResultable &other)
182 : m_result(other.m_result)
183 , d(new Private(*other.d))
184{
185}
186
187KDbResultable::~KDbResultable()
188{
189 delete d;
190}
191
192KDbResultable& KDbResultable::operator=(const KDbResultable &other)
193{
194 d->messageHandler = other.messageHandler();
195 m_result = other.m_result;
196 return *this;
197}
198
199KDbResult KDbResultable::result() const
200{
201 return m_result;
202}
203
204void KDbResultable::clearResult()
205{
206 m_result = KDbResult();
207}
208
210{
211 return QString();
212}
213
215{
216 d->messageHandler = handler;
217}
218
220{
221 return d->messageHandler;
222}
223
224void KDbResultable::showMessage()
225{
226 if (d->messageHandler && m_result.isError()) {
227 d->messageHandler->showErrorMessage(m_result);
228 }
229}
230
231QDebug operator<<(QDebug dbg, const KDbResultInfo &info)
232{
233 dbg.nospace() << "ResultInfo(";
234 dbg.space() << "success:" << info.success
235 << "allowToDiscardChanges:" << info.allowToDiscardChanges
236 << "message:" << info.message
237 << "description:" << info.description
238 << "column:" << info.column;
239 dbg.nospace() << ")";
240 return dbg.space();
241}
242
244{
245 success = true;
246 allowToDiscardChanges = false;
247 column = -1;
248 message.clear();
250}
virtual void showErrorMessage(KDbMessageHandler::MessageType messageType, const QString &message, const QString &details=QString(), const QString &caption=QString())=0
bool success
result of the operation, true by default
Definition KDbError.h:124
QString description
Detailed error description, empty by default.
Definition KDbError.h:129
QString message
Error message, empty by default.
Definition KDbError.h:128
bool allowToDiscardChanges
True if changes can be discarded, false by default If true, additional "Discard changes" message box ...
Definition KDbError.h:125
int column
Faulty column, -1 (the default) means: there is no faulty column.
Definition KDbError.h:130
QString messageTitle
QString serverMessage
bool isError() const
Definition KDbResult.cpp:64
void prependMessage(int code, const QString &message)
Sets result code and prepends message to an existing message.
Definition KDbResult.cpp:80
void setServerErrorCode(int errorCode)
Sets an implementation-specific error code of server-side operation.
Definition KDbResult.cpp:74
QString message
void init(int code, const QString &message)
Definition KDbResult.cpp:51
Interface for classes providing a result.
void setMessageHandler(KDbMessageHandler *handler)
Sets message handler to handler.
virtual QString serverResultName() const
KDbMessageHandler * messageHandler() const
const QList< QKeySequence > & copy()
bool operator==(const StyleDelim &l, const StyleDelim &r)
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
QCA_EXPORT void init()
QDebug & nospace()
QDebug & space()
void clear()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:12:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.