language.rst 8.99 KB
Newer Older
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
.. -*- coding: utf-8 -*-

.. _RQL:

RQL syntax
----------

Reserved keywords
~~~~~~~~~~~~~~~~~
The keywords are not case sensitive.

::

     DISTINCT, INSERT, SET, DELETE,
     WHERE, AND, OR, NOT, EXISTS,
     IN, LIKE, UNION, WITH, BEING,
     TRUE, FALSE, NULL, TODAY, NOW,
     LIMIT, OFFSET,
     HAVING, GROUPBY, ORDERBY, ASC, DESC


Variables and Typing
~~~~~~~~~~~~~~~~~~~~

With RQL, we do not distinguish between entities and attributes. The
value of an attribute is considered an entity of a particular type (see
below), linked to one (real) entity by a relation called the name of
the attribute.

Entities and values to browse and/or select are represented in
the query by *variables* that must be written in capital letters.

There is a special type **Any**, referring to a non specific type.

We can restrict the possible types for a variable using the
special relation **is**.
The possible type(s) for each variable is derived from the schema
according to the constraints expressed above and thanks to the relations between
each variable.

Built-in types
``````````````

The base types supported are string (between double or single quotes),
integers or floats (the separator is '.'), dates and
boolean. We expect to receive a schema in which types String,
Int, Float, Date and Boolean are defined.

* `String` (literal: between double or single quotes).
* `Int`, `Float` (separator being'.').
* `Date`, `Datetime`, `Time` (literal: string YYYY/MM/DD [hh:mm] or keywords
  `TODAY` and `NOW`).
* `Boolean` (keywords `TRUE` and `FALSE`).
* `Keyword` NULL.


Operators
~~~~~~~~~

Logical Operators
`````````````````
::

     AND, OR, NOT, ','

  ',' is equivalent to 'AND' but with the smallest among the priority
  of logical operators (see :ref:`PriorityOperators`).

Mathematical Operators
Nicolas Chauvat's avatar
Nicolas Chauvat committed
70
```````````````````````
71
72
73
74
75
76
77
78
79
80
81
82
83
::

     +, -, *, /

Comparison operators
````````````````````
::

     =, <, <=, >=, >, ~=, IN, LIKE

* The operator `=` is the default operator.

* The operator `LIKE` equivalent to `~=` can be used with the
Nicolas Chauvat's avatar
Nicolas Chauvat committed
84
  special character `%` in a string to indicate that the chain
85
86
87
88
89
90
91
92
  must start or finish by a prefix/suffix:
  ::

     Any X WHERE X name ~= 'Th%'
     Any X WHERE X name LIKE '%lt'

* The operator `IN` provides a list of possible values:
  ::
Nicolas Chauvat's avatar
Nicolas Chauvat committed
93

94
95
96
    Any X WHERE X name IN ( 'chauvat', 'fayolle', 'di mascio', 'thenault')


Nicolas Chauvat's avatar
Nicolas Chauvat committed
97
XXX nico: "A trick <> 'bar'" wouldn't it be more convenient than
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
"NOT A trick 'bar'" ?

.. _PriorityOperators:

Operators priority
``````````````````

1. '*', '/'

2. '+', '-'

3. 'not'

4 'and'

5 'or'

6 ','


Search Query
~~~~~~~~~~~~

   [ `DISTINCT`] <entity type> V1 (, V2) \ *
   [ `GROUPBY` V1 (V2) \*] [ `ORDERBY` <orderterms>]
   [ `LIMIT` <value>] [ `OFFSET` <value>]
   [ `WHERE` <restriction>]
   [ `WITH` V1 (, V2) \ * BEING (<query>)]
   [ `HAVING` <restriction>]
   [ `UNION` <query>]

:entity type:
   Type of selected variables.
   The special type `Any` is equivalent to not specify a type.
:restriction:
Nicolas Chauvat's avatar
Nicolas Chauvat committed
133
   list of conditions to test successively
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
     `V1 relation V2 | <static value>`
:orderterms:
   Definition of the selection order: variable or column number followed by
   sorting method ( `ASC`, `DESC`), ASC is the default.
:note for grouped queries:
   For grouped queries (e.g., a clause `GROUPBY`), all
   selected variables must be aggregated or grouped.


Sorting and groups
``````````````````

- For grouped queries (e.g. with a GROUPBY clause), all
  selected variables should be grouped.

- To group and/or sort by attributes, we can do: "X,L user U, U
  login L GROUPBY L, X ORDERBY L"

- If the sorting method (SORT_METHOD) is not specified, then the sorting is
  ascendant.

- Aggregate Functions: COUNT, MIN, MAX, AVG, SUM


Negation
````````

* A query such as `Document X WHERE NOT X owned_by U` means "the
  documents have no relation `owned_by`".
* But the query `Document X WHERE NOT X owned_by U, U login "syt"`
  means "the documents have no relation `owned_by` with the user
  syt". They may have a relation "owned_by" with another user.

Identity
````````

Nicolas Chauvat's avatar
Nicolas Chauvat committed
170
You can use the special relation `identity` in a query to
171
172
173
174
175
176
177
178
179
180
181
182
183
add an identity constraint between two variables. This is equivalent
to ``is`` in python::

   Any A WHERE A comments B, A identity B

return all objects that comment themselves. The relation
`identity` is especially useful when defining the rules for securities
with `RQLExpressions`.


Limit / offset
``````````````
::
Nicolas Chauvat's avatar
Nicolas Chauvat committed
184

185
186
187
188
189
    Any P ORDERBY N LIMIT 5 OFFSET 10 WHERE P is Person, P firstname N

Function calls
``````````````
::
Nicolas Chauvat's avatar
Nicolas Chauvat committed
190

