summaryrefslogtreecommitdiffstats
path: root/src/partition/jsondbqueryparser.cpp
blob: 39eb6b81d06de9ea9a32a444df01c79e71afa4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include <QString>

#include "jsondbqueryparser.h"
#include "jsondbquerytokenizer_p.h"
#include "jsondbutils_p.h"
#include "jsondbstrings.h"
#include "jsondbsettings.h"

QT_BEGIN_NAMESPACE_JSONDB_PARTITION

class JsonDbQueryParserPrivate
{
    Q_DECLARE_PUBLIC(JsonDbQueryParser)
public:
    JsonDbQueryParserPrivate(JsonDbQueryParser *q)
        : q_ptr(q) { }
    bool parse();
    QJsonValue termValue(const JsonDbQueryTerm &term) const;

    JsonDbQueryParser *q_ptr;
    QString query;
    QMap<QString, QJsonValue> bindings;
    JsonDbQuery spec;
    QString errorString;
};

static QJsonObject parseJsonObject(JsonDbQueryTokenizer &tokenizer, bool *ok);

static QJsonValue parseJsonLiteral(const QString &json, JsonDbQueryTerm *term, bool *ok)
{
    const ushort trueLiteral[] = {'t','r','u','e', 0};
    const ushort falseLiteral[] = {'f','a','l','s','e', 0};
    const ushort *literal = json.utf16();
    QJsonValue value(QJsonValue::Undefined);
    Q_ASSERT(ok != NULL);
    *ok = true;
    switch (literal[0]) {
    case '"':
        value = json.mid(1, json.size()-2);
        break;
    case 't':
        // we will interpret  "true0something" as true is it a real problem ?
        for (int i = 1; i < 5 /* 'true0' length */; ++i) {
            if (trueLiteral[i] != literal[i]) {
                *ok = false;
                return value;
            }
        }
        value = true;
        break;
    case 'f':
        // we will interpret  "false0something" as false is it a real problem ?
        for (int i = 1; i < 6  /* 'false0' length */; ++i) {
            if (falseLiteral[i] != literal[i]) {
                *ok = false;
                return value;
            }
        }
        value = false;
        break;
    case '%':
    {
        const QString name = json.mid(1);
        if (term)
            term->setVariable(name);
        break;
    }
    case 0:
        // This can happen if json.length() == 0
        *ok = false;
        return value;
    default:
        int result = json.toInt(ok);
        if (*ok) {
            value = result;
        } else {
            // bad luck, it can be only a double
            value = json.toDouble(ok);
        }
    }
    if (term)
        term->setValue(value);
    return value;
}

static QJsonArray parseJsonArray(JsonDbQueryTokenizer &tokenizer, bool *ok)
{
    QJsonArray array;

    Q_ASSERT(ok != 0);
    *ok = true;
    for (QString tkn = tokenizer.pop(); !tkn.isEmpty(); tkn = tokenizer.pop()) {
        if (tkn == QLatin1Char(']') || tkn == QLatin1Char('|'))
            break;
        else if (tkn == QLatin1Char('['))
            array.append(parseJsonArray(tokenizer, ok));
        else if (tkn == QLatin1Char('{'))
            array.append(parseJsonObject(tokenizer, ok));
        else
            array.append(parseJsonLiteral(tkn, 0, ok));
        tkn = tokenizer.pop();
        if (tkn == QLatin1Char(']')) {
            break;
        } else if (tkn != QLatin1Char(',')) {
            *ok = false;
            break;
        }
    }
    return array;
}

static QJsonObject parseJsonObject(JsonDbQueryTokenizer &tokenizer, bool *ok)
{
    QJsonObject object;
    Q_ASSERT(ok != 0);
    *ok = true;
    for (QString tkn = tokenizer.popIdentifier(); !tkn.isEmpty(); tkn = tokenizer.popIdentifier()) {
        if (tkn == QLatin1Char('}') || tkn == QLatin1Char('|'))
            break;
        QString key = tkn;
        tkn = tokenizer.pop();
        if (tkn != QLatin1Char(':')) {
            *ok = false;
            break;
        }

        tkn = tokenizer.pop();
        QJsonValue value;
        if (tkn == QLatin1Char('['))
            value = parseJsonArray(tokenizer, ok);
        else if (tkn == QLatin1Char('{'))
            value = parseJsonObject(tokenizer, ok);
        else
            value = parseJsonLiteral(tkn, 0, ok);
        object.insert(key, value);
        tkn = tokenizer.pop();
        if (tkn == QLatin1Char('}')) {
            break;
        } else if (tkn != QLatin1Char(',')) {
            *ok = false;
            break;
        }
    }
    return object;
}

