doc/book/annexes/rql/intro.rst
changeset 10491 c67bcee93248
parent 7529 2fdc310be7cd
equal deleted inserted replaced
10490:76ab3c71aff2 10491:c67bcee93248
       
     1 
       
     2 .. _rql_intro:
       
     3 
       
     4 Introduction
       
     5 ------------
       
     6 
       
     7 Goals of RQL
       
     8 ~~~~~~~~~~~~
       
     9 
       
    10 The goal is to have a semantic language in order to:
       
    11 
       
    12 - query relations in a clear syntax
       
    13 - empowers access to data repository manipulation
       
    14 - making attributes/relations browsing easy
       
    15 
       
    16 As such, attributes will be regarded as cases of special relations (in
       
    17 terms of usage, the user should see no syntactic difference between an
       
    18 attribute and a relation).
       
    19 
       
    20 Comparison with existing languages
       
    21 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
    22 
       
    23 SQL
       
    24 ```
       
    25 
       
    26 RQL may remind of SQL but works at a higher abstraction level (the *CubicWeb*
       
    27 framework generates SQL from RQL to fetch data from relation databases). RQL is
       
    28 focused on browsing relations. The user needs only to know about the *CubicWeb*
       
    29 data model he is querying, but not about the underlying SQL model.
       
    30 
       
    31 Sparql
       
    32 ``````
       
    33 
       
    34 The query language most similar to RQL is SPARQL_, defined by the W3C to serve
       
    35 for the semantic web.
       
    36 
       
    37 Versa
       
    38 `````
       
    39 
       
    40 We should look in more detail, but here are already some ideas for the moment
       
    41 ... Versa_ is the language most similar to what we wanted to do, but the model
       
    42 underlying data being RDF, there are some things such as namespaces or
       
    43 handling of the RDF types which does not interest us. On the functionality
       
    44 level, Versa_ is very comprehensive including through many functions of
       
    45 conversion and basic types manipulation, which we may want to look at one time
       
    46 or another.  Finally, the syntax is a little esoteric.
       
    47 
       
    48 Datalog
       
    49 ```````
       
    50 
       
    51 Datalog_ is a prolog derived query langage which applies to relational
       
    52 databases. It is more expressive than RQL in that it accepts either
       
    53 extensional_ and intensional_ predicates (or relations). As of now,
       
    54 RQL only deals with intensional relations.
       
    55 
       
    56 The different types of queries
       
    57 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
    58 
       
    59 Search (`Any`)
       
    60    Extract entities and attributes of entities.
       
    61 
       
    62 Insert entities (`INSERT`)
       
    63    Insert new entities or relations in the database.
       
    64    It can also directly create relationships for the newly created entities.
       
    65 
       
    66 Update entities, create relations (`SET`)
       
    67    Update existing entities in the database,
       
    68    or create relations between existing entities.
       
    69 
       
    70 Delete entities or relationship (`DELETE`)
       
    71    Remove entities or relations existing in the database.
       
    72 
       
    73 
       
    74 RQL relation expressions
       
    75 ~~~~~~~~~~~~~~~~~~~~~~~~
       
    76 
       
    77 RQL expressions apply to a live database defined by a
       
    78 :ref:`datamodel_definition`. Apart from the main type, or head, of the
       
    79 expression (search, insert, etc.) the most common constituent of an
       
    80 RQL expression is a (set of) relation expression(s).
       
    81 
       
    82 An RQL relation expression contains three components:
       
    83 
       
    84 * the subject, which is an entity type
       
    85 * the predicate, which is a relation definition (an arc of the schema)
       
    86 * the object, which is either an attribute or a relation to another entity
       
    87 
       
    88 .. image:: Graph-ex.gif
       
    89     :alt: <subject> <predicate> <object>
       
    90     :align: center
       
    91 
       
    92 .. warning::
       
    93 
       
    94  A relation is always expressed in the order: ``subject``,
       
    95  ``predicate``, ``object``.
       
    96 
       
    97  It is important to determine if the entity type is subject or object
       
    98  to construct a valid expression. Inverting the subject/object is an
       
    99  error since the relation cannot be found in the schema.
       
   100 
       
   101  If one does not have access to the code, one can find the order by
       
   102  looking at the schema image in manager views (the subject is located
       
   103  at the beginning of the arrow).
       
   104 
       
   105 An example of two related relation expressions::
       
   106 
       
   107   P works_for C, P name N
       
   108 
       
   109 RQL variables represent typed entities. The type of entities is
       
   110 either automatically inferred (by looking at the possible relation
       
   111 definitions, see :ref:`RelationDefinition`) or explicitely constrained
       
   112 using the ``is`` meta relation.
       
   113 
       
   114 In the example above, we barely need to look at the schema. If
       
   115 variable names (in the RQL expression) and relation type names (in the
       
   116 schema) are expresssively designed, the human reader can infer as much
       
   117 as the |cubicweb| querier.
       
   118 
       
   119 The ``P`` variable is used twice but it always represent the same set
       
   120 of entities. Hence ``P works_for C`` and ``P name N`` must be
       
   121 compatible in the sense that all the Ps (which *can* refer to
       
   122 different entity types) must accept the ``works_for`` and ``name``
       
   123 relation types. This does restrict the set of possible values of P.
       
   124 
       
   125 Adding another relation expression::
       
   126 
       
   127   P works_for C, P name N, C name "logilab"
       
   128 
       
   129 This further restricts the possible values of P through an indirect
       
   130 constraint on the possible values of ``C``. The RQL-level unification_
       
   131 happening there is translated to one (or several) joins_ at the
       
   132 database level.
       
   133 
       
   134 .. note::
       
   135 
       
   136  In |cubicweb|, the term `relation` is often found without ambiguity
       
   137  instead of `predicate`.  This predicate is also known as the
       
   138  `property` of the triple in `RDF concepts`_
       
   139 
       
   140 
       
   141 RQL Operators
       
   142 ~~~~~~~~~~~~~
       
   143 
       
   144 An RQL expression's head can be completed using various operators such
       
   145 as ``ORDERBY``, ``GROUPBY``, ``HAVING``, ``LIMIT`` etc.
       
   146 
       
   147 RQL relation expressions can be grouped with ``UNION`` or
       
   148 ``WITH``. Predicate oriented keywords such as ``EXISTS``, ``OR``,
       
   149 ``NOT`` are available.
       
   150 
       
   151 The complete zoo of RQL operators is described extensively in the
       
   152 following chapter (:ref:`RQL`).
       
   153 
       
   154 .. _RDF concepts: http://www.w3.org/TR/rdf-concepts/
       
   155 .. _Versa: http://wiki.xml3k.org/Versa
       
   156 .. _SPARQL: http://www.w3.org/TR/rdf-sparql-query/
       
   157 .. _unification: http://en.wikipedia.org/wiki/Unification_(computing)
       
   158 .. _joins: http://en.wikipedia.org/wiki/Join_(SQL)
       
   159 .. _Datalog: http://en.wikipedia.org/wiki/Datalog
       
   160 .. _intensional: http://en.wikipedia.org/wiki/Intensional_definition
       
   161 .. _extensional: http://en.wikipedia.org/wiki/Extension_(predicate_logic)
       
   162