summaryrefslogtreecommitdiffstats
path: root/src/imports/jsondb/jsondbpartition.cpp
blob: b98e8fcf1d40d0fc20a898371a3e029061354c36 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/****************************************************************************
**
** 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 "private/jsondb-strings_p.h"
#include "jsondbpartition.h"
#include "jsondbnotification.h"
#include "plugin.h"
#include "jsondb-client.h"
#include "jsondbqueryobject.h"
#include "jsondbchangessinceobject.h"
#include <qdebug.h>

QT_BEGIN_NAMESPACE_JSONDB

/*!
    \qmlclass Partition
    \inqmlmodule QtJsonDb
    \since 1.x

    The partition object allows you find, create, update, or remove objects from JsonDb.
    It also allows to create query, notification  and changesSince objects, query. A partition
    object is identified by its name.

    Most of the methods take script objects as parameters. The last parameter can be
    a callback function.
*/

JsonDbPartition::JsonDbPartition(const QString &partitionName, QObject *parent)
    :QObject(parent)
    ,_name(partitionName)
{
    connect(&jsonDb, SIGNAL(response(int,const QVariant&)),
            this, SLOT(dbResponse(int,const QVariant&)));
    connect(&jsonDb, SIGNAL(error(int,int,QString)),
            this, SLOT(dbErrorResponse(int,int,QString)));
}

JsonDbPartition::~JsonDbPartition()
{
}

/*!
    \qmlproperty string QtJsonDb::Partition::name
     Holds the human readable name of the partition.
*/

QString JsonDbPartition::name() const
{
    return _name;
}

void JsonDbPartition::setName(const QString &partitionName)
{
    if (partitionName != _name) {
        _name = partitionName;
        foreach (QPointer<JsonDbNotify> notify, notifications) {
            removeNotification(notify);
        }
        notifications.clear();
        // tell notifications to resubscribe
        emit nameChanged(partitionName);
    }
}

namespace {
static QVariant qjsvalue_to_qvariant(const QJSValue &value)
{
    if (value.isQObject()) {
        // We need the QVariantMap & not the QObject wrapper
        return qjsvalue_cast<QVariantMap>(value);
    } else {
        // Converts to either a QVariantList or a QVariantMap
        return value.toVariant();
    }
}
}
/*!
    \qmlmethod int QtJsonDb::Partition::create(object newObject, object options, function callback)

    Creates the \a newObject (or list of objects) in the partition. The callback will be called
    in case of failure or success. It returns the id of the request. If it fails to create an
    object, the whole transaction will be aborted. The \a options is not used now. The \a options and \a
    callback parameters are optional. If the \a callback parameter is used, it has to be the
    last parameter specified in the function call. The \a callback has the following signature.

    \code
    import QtJsonDb 1.0 as JsonDb
    function createCallback(error, response) {
        if (error) {
            // communication error or failed to create one or more objects.
            // 'error' object is only defined in case of an error otherwise undefined.
            console.log("Create Error :"+JSON.stringify(error));
            return;
        }
        console.log("response.id = "+response.id +" count = "+response.items.length);
        // response.items is an array of objects, the order of the request is preserved
        for (var i = 0; i < response.items.length; i++) {
            console.log("_uuid = "+response.items[i]._uuid +" ._version = "+response.items[i]._version);
        }

    }
    myPartition.create({"_type":"Contact", "firstName":firstName, "lastName":lastName }, createCallback)
    \endcode

    The \a error is an object of type  {code: errorCode, message: "plain text" }. This is
    only defined in case of an error. The \a response object conatins the following properties :
    \list
    \li id -  The id of the request.
    \li stateNumber - The state label of the partition this write was committed in.
    \li items - An array of objects (in the same order as the request). Each item in the array has
    the following properties:
    \list
    \li _uuid - The _uuid of the newly created object
    \li _version - The _version of the newly created object
    \endlist
    \endlist

*/

int JsonDbPartition::create(const QJSValue &object,  const QJSValue &options, const QJSValue &callback)
{
    QJSValue actualOptions = options;
    QJSValue actualCallback = callback;
    if (options.isCallable()) {
        if (!callback.isUndefined()) {
            qWarning() << "Callback should be the last parameter.";
            return -1;
        }
        actualCallback = actualOptions;
        actualOptions = QJSValue(QJSValue::UndefinedValue);
    }
    //#TODO ADD options
    int id = jsonDb.create(qjsvalue_to_qvariant(object), _name);
    createCallbacks.insert(id, actualCallback);
    return id;
}