bool JsonDbQueryParserPrivate::parse()
{
    spec = JsonDbQuery();

    if (!query.startsWith(QLatin1Char('[')))
        return false;
    spec.query = query;
    spec.bindings = bindings;

    QStringList queryExplanation;
    bool parseError = false;
    JsonDbQueryTokenizer tokenizer(query);
    QString token;
    while (!parseError
           && !(token = tokenizer.pop()).isEmpty()) {
        if (token != QLatin1Char('[')) {
            qCritical() << "unexpected token" << token;
            break;
        }
        token = tokenizer.pop();
        if (token == QLatin1Char('?')) {
            JsonDbOrQueryTerm oqt;
            do {
                QString fieldSpec = tokenizer.popIdentifier();
                if (fieldSpec == QLatin1Char('|'))
                    fieldSpec = tokenizer.popIdentifier();

                QString opOrJoin = tokenizer.pop();
                QString op;
                QStringList joinFields;
                QString joinField;
                while (opOrJoin == QLatin1String("->")) {
                    joinFields.append(fieldSpec);
                    fieldSpec = tokenizer.popIdentifier();
                    opOrJoin = tokenizer.pop();
                }
                if (joinFields.size())
                    joinField = joinFields.join(QStringLiteral("->"));
                op = opOrJoin;


                JsonDbQueryTerm term;
                if (!joinField.isEmpty())
                    term.setJoinField(joinField);
                if (fieldSpec.startsWith(QLatin1Char('%'))) {
                    const QString name = fieldSpec.mid(1);
                    QJsonValue val = bindings.value(name, QJsonValue(QJsonValue::Undefined));
                    if (!val.isUndefined())
                        spec.bindings.insert(name, val);
                    term.setPropertyVariable(name);
                } else {
                    term.setPropertyName(fieldSpec);
                }
                term.setOp(op);
                if (op == QLatin1String("=~")
                        || op == QLatin1String("!=~")) {
                    QString tvs = tokenizer.pop();
                    int sepPos = 1; // assuming it's a literal "/regexp/modifiers"
                    if (tvs.startsWith(QLatin1Char('%'))) {
                        const QString name = tvs.mid(1);
                        if (bindings.contains(name)) {
                            tvs = bindings.value(name).toString();
                            sepPos = 0;
                        }
                    } else if (!tvs.startsWith(QLatin1Char('\"'))) {
                        queryExplanation.append(QStringLiteral("Failed to parse query regular expression '%1' in query '%2' %3 op %4")
                                                .arg(tvs, query, fieldSpec, op));
                        parseError = true;
                        break;
                    }
                    QChar sep = tvs[sepPos];
                    int eor = sepPos;
                    do {
                        eor = tvs.indexOf(sep, eor+1); // end of regexp;
                        //qDebug() << "tvs" << tvs << "eor" << eor << "tvs[eor-1]" << ((eor > 0) ? tvs[eor-1] : QChar('*'));
                        if (eor <= sepPos) {
                            parseError = true;
                            break;
                        }
                    } while ((eor > 0) && (tvs[eor-1] == '\\'));
                    QString modifiers = tvs.mid(eor+1,tvs.size()-eor-2*sepPos);
                    if (jsondbSettings->debug()) {
                        qDebug() << "modifiers" << modifiers;
                        qDebug() << "regexp" << tvs.mid(sepPos + 1, eor-sepPos-1);
                    }
                    if (modifiers.contains('w'))
                        term.regExp().setPatternSyntax(QRegExp::Wildcard);
                    if (modifiers.contains('i'))
                        term.regExp().setCaseSensitivity(Qt::CaseInsensitive);
                    //qDebug() << "pattern" << tvs.mid(2, eor-2);
                    term.regExp().setPattern(tvs.mid(sepPos + 1, eor-sepPos-1));
                } else if (op != QLatin1String("exists") && op != QLatin1String("notExists")) {
                    bool ok = true;

                    QString value = tokenizer.pop();
                    if (value == QLatin1Char('[')) {
                        QJsonValue value = parseJsonArray(tokenizer, &ok);
                        if (ok)
                            term.setValue(value);
                    } else if (value == QLatin1Char('{')) {
                        QJsonValue value = parseJsonObject(tokenizer, &ok);
                        if (ok)
                            term.setValue(value);
                    } else {
                        parseJsonLiteral(value, &term, &ok);
                    }

                    if (!ok) {
                        queryExplanation.append(QStringLiteral("Failed to parse query value '%1' in query '%2' %3 op %4")
                                                .arg(value, query, fieldSpec, op));
                        parseError = true;
                        break;
                    }
                }

                oqt.addTerm(term);
            } while (tokenizer.peek() != QLatin1Char(']'));
            spec.queryTerms.append(oqt);
        } else if (token == QLatin1Char('=')) {
            QString curlyBraceToken = tokenizer.pop();
            if (curlyBraceToken != QLatin1Char('{')) {
                queryExplanation.append(QStringLiteral("Parse error: expecting '{' but got '%1'")
                                        .arg(curlyBraceToken));
                parseError = true;
                break;
            }
            QString nextToken;
            while (!(nextToken = tokenizer.popIdentifier()).isEmpty()) {
                if (nextToken == QLatin1Char('}')) {
                    break;
                } else {
                    spec.mapKeyList.append(nextToken);
                    QString colon = tokenizer.pop();
                    if (colon != QLatin1Char(':')) {
                        queryExplanation.append(QStringLiteral("Parse error: expecting ':' but got '%1'").arg(colon));
                        parseError = true;
                        break;
                    }
                    nextToken = tokenizer.popIdentifier();

                    while (tokenizer.peek() == QLatin1String("->")) {
                        QString op = tokenizer.pop();
                        nextToken.append(op);
                        nextToken.append(tokenizer.popIdentifier());
                    }
                    spec.mapExpressionList.append(nextToken);
                    QString maybeComma = tokenizer.pop();
                    if (maybeComma == QLatin1Char('}')) {
                        tokenizer.push(maybeComma);
                        continue;
                    } else if (maybeComma != QLatin1Char(',')) {
                        queryExplanation.append(QStringLiteral("Parse error: expecting ',', or '}' but got '%1'")
                                                .arg(maybeComma));
                        parseError = true;
                        break;
                    }
                }
            }
        } else if (token == QLatin1Char('/') || token == QLatin1Char('\\') ||
                   token == QLatin1Char('>') || token == QLatin1Char('<')) {
            QString ordering = token;
            JsonDbOrderTerm term;
            term.propertyName = tokenizer.popIdentifier();
            term.ascending = ordering == QLatin1Char('/') || ordering == QLatin1Char('>');
            spec.orderTerms.append(term);
        } else if (token == QLatin1String("count")) {
            spec.aggregateOperation = QStringLiteral("count");
        } else if (token == QLatin1Char('*')) {
            // match all objects
        } else {
            queryExplanation.append(QStringLiteral("Parse error: expecting '?', '/', '\\', or 'count' but got '%1'").arg(token));
            parseError = true;
            break;
        }
        QString closeBracket = tokenizer.pop();
        if (closeBracket != QLatin1Char(']')) {
            queryExplanation.append(QStringLiteral("Parse error: expecting ']' but got '%1'").arg(closeBracket));
            parseError = true;
            break;
        }
    }

    if (parseError) {
        errorString = queryExplanation.join(QLatin1String(";"));
        spec = JsonDbQuery();
        return false;
    }

    foreach (const JsonDbOrQueryTerm &oqt, spec.queryTerms) {
        foreach (const JsonDbQueryTerm &term, oqt.terms()) {
            if (term.propertyName() == JsonDbString::kTypeStr) {
                if (term.op() == QLatin1Char('=')) {
                    spec.mMatchedTypes.insert(termValue(term).toString());
                } else if (term.op() == QLatin1String("!=")) {
                    spec.mMatchedTypes.insert(termValue(term).toString());
                } else if (term.op() == QLatin1String("in")) {
                    foreach (const QJsonValue &v, termValue(term).toArray())
                        spec.mMatchedTypes.insert(v.toString());
                }
            }
        }
    }

    if (!spec.queryTerms.size() && !spec.orderTerms.size()) {
        // match everything -- sort on type
        JsonDbOrderTerm term;
        term.propertyName = JsonDbString::kTypeStr;
        term.ascending = true;
        spec.orderTerms.append(term);
    }

    return true;
}

