summaryrefslogtreecommitdiffstats
path: root/src/client/qjsondbmodelutils_p.cpp
blob: d9003ab8ba79d9b8e33c53bf5ea42c4eea483f0a (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qjsondbmodelutils_p.h"
#include <qdebug.h>
#include <QJsonValue>
#include <QJsonArray>

QT_BEGIN_NAMESPACE_JSONDB

SortingKey::SortingKey(int partitionIndex, const QVariantMap &object, const QList<bool> &directions, const QList<QStringList> &paths, const SortIndexSpec &spec){
    QVariantList values;
    for (int i = 0; i < paths.size(); i++)
        values.append(lookupProperty(object, paths[i]));
    QByteArray uuid = QUuid(object[QLatin1String("_uuid")].toString()).toRfc4122();
    d = new SortingKeyPrivate(partitionIndex, uuid, directions, values, spec);

}

SortingKey::SortingKey(int partitionIndex, const QVariantList &object, const QList<bool> &directions, const SortIndexSpec &spec)
{
    QVariantList values = object.mid(1);
    QByteArray uuid = QUuid(object.at(0).toString()).toRfc4122();
    d = new SortingKeyPrivate(partitionIndex, uuid, directions, values, spec);
}

SortingKey::SortingKey(int partitionIndex, const QByteArray &uuid, const QVariantList &object, const QList<bool> &directions, const SortIndexSpec &spec)
{
    d = new SortingKeyPrivate(partitionIndex, uuid, directions, object, spec);
}

SortingKey::SortingKey(int partitionIndex, const QByteArray &uuid, const QVariant &value, bool direction, const SortIndexSpec &spec)
{
    d = new SortingKeyPrivate(partitionIndex, uuid, direction, value, spec);
}

SortingKey::SortingKey(const SortingKey &other)
    :d(other.d)
{
}

int SortingKey::partitionIndex() const
{
    return d->partitionIndex;
}

QVariant SortingKey::value() const
{
    if (d->count == 1)
        return d->values[0];
    else if (d->count == 0)
        return QVariant();
    QVariantList ret;
    for (int i = 0; i < d->count; i++)
        ret << d->values[i];
    return ret;
}

static bool operator<(const QVariant& lhs, const QVariant& rhs)
{
    if ((lhs.type() == QVariant::Int) && (rhs.type() == QVariant::Int))
        return lhs.toInt() < rhs.toInt();
    else if ((lhs.type() == QVariant::LongLong) && (rhs.type() == QVariant::LongLong))
        return lhs.toLongLong() < rhs.toLongLong();
    else if ((lhs.type() == QVariant::Double) && (rhs.type() == QVariant::Double))
        return lhs.toFloat() < rhs.toFloat();
    return (QString::compare(lhs.toString(), rhs.toString(), Qt::CaseInsensitive ) < 0);
}

static int equalWithSpec(const QVariant& lhs, const QVariant& rhs, const SortIndexSpec &spec)
{
    // Supports only string and UUID
    if (spec.type == SortIndexSpec::String) {
        Qt::CaseSensitivity cs = spec.caseSensitive ? Qt::CaseSensitive :Qt::CaseInsensitive;
        return QString::compare(lhs.toString(), rhs.toString(), cs);
    } else if (spec.type == SortIndexSpec::UUID) {
        QByteArray lhsUuid = lhs.toByteArray();
        QByteArray rhsUuid = rhs.toByteArray();
        return memcmp(lhsUuid.constData(), rhsUuid.constData(), qMin(lhsUuid.size(), rhsUuid.size()));
    }
    return -1;
}

bool SortingKey::operator <(const SortingKey &rhs) const
{
    const SortingKeyPrivate *dLhs = d;
    const SortingKeyPrivate *dRhs = rhs.d;
    const int nKeys = dLhs->count;
    for (int i = 0; i < nKeys; i++) {
        const QVariant &lhsValue = dLhs->values[i];
        const QVariant &rhsValue = dRhs->values[i];
        int cmp = -1;
        // The index spec is only applied to the first item
        if (!i && (dLhs->indexSpec.type == SortIndexSpec::String || dLhs->indexSpec.type == SortIndexSpec::UUID)) {
            if ((cmp = equalWithSpec(lhsValue, rhsValue, dLhs->indexSpec))) {
                return (dLhs->directions[i] ? (cmp < 0) : (cmp > 0));
            }
        } else if (lhsValue != rhsValue) {
            return (dLhs->directions[i] ? lhsValue < rhsValue : rhsValue < lhsValue);
        }
    }
    int cmp = memcmp(dLhs->uuid.constData(), dRhs->uuid.constData(), qMin(dLhs->uuid.size(), dRhs->uuid.size()));
    // In case of even score jsondb sorts according to _uuid in the same direction as the last sort item
    if (nKeys)
        return (dLhs->directions[0] ? (cmp < 0) : (cmp > 0));
    return (cmp < 0);
}

bool SortingKey::operator ==(const SortingKey &rhs) const
{
    bool equal = true;
    const SortingKeyPrivate *dLhs = d;
    const SortingKeyPrivate *dRhs = rhs.d;
    const int nKeys = dLhs->count;
    for (int i = 0; i < nKeys; i++) {
        const QVariant &lhsValue = dLhs->values[i];
        const QVariant &rhsValue = dRhs->values[i];
        // The index spec is only applied to the first item
        if (!i && (dLhs->indexSpec.type == SortIndexSpec::String || dLhs->indexSpec.type == SortIndexSpec::UUID)) {
            if (equalWithSpec(lhsValue, rhsValue, dLhs->indexSpec)) {
                equal = false;
                break;
            }
        } else if (lhsValue != rhsValue) {
            equal = false;
            break;
        }
    }
    if (equal) {
        return (memcmp(dLhs->uuid.constData(), dRhs->uuid.constData(), qMin(dLhs->uuid.size(), dRhs->uuid.size())) == 0);
    }
    return false;
}

QVariant lookupProperty(QVariantMap object, const QStringList &path)
{
    if (!path.size()) {
        return QVariant();
    }
    QVariantMap emptyMap;
    QVariantList emptyList;
    QVariantList objectList;
    for (int i = 0; i < path.size() - 1; i++) {
        const QString &key = path.at(i);
        // this part of the property is a list
        if (!objectList.isEmpty()) {
            bool ok = false;
            int index = key.toInt(&ok);
            if (ok && (index >= 0) && (objectList.count() > index)) {
                if (objectList.at(index).type() == QVariant::List) {
                    objectList = objectList.at(index).toList();
                    object = emptyMap;
                } else  {
                    object = objectList.at(index).toMap();
                    objectList = emptyList;
                }
                continue;
            }
        }
        // this part is a map
        if (object.contains(key)) {
            if (object.value(key).type() == QVariant::List) {
                objectList = object.value(key).toList();
                object = emptyMap;
            } else  {
                object = object.value(key).toMap();
                objectList = emptyList;
            }
        } else {
            return QVariant();
        }
    }
    const QString &key = path.last();
    // get the last part from the list
    if (!objectList.isEmpty()) {
        bool ok = false;
        int index = key.toInt(&ok);
        if (ok && (index >= 0) && (objectList.count() > index)) {
            return objectList.at(index);
        }
    }
    // if the last part is in a map
    return object.value(key);
}

QJsonValue lookupJsonProperty(QJsonObject object, const QStringList &path)
{
    if (!path.size()) {
        return QJsonValue();
    }
    QJsonObject emptyObject;
    QJsonArray emptyList;
    QJsonArray objectList;
    for (int i = 0; i < path.size() - 1; i++) {
        const QString &key = path.at(i);
        // this part of the property is a list
        if (!objectList.isEmpty()) {
            bool ok = false;
            int index = key.toInt(&ok);
            if (ok && (index >= 0) && (objectList.count() > index)) {
                if (objectList.at(index).type() == QJsonValue::Array) {
                    objectList = objectList.at(index).toArray();
                    object = emptyObject;
                } else  {
                    object = objectList.at(index).toObject();
                    objectList = emptyList;
                }
                continue;
            }
        }
        // this part is a map
        if (object.contains(key)) {
            if (object.value(key).type() == QJsonValue::Array) {
                objectList = object.value(key).toArray();
                object = emptyObject;
            } else  {
                object = object.value(key).toObject();
                objectList = emptyList;
            }
        } else {
            return QJsonValue();
        }
    }
    const QString &key = path.last();
    // get the last part from the array
    if (!objectList.isEmpty()) {
        bool ok = false;
        int index = key.toInt(&ok);
        if (ok && (index >= 0) && (objectList.count() > index)) {
            return objectList.at(index);
        }
    }
    // if the last part is in a map
    return object.value(key);
}

QString removeArrayOperator(QString propertyName)
{
    propertyName.replace(QLatin1String("["), QLatin1String("."));
    propertyName.remove(QLatin1Char(']'));
    return propertyName;
}

QList<QJsonObject> qvariantlist_to_qjsonobject_list(const QVariantList &list)
{
    QList<QJsonObject> objects;
    int count = list.count();
    for (int i = 0; i < count; i++) {
        objects.append(QJsonObject::fromVariantMap(list[i].toMap()));
    }
    return objects;
}

QVariantList qjsonobject_list_to_qvariantlist(const QList<QJsonObject> &list)
{
    QVariantList objects;
    int count = list.count();
    for (int i = 0; i < count; i++) {
        objects.append(list[i].toVariantMap());
    }
    return objects;
}

ModelRequest::ModelRequest(QObject *parent)
    :QObject(parent)
{
}

ModelRequest::~ModelRequest()
{
    resetRequest();
}

QJsonDbReadRequest* ModelRequest::newRequest(int newIndex)
{
    resetRequest();
    index = newIndex;
    request = new QJsonDbReadRequest();
    connect(request, SIGNAL(finished()), this, SLOT(onQueryFinished()));
    connect(request, SIGNAL(finished()), request, SLOT(deleteLater()));
    connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
            this, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
    connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
            request, SLOT(deleteLater()));
    return request;
}

void ModelRequest::resetRequest()
{
    if (request) {
        delete request;
        request = 0;
    }
}

void ModelRequest::onQueryFinished()
{
    emit finished(index, request->takeResults(), request->sortKey());
}

#include "moc_qjsondbmodelutils_p.cpp"
QT_END_NAMESPACE_JSONDB