doc/book/en/development/devcore/dbapi.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 14 Apr 2010 16:56:33 +0200
branchstable
changeset 5257 a31fbcfa8c3b
parent 5190 73bdc50d6af1
child 5307 228932b4f8c5
permissions -rw-r--r--
[doc/book] misc fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
API Python/RQL
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
The Python API developped to interface with RQL is inspired from the standard db-api,
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
with a Connection object having the methods cursor, rollback and commit essentially.
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
     8
The most important method is the `execute` method of a cursor.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    10
.. sourcecode:: python
5257
a31fbcfa8c3b [doc/book] misc fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5190
diff changeset
    11
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    12
  execute(rqlstring, args=None, cachekey=None, build_descr=True)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    13
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
:rqlstring: the RQL query to execute (unicode)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
:args: if the query contains substitutions, a dictionary containing the values to use
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    16
:cachekey:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
   an implementation detail of the RQL cache implies that if a substitution
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    18
   is used to introduce an eid *susceptible to raise the ambiguities in the query
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
   type resolution*, then we have to specify the corresponding key in the dictionary
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
   through this argument
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    23
The `Connection` object owns the methods `commit` and `rollback`. You
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    24
*should never need to use them* during the development of the web
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    25
interface based on the *CubicWeb* framework as it determines the end
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    26
of the transaction depending on the query execution success. They are
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    27
however useful in other contexts such as tests.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
.. note::
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    30
  While executing update queries (SET, INSERT, DELETE), if a query generates
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
  an error related to security, a rollback is automatically done on the current
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
  transaction.
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    33
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    34
Executing RQL queries from a view or a hook
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    35
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    36
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    37
When you're within code of the web interface, the db-api like connexion is
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    38
handled by the request object. You should not have to access it directly, but
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    39
use the `execute` method directly available on the request, eg:
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    40
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    41
   rset = self._cw.execute(rqlstring, kwargs)
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    42
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    43
Similarly, on the server side (eg in hooks), there is no db-api connexion (since
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    44
you're directly inside the data-server), so you'll have to use the execute method
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    45
of the session object.
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    46
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    47
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    48
Important note about proper usage of .execute
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    49
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    50
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    51
Let's say you want to get T which is in configuration C, this translates to:
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    52
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    53
.. sourcecode:: python
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    54
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    55
   self._cw.execute('Any T WHERE T in_conf C, C eid %s' % entity.eid)
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    56
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    57
But it must be written in a syntax that will benefit from the use
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    58
of a cache on the RQL server side:
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    59
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    60
.. sourcecode:: python
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    61
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    62
   self._cw.execute('Any T WHERE T in_conf C, C eid %(x)s', {'x': entity.eid})
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    63
5189
84d4587a92bc [doc/book] rql/dbapi cleanup, rip cachekey (prematurely ?)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4441
diff changeset
    64
The syntax tree is built once for the "generic" RQL and can be re-used
5190
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    65
with a number of different eids. There rql IN operator is an exception
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    66
to this rule.
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    67
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    68
.. sourcecode:: python
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    69
5190
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    70
   self._cw.execute('Any T WHERE T in_conf C, C name IN (%s)' % ','.join(['foo', 'bar']))
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    71
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    72
Alternativelly, some of the common data related to an entity can be
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    73
obtained from the `entity.related()` method (which is used under the
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    74
hood by the orm when you use attribute access notation on an entity to
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    75
get a relation. The initial request would then be translated to:
4441
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    76
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    77
.. sourcecode:: python
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    78
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    79
   entity.related('in_conf', 'object')
550cf406dbc6 moved content to the dbapi section
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    80
5190
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    81
Additionnaly this benefits from the fetch_attrs policy eventually
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    82
defined on the class element, which says which attributes must be also
73bdc50d6af1 [doc/book] dbapi: talk about IN exception, simplify
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5189
diff changeset
    83
loaded when the entity is loaded through the orm.