diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2012-05-29 15:27:35 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-29 15:59:04 +0200 |
commit | 001b688e54630f753e3ca3933cca2c2290210ec7 (patch) | |
tree | 546e0ab28910473fae7208c311418ebba78cbfed | |
parent | 79f51d6718ffb06b3c8ece31c25b5949ca4dcc7d (diff) |
Changed behavior of QJsonDbConnection::defaultConnection().
It is much more convenient if it is safe to assume that the returned default
connection is already connected, or will be autotomatically connected.
Change-Id: I5e0d89ad34f156b5cee5ca6e0f25eb79c32a2bca
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r-- | src/client/qjsondbconnection.cpp | 18 | ||||
-rw-r--r-- | src/client/qjsondbconnection_p.h | 1 | ||||
-rw-r--r-- | tests/auto/qjsondbrequest/testqjsondbrequest.cpp | 15 |
3 files changed, 33 insertions, 1 deletions
diff --git a/src/client/qjsondbconnection.cpp b/src/client/qjsondbconnection.cpp index b1209496..67ca409e 100644 --- a/src/client/qjsondbconnection.cpp +++ b/src/client/qjsondbconnection.cpp @@ -160,7 +160,7 @@ Q_GLOBAL_STATIC(QThreadStorage<QJsonDbConnection *>, _q_defaultConnection); */ QJsonDbConnectionPrivate::QJsonDbConnectionPrivate(QJsonDbConnection *q) - : q_ptr(q), status(QJsonDbConnection::Unconnected), autoReconnectEnabled(true), + : q_ptr(q), status(QJsonDbConnection::Unconnected), autoConnect(false), autoReconnectEnabled(true), explicitDisconnect(false), timeoutTimer(q), stream(0), lastRequestId(0), privatePartitionProcessing(0), privatePartitionHandler(0) { @@ -621,6 +621,10 @@ bool QJsonDbConnection::send(QJsonDbRequest *request) qWarning("QJsonDbConnection: cannot send request that is currently active."); return false; } + if (d->autoConnect) { + d->autoConnect = false; + connectToServer(); + } QJsonDbRequestPrivate *drequest = request->d_func(); drequest->setStatus(QJsonDbRequest::Queued); if (drequest->internal) @@ -839,6 +843,10 @@ bool QJsonDbConnection::addWatcher(QJsonDbWatcher *watcher) Q_D(QJsonDbConnection); if (!watcher) return false; + if (d->autoConnect) { + d->autoConnect = false; + connectToServer(); + } QJsonDbWatcherPrivate *dwatcher = watcher->d_func(); if (dwatcher->status != QJsonDbWatcher::Inactive) { qWarning("QJsonDbConnection: cannot add watcher that is already active."); @@ -881,6 +889,10 @@ bool QJsonDbConnection::removeWatcher(QJsonDbWatcher *watcher) This transfers ownership of \a connection to QtJsonDb, it will be deleted on thread exit, or on the next call to setDefaultConnection(). + The given default connection should typically be connected, so that the + users of defaultConnection() could assume there is no need to explicitly + call connectToServer(). + \sa QJsonDbConnection::defaultConnection() */ void QJsonDbConnection::setDefaultConnection(QJsonDbConnection *connection) @@ -893,6 +905,9 @@ void QJsonDbConnection::setDefaultConnection(QJsonDbConnection *connection) connection has been set for the current thread with setDefaultConnection(), a new connection is created. + The returned default connection can be assumed to be connected, so + typically there is no need to call connectToServer() on it. + The returned object is owned by QtJsonDb and should not be deleted. \sa QJsonDbConnection::setDefaultConnection() @@ -903,6 +918,7 @@ QJsonDbConnection *QJsonDbConnection::defaultConnection() QJsonDbConnection *defaultConnection = storage->localData(); if (!defaultConnection) { defaultConnection = new QJsonDbConnection; + defaultConnection->d_func()->autoConnect = true; storage->setLocalData(defaultConnection); } return defaultConnection; diff --git a/src/client/qjsondbconnection_p.h b/src/client/qjsondbconnection_p.h index 74800566..4647e2b4 100644 --- a/src/client/qjsondbconnection_p.h +++ b/src/client/qjsondbconnection_p.h @@ -106,6 +106,7 @@ public: QJsonDbConnection *q_ptr; QString socketName; QJsonDbConnection::Status status; + bool autoConnect; bool autoReconnectEnabled; bool explicitDisconnect; QTimer timeoutTimer; diff --git a/tests/auto/qjsondbrequest/testqjsondbrequest.cpp b/tests/auto/qjsondbrequest/testqjsondbrequest.cpp index 7a4dd110..0a9c7d1b 100644 --- a/tests/auto/qjsondbrequest/testqjsondbrequest.cpp +++ b/tests/auto/qjsondbrequest/testqjsondbrequest.cpp @@ -55,6 +55,7 @@ #include <QDir> #include <QFile> #include <QFileInfo> +#include <QTimer> #include <pwd.h> #include <signal.h> @@ -108,6 +109,7 @@ private slots: void bindings(); void replaceFromNull(); void multiplerequests(); + void defaultConnection(); private: bool writeTestObject(QObject* parent, const QString &type, int value, const QString &partition = QString()); @@ -1191,6 +1193,19 @@ void TestQJsonDbRequest::multiplerequests() QCOMPARE(results.size(), 0); } +void TestQJsonDbRequest::defaultConnection() +{ + // make sure that the default connection connects automatically + QJsonDbConnection *connection = QJsonDbConnection::defaultConnection(); + QJsonDbReadRequest request(QStringLiteral("[?_type=\"Foo\"]")); + QEventLoop ev; + QObject::connect(&request, SIGNAL(finished()), &ev, SLOT(quit())); + QTimer::singleShot(10000, &ev, SLOT(quit())); + QVERIFY(connection->send(&request)); + ev.exec(); + QCOMPARE((int)request.status(), (int)QJsonDbRequest::Finished); +} + void TestQJsonDbRequest::bindings() { { |