/*!
    \qmlmethod int QtJsonDb::Partition::update(object updatedObject, object options, function callback)

    Update the object \a updatedObject (or list of objects) in the partition. Returns the id of this
    request. If the request fails to update an object, the whole transaction will be aborted. The
    \a options specifies how update should be handled.
    \list
    \li options.mode - Supported values: "normal", "forced".
    \list
    \li"normal" creates the document if it does not exist otherwise it enforces that the
    "_version" matches or fails - lost update prevention.
    \li"forced" ignores the existing database content.
    \endlist
    Default is "normal"
    \endlist

    The callback will be called in case of failure or success. The \a options and \a callback parameters
    are optional. If the \a callback parameter is used, it has to be the last parameter specified in the
    function call. The \a callback has the following signature.

    \code
    import QtJsonDb 1.0 as JsonDb
    function updateCallback(error, response) {
        if (error) {
            // communication error or failed to create one or more objects.
            // 'error' object is only defined in case of an error otherwise undefined.
            console.log("Update Error :"+JSON.stringify(error));
            return;
        }
        console.log("response.id = "+response.id +" count = "+response.items.length);
        // response.items is an array of objects, the order of the request is preserved
        for (var i = 0; i < response.items.length; i++) {
            console.log("_uuid = "+response.items[i]._uuid +" ._version = "+response.items[i]._version);
        }

    }
    myPartition.update(updatedObject, updateCallback)
    \endcode

    The \a error is an object of type  {code: errorCode, message: "plain text" }. This is
    only defined in case of an error. The \a response object conatins the following properties :
    \list
    \li id -  The id of the request.
    \li stateNumber - The state label of the partition this write was committed in.
    \li items - An array of objects (in the same order as the request). Each item in the array has
    the following properties:
    \list
    \li _uuid - The _uuid of the newly created object
    \li _version - The _version of the newly created object
    \endlist
    \endlist

*/


int JsonDbPartition::update(const QJSValue &object,  const QJSValue &options, const QJSValue &callback)
{
    QJSValue actualOptions = options;
    QJSValue actualCallback = callback;
    if (options.isCallable()) {
        if (!callback.isUndefined()) {
            qWarning() << "Callback should be the last parameter.";
            return -1;
        }
        actualCallback = actualOptions;
        actualOptions = QJSValue(QJSValue::UndefinedValue);
    }
    //#TODO ADD options
    int id = jsonDb.update(qjsvalue_to_qvariant(object), _name);
    updateCallbacks.insert(id, actualCallback);
    return id;
}

/*!
    \qmlmethod int QtJsonDb::Partition::remove(object objectToRemove, object options, function callback)

    Removes the \a objectToRemove (or list of objects) from the partition. It returns the id of this
    request. The \a options specifies how removal should be handled.
    \list
    \li options.mode - Supported values: "normal", which requires a an object with _uuid and
    _version set to be passed.
    \endlist

    The callback will be called in case of failure or success. The \a options and \a callback parameters
    are optional. If the \a callback parameter is used, it has to be the last parameter specified in the
    function call. The \a callback has the following signature.

    \code
    import QtJsonDb 1.0 as JsonDb
    function removeCallback(error, response) {
        if (error) {
            // communication error or failed to create one or more objects.
            // 'error' object is only defined in case of an error otherwise undefined.
            console.log("Update Error :"+JSON.stringify(error));
            return;
        }
        console.log("response.id = "+response.id +" count = "+response.items.length);
        // response.items is an array of objects, the order of the request is preserved
        for (var i = 0; i < response.items.length; i++) {
            console.log("_uuid = "+response.items[i]._uuid);
        }

    }
    myPartition.remove({"_uuid":"xxxx-xxxx-xxxx", "_version":"1-xxxx-xxxx-xxxx"}, removeCallback)
    \endcode

    The \a error is an object of type  {code: errorCode, message: "plain text" }. This is
    only defined in case of an error. The \a response object conatins the following properties :
    \list
    \li id -  The id of the request.
    \li stateNumber - The state label of the partition this write was committed in.
    \li items - An array of objects (in the same order as the request). Each item in the array has
    the following properties:
    \list
    \li _uuid - The _uuid of the newly created object
    \endlist
    \endlist

*/

