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