37 |
38 |
38 from cubicweb import (UnknownEid, AuthenticationError, ValidationError, Binary, |
39 from cubicweb import (UnknownEid, AuthenticationError, ValidationError, Binary, |
39 UniqueTogetherError, UndoTransactionException, ViolatedConstraint) |
40 UniqueTogetherError, UndoTransactionException, ViolatedConstraint) |
40 from cubicweb import transaction as tx, server, neg_role, _ |
41 from cubicweb import transaction as tx, server, neg_role, _ |
41 from cubicweb.utils import QueryCache |
42 from cubicweb.utils import QueryCache |
|
43 from cubicweb.debug import emit_to_debug_channel |
42 from cubicweb.schema import VIRTUAL_RTYPES |
44 from cubicweb.schema import VIRTUAL_RTYPES |
43 from cubicweb.cwconfig import CubicWebNoAppConfiguration |
45 from cubicweb.cwconfig import CubicWebNoAppConfiguration |
44 from cubicweb.server import hook |
46 from cubicweb.server import hook |
45 from cubicweb.server import schema2sql as y2sql |
47 from cubicweb.server import schema2sql as y2sql |
46 from cubicweb.server.utils import crypt_password, verify_and_update |
48 from cubicweb.server.utils import crypt_password, verify_and_update |
682 @statsd_timeit |
684 @statsd_timeit |
683 def doexec(self, cnx, query, args=None, rollback=True, rql_query_tracing_token=None): |
685 def doexec(self, cnx, query, args=None, rollback=True, rql_query_tracing_token=None): |
684 """Execute a query. |
686 """Execute a query. |
685 it's a function just so that it shows up in profiling |
687 it's a function just so that it shows up in profiling |
686 """ |
688 """ |
|
689 |
|
690 query_debug_informations = { |
|
691 "sql": query, |
|
692 "args": args, |
|
693 "rollback": False, |
|
694 "rql_query_tracing_token": rql_query_tracing_token, |
|
695 } |
|
696 |
|
697 start = time.time() |
|
698 |
687 cursor = cnx.cnxset.cu |
699 cursor = cnx.cnxset.cu |
688 if server.DEBUG & server.DBG_SQL: |
700 if server.DEBUG & server.DBG_SQL: |
689 print('exec', highlight_terminal(query, "SQL"), args, cnx.cnxset.cnx) |
701 print('exec', highlight_terminal(query, "SQL"), args, cnx.cnxset.cnx) |
690 try: |
702 try: |
691 # str(query) to avoid error if it's a unicode string |
703 # str(query) to avoid error if it's a unicode string |
697 self.info("sql: %r\n args: %s\ndbms message: %r", |
709 self.info("sql: %r\n args: %s\ndbms message: %r", |
698 query, args, ex.args[0]) |
710 query, args, ex.args[0]) |
699 if rollback: |
711 if rollback: |
700 try: |
712 try: |
701 cnx.cnxset.rollback() |
713 cnx.cnxset.rollback() |
|
714 query_debug_informations["rollback"] = True |
702 if self.repo.config.mode != 'test': |
715 if self.repo.config.mode != 'test': |
703 self.debug('transaction has been rolled back') |
716 self.debug('transaction has been rolled back') |
704 except Exception: |
717 except Exception: |
705 pass |
718 pass |
706 if any(cls.__name__ for cls in ex.__class__.__mro__ |
719 if any(cls.__name__ for cls in ex.__class__.__mro__ |
739 if mo is not None: |
752 if mo is not None: |
740 # sqlite3 (old) |
753 # sqlite3 (old) |
741 raise ViolatedConstraint(cnx, cstrname=mo.group(1), |
754 raise ViolatedConstraint(cnx, cstrname=mo.group(1), |
742 query=query) |
755 query=query) |
743 raise |
756 raise |
|
757 finally: |
|
758 query_debug_informations["time"] = (time.time() - start) * 1000 |
|
759 emit_to_debug_channel("sql", query_debug_informations) |
744 return cursor |
760 return cursor |
745 |
761 |
746 @statsd_timeit |
762 @statsd_timeit |
747 def doexecmany(self, cnx, query, args): |
763 def doexecmany(self, cnx, query, args): |
748 """Execute a query. |
764 """Execute a query. |