summaryrefslogtreecommitdiffstats
path: root/src/client/qjsondbreadrequest.cpp
blob: 2a5d82f4feccf90a45e4423deb848f7ce3d0bbf1 (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
/****************************************************************************
**
** 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 "qjsondbreadrequest_p.h"
#include "qjsondbstrings_p.h"

#include <QJsonArray>
#include <QVariant>
#include <QDebug>

QT_BEGIN_NAMESPACE_JSONDB

/*!
    \class QJsonDbReadRequest
    \inmodule QtJsonDb

    \brief The QJsonDbReadRequest class allows to query database.

    See \l{Queries} for documentation on the query string format.

    \code
        QJsonDbReadRequest *request = new QJsonDbReadRequest;
        request->setQuery(QStringLiteral("[?_type=\"Foo\"]"));
        connect(request, SIGNAL(finished()), this, SLOT(onQueryFinished()));
        connect(request, SIGNAL(finished()), request, SLOT(deleteLater()));
        connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                this, SLOT(onQueryError(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
        connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                request, SLOT(deleteLater()));
        QJsonDbConnection *connection = new QJsonDbConnection;
        connection->send(request);
    \endcode
*/
/*!
    \enum QJsonDbReadRequest::ErrorCode

    This enum describes database connection errors for read requests that can
    be emitted by the error() signal.

    \value NoError
    \value InvalidRequest
    \value OperationNotPermitted
    \value InvalidPartition Invalid partition.
    \value DatabaseConnectionError
    \value PartitionUnavailable
    \value MissingQuery Missing query field.
    \value InvalidMessage
    \value InvalidLimit Invalid limit field

    \sa error(), QJsonDbRequest::ErrorCode
*/

QJsonDbReadRequestPrivate::QJsonDbReadRequestPrivate(QJsonDbReadRequest *q)
    : QJsonDbRequestPrivate(q), queryLimit(-1), stateNumber(0)
{
}

/*!
    Constructs a new query request object with the given \a parent.
*/
QJsonDbReadRequest::QJsonDbReadRequest(QObject *parent)
    : QJsonDbRequest(new QJsonDbReadRequestPrivate(this), parent)
{
}

/*!
    Constructs a new query request object with the given \a query string and \a
    parent.
*/
QJsonDbReadRequest::QJsonDbReadRequest(const QString &query, QObject *parent)
    : QJsonDbRequest(new QJsonDbReadRequestPrivate(this), parent)
{
    Q_D(QJsonDbReadRequest);
    d->query = query;
}

/*!
    \internal
*/
QJsonDbReadRequest::QJsonDbReadRequest(QJsonDbReadRequestPrivate *dd, QObject *parent)
    : QJsonDbRequest(dd, parent)
{
    Q_ASSERT(dd != 0);
}

/*!
    Destroys the request object.
*/
QJsonDbReadRequest::~QJsonDbReadRequest()
{
}

/*!
    \property QJsonDbReadRequest::query

    \brief the query string

    Specifies the query string for the request in the format described in
    \l{Queries}.

    \sa queryLimit, bindValue()
*/
QString QJsonDbReadRequest::query() const
{
    Q_D(const QJsonDbReadRequest);
    return d->query;
}

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

/*!
    \property QJsonDbReadRequest::queryLimit
    \brief the limit of a query

    Specifies the maximum amount of amount that should be fetched from the
    database.

    \sa query
*/
int QJsonDbReadRequest::queryLimit() const
{
    Q_D(const QJsonDbReadRequest);
    return d->queryLimit;
}

void QJsonDbReadRequest::setQueryLimit(int limit)
{
    Q_D(QJsonDbReadRequest);
    JSONDB_CHECK_REQUEST_STATUS;
    d->queryLimit = limit;
}

/*!
    Set the placeholder \a placeHolder to be bound to value \a val in the query
    string. Note that '%' is the only placeholder mark supported by the query.
    The marker '%' should not be included in the \a placeHolder name.

    \code
        QJsonDbReadRequest *query = new QJsonDbReadRequest;
        query->setQuery(QStringLiteral("[?_type=\"Person\"][?firstName = %name]"));
        query->bindValue(QStringLiteral("name"), QLatin1String("Malcolm"));
    \endcode

    \sa query, boundValue(), boundValues(), clearBoundValues()
*/
void QJsonDbReadRequest::bindValue(const QString &placeHolder, const QJsonValue &val)
{
    Q_D(QJsonDbReadRequest);
    JSONDB_CHECK_REQUEST_STATUS;
    d->bindings.insert(placeHolder, val);
}

/*!
    Returns the value for the \a placeHolder.
*/
QJsonValue QJsonDbReadRequest::boundValue(const QString &placeHolder) const
{
    Q_D(const QJsonDbReadRequest);
    JSONDB_CHECK_REQUEST_STATUS;
    return d->bindings.value(placeHolder, QJsonValue(QJsonValue::Undefined));
}

/*!
    Returns a map of the bound values.
*/
QMap<QString,QJsonValue> QJsonDbReadRequest::boundValues() const
{
    Q_D(const QJsonDbReadRequest);
    return d->bindings;
}

/*!
    Clears all bound values.
*/
void QJsonDbReadRequest::clearBoundValues()
{
    Q_D(QJsonDbReadRequest);
    JSONDB_CHECK_REQUEST_STATUS;
    d->bindings.clear();
}

/*!
    \property QJsonDbReadRequest::stateNumber

    Returns a database state number that the query was executed on.

    The property is populated after started() signal was emitted.

    \sa started()
*/
quint32 QJsonDbReadRequest::stateNumber() const
{
    Q_D(const QJsonDbReadRequest);
    return d->stateNumber;
}

/*!
    \property QJsonDbReadRequest::sortKey

    Returns a field that was used as a sort key when executing a query.

    The results of the query are ordered by that field.

    The property is populated after started() signal was emitted.

    \sa started(), takeResults()
*/
QString QJsonDbReadRequest::sortKey() const
{
    Q_D(const QJsonDbReadRequest);
    return d->sortKey;
}

QJsonObject QJsonDbReadRequestPrivate::getRequest() const
{
    Q_Q(const QJsonDbReadRequest);
    QJsonObject object;
    object.insert(JsonDbStrings::Property::query(), query);
    if (queryLimit != -1)
        object.insert(JsonDbStrings::Property::queryLimit(), queryLimit);
    QVariant v = q->property("queryOffset");
    if (v.isValid())
        object.insert(JsonDbStrings::Property::queryOffset(), v.toInt());
    if (!bindings.isEmpty()) {
        QJsonObject b;
        QMap<QString, QJsonValue>::const_iterator it, e;
        for (it = bindings.begin(), e = bindings.end(); it != e; ++it)
            b.insert(it.key(), it.value());
        object.insert(JsonDbStrings::Property::bindings(), b);
    }
    QJsonObject request;
    request.insert(JsonDbStrings::Protocol::action(), JsonDbStrings::Protocol::query());
    request.insert(JsonDbStrings::Protocol::object(), object);
    request.insert(JsonDbStrings::Protocol::partition(), partition);
    request.insert(JsonDbStrings::Protocol::requestId(), requestId);
    return request;
}

void QJsonDbReadRequestPrivate::handleResponse(const QJsonObject &response)
{
    Q_Q(QJsonDbReadRequest);

    stateNumber = static_cast<quint32>(response.value(JsonDbStrings::Property::state()).toDouble());
    sortKey = response.value(JsonDbStrings::Property::sortKeys()).toArray().at(0).toString();

    setStatus(QJsonDbRequest::Receiving);
    emit q->started();

    QJsonArray list = response.value(JsonDbStrings::Protocol::data()).toArray();
    results.reserve(results.size() + list.size());
    foreach (const QJsonValue &v, list)
        results.append(v.toObject());

    emit q->resultsAvailable(results.size());
    setStatus(QJsonDbRequest::Finished);
    emit q->finished();
}

void QJsonDbReadRequestPrivate::handleError(int code, const QString &message)
{
    Q_Q(QJsonDbReadRequest);
    setStatus(QJsonDbRequest::Error);
    emit q->error(QJsonDbRequest::ErrorCode(code), message);
}

/*!
    \class QJsonDbReadObjectRequest
    \inmodule QtJsonDb

    \brief The QJsonDbReadObjectRequest class allows to retrieve one object by its UUID.

    To retrieve object content for a known uuid:

    \code
        QJsonDbReadObjectRequest *request = new QJsonDbReadObjectRequest(this);
        request->setUuid(objectUuid);
        connect(request, SIGNAL(objectAvailable(QJsonObject), this, SLOT(onObjectAvailable(QJsonObject)));
        connect(request, SIGNAL(objectUnavailable(QUuid), this, SLOT(onObjectNotFound()));
        connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                this, SLOT(onError(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
        connection->send(request);
    \endcode
*/

QJsonDbReadObjectRequestPrivate::QJsonDbReadObjectRequestPrivate(QJsonDbReadObjectRequest *q)
    : QJsonDbReadRequestPrivate(q)
{
}

/*!
    Constructs a new QJsonDbReadObjectRequest object with the given \a parent.
*/
QJsonDbReadObjectRequest::QJsonDbReadObjectRequest(QObject *parent)
    : QJsonDbReadRequest(new QJsonDbReadObjectRequestPrivate(this), parent)
{
    connect(this, SIGNAL(finished()), this, SLOT(_q_onFinished()));
}

/*!
    Constructs a new QJsonDbReadObjectRequest object with the given \a parent
    to retrieve content of the object with the given \a uuid.
*/
QJsonDbReadObjectRequest::QJsonDbReadObjectRequest(const QUuid &uuid, QObject *parent)
    : QJsonDbReadRequest(new QJsonDbReadObjectRequestPrivate(this), parent)
{
    connect(this, SIGNAL(finished()), this, SLOT(_q_onFinished()));
    setUuid(uuid);
}

/*!
    \property QJsonDbReadObjectRequest::uuid
    Specifies UUID of the object to retrieve from the database.
*/
QUuid QJsonDbReadObjectRequest::uuid() const
{
    Q_D(const QJsonDbReadObjectRequest);
    return d->uuid;
}

void QJsonDbReadObjectRequest::setUuid(const QUuid &uuid)
{
    Q_D(QJsonDbReadObjectRequest);
    d->uuid = uuid;
    setQuery(QStringLiteral("[?_uuid = %uuid]"));
    bindValue(QStringLiteral("uuid"), uuid.toString());
}

void QJsonDbReadObjectRequestPrivate::_q_onFinished()
{
    Q_Q(QJsonDbReadObjectRequest);
    if (results.size() > 1) {
        qWarning() << "QJsonDbReadObjectRequest: instead of 1 object, got" << results.size() << "object(s)";
        return;
    }
    if (results.size() == 0) {
        emit q->objectUnavailable(uuid);
        return;
    }
    QJsonObject object = results.at(0);
    emit q->objectAvailable(object);
}

/*!
    \fn void QJsonDbReadObjectRequest::objectAvailable(const QJsonObject &object)

    This signal is emitted when the request is complete and the \a object was
    successfully retrieve from the database.

    This is just a convenience signal that can be used instead of finished().

    \sa objectUnavailable(), error()
*/

/*!
    \fn void QJsonDbReadObjectRequest::objectUnavailable(const QUuid &uuid)

    This signal is emitted when the request is complete, but the requested
    object with the given \a uuid was not found in the database.

    This is just a convenience signal that can be used instead of finished().

    \sa objectAvailable(), error()
*/

#include "moc_qjsondbreadrequest.cpp"

QT_END_NAMESPACE_JSONDB