int JsonDbPartition::remove(const QJSValue &object,  const QJSValue &options, const QJSValue &callback)
{
    QJSValue actualOptions = options;
    QJSValue actualCallback = callback;
    if (options.isCallable()) {
        if (!callback.isUndefined()) {
            qWarning() << "Callback should be the last parameter.";
            return -1;
        }
        actualCallback = actualOptions;
        actualOptions = QJSValue(QJSValue::UndefinedValue);
    }
    //#TODO ADD options
    int id = jsonDb.remove(qjsvalue_to_qvariant(object), _name);
    removeCallbacks.insert(id, actualCallback);
    return id;
}

/*!
    \qmlmethod QtJsonDb::Partition::find(string query, object options, function callback)

    Finds the objects matching the \a query string in the partition. The \a options specifies
    how query should be handled. The \a query should be specified in JsonQuery format.
    \a options support the following properties.
    \list
    \li options.limit - Maximum number of items to be fetched
    \li options.bindings - Holds the bindings object for the placeholders used in the query string. Note that
    the placeholder marker '%' should not be included as part of the keys.
    \endlist

    The callback will be called in case of failure or success. It has the following signature
    \code
    function findCallback(error, response) {
        if (error) {
            // 'error' object is only defined in case of an error otherwise undefined.
            console.log("Update Error :"+JSON.stringify(error));
            return;
        }
        console.log("response.id = "+response.id +" count = "+response.items.length);
        // response.items is an array of objects
        for (var i = 0; i < response.items.length; i++) {
            console.log("_uuid = "+response.items[i]._uuid);
        }
    }
    \endcode

    The \a error is an object of type  {code: errorCode, message: "plain text" }. This is
    only defined in case of an error. The \a response object contains the following properties :
    \list
    \li id -  The id of the request.
    \li stateNumber - The state label of the partition this write was committed in.
    \li items - An array of objects
    \endlist

    \sa QtJsonDb::Query

*/

int JsonDbPartition::find(const QString &query, const QJSValue &options, const QJSValue &callback)
{
    QJSValue actualOptions = options;
    QJSValue actualCallback = callback;
    if (options.isCallable()) {
        if (!callback.isUndefined()) {
            qWarning() << "Callback should be the last parameter.";
            return -1;
        }
        actualCallback = actualOptions;
        actualOptions = QJSValue(QJSValue::UndefinedValue);
    }
    JsonDbQueryObject *newQuery = new JsonDbQueryObject();
    newQuery->setQuery(query);
    if (!actualOptions.isUndefined()) {
        QVariantMap opt = actualOptions.toVariant().toMap();
        if (opt.contains(QLatin1String("limit")))
            newQuery->setLimit(opt.value(QLatin1String("limit")).toInt());
        if (opt.contains(QLatin1String("bindings")))
            newQuery->setBindings(opt.value(QLatin1String("bindings")).toMap());
    }
    newQuery->setPartition(this);
    connect(newQuery, SIGNAL(finished()), this, SLOT(queryFinished()));
    connect(newQuery, SIGNAL(statusChanged(JsonDbQueryObject::Status)), this, SLOT(queryStatusChanged()));
    findCallbacks.insert(newQuery, actualCallback);
    newQuery->componentComplete();
    int id = newQuery->start();
    findIds.insert(newQuery, id);
    return id;
}

/*!
    \qmlmethod object QtJsonDb::Partition::createNotification(query)

    Create the Notification object for the specifed \a query.The script engine
    decides the life time of the returned object. The returned object can be saved
    in a 'property var' until it is required.

    \code
    import QtJsonDb 1.0 as JsonDb
    property var createNotification;
    function onCreateNotification(result, action, stateNumber)
    {
        console.log("create Notification : object " + result._uuid );
        console.log(result._type + result.firstName + " " + result.lastName );
    }

    Component.onCompleted: {
        createNotification = nokiaPartition.createNotification('[?_type="Contact"]');
        createNotification.notification.connect(onCreateNotification);
    }
    \endcode
    \sa QtJsonDb::Notification

*/

JsonDbNotify* JsonDbPartition::createNotification(const QString &query)
{
    JsonDbNotify* notify = new JsonDbNotify();
    notify->setPartition(this);
    notify->setQuery(query);
    notify->componentComplete();
    QDeclarativeEngine::setObjectOwnership(notify, QDeclarativeEngine::JavaScriptOwnership);
    return notify;
}

