KDb

KDbParser.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
3 Copyright (C) 2004-2018 Jarosław Staniek <staniek@kde.org>
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
30bool parseData();
31
32//! Cache
33class ParserStatic
34{
35public:
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;
48private:
49 Q_DISABLE_COPY(ParserStatic)
50};
51
52Q_GLOBAL_STATIC(ParserStatic, KDb_parserStatic)
53
55 : d(new KDbParserPrivate)
56{
57 d->connection = connection;
58}
59
60KDbParser::~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
110void 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
145class Q_DECL_HIDDEN KDbParserError::Private
146{
147public:
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 }
160 QString message;
161 QByteArray token;
162 int position = -1;
163};
164
166 : d(new Private)
167{
168}
169
170KDbParserError::KDbParserError(const QString &type, const QString &message, const QByteArray &token,
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
186KDbParserError::~KDbParserError()
187{
188 delete d;
189}
190
191KDbParserError& KDbParserError::operator=(const KDbParserError &other)
192{
193 if (this != &other) {
194 d->copy(*other.d);
195 }
196 return *this;
197}
198
199bool 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
219QDebug 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}
Provides database connection, allowing queries and data modification.
Specialized string for escaping.
Meta-data for a field.
Definition KDbField.h:72
Provides detailed error description about KDbParser.
Definition KDbParser.h:41
QString type() const
int position() const
KDbParserError()
Empty constructor.
QString message() const
A parser tool for SQL statements.
Definition KDbParser.h:104
KDbQuerySchema * query()
Definition KDbParser.cpp:83
bool parse(const KDbEscapedString &sql, KDbQuerySchema *query=nullptr)
Clears the parser's status and runs the parsing for a raw SQL statement.
KDbConnection * connection()
Definition KDbParser.cpp:90
KDbEscapedString statement() const
KDbTableSchema * table()
Definition KDbParser.cpp:76
StatementType statementType() const
Definition KDbParser.cpp:65
QString statementTypeString() const
Definition KDbParser.cpp:70
void reset()
Reset the parser's status (table, query, error, statement, statement type).
StatementType
The type of the statement.
Definition KDbParser.h:111
KDbParserError error() const
KDbQuerySchema provides information about database query.
Type type(const QSqlDatabase &db)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
const QList< QKeySequence > & copy()
bool operator==(const StyleDelim &l, const StyleDelim &r)
QDebug & maybeSpace()
QDebug & space()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.