QJsonValue JsonDbQueryParserPrivate::termValue(const JsonDbQueryTerm &term) const
{
    return term.hasValue() ? term.value() : bindings.value(term.variable(), QJsonValue(QJsonValue::Undefined));
}

JsonDbQueryParser::JsonDbQueryParser()
    : d_ptr(new JsonDbQueryParserPrivate(this))
{
}

JsonDbQueryParser::~JsonDbQueryParser()
{
}

void JsonDbQueryParser::setQuery(const QString &query)
{
    Q_D(JsonDbQueryParser);
    d->query = query;
}

QString JsonDbQueryParser::query() const
{
    Q_D(const JsonDbQueryParser);
    return d->query;
}

void JsonDbQueryParser::setBindings(const QJsonObject &bindings)
{
    Q_D(JsonDbQueryParser);
    d->bindings.clear();
    QJsonObject::const_iterator it = bindings.constBegin(), e = bindings.constEnd();
    for (; it != e; ++it)
        d->bindings.insert(it.key(), it.value());
}

void JsonDbQueryParser::setBindings(const QMap<QString, QJsonValue> &bindings)
{
    Q_D(JsonDbQueryParser);
    d->bindings = bindings;
}

QMap<QString, QJsonValue> JsonDbQueryParser::bindings() const
{
    Q_D(const JsonDbQueryParser);
    return d->bindings;
}

bool JsonDbQueryParser::parse()
{
    Q_D(JsonDbQueryParser);
    return d->parse();
}

QString JsonDbQueryParser::errorString() const
{
    Q_D(const JsonDbQueryParser);
    return d->errorString;
}

JsonDbQuery JsonDbQueryParser::result() const
{
    Q_D(const JsonDbQueryParser);
    return d->spec;
}

QT_END_NAMESPACE_JSONDB_PARTITION