summaryrefslogtreecommitdiffstats
path: root/doc/JsonDB-Client.txt
blob: e4516c4fd80c934af5ec69ce0804da319faf3900 (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
JsonDB Client Library classes
=============================


The JsonDB wire protocol is serialized QVariant objects.  The
VariantStream class handles the serialization.


JsonDbConnection
================

The JsonDbConnection class encapsulates a single database connection.
A database connection can be to a local unix socket or a remote TCP
socket.  By default, the local socket used will be "jsondb".

Each connection can have a security token.  The security token must be
set before the connection to the database is made.  When the
connection is made, the first message sent to the database informs the
database of the security token.

For example:

  JsonDbConnection *connection = new JsonDbConnection;
  connection->setToken("MyMagicToken");
  connection->connectToServer("jsondb-test");

or
  
  JsonDbConnection *connection = new JsonDbConnection;
  connection->setToken("AnotherToken");
  connection->connectToHost( "testserver.nrcc.noklab.com", 8801 );


In most instances you will be connecting to the "jsondb" server on the
local machine.  The static function "instance()" can be used to
automatically retrieve the default connection.  For example:

  JsonDbConnection::setDefaultToken( "MagicToken" );  // Set this once
  ...

  JsonDbConnection *connection = JsonDbConnection::instance();

The first use of instance() will create a singleton connection object
and connect it to the local database.


To use the JsonDbConnection, you first connect to the "response",
"error" and optionally "notified" signals.  Then create a request
object.  The common request objects are provided through static
convenience functions: makeFindRequest, makeQueryRequest, etc.  The
"request" member function issues the database command and returns a
request id.  Eventually either a reponse or error signal will be
returned with that id.


There are two static convenience functions for issuing a quick request
and getting a direct answer.

The "oneShot" static function makes a single request.  The response
and error signal handling slots are connected just for the duration of
the call.  For example:

  JsonDbConnection *conn = JsonDbConnection::instance();
  conn->oneShot(JsonDbRequest::makeQueryRequest("[?_type=\"IMAGE\"]"),
                this, SLOT(handleOneShot(const QVariant&)));

The "handleOneShot()" function will be called with the result of the
query.  If you want to catch errors, pass an error function as well.
Note: you don't have to pass any slots, in which case the result will
be ignored.

The "sync" static function makes a single request and blocks until a
result is obtained.  You should not use this function in interactive
or time-critical code.  For example:

   QVariant result = JsonDbConnection::instance()->sync( 
        JsonDbConnection::makeQueryRequest("[?_type=\"TURTLE\"]"));


============
JsonDbClient
============

The challenge with a JsonDbConnection object is that a single process
may have several elements using the connection simultaneously.  Which
means that the responses coming back from the database need to be
de-multiplexed.  The JsonDbClient object handles the demultiplexing.

Most client code should use JsonDbClient objects directly and avoid
using the JsonDbConnection objects.

Typical use:

   JsonDbClient client;    // Constructs a client of the
                           // JsonDbConnection->instance() object

   connect(&client, SIGNAL(response(int,const QVariant&)),
            this, SLOT(handleResponse(int, const QVariant&)));			   
   int id = client.query("[?_type=\"Toad\"]);


The JsonDbClient object has convenience member functions "find",
"query", "create", "update", and "remove" which wrap appropriate
JsonDbConnection::makeXXXRequest function calls and issue the request.