/*!
    \qmlmethod object QtJsonDb::Partition::createQuery(query, limit, bindings)

    Create the Query object with the specified \a query string and other parameters.
    Users have to call start() to start the query in this partition. The script engine
    decides the life time of the returned object. The returned object can be saved
    in a 'property var' until it is required.

    \code
    import QtJsonDb 1.0 as JsonDb
    property var queryObject;
    function onFinished()
    {
        var results = queryObject.takeResults();
        console.log("Results: Count" + results.length );
    }

    Component.onCompleted: {
        var bindings = {'firstName':'Book'};
        queryObject = nokiaPartition.createQuery('[?_type="Contact"][?name=%firstName]', -1, bindings);
        queryObject.finished.connect(onFinished);
        queryObject.start();
    }
    \endcode
    \sa QtJsonDb::Query

*/

JsonDbQueryObject* JsonDbPartition::createQuery(const QString &query, int limit, QVariantMap bindings)
{
    JsonDbQueryObject* queryObject = new JsonDbQueryObject();
    queryObject->setQuery(query);
    queryObject->setBindings(bindings);
    queryObject->setLimit(limit);
    queryObject->setPartition(this);
    queryObject->componentComplete();
    QDeclarativeEngine::setObjectOwnership(queryObject, QDeclarativeEngine::JavaScriptOwnership);
    return queryObject;
}

/*!
    \qmlmethod object QtJsonDb::Partition::createChangesSince(stateNumber, types)

    Create the ChangesSince object. It will set the \a stateNumber, filter \a types parameters
    of the object. Users have to call start() to start the changesSince query in this partition.
    The script engine decides the life time of the returned object. The returned object can be
    saved in a 'property var' until it is required.

    \code
    import QtJsonDb 1.0 as JsonDb
    property var changesObject;
    function onFinished()
    {
        var results = queryObject.takeResults();
        console.log("Results: Count + results.length );
    }

    Component.onCompleted: {
        changesObject = nokiaPartition.createChangesSince(10, ["Contact"]);
        changesObject.finished.connect(onFinished);
        changesObject.start();

    }
    \endcode
    \sa QtJsonDb::ChangesSince

*/

JsonDbChangesSinceObject* JsonDbPartition::createChangesSince(int stateNumber, const QStringList &types)
{
    JsonDbChangesSinceObject* changesSinceObject = new JsonDbChangesSinceObject();
    changesSinceObject->setTypes(types);
    changesSinceObject->setStateNumber(stateNumber);
    changesSinceObject->setPartition(this);
    changesSinceObject->componentComplete();
    QDeclarativeEngine::setObjectOwnership(changesSinceObject, QDeclarativeEngine::JavaScriptOwnership);
    return changesSinceObject;
}

QDeclarativeListProperty<QObject> JsonDbPartition::childElements()
{
    return QDeclarativeListProperty<QObject>(this, childQMLElements);
}

void JsonDbPartition::updateNotification(JsonDbNotify *notify)
{
    JsonDbClient::NotifyTypes notifyActions = JsonDbClient::NotifyCreate
            | JsonDbClient::NotifyUpdate| JsonDbClient::NotifyRemove;
    notify->uuid= jsonDb.registerNotification(notifyActions, notify->query(), _name
                                              , notify, SLOT(dbNotified(QString,QtAddOn::JsonDb::JsonDbNotification))
                                              , notify, SLOT(dbNotifyReadyResponse(int,QVariant))
                                              , SLOT(dbNotifyErrorResponse(int,int,QString)));
    notifications.insert(notify->uuid, notify);
}


void JsonDbPartition::removeNotification(JsonDbNotify *notify)
{
    if (notifications.contains(notify->uuid)) {
        jsonDb.unregisterNotification(notify->uuid);
        notifications.remove(notify->uuid);
    }
}

