doc/book/en/annexes/rql/language.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 29 Jun 2011 18:27:01 +0200
changeset 7578 49be8619b745
parent 7529 2fdc310be7cd
child 7632 3c9dfc6e820b
permissions -rw-r--r--
[book] rewrite / complete the RQL language chapter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
.. _RQL:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
RQL syntax
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
     8
.. _RQLKeywords:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
Reserved keywords
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
3686
d44b17016944 [doc] document rql operator ILIKE
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    14
  AND, ASC, BEING, DELETE, DESC, DISTINCT, EXISTS, FALSE, GROUPBY,
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    15
  HAVING, ILIKE, INSERT, LIKE, LIMIT, NOT, NOW, NULL, OFFSET,
3686
d44b17016944 [doc] document rql operator ILIKE
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    16
  OR, ORDERBY, SET, TODAY, TRUE, UNION, WHERE, WITH
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    18
The keywords are not case sensitive. You should not use them when defining your
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    19
schema, or as RQL variable names.
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
    20
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    21
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    22
.. _RQLCase:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    23
Case
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    24
~~~~
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    25
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    26
* Variables should be all upper-cased.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    27
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    28
* Relation should be all lower-cased and match exactly names of relations defined
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    29
  in the schema.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    30
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    31
* Entity types should start with an upper cased letter and be followed by at least
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    32
  a lower cased latter.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    33
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    34
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    35
.. _RQLVariables:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    36
Variables and typing
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
~~~~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    39
Entities and values to browse and/or select are represented in the query by
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    40
*variables* that must be written in capital letters.
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
    41
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    42
With RQL, we do not distinguish between entities and attributes. The value of an
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    43
attribute is considered as an entity of a particular type (see below), linked to
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    44
one (real) entity by a relation called the name of the attribute, where the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    45
entity is the subject and the attribute the object.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    47
The possible type(s) for each variable is derived from the schema according to
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    48
the constraints expressed above and thanks to the relations between each
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    49
variable.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    51
We can restrict the possible types for a variable using the special relation
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    52
**is** in the restrictions.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    55
Virtual relations
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    56
~~~~~~~~~~~~~~~~~
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    57
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    58
Those relations may only be used in RQL query but are not actual attributes of
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    59
your entities.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    60
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    61
* `has_text`: relation to use to query the full text index (only for entities
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    62
  having fulltextindexed attributes).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    63
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    64
* `identity`: relation to use to tell that a RQL variable is the same as another
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    65
  when you've to use two different variables for querying purpose. On the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    66
  opposite it's also useful together with the `NOT`_ operator to tell that two
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    67
  variables should not identify the same entity
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    68
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    69
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    70
.. _RQLLiterals:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    71
Literal expressions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    72
~~~~~~~~~~~~~~~~~~~
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    73
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    74
Bases types supported by RQL are those supported by yams schema. Literal values
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    75
are expressed as explained below:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    76
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    77
* string should be between double or single quotes. If the value contains a
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    78
  quote, it should be preceded by a backslash '\'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    79
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    80
* floats separator is dot '.'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    81
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    82
* boolean values are :keyword:`TRUE` and :keyword:`FALSE` keywords
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    83
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    84
* date and time should be expressed as a string with ISO notation : YYYY/MM/DD
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    85
  [hh:mm], or using keywords :keyword:`TODAY` and :keyword:`NOW`
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    86
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    87
You may also use the :keyword:`NULL` keyword, meaning 'unspecified'.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    88
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    89
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    90
.. _RQLOperators:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
Operators
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    94
.. _RQLLogicalOperators:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    95
Logical operators
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
`````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
     AND, OR, NOT, ','
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   101
',' is equivalent to 'AND' but with the smallest among the priority of logical
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   102
operators (see :ref:`RQLOperatorsPriority`).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   104
.. _RQLMathematicalOperators:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   105
Mathematical operators
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   106
``````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
     +, -, *, /
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   111
Those should behave as you expect.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   112
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   113
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   114
.. _RQLComparisonOperators:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
Comparison operators
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   116
````````````````````
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   117
 ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   119
     =, !=, <, <=, >=, >, IN
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   120
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   121
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   122
The syntax to use comparison operators is:
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   123
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   124
    `VARIABLE attribute <operator> VALUE`
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   126
The `=` operator is the default operator and can be omitted, i.e. :
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   127
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   128
    `VARIABLE attribute = VALUE`
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   130
is equivalent to
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   132
    `VARIABLE attribute VALUE`
3686
d44b17016944 [doc] document rql operator ILIKE
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   133
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   134
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   135
The operator `IN` provides a list of possible values: ::
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   136
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   137
    Any X WHERE X name IN ('chauvat', 'fayolle', 'di mascio', 'thenault')
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   140
.. _RQLStringOperators:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   141
String operators
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   142
````````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   143
::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   144
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   145
  LIKE, ILIKE, ~=, REGEXP
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   146
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   147
The :keyword:`LIKE` string operator can be used with the special character `%` in
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   148
a string as wild-card: ::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   149
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   150
     # match every entity whose name starts with 'Th'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   151
     Any X WHERE X name ~= 'Th%'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   152
     # match every entity whose name endswith 'lt'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   153
     Any X WHERE X name LIKE '%lt'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   154
     # match every entity whose name contains a 'l' and a 't'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   155
     Any X WHERE X name LIKE '%l%t%'
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   156
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   157
:keyword:`ILIKE` is the case insensitive version of :keyword:`LIKE`. It's not
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   158
available on all backend (e.g. sqlite doesn't support it). If not available for
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   159
your backend, :keyword:`ILIKE` will behave like :keyword:`LIKE`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   160
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   161
`~=` is a shortcut version of :keyword:`ILIKE`, or of :keyword:`LIKE` when the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   162
former is not available on the back-end.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   163
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   164
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   165
The :keyword:`REGEXP` is an alternative to :keyword:`LIKE` that supports POSIX
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   166
regular expressions::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   167
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   168
   # match entities whose title starts with a digit
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   169
   Any X WHERE X title REGEXP "^[0-9].*"
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   171
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   172
The underlying SQL operator used is back-end-dependent :
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   173
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   174
- the ``~`` operator is used for postgresql,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   175
- the ``REGEXP`` operator for mysql and sqlite.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   176
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   177
Other back-ends are not supported yet.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   178
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   179
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   180
.. _RQLOperatorsPriority:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
Operators priority
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
``````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   183
6324
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   184
#. "(", ")"
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   185
#. '*', '/'
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   186
#. '+', '-'
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   187
#. 'NOT'
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   188
#. 'AND'
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   189
#. 'OR'
bdb85e3602c8 [book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6120
diff changeset
   190
#. ','
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   191
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   192
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   193
.. _RQLSearchQuery:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   194
Search Query
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   195
~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   196
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   197
Simplified grammar of search query: ::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   198
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   199
   [ `DISTINCT`] `Any` V1 (, V2) \*
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   200
   [ `GROUPBY` V1 (, V2) \*] [ `ORDERBY` <orderterms>]
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   201
   [ `LIMIT` <value>] [ `OFFSET` <value>]
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   202
   [ `WHERE` <triplet restrictions>]
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   203
   [ `WITH` V1 (, V2)\* BEING (<query>)]
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   204
   [ `HAVING` <other restrictions>]
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   205
   [ `UNION` <query>]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   207
Selection
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   208
`````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   209
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   210
The fist occuring clause is the selection of terms that should be in the result
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   211
set.  Terms may be variable, literals, function calls, arithmetic, etc. and each
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   212
term is separated by a comma.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   213
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   214
There will be as much column in the result set as term in this clause, respecting
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   215
order.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   216
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   217
Syntax for function call is somewhat intuitive, for instance: ::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   218
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   219
    Any UPPER(N) WHERE P firstname N
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   220
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   221
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   222
Grouping and aggregating
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   223
````````````````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   224
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   225
The :keyword:`GROUPBY` keyword is followed by a list of terms on which results
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   226
should be grouped. They are usually used with aggregate functions, responsible to
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   227
aggregate values for each group (see :ref:`RQLAggregateFunctions`).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   228
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   229
For grouped queries, all selected variables must be either aggregated (i.e. used
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   230
by an aggregate function) or grouped (i.e. listed in the :keyword:`GROUPBY`
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   231
clause).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   232
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   233
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   234
Sorting
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   235
```````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   236
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   237
The :keyword:`ORDERBY` keyword if followed by the definition of the selection
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   238
order: variable or column number followed by sorting method (:keyword:`ASC`,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   239
:keyword:`DESC`), :keyword:`ASC` being the default. If the sorting method is not
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   240
specified, then the sorting is ascendant (`ASC`).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   241
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   242
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   243
Pagination
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   244
``````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   245
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   246
The :keyword:`LIMIT` and :keyword:`OFFSET` keywords may be respectively used to
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   247
limit the number of results and to tell from which result line to start (for
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   248
instance, use `LIMIT 20` to get the first 20 results, then `LIMIT 20 OFFSET 20`
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   249
to get the next 20.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   250
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   251
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   252
Restrictions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   253
````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   254
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   255
The :keyword:`WHERE` keyword introduce one of the "main" part of the query, where
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   256
you "define" variables and add some restrictions telling what you're interested
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   257
in.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   258
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   259
It's a list of triplets "subject relation object", e.g. `V1 relation
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   260
(V2 | <static value>)`. Triplets are separated using :ref:`RQLLogicalOperators`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   261
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   262
.. Note:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   263
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   264
  About the negation operator (:keyword:`NOT`):
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   265
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   266
  * "NOT X relation Y" is equivalent to "NOT EXISTS(X relation Y)"
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   267
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   268
  * `Any X WHERE NOT X owned_by U` means "entities that have no relation
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   269
    `owned_by`".
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   270
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   271
  * `Any X WHERE NOT X owned_by U, U login "syt"` means "the entity have no
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   272
     relation `owned_by` with the user syt". They may have a relation "owned_by"
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   273
     with another user.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   274
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   275
In this clause, you can also use :keyword:`EXISTS` when you want to know if some
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   276
expression is true and do not need the complete set of elements that make it
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   277
true. Testing for existence is much faster than fetching the complete set of
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   278
results, especially when you think about using `OR` against several expressions. For instance
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   279
if you want to retrieve versions which are in state "ready" or tagged by
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   280
"priority", you should write :
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   281
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   282
::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   283
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   284
    Any X ORDERBY PN,N
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   285
    WHERE X num N, X version_of P, P name PN,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   286
          EXISTS(X in_state S, S name "ready")
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   287
          OR EXISTS(T tags X, T name "priority")
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   288
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   289
not
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   290
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   291
::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   292
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   293
    Any X ORDERBY PN,N
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   294
    WHERE X num N, X version_of P, P name PN,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   295
          (X in_state S, S name "ready")
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   296
          OR (T tags X, T name "priority")
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   297
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   298
Both queries aren't at all equivalent :
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   299
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   300
* the former will retrieve all versions, then check for each one which are in the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   301
  matching state of or tagged by the expected tag,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   302
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   303
* the later will retrieve all versions, state and tags (cartesian product!),
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   304
  compute join and then exclude each row which are in the matching state of or
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   305
  tagged by the expected tag. This implies that : you won't get any result if the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   306
  in_state or tag
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7081
diff changeset
   307
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7081
diff changeset
   308
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   309
You can also use the question mark (`?`) to mark optional relations which allow
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   310
you to select entities related **or not** to another. It is a similar concept
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   311
that the `Left outer join`_:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   312
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   313
    the result of a left outer join (or simply left join) for table A and B
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   314
    always contains all records of the "left" table (A), even if the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   315
    join-condition does not find any matching record in the "right" table (B).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   316
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   317
You must use the `?` behind a variable to specify that the relation toward it
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   318
is optional. For instance:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   319
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   320
- Anomalies of a project attached or not to a version ::
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   321
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   322
       Any X, V WHERE X concerns P, P eid 42, X corrected_in V?
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   323
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   324
  You will get a result set containing all the project's tickets, with either the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   325
  version in which it's corrected or None for tickets not related to a version.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   326
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   327
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   328
- All cards and the project they document if any ::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   329
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   330
       Any C, P WHERE C is Card, P? documented_by C
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   331
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   332
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   333
Having restrictions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   334
```````````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   335
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   336
The :keyword:`HAVING` clause, as in SQL, may be used to restrict a query
6120
c000e41316ec [book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5924
diff changeset
   337
according to value returned by an aggregate function, e.g.::
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   338
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   339
    Any X GROUPBY X WHERE X relation Y HAVING COUNT(Y) > 10
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   340
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   341
It may however be used for something else: In the :keyword:`WHERE` clause, we are
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   342
limited to triplet expressions, so some things may not be expressed there. Let's
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   343
take an example : if you want to get people whose upper-cased first name equals to
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   344
another person upper-cased first name. There is no proper way to express this
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   345
using triplet, so you should use something like: ::
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   346
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   347
    Any X WHERE X firstname XFN, Y firstname YFN, NOT X identity Y HAVING UPPER(XFN) = UPPER(YFN)
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   348
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   349
Another example: imagine you want person born in 2000: ::
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   350
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   351
    Any X WHERE X birthday XB HAVING YEAR(XB) = 2000
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   352
6120
c000e41316ec [book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5924
diff changeset
   353
Notice that while we would like this to work without the HAVING clause, this
c000e41316ec [book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5924
diff changeset
   354
can't be currently be done because it introduces an ambiguity in RQL's grammar
c000e41316ec [book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5924
diff changeset
   355
that can't be handled by Yapps_, the parser's generator we're using.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   356
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   357
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   358
Sub-queries
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   359
```````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   360
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   361
The :keyword:`WITH` keyword introduce sub-queries clause. Each sub-query has the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   362
form:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   363
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   364
  V1(,V2) BEING (rql query)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   365
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   366
Variables at the left of the :keyword:`BEING` keyword defines into which
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   367
variables results from the sub-query will be mapped to into the outer query.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   368
Sub-queries are separated from each other using a comma.
6120
c000e41316ec [book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5924
diff changeset
   369
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   370
Let's say we want to retrieve for each project its number of versions and its
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   371
number of tickets. Due to the nature of relational algebra behind the scene, this
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   372
can't be achieved using a single query. You have to write something along the
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   373
line of: ::
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   374
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   375
  Any X, VC, TC WHERE X identity XX
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   376
  WITH X, VC BEING (Any X, COUNT(V) GROUPBY X WHERE V version_of X),
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   377
       XX, TC BEING (Any X, COUNT(T) GROUPBY X WHERE T ticket_of X)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   378
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   379
Notice that we can't reuse a same variable name as alias for two different
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   380
sub-queries, hence the usage of 'X' and 'XX' in this example, which are then
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   381
unified using the special `identity` relation (see :ref:`XXX`).
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   382
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   383
.. Warning:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   384
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   385
  Sub-queries define a new variable scope, so even if a variable has the same name
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   386
  in the outer query and in the sub-query, they technically **aren't* the same
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   387
  variable. So ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   388
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   389
     Any W, REF WITH W, REF BEING
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   390
         (Any W, REF WHERE W is Workcase, W ref REF,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   391
                           W concerned_by D, D name "Logilab")
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   392
  could be written:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   393
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   394
     Any W, REF WITH W, REF BEING
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   395
        (Any W1, REF1 WHERE W1 is Workcase, W1 ref REF1,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   396
                            W1 concerned_by D, D name "Logilab")
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   397
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   398
  Also, when a variable is coming from a sub-query, you currently can't reference
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   399
  its attribute or inlined relations in the outer query, you've to fetch them in
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   400
  the sub-query. For instance, let's say we want to sort by project name in our
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   401
  first example, we would have to write ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   402
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   403
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   404
    Any X, VC, TC ORDERBY XN WHERE X identity XX
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   405
    WITH X, XN, VC BEING (Any X, COUNT(V) GROUPBY X,XN WHERE V version_of X, X name XN),
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   406
         XX, TC BEING (Any X, COUNT(T) GROUPBY X WHERE T ticket_of X)
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   407
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   408
  instead of ::
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   409
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   410
    Any X, VC, TC ORDERBY XN WHERE X identity XX, X name XN,
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   411
    WITH X, XN, VC BEING (Any X, COUNT(V) GROUPBY X WHERE V version_of X),
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   412
         XX, TC BEING (Any X, COUNT(T) GROUPBY X WHERE T ticket_of X)
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   413
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   414
  which would result in a SQL execution error.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   415
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   416
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   417
Union
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   418
`````
7081
6beb7f1e05fa [doc] complete description of EXISTS in rql/language.rst
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6324
diff changeset
   419
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   420
You may get a result set containing the concatenation of several queries using
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   421
the :keyword:`UNION`. The selection of each query should have the same number of
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   422
columns.
7081
6beb7f1e05fa [doc] complete description of EXISTS in rql/language.rst
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6324
diff changeset
   423
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   424
::
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   425
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   426
    (Any X, XN WHERE X is Person, X surname XN) UNION (Any X,XN WHERE X is Company, X name XN)
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   427
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   428
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   429
.. _RQLFunctions:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   430
Available functions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   431
~~~~~~~~~~~~~~~~~~~
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   432
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   433
Below is the list of aggregate and transformation functions that are supported
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   434
nativly by the framework. Notice that cubes may define additional functions.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   435
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   436
.. _RQLAggregateFunctions:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   437
Aggregate functions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   438
```````````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   439
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   440
| :function:`COUNT`      | return the number of rows                                |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   441
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   442
| :function:`MIN`        | return the minimum value                                 |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   443
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   444
| :function:`MAX`        | return the maximum value                                 |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   445
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   446
| :function:`AVG`        | return the average value                                 |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   447
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   448
| :function:`SUM`        | return the sum of values                                 |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   449
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   450
| :function:`COMMA_JOIN` | return each value separated by a comma (for string only) |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   451
+------------------------+----------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   452
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   453
All aggregate functions above take a single argument. Take care some aggregate
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   454
functions (e.g. :keyword:`MAX`, :keyword:`MIN`) may return `None` if there is no
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   455
result row.
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   456
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   457
.. _RQLStringFunctions:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   458
String transformation functions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   459
```````````````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   460
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   461
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   462
| :function:`UPPER(String)`   | upper case the string                                           |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   463
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   464
| :function:`LOWER(String)`   | lower case the string                                           |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   465
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   466
| :function:`LENGTH(String)`  | return the length of the string                                 |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   467
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   468
| :function:`SUBSTRING(       | extract from the string a string starting at given index and of |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   469
|    String, start, length)`  | given length                                                    |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   470
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   471
| :function:`LIMIT_SIZE(      | if the length of the string is greater than given max size,     |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   472
|  String, max size)`         | strip it and add ellipsis ("..."). The resulting string will    |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   473
|                             | hence have max size + 3 characters                              |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   474
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   475
| :function:`TEXT_LIMIT_SIZE( | similar to the above, but allow to specify the MIME type of the |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   476
|  String, format, max size)` | text contained by the string. Supported formats are text/html,  |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   477
|                             | text/xhtml and text/xml. All others will be considered as plain |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   478
|                             | text. For non plain text format, sgml tags will be first removed|
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   479
|                             | before limiting the string.                                     |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   480
+-----------------------------+-----------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   481
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   482
.. _RQLDateFunctions:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   483
Date extraction functions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   484
`````````````````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   485
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   486
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   487
| :function:`YEAR(Date)`       | return the year of a date or datetime  |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   488
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   489
| :function:`MONTH(Date)`      | return the year of a date or datetime  |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   490
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   491
| :function:`DAY(Date)`        | return the year of a date or datetime  |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   492
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   493
| :function:`HOUR(Datetime)`   | return the year of a datetime          |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   494
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   495
| :function:`MINUTE(Datetime)` | return the year of a datetime          |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   496
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   497
| :function:`SECOND(Datetime)` | return the year of a datetime          |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   498
+------------------------------+----------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   499
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   500
.. _RQLOtherFunctions:
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   501
Other functions
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   502
```````````````
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   503
+---------------------------+--------------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   504
| :function:`ABS(num)`       | return the absolute value of a number                             |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   505
+---------------------------+--------------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   506
| :function:`RANDOM()`      | return a pseudo-random value from 0.0 to 1.0                       |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   507
+---------------------------+--------------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   508
| :function:`FSPATH(X)`     | expect X to be an attribute whose value is stored in a             |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   509
|                           | :ref:`BFSStorage` and return its path on the file system           |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   510
+---------------------------+--------------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   511
| :function:`FTKIRANK(X)`   | expect X to be an entity used in a has_text relation, and return a |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   512
|                           | number corresponding to the rank order of each resulting entity    |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   513
+---------------------------+--------------------------------------------------------------------+
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   514
| :function:`CAST(Type, X)` | expect X to be an attribute and return it casted into the given    |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   515
|                           | final type                                                         |
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   516
+---------------------------+--------------------------------------------------------------------+
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   517
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   518
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   519
.. _RQLExamples:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   520
Examples
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   521
~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   522
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   523
- *Search for the object of identifier 53*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   524
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   525
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   526
        Any WHERE X
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   527
        X eid 53
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   528
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   529
- *Search material such as comics, owned by syt and available*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   530
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   531
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   532
        Any X WHERE X is Document
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   533
        X occurence_of F, F class C, C name 'Comics'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   534
        X owned_by U, U login 'syt'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   535
        X available TRUE
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   536
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   537
- *Looking for people working for eurocopter interested in training*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   538
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   539
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   540
        Any P WHERE
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   541
        P is Person, P work_for S, S name 'Eurocopter'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   542
        P interested_by T, T name 'training'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   543
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   544
- *Search note less than 10 days old written by jphc or ocy*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   545
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   546
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   547
        Any N WHERE
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   548
        N is Note, N written_on D, D day> (today -10),
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   549
        N written_by P, P name 'jphc' or P name 'ocy'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   550
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   551
- *Looking for people interested in training or living in Paris*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   552
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   553
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   554
        Any P WHERE
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   555
        P is Person, (P interested_by T, T name 'training') OR
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   556
        (P city 'Paris')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   557
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   558
- *The surname and firstname of all people*
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   559
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   560
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   561
        Any N, P WHERE
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   562
        X is Person, X name N, X firstname P
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   563
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   564
  Note that the selection of several entities generally force
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   565
  the use of "Any" because the type specification applies otherwise
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   566
  to all the selected variables. We could write here
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   567
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   568
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   569
        String N, P WHERE
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   570
        X is Person, X name N, X first_name P
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   571
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   572
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   573
  Note: You can not specify several types with * ... where X is FirstType or X is SecondType*.
2043
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1714
diff changeset
   574
  To specify several types explicitly, you have to do
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   575
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   576
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   577
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   578
        Any X where X is in (FirstType, SecondType)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   579
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   580
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   581
.. _RQLInsertQuery:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   582
Insertion query
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   583
~~~~~~~~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   584
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   585
    `INSERT` <entity type> V1 (, <entity type> V2) \ * `:` <assignments>
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   586
    [ `WHERE` <restriction>]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   587
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   588
:assignments:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   589
   list of relations to assign in the form `V1 relationship V2 | <static value>`
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   590
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   591
The restriction can define variables used in assignments.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   592
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   593
Caution, if a restriction is specified, the insertion is done for
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   594
*each line result returned by the restriction*.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   595
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   596
- *Insert a new person named 'foo'*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   597
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   598
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   599
        INSERT Person X: X name 'foo'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   600
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   601
- *Insert a new person named 'foo', another called 'nice' and a 'friend' relation
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   602
  between them*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   603
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   604
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   605
        INSERT Person X, Person Y: X name 'foo', Y name 'nice', X friend Y
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   606
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   607
- *Insert a new person named 'foo' and a 'friend' relation with an existing
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   608
  person called 'nice'*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   609
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   610
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   611
        INSERT Person X: X name 'foo', X friend  Y WHERE name 'nice'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   612
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   613
.. _RQLSetQuery:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   614
Update and relation creation queries
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   616
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   617
    `SET` <assignements>
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   618
    [ `WHERE` <restriction>]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   619
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   620
Caution, if a restriction is specified, the update is done *for
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   621
each result line returned by the restriction*.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   622
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   623
- *Renaming of the person named 'foo' to 'bar' with the first name changed*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   624
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   625
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   626
        SET X name 'bar', X firstname 'original' WHERE X is Person, X name 'foo'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   627
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
   628
- *Insert a relation of type 'know' between objects linked by
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   629
  the relation of type 'friend'*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   630
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   631
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   632
        SET X know Y  WHERE X friend Y
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   633
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   634
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   635
.. _RQLDeleteQuery:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   636
Deletion query
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   637
~~~~~~~~~~~~~~
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   638
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   639
    `DELETE` (<entity type> V) | (V1 relation v2 ),...
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   640
    [ `WHERE` <restriction>]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   641
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   642
Caution, if a restriction is specified, the deletion is made *for
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   643
each line result returned by the restriction*.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   644
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   645
- *Deletion of the person named 'foo'*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   646
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   647
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   648
        DELETE Person X WHERE X name 'foo'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   649
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   650
- *Removal of all relations of type 'friend' from the person named 'foo'*
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   651
  ::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   652
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   653
        DELETE X friend Y WHERE X is Person, X name 'foo'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   654
5924
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   655
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   656
.. _Yapps: http://theory.stanford.edu/~amitp/yapps/
b218df942dd4 [book] update rql documentation and some erroneous/deprecated examples
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5393
diff changeset
   657
.. _Left outer join: http://en.wikipedia.org/wiki/Join_(SQL)#Left_outer_join
7578
49be8619b745 [book] rewrite / complete the RQL language chapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   658