doc/book/en/20-08-rql.en.txt
changeset 108 60faaa480f02
parent 93 9c919a47e140
equal deleted inserted replaced
107:4fe4ce7e2544 108:60faaa480f02
       
     1 .. -*- coding: utf-8 -*-
       
     2 
       
     3 RQL language (Relation Query Language)
       
     4 ======================================
       
     5 
       
     6 XXXFIXME MERGE WITH 16-rql.en.txt
       
     7 
       
     8 Introduction
       
     9 ------------
       
    10 * RQL language focuses on browsing relations.
       
    11 * Attributes are considered as particular relations.
       
    12 * RQL is inspired from SQL but is a high level language.
       
    13 * A good knowledge of Erudi's schemas defining the application is required.
       
    14 
       
    15 
       
    16 Types of requests
       
    17 -----------------
       
    18 
       
    19 Search (`Any`)
       
    20   query the repository to extract entities and/or attributes.
       
    21 
       
    22 Insertion (`INSERT`)
       
    23   insert new entities in the database.
       
    24 
       
    25 Updates of entities, creation of relations (`SET`)
       
    26   update existing entities in the database, or create relations between existing
       
    27   entities
       
    28 
       
    29 Deletion of entities or relations (`DELETE`)
       
    30   delete existing entities and relations from the database.
       
    31 
       
    32 
       
    33 Variables and typing
       
    34 --------------------
       
    35 
       
    36 Entities and values to browse and/or select are set in the query through *variables*
       
    37 which should be written in capital letters.
       
    38 
       
    39 The possible types for each variable can be deducted from the schema depending on
       
    40 the conditions expressed in the query.
       
    41 
       
    42 You can force the possible types for a variable thanks to the special relation `is`.
       
    43 
       
    44 
       
    45 
       
    46 Built-in types
       
    47 --------------
       
    48 * `String` (literal: between double or single quotes).
       
    49 * `Int`, `Float` (separator is '.').
       
    50 * `Date`, `Datetime`, `Time` (literal: pattern YYYY/MM/DD[ hh:mm] or keywords
       
    51   `TODAY` and `NOW`).
       
    52 * `Boolean` (keywords `TRUE` et `FALSE`).
       
    53 * keyword `NULL`.
       
    54 
       
    55 Operators
       
    56 ----------
       
    57 * Logical operators: `AND`, `OR`, `,`.
       
    58 * Mathematical operators: `+`, `-`, `*`, `/`.
       
    59 * Comparison operators: `=`, `<`, `<=`, `>=`, `>`, `~=`, `LIKE`, `IN`.
       
    60 
       
    61   * The operator `=` is the default operator.
       
    62 
       
    63   * The operator `LIKE` / `~=` allows the use of the character `%` in a string
       
    64     to indicate that the string should start/end with a prefix/suffix::
       
    65     
       
    66       Any X WHERE X nom ~= 'Th%'
       
    67       Any X WHERE X nom LIKE '%lt'
       
    68 
       
    69   * The operator `IN` allows to provide a list of possible values::
       
    70 
       
    71       Any X WHERE X nom IN ('chauvat', 'fayolle', 'di mascio', 'thenault')
       
    72 
       
    73 Search query
       
    74 ------------
       
    75 
       
    76   [`DISTINCT`] <entity type> V1(, V2)\*
       
    77   [`GROUPBY` V1(, V2)\*]  [`ORDERBY` <orderterms>]
       
    78   [`WHERE` <condition>] 
       
    79   [`LIMIT` <value>] [`OFFSET` <value>]
       
    80 
       
    81 :entity type:
       
    82   Type of the selected variable
       
    83   Special type `Any` is equivalent to not specify a type
       
    84 :condition:
       
    85   list of relations to browse following the pattern 
       
    86     `V1 relation V2|<static value>`
       
    87 :orderterms:
       
    88   Setting of the selection order : variable or column number followed by the
       
    89   sorting method (`ASC`, `DESC`), ASC being the default value.
       
    90 :note  for grouped queries:
       
    91   For grouped queries (e.g. using function `GROUPBY`), all the selected 
       
    92   variables must be grouped or aggregated.
       
    93 
       
    94 Examples - search
       
    95 ~~~~~~~~~~~~~~~~~
       
    96 ::
       
    97 
       
    98       Any X WHERE X eid 53
       
    99       Personne X
       
   100       Personne X WHERE X travaille_pour S, S nom "logilab"
       
   101       Any E,COUNT(X) GROUPBY E ORDERBY EN WHERE X is E, E name EN 
       
   102       Any E,COUNT(X) GROUPBY E ORDERBY 2 WHERE X is E 
       
   103 
       
   104 
       
   105 Advanced features
       
   106 ~~~~~~~~~~~~~~~~~
       
   107 * Aggregate functions: `COUNT`, `MIN`, `MAX`, `SUM`.
       
   108 * String functions:`UPPER`, `LOWER`.
       
   109 * Optional relations:
       
   110 
       
   111   * They allow to select entities related to others or not.
       
   112 
       
   113   * You should use `?` behind the variable to specify the relation to itself is
       
   114     optional.
       
   115 
       
   116     - Project anomalies related to a version or not::
       
   117 
       
   118         Any X,V WHERE X concerns P, P eid 42, X corrected_in V?
       
   119 
       
   120     - All the cards and the project they document otherwise ::
       
   121 
       
   122         Any C,P WHERE C is Card, P? documented_by C
       
   123 
       
   124 Negation
       
   125 ~~~~~~~~
       
   126 * A query such as `Document X WHERE NOT X owned_by U` is equivalent to 
       
   127   "the documents which do not have relation `owned_by`".
       
   128 * Whereas the query `Document X WHERE NOT X owned_by U, U login "syt"`
       
   129   is equivalent to "the documents which do not have relation `owned_by`
       
   130   with the user syt". They could have a relation with other users.
       
   131 
       
   132 
       
   133 Identity
       
   134 ~~~~~~~~
       
   135 
       
   136 We could use the special relation `identity` in a query in order to add a
       
   137 condition of identity between two variables. This is equivalent to ``is``
       
   138 in Python.
       
   139 
       
   140   Any A WHERE A comments B, A identity B
       
   141 
       
   142 returns the set of objects which comment themselves. The relation `identity`
       
   143 is very usefull while defining security rules with `RQLExpressions`.
       
   144 
       
   145 Insertion queries
       
   146 -----------------
       
   147    `INSERT` <entity type> V1(, <entity type> V2)\* `:` <assignments>
       
   148    [`WHERE` <condition>] 
       
   149 
       
   150 :assignments:
       
   151   list of relations to assign such as `V1 relation V2|<static value>`
       
   152 
       
   153 The condition allow to define the variables we would use in assignments.
       
   154 
       
   155 Be careful, if a condition is specified, the insertion is done *for each result
       
   156 returned by the condition*.
       
   157  
       
   158 Examples - insertion
       
   159 ~~~~~~~~~~~~~~~~~~~~~
       
   160 * Insertion of a new person named 'bidule'::
       
   161 
       
   162        INSERT Person X: X name 'bidule'
       
   163 
       
   164 * Insertion of a new person named 'bidule', another named
       
   165   'chouette' and a relation 'friend' between them::
       
   166 
       
   167        INSERT Person X, Person Y: X name 'bidule', Y name 'chouette', X friend Y
       
   168 
       
   169 * Insertion of a new person named 'bidule' and a relation 'friend'with an 
       
   170   existing person 'chouette'::
       
   171 
       
   172        INSERT Person X: X name 'bidule', X friend Y WHERE Y name 'chouette'
       
   173 
       
   174 
       
   175 Update queries
       
   176 --------------
       
   177    `SET` <assignments>
       
   178    [`WHERE` <condition>] 
       
   179 
       
   180 Be careful, if a condition is specified, the update is done *for each result
       
   181 returned by the condition*.
       
   182 
       
   183 Examples - update 
       
   184 ~~~~~~~~~~~~~~~~~
       
   185 * Renaming of the person named 'bidule' to 'toto', with change on the first name::
       
   186 
       
   187        SET X name 'toto', X firstname 'original' WHERE X is 'Person', X name 'bidule'
       
   188 
       
   189 * Insertion of a relation of type 'know' between two objects linked with the relation
       
   190   of type 'friend' ::
       
   191 
       
   192        SET X know Y WHERE X friend Y
       
   193 
       
   194 Deletion queries
       
   195 ----------------
       
   196    `DELETE` (<entity type> V) | (V1 relation v2),...
       
   197    [`WHERE` <condition>] 
       
   198 
       
   199 
       
   200 Be careful, if a condition is specified, the deletion is done *for each result
       
   201 returned by the condition*.
       
   202 
       
   203 
       
   204 Examples
       
   205 ~~~~~~~~
       
   206 * Deletion of the person named 'toto'::
       
   207 
       
   208        DELETE Person X WHERE X name 'toto'
       
   209 
       
   210 * Deletion of all the relations of type 'friend' linked to the person named 
       
   211   'toto'::
       
   212 
       
   213        DELETE X friend Y WHERE X is 'Person', X name 'toto'