dbapi.py
changeset 5174 78438ad513ca
parent 5100 04c71ebf38a5
child 5223 6abd6e3599f4
equal deleted inserted replaced
5173:73760bbb66bd 5174:78438ad513ca
    12 __docformat__ = "restructuredtext en"
    12 __docformat__ = "restructuredtext en"
    13 
    13 
    14 from logging import getLogger
    14 from logging import getLogger
    15 from time import time, clock
    15 from time import time, clock
    16 from itertools import count
    16 from itertools import count
       
    17 from warnings import warn
    17 
    18 
    18 from logilab.common.logging_ext import set_log_methods
    19 from logilab.common.logging_ext import set_log_methods
    19 from logilab.common.decorators import monkeypatch
    20 from logilab.common.decorators import monkeypatch
    20 from logilab.common.deprecation import deprecated
    21 from logilab.common.deprecation import deprecated
    21 
    22 
   280     def set_user(self, user):
   281     def set_user(self, user):
   281         self._user = user
   282         self._user = user
   282         if user:
   283         if user:
   283             self.set_entity_cache(user)
   284             self.set_entity_cache(user)
   284 
   285 
   285     def execute(self, *args, **kwargs):
   286     def execute(self, rql, args=None, eid_key=None, build_descr=True):
   286         """Session interface compatibility"""
   287         if eid_key is not None:
   287         return self.cursor.execute(*args, **kwargs)
   288             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
       
   289                  DeprecationWarning, stacklevel=2)
       
   290         return self.cursor.execute(rql, args, build_descr=build_descr)
   288 
   291 
   289 set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi'))
   292 set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi'))
   290 
   293 
   291 
   294 
   292 # exceptions ##################################################################
   295 # exceptions ##################################################################
   704         exception will be raised if any operation is attempted with the cursor.
   707         exception will be raised if any operation is attempted with the cursor.
   705         """
   708         """
   706         self._closed = True
   709         self._closed = True
   707 
   710 
   708 
   711 
   709     def execute(self, operation, parameters=None, eid_key=None, build_descr=True):
   712     def execute(self, rql, args=None, eid_key=None, build_descr=True):
   710         """Prepare and execute a database operation (query or command).
   713         """execute a rql query, return resulting rows and their description in
   711         Parameters may be provided as sequence or mapping and will be bound to
   714         a :class:`~cubicweb.rset.ResultSet` object
   712         variables in the operation.  Variables are specified in a
   715 
   713         database-specific notation (see the module's paramstyle attribute for
   716         * `rql` should be an Unicode string or a plain ASCII string, containing
   714         details).
   717           the rql query
   715 
   718 
   716         A reference to the operation will be retained by the cursor.  If the
   719         * `args` the optional args dictionary associated to the query, with key
   717         same operation object is passed in again, then the cursor can optimize
   720           matching named substitution in `rql`
   718         its behavior.  This is most effective for algorithms where the same
   721 
   719         operation is used, but different parameters are bound to it (many
   722         * `build_descr` is a boolean flag indicating if the description should
   720         times).
   723           be built on select queries (if false, the description will be en empty
   721 
   724           list)
   722         For maximum efficiency when reusing an operation, it is best to use the
   725 
   723         setinputsizes() method to specify the parameter types and sizes ahead
   726         on INSERT queries, there will be one row for each inserted entity,
   724         of time.  It is legal for a parameter to not match the predefined
   727         containing its eid
   725         information; the implementation should compensate, possibly with a loss
   728 
   726         of efficiency.
   729         on SET queries, XXX describe
   727 
   730 
   728         The parameters may also be specified as list of tuples to e.g. insert
   731         DELETE queries returns no result.
   729         multiple rows in a single operation, but this kind of usage is
   732 
   730         depreciated: executemany() should be used instead.
   733         .. Note::
   731 
   734           to maximize the rql parsing/analyzing cache performance, you should
   732         Return values are not defined by the DB-API, but this here it returns a
   735           always use substitute arguments in queries, i.e. avoid query such as::
   733         ResultSet object.
   736 
   734         """
   737             execute('Any X WHERE X eid 123')
   735         self._res = rset = self._repo.execute(self._sessid, operation,
   738 
   736                                               parameters, eid_key, build_descr)
   739           use::
       
   740 
       
   741             execute('Any X WHERE X eid %(x)s', {'x': 123})
       
   742         """
       
   743         if eid_key is not None:
       
   744             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
       
   745                  DeprecationWarning, stacklevel=2)
       
   746         self._res = rset = self._repo.execute(self._sessid, rql,
       
   747                                               args, build_descr)
   737         rset.req = self.req
   748         rset.req = self.req
   738         self._index = 0
   749         self._index = 0
   739         return rset
   750         return rset
   740 
   751 
   741 
   752