doc/book/devrepo/devcore/dbapi.rst
author Christophe de Vienne <christophe@unlish.com>
Thu, 08 Jan 2015 22:11:06 +0100
changeset 10491 c67bcee93248
permissions -rw-r--r--
[doc] Restructure the documentation * Create a new index file * Move the sphinx configuration files do the documentation root * Move book/README to dev/documenting.rst * Move book/mode_plan.py to tools/ * Move book/en/images to images * Move book/en/* to book/ * Move changelogs to changes/* * Adapt the Makefile * Add a title to the javascript api index Related to #4832808
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10491
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
.. _dbapi:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
Python/RQL API
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
~~~~~~~~~~~~~~
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
The Python API developped to interface with RQL is inspired from the standard db-api,
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
with a Connection object having the methods cursor, rollback and commit essentially.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
The most important method is the `execute` method of a cursor.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
   execute(rqlstring, args=None, build_descr=True)
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
:rqlstring: the RQL query to execute (unicode)
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
:args: if the query contains substitutions, a dictionary containing the values to use
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
The `Connection` object owns the methods `commit` and `rollback`. You
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
*should never need to use them* during the development of the web
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
interface based on the *CubicWeb* framework as it determines the end
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
of the transaction depending on the query execution success. They are
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
however useful in other contexts such as tests or custom controllers.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
.. note::
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
  If a query generates an error related to security (:exc:`Unauthorized`) or to
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
  integrity (:exc:`ValidationError`), the transaction can still continue but you
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
  won't be able to commit it, a rollback will be necessary to start a new
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
  transaction.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
  Also, a rollback is automatically done if an error occurs during commit.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
.. note::
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
   A :exc:`ValidationError` has a `entity` attribute. In CubicWeb,
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
   this atttribute is set to the entity's eid (not a reference to the
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
   entity itself).
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
Executing RQL queries from a view or a hook
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
When you're within code of the web interface, the db-api like connexion is
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
handled by the request object. You should not have to access it directly, but
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
use the `execute` method directly available on the request, eg:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
   rset = self._cw.execute(rqlstring, kwargs)
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
Similarly, on the server side (eg in hooks), there is no db-api connexion (since
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
you're directly inside the data-server), so you'll have to use the execute method
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
of the session object.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
Proper usage of `.execute`
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
~~~~~~~~~~~~~~~~~~~~~~~~~~
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
Let's say you want to get T which is in configuration C, this translates to:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
   self._cw.execute('Any T WHERE T in_conf C, C eid %s' % entity.eid)
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
But it must be written in a syntax that will benefit from the use
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    64
of a cache on the RQL server side:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    65
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    66
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
   self._cw.execute('Any T WHERE T in_conf C, C eid %(x)s', {'x': entity.eid})
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
The syntax tree is built once for the "generic" RQL and can be re-used
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
with a number of different eids. There rql IN operator is an exception
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
to this rule.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
   self._cw.execute('Any T WHERE T in_conf C, C name IN (%s)'
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
                    % ','.join(['foo', 'bar']))
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    79
Alternativelly, some of the common data related to an entity can be
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
obtained from the `entity.related()` method (which is used under the
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
hood by the orm when you use attribute access notation on an entity to
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
get a relation. The initial request would then be translated to:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    83
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    84
.. sourcecode:: python
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    85
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    86
   entity.related('in_conf', 'object')
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    87
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    88
Additionnaly this benefits from the fetch_attrs policy (see
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    89
:ref:`FetchAttrs`) eventually defined on the class element, which says
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
which attributes must be also loaded when the entity is loaded through
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    91
the orm.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    92
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    93
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    94
.. _resultset:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    95
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    96
The `ResultSet` API
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    97
~~~~~~~~~~~~~~~~~~~
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    98
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    99
ResultSet instances are a very commonly manipulated object. They have
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   100
a rich API as seen below, but we would like to highlight a bunch of
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   101
methods that are quite useful in day-to-day practice:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   102
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   103
* `__str__()` (applied by `print`) gives a very useful overview of both
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   104
  the underlying RQL expression and the data inside; unavoidable for
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   105
  debugging purposes
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   106
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   107
* `printable_rql()` produces back a well formed RQL expression as a
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   108
  string; it is very useful to build views
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   109
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   110
* `entities()` returns a generator on all entities of the result set
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   111
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   112
* `get_entity(row, col)` gets the entity at row, col coordinates; one
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   113
  of the most used result set method
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   114
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   115
.. autoclass:: cubicweb.rset.ResultSet
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   116
   :members:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   117
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   118
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   119
The `Cursor` and `Connection` API
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   121
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   122
The whole cursor API is developped below.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   123
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   124
.. note::
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   125
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   126
  In practice you'll usually use the `.execute` method on the _cw object of
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   127
  appobjects. Usage of other methods is quite rare.
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   128
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   129
.. autoclass:: cubicweb.dbapi.Cursor
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   130
   :members:
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   131
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   132
.. autoclass:: cubicweb.dbapi.Connection
c67bcee93248 [doc] Restructure the documentation
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   133
   :members: