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