191
192
193
    Any UPPER(N) WHERE P firstname N

Functions on string: UPPER, LOWER
Nicolas Chauvat's avatar
Nicolas Chauvat committed
194

195
196
197
Exists
``````
::
Nicolas Chauvat's avatar
Nicolas Chauvat committed
198

199
    Any X ORDERBY PN,N
Nicolas Chauvat's avatar
Nicolas Chauvat committed
200
    WHERE X num N, X version_of P, P name PN,
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
          EXISTS(X in_state S, S name IN ("dev", "ready"))
          OR EXISTS(T tags X, T name "priority")


Optional relations (Left outer join)
````````````````````````````````````

* They allow you to select entities related or not to another.

* You must use the `?` behind the variable to specify that the relation
  toward it is optional:

   - Anomalies of a project attached or not to a version ::

       Any X, V WHERE X concerns P, P eid 42, X corrected_in V?

   - All cards and the project they document if necessary ::

       Any C, P WHERE C is Card, P? documented_by C

    Any T,P,V WHERE T is Ticket, T concerns P, T done_in V?
Nicolas Chauvat's avatar
Nicolas Chauvat committed
222
223


224
225
226
Having
``````
::
Nicolas Chauvat's avatar
Nicolas Chauvat committed
227

228
229
230
231
232
233
234
    Any X GROUPBY X WHERE X knows Y HAVING COUNT(Y) > 10

Subqueries
``````````
::

    (Any X WHERE X is Person) UNION (Any X WHERE X is Company)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
235

236
237

     DISTINCT Any W, REF
Nicolas Chauvat's avatar
Nicolas Chauvat committed
238
        WITH W, REF BEING
239
            (
Nicolas Chauvat's avatar
Nicolas Chauvat committed
240
	      (Any W, REF WHERE W is Workcase, W ref REF,
241
                                 W concerned_by D, D name "Logilab")
Nicolas Chauvat's avatar
Nicolas Chauvat committed
242
               UNION
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
              (Any W, REF WHERE W is Workcase, W ref REF, '
                                W split_into WP, WP name "WP1")
            )


Examples
````````

- *Search for the object of identifier 53*
  ::

        Any WHERE X
        X eid 53

- *Search material such as comics, owned by syt and available*
  ::

        Any X WHERE X is Document
        X occurence_of F, F class C, C name 'Comics'
        X owned_by U, U login 'syt'
        X available TRUE

- *Looking for people working for eurocopter interested in training*
  ::

        Any P WHERE
        P is Person, P work_for S, S name 'Eurocopter'
        P interested_by T, T name 'training'

- *Search note less than 10 days old written by jphc or ocy*
  ::

        Any N WHERE
        N is Note, N written_on D, D day> (today -10),
        N written_by P, P name 'jphc' or P name 'ocy'

- *Looking for people interested in training or living in Paris*
  ::

        Any P WHERE
        P is Person, (P interested_by T, T name 'training') OR
        (P city 'Paris')

- *The name and surname of all people*
  ::

        Any N, P WHERE
        X is Person, X name N, X first_name P

  Note that the selection of several entities generally force
  the use of "Any" because the type specification applies otherwise
  to all the selected variables. We could write here
  ::

        String N, P WHERE
        X is Person, X name N, X first_name P


  Note: You can not specify several types with * ... where X is FirstType or X is SecondType*.
Sylvain Thénault's avatar
typo    
Sylvain Thénault committed
302
  To specify several types explicitly, you have to do
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

  ::

        Any X where X is in (FirstType, SecondType)


Insertion query
~~~~~~~~~~~~~~~~

    `INSERT` <entity type> V1 (, <entity type> V2) \ * `:` <assignments>
    [ `WHERE` <restriction>]

:assignments:
   list of relations to assign in the form `V1 relationship V2 | <static value>`

The restriction can define variables used in assignments.

Nicolas Chauvat's avatar
Nicolas Chauvat committed
320
Caution, if a restriction is specified, the insertion is done for
321
322
323
324
325
326
327
328
329
330
331
332
333
*each line result returned by the restriction*.

- *Insert a new person named 'foo'*
  ::

        INSERT Person X: X name 'foo'

- *Insert a new person named 'foo', another called 'nice' and a 'friend' relation
  between them*
  ::

        INSERT Person X, Person Y: X name 'foo', Y name 'nice', X friend Y

Nicolas Chauvat's avatar
Nicolas Chauvat committed
334
- *Insert a new person named 'foo' and a 'friend' relation with an existing
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  person called 'nice'*
  ::

        INSERT Person X: X name 'foo', X friend  Y WHERE name 'nice'

Update and relation creation queries
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    `SET` <assignements>
    [ `WHERE` <restriction>]

Caution, if a restriction is specified, the update is done *for
each result line returned by the restriction*.

- *Renaming of the person named 'foo' to 'bar' with the first name changed*
  ::

        SET X name 'bar', X first_name 'original' WHERE X is Person, X name 'foo'

Nicolas Chauvat's avatar
Nicolas Chauvat committed
353
- *Insert a relation of type 'know' between objects linked by
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  the relation of type 'friend'*
  ::

        SET X know Y  WHERE X friend Y


Deletion query
~~~~~~~~~~~~~~
    `DELETE` (<entity type> V) | (V1 relation v2 ),...
    [ `WHERE` <restriction>]

Caution, if a restriction is specified, the deletion is made *for
each line result returned by the restriction*.

- *Deletion of the person named 'foo'*
  ::

        DELETE Person X WHERE X name 'foo'

- *Removal of all relations of type 'friend' from the person named 'foo'*
  ::

        DELETE X friend Y WHERE X is Person, X name 'foo'