KDb

KDbParser.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003 Lucijan Busch <[email protected]>
3  Copyright (C) 2004-2018 JarosÅ‚aw Staniek <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "KDbParser.h"
22 #include "KDbParser_p.h"
23 #include "generated/sqlparser.h"
24 
25 #include "KDbConnection.h"
26 #include "KDbTableSchema.h"
27 
28 #include <vector>
29 
30 bool parseData();
31 
32 //! Cache
33 class ParserStatic
34 {
35 public:
36  ParserStatic()
37  : statementTypeStrings({
38  QLatin1String("None"),
39  QLatin1String("Select"),
40  QLatin1String("CreateTable"),
41  QLatin1String("AlterTable"),
42  QLatin1String("Insert"),
43  QLatin1String("Update"),
44  QLatin1String("Delete")})
45  {
46  }
47  const std::vector<QString> statementTypeStrings;
48 private:
49  Q_DISABLE_COPY(ParserStatic)
50 };
51 
52 Q_GLOBAL_STATIC(ParserStatic, KDb_parserStatic)
53 
55  : d(new KDbParserPrivate)
56 {
57  d->connection = connection;
58 }
59 
60 KDbParser::~KDbParser()
61 {
62  delete d;
63 }
64 
66 {
67  return d->statementType;
68 }
69 
71 {
72  Q_ASSERT(size_t(d->statementType) < sizeof(KDb_parserStatic->statementTypeStrings));
73  return KDb_parserStatic->statementTypeStrings[d->statementType];
74 }
75 
77 {
78  KDbTableSchema *t = d->table;
79  d->table = nullptr;
80  return t;
81 }
82 
84 {
85  KDbQuerySchema *s = d->query;
86  d->query = nullptr;
87  return s;
88 }
89 
91 {
92  return d->connection;
93 }
94 
96 {
97  return d->connection;
98 }
99 
101 {
102  return d->error;
103 }
104 
106 {
107  return d->sql;
108 }
109 
110 void KDbParser::init()
111 {
112  if (d->initialized)
113  return;
114  // nothing to do
115  d->initialized = true;
116 }
117 
119 {
120  init();
121  reset();
122  d->sql = sql;
123  d->query = query;
124 
125  KDbParser *oldParser = globalParser;
126  KDbField *oldField = globalField;
127  globalParser = this;
128  globalField = nullptr;
129  bool res = parseData();
130  globalParser = oldParser;
131  globalField = oldField;
132  if (query) { // if existing query was supplied to parse() nullptr should be returned by query()
133  d->query = nullptr;
134  }
135  return res;
136 }
137 
139 {
140  d->reset();
141 }
142 
143 //-------------------------------------
144 
145 class Q_DECL_HIDDEN KDbParserError::Private
146 {
147 public:
148  Private() {}
149  Private(const Private &other) {
150  copy(other);
151  }
152 #define KDbParserErrorPrivateArgs(o) std::tie(o.type, o.message, o.token, o.position)
153  void copy(const Private &other) {
154  KDbParserErrorPrivateArgs((*this)) = KDbParserErrorPrivateArgs(other);
155  }
156  bool operator==(const Private &other) const {
157  return KDbParserErrorPrivateArgs((*this)) == KDbParserErrorPrivateArgs(other);
158  }
159  QString type;
161  QByteArray token;
162  int position = -1;
163 };
164 
166  : d(new Private)
167 {
168 }
169 
171  int position)
172  : d(new Private)
173 {
174  d->type = type;
175  d->message = message;
176  d->token = token;
177  d->position = position;
178 }
179 
181  : d(new Private(*other.d))
182 {
183  *d = *other.d;
184 }
185 
186 KDbParserError::~KDbParserError()
187 {
188  delete d;
189 }
190 
191 KDbParserError& KDbParserError::operator=(const KDbParserError &other)
192 {
193  if (this != &other) {
194  d->copy(*other.d);
195  }
196  return *this;
197 }
198 
199 bool KDbParserError::operator==(const KDbParserError &other) const
200 {
201  return *d == *other.d;
202 }
203 
205 {
206  return d->type;
207 }
208 
210 {
211  return d->message;
212 }
213 
215 {
216  return d->position;
217 }
218 
219 QDebug operator<<(QDebug dbg, const KDbParserError& error)
220 {
221  QDebugStateSaver saver(dbg);
222  if (error.type().isEmpty() && error.message().isEmpty()) {
223  dbg.space() << "KDb:KDbParserError: None";
224  } else {
225  dbg.space() << "KDb:KDbParserError: type=" << error.type() << "message=" << error.message()
226  << "pos=" << error.position() << ")";
227  }
228  return dbg.maybeSpace();
229 }
KDbParserError()
Empty constructor.
Definition: KDbParser.cpp:165
KDbConnection * connection()
Definition: KDbParser.cpp:90
Type type(const QSqlDatabase &db)
Specialized string for escaping.
QDebug & space()
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
void reset()
Reset the parser's status (table, query, error, statement, statement type).
Definition: KDbParser.cpp:138
StatementType statementType() const
Definition: KDbParser.cpp:65
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QDebug & maybeSpace()
int position() const
Definition: KDbParser.cpp:214
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
A parser tool for SQL statements.
Definition: KDbParser.h:103
QString message() const
Definition: KDbParser.cpp:209
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
bool parse(const KDbEscapedString &sql, KDbQuerySchema *query=nullptr)
Clears the parser's status and runs the parsing for a raw SQL statement.
Definition: KDbParser.cpp:118
QString type() const
Definition: KDbParser.cpp:204
KDbQuerySchema * query()
Definition: KDbParser.cpp:83
QString statementTypeString() const
Definition: KDbParser.cpp:70
KDbTableSchema * table()
Definition: KDbParser.cpp:76
KDbQuerySchema provides information about database query.
KDbEscapedString statement() const
Definition: KDbParser.cpp:105
Meta-data for a field.
Definition: KDbField.h:71
KDbParserError error() const
Definition: KDbParser.cpp:100
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
Provides detailed error description about KDbParser.
Definition: KDbParser.h:40
QString message
StatementType
The type of the statement.
Definition: KDbParser.h:111
const QList< QKeySequence > & copy()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.