KDb

KDbResult.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003-2016 JarosÅ‚aw Staniek <[email protected]>
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
26 class KDbResult { Q_DECLARE_TR_FUNCTIONS(KDbResult) };
27 class KDbResultable {};
28 #endif
29 
30 #define ERRMSG(a) \
31  { if (m_msgHandler) m_msgHandler->showErrorMessage(a); }
32 
33 #define STORE_PREV_ERR \
34 
35 KDbResult::KDbResult(int code, const QString& message)
36  : d(new Data)
37 {
38  init(code, message);
39 }
40 
41 KDbResult::KDbResult(const QString& message)
42  : d(new Data)
43 {
44  init(ERR_OTHER, message);
45 }
46 
47 KDbResult::~KDbResult()
48 {
49 }
50 
51 void 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 
64 bool KDbResult::isError() const
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 
74 void KDbResult::setServerErrorCode(int errorCode)
75 {
76  d->serverErrorCode = errorCode;
77  d->serverErrorCodeSet = true;
78 }
79 
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 
103 QDebug 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 /*
125 KDbMessageHandler::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 /*
148 void 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 
157 class Q_DECL_HIDDEN KDbResultable::Private
158 {
159 public:
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 
176 KDbResultable::KDbResultable()
177  : d(new Private)
178 {
179 }
180 
181 KDbResultable::KDbResultable(const KDbResultable &other)
182  : m_result(other.m_result)
183  , d(new Private(*other.d))
184 {
185 }
186 
187 KDbResultable::~KDbResultable()
188 {
189  delete d;
190 }
191 
192 KDbResultable& KDbResultable::operator=(const KDbResultable &other)
193 {
194  d->messageHandler = other.messageHandler();
195  m_result = other.m_result;
196  return *this;
197 }
198 
199 KDbResult KDbResultable::result() const
200 {
201  return m_result;
202 }
203 
204 void 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 
224 void KDbResultable::showMessage()
225 {
226  if (d->messageHandler && m_result.isError()) {
227  d->messageHandler->showErrorMessage(m_result);
228  }
229 }
230 
231 QDebug 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();
249  description.clear();
250 }
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void init(int code, const QString &message)
Definition: KDbResult.cpp:51
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
QCA_EXPORT void init()
QDebug & nospace()
void clear()
bool success
result of the operation, true by default
Definition: KDbError.h:124
QDebug & space()
QString message
Error message, empty by default.
Definition: KDbError.h:128
void setMessageHandler(KDbMessageHandler *handler)
Sets message handler to handler.
Definition: KDbResult.cpp:214
bool allowToDiscardChanges
True if changes can be discarded, false by default If true, additional "Discard changes" message box ...
Definition: KDbError.h:125
Interface for classes providing a result.
void prependMessage(int code, const QString &message)
Sets result code and prepends message to an existing message.
Definition: KDbResult.cpp:80
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
virtual QString serverResultName() const
Definition: KDbResult.cpp:209
bool isEmpty() const const
QString description
Detailed error description, empty by default.
Definition: KDbError.h:129
bool isError() const
Definition: KDbResult.cpp:64
int column
Faulty column, -1 (the default) means: there is no faulty column.
Definition: KDbError.h:130
QString message
KDbMessageHandler * messageHandler() const
Definition: KDbResult.cpp:219
QString serverMessage
QString message
QString messageTitle
void setServerErrorCode(int errorCode)
Sets an implementation-specific error code of server-side operation.
Definition: KDbResult.cpp:74
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Sep 26 2022 04:04:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.