void JsonDbPartition::call(QMap<int, QJSValue> &callbacks, int id, const QVariant &result)
{
    // Make sure that id exists in the map.
    QJSValue callback = callbacks[id];
    QJSEngine *engine = callback.engine();
    if (!engine) {
        callbacks.remove(id);
        return;
    }
    QJSValueList args;
    QVariantMap object = result.toMap();
    // object : id  , statenumber , items
    QJSValue response= engine->newObject();
    response.setProperty(JsonDbString::kStateNumberStr, object.value(JsonDbString::kStateNumberStr).toInt());
    response.setProperty(JsonDbString::kIdStr,  id);

    // response object : object { _version & _uuid } (can be a list)
    if (object.contains(QLatin1String("data"))) {
        QJSValue items = engine->toScriptValue(object.value(QLatin1String("data")));
        response.setProperty(QLatin1String("items"), items);
    } else {
        // Create an array with a single element
        QJSValue responseObject = engine->newObject();
        responseObject.setProperty(JsonDbString::kUuidStr, object.value(JsonDbString::kUuidStr).toString());
        responseObject.setProperty(JsonDbString::kVersionStr, object.value(JsonDbString::kVersionStr).toString());
        QJSValue items = engine->newArray(1);
        items.setProperty(0, responseObject);
        response.setProperty(QLatin1String("items"), items);
    }
    args << QJSValue(QJSValue::UndefinedValue) << response;
    callback.call(args);
    callbacks.remove(id);
}

void JsonDbPartition::callErrorCallback(QMap<int, QJSValue> &callbacks, int id, int code, const QString &message)
{
    // Make sure that id exists in the map.
    QJSValue callback = callbacks[id];
    QJSEngine *engine = callback.engine();
    if (!engine) {
        callbacks.remove(id);
        return;
    }
    QJSValueList args;
    QVariantMap error;
    error.insert(QLatin1String("code"), code);
    error.insert(QLatin1String("message"), message);

    // object : id
    QJSValue response = engine->newObject();
    response.setProperty(JsonDbString::kStateNumberStr, -1);
    response.setProperty(JsonDbString::kIdStr,  id);
    response.setProperty(QLatin1String("items"), engine->newArray());

    args << engine->toScriptValue(QVariant(error))<< response;
    callback.call(args);
    callbacks.remove(id);
}


void JsonDbPartition::dbResponse(int id, const QVariant &result)
{
    if (createCallbacks.contains(id)) {
        call(createCallbacks, id, result);
    } else if (updateCallbacks.contains(id)) {
        call(updateCallbacks, id, result);
    } else if (removeCallbacks.contains(id)) {
        call(removeCallbacks, id, result);
    }
}

void JsonDbPartition::dbErrorResponse(int id, int code, const QString &message)
{
    if (createCallbacks.contains(id)) {
        callErrorCallback(createCallbacks, id, code, message);
    } else if (removeCallbacks.contains(id)) {
        callErrorCallback(removeCallbacks, id, code, message);
    } else if (updateCallbacks.contains(id)) {
        callErrorCallback(updateCallbacks, id, code, message);
    }
}

void JsonDbPartition::queryFinished()
{
    JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender());
    if (object) {
        int id = findIds.value(object);
        QJSValue callback = findCallbacks.value(object);
        QJSEngine *engine = callback.engine();
        if (engine && callback.isCallable()) {
            QJSValueList args;
            // object : id  , statenumber , items
            QJSValue response= engine->newObject();
            response.setProperty(JsonDbString::kStateNumberStr, object->stateNumber());
            response.setProperty(JsonDbString::kIdStr,  id);
            response.setProperty(QLatin1String("items"), engine->toScriptValue(object->takeResults()));
            args << QJSValue(QJSValue::UndefinedValue) << response;
            callback.call(args);
        }
        findIds.remove(object);
        findCallbacks.remove(object);
        object->deleteLater();
    }
}

void JsonDbPartition::queryStatusChanged()
{
    JsonDbQueryObject *object = qobject_cast<JsonDbQueryObject*>(sender());
    if (object && object->status() == JsonDbQueryObject::Error) {
        int id = findIds.value(object);
        QJSValue callback = findCallbacks.value(object);
        QJSEngine *engine = callback.engine();
        if (engine && callback.isCallable()) {
            QJSValueList args;

            QJSValue response = engine->newObject();
            response.setProperty(JsonDbString::kStateNumberStr, -1);
            response.setProperty(JsonDbString::kIdStr,  id);
            response.setProperty(QLatin1String("items"), engine->newArray());

            args << engine->toScriptValue(object->error())<< response;
            callback.call(args);
        }
        findIds.remove(object);
        findCallbacks.remove(object);
        object->deleteLater();
    }

}

#include "moc_jsondbpartition.cpp"
QT_END_NAMESPACE_JSONDB