dbapi.py
changeset 5863 4495b9bc49df
parent 5815 282194aa43f3
parent 5859 3da3574fe397
child 6012 d56fd78006cd
child 6061 7cb29eab1c9d
equal deleted inserted replaced
5858:384d34e76d6d 5863:4495b9bc49df
   438         if eid_key is not None:
   438         if eid_key is not None:
   439             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
   439             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
   440                  DeprecationWarning, stacklevel=2)
   440                  DeprecationWarning, stacklevel=2)
   441         # XXX use named argument for build_descr in case repo is < 3.8
   441         # XXX use named argument for build_descr in case repo is < 3.8
   442         rset = self._repo.execute(self._sessid, rql, args,
   442         rset = self._repo.execute(self._sessid, rql, args,
   443                                   build_descr=build_descr, txid=self._txid())
   443                                   build_descr=build_descr, **self._txid())
   444         rset.req = self.req
   444         rset.req = self.req
   445         return rset
   445         return rset
   446 
   446 
   447 
   447 
   448 class LogCursor(Cursor):
   448 class LogCursor(Cursor):
   477         self._close_on_del = getattr(cnxprops, 'close_on_del', True)
   477         self._close_on_del = getattr(cnxprops, 'close_on_del', True)
   478         self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro')
   478         self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro')
   479         if cnxprops and cnxprops.log_queries:
   479         if cnxprops and cnxprops.log_queries:
   480             self.executed_queries = []
   480             self.executed_queries = []
   481             self.cursor_class = LogCursor
   481             self.cursor_class = LogCursor
       
   482         if self._cnxtype == 'pyro':
       
   483             # check client/server compat
       
   484             if self._repo.get_versions()['cubicweb'] < (3, 8, 6):
       
   485                 self._txid = lambda cursor=None: {}
   482 
   486 
   483     def __repr__(self):
   487     def __repr__(self):
   484         if self.anonymous_connection:
   488         if self.anonymous_connection:
   485             return '<Connection %s (anonymous)>' % self.sessionid
   489             return '<Connection %s (anonymous)>' % self.sessionid
   486         return '<Connection %s>' % self.sessionid
   490         return '<Connection %s>' % self.sessionid
   494         else:
   498         else:
   495             self.rollback()
   499             self.rollback()
   496             return False #propagate the exception
   500             return False #propagate the exception
   497 
   501 
   498     def _txid(self, cursor=None): # XXX could now handle various isolation level!
   502     def _txid(self, cursor=None): # XXX could now handle various isolation level!
   499         return currentThread().getName()
   503         # return a dict as bw compat trick
       
   504         return {'txid': currentThread().getName()}
   500 
   505 
   501     def request(self):
   506     def request(self):
   502         return DBAPIRequest(self.vreg, DBAPISession(self))
   507         return DBAPIRequest(self.vreg, DBAPISession(self))
   503 
   508 
   504     def check(self):
   509     def check(self):
   632                 pass
   637                 pass
   633 
   638 
   634     def describe(self, eid):
   639     def describe(self, eid):
   635         if self._closed is not None:
   640         if self._closed is not None:
   636             raise ProgrammingError('Closed connection')
   641             raise ProgrammingError('Closed connection')
   637         return self._repo.describe(self.sessionid, eid, txid=self._txid())
   642         return self._repo.describe(self.sessionid, eid, **self._txid())
   638 
   643 
   639     def close(self):
   644     def close(self):
   640         """Close the connection now (rather than whenever __del__ is called).
   645         """Close the connection now (rather than whenever __del__ is called).
   641 
   646 
   642         The connection will be unusable from this point forward; an Error (or
   647         The connection will be unusable from this point forward; an Error (or
   645         connection.  Note that closing a connection without committing the
   650         connection.  Note that closing a connection without committing the
   646         changes first will cause an implicit rollback to be performed.
   651         changes first will cause an implicit rollback to be performed.
   647         """
   652         """
   648         if self._closed:
   653         if self._closed:
   649             raise ProgrammingError('Connection is already closed')
   654             raise ProgrammingError('Connection is already closed')
   650         self._repo.close(self.sessionid, txid=self._txid())
   655         self._repo.close(self.sessionid, **self._txid())
   651         del self._repo # necessary for proper garbage collection
   656         del self._repo # necessary for proper garbage collection
   652         self._closed = 1
   657         self._closed = 1
   653 
   658 
   654     def commit(self):
   659     def commit(self):
   655         """Commit pending transaction for this connection to the repository.
   660         """Commit pending transaction for this connection to the repository.
   659 
   664 
   660         If the transaction is undoable, a transaction id will be returned.
   665         If the transaction is undoable, a transaction id will be returned.
   661         """
   666         """
   662         if not self._closed is None:
   667         if not self._closed is None:
   663             raise ProgrammingError('Connection is already closed')
   668             raise ProgrammingError('Connection is already closed')
   664         return self._repo.commit(self.sessionid, txid=self._txid())
   669         return self._repo.commit(self.sessionid, **self._txid())
   665 
   670 
   666     def rollback(self):
   671     def rollback(self):
   667         """This method is optional since not all databases provide transaction
   672         """This method is optional since not all databases provide transaction
   668         support.
   673         support.
   669 
   674 
   672         a connection without committing the changes first will cause an implicit
   677         a connection without committing the changes first will cause an implicit
   673         rollback to be performed.
   678         rollback to be performed.
   674         """
   679         """
   675         if not self._closed is None:
   680         if not self._closed is None:
   676             raise ProgrammingError('Connection is already closed')
   681             raise ProgrammingError('Connection is already closed')
   677         self._repo.rollback(self.sessionid, txid=self._txid())
   682         self._repo.rollback(self.sessionid, **self._txid())
   678 
   683 
   679     def cursor(self, req=None):
   684     def cursor(self, req=None):
   680         """Return a new Cursor Object using the connection.
   685         """Return a new Cursor Object using the connection.
   681 
   686 
   682         On pyro connection, you should get cursor after calling if
   687         On pyro connection, you should get cursor after calling if
   712 
   717 
   713         * `public`: when additional filtering is provided, their are by default
   718         * `public`: when additional filtering is provided, their are by default
   714           only searched in 'public' actions, unless a `public` argument is given
   719           only searched in 'public' actions, unless a `public` argument is given
   715           and set to false.
   720           and set to false.
   716         """
   721         """
       
   722         actionfilters.update(self._txid())
   717         txinfos = self._repo.undoable_transactions(self.sessionid, ueid,
   723         txinfos = self._repo.undoable_transactions(self.sessionid, ueid,
   718                                                    txid=self._txid(),
       
   719                                                    **actionfilters)
   724                                                    **actionfilters)
   720         if req is None:
   725         if req is None:
   721             req = self.request()
   726             req = self.request()
   722         for txinfo in txinfos:
   727         for txinfo in txinfos:
   723             txinfo.req = req
   728             txinfo.req = req
   729         raise `NoSuchTransaction` if not found or if session's user is not
   734         raise `NoSuchTransaction` if not found or if session's user is not
   730         allowed (eg not in managers group and the transaction doesn't belong to
   735         allowed (eg not in managers group and the transaction doesn't belong to
   731         him).
   736         him).
   732         """
   737         """
   733         txinfo = self._repo.transaction_info(self.sessionid, txuuid,
   738         txinfo = self._repo.transaction_info(self.sessionid, txuuid,
   734                                              txid=self._txid())
   739                                              **self._txid())
   735         if req is None:
   740         if req is None:
   736             req = self.request()
   741             req = self.request()
   737         txinfo.req = req
   742         txinfo.req = req
   738         return txinfo
   743         return txinfo
   739 
   744 
   746         raise `NoSuchTransaction` if the transaction is not found or if
   751         raise `NoSuchTransaction` if the transaction is not found or if
   747         session's user is not allowed (eg not in managers group and the
   752         session's user is not allowed (eg not in managers group and the
   748         transaction doesn't belong to him).
   753         transaction doesn't belong to him).
   749         """
   754         """
   750         return self._repo.transaction_actions(self.sessionid, txuuid, public,
   755         return self._repo.transaction_actions(self.sessionid, txuuid, public,
   751                                               txid=self._txid())
   756                                               **self._txid())
   752 
   757 
   753     def undo_transaction(self, txuuid):
   758     def undo_transaction(self, txuuid):
   754         """Undo the given transaction. Return potential restoration errors.
   759         """Undo the given transaction. Return potential restoration errors.
   755 
   760 
   756         raise `NoSuchTransaction` if not found or if session's user is not
   761         raise `NoSuchTransaction` if not found or if session's user is not
   757         allowed (eg not in managers group and the transaction doesn't belong to
   762         allowed (eg not in managers group and the transaction doesn't belong to
   758         him).
   763         him).
   759         """
   764         """
   760         return self._repo.undo_transaction(self.sessionid, txuuid,
   765         return self._repo.undo_transaction(self.sessionid, txuuid,
   761                                            txid=self._txid())
   766                                            **self._txid())