server/sources/native.py
changeset 8695 358d8bed9626
parent 8694 d901c36bcfce
child 8707 28cbd267e96b
equal deleted inserted replaced
8694:d901c36bcfce 8695:358d8bed9626
    81         """
    81         """
    82         if server.DEBUG & server.DBG_SQL:
    82         if server.DEBUG & server.DBG_SQL:
    83             print 'exec', query, args
    83             print 'exec', query, args
    84         try:
    84         try:
    85             self.cu.execute(str(query), args)
    85             self.cu.execute(str(query), args)
    86         except Exception, ex:
    86         except Exception as ex:
    87             print "sql: %r\n args: %s\ndbms message: %r" % (
    87             print "sql: %r\n args: %s\ndbms message: %r" % (
    88                 query, args, ex.args[0])
    88                 query, args, ex.args[0])
    89             raise
    89             raise
    90 
    90 
    91     def fetchall(self):
    91     def fetchall(self):
   409         self.init_creating(source_entity._cw.cnxset)
   409         self.init_creating(source_entity._cw.cnxset)
   410         try:
   410         try:
   411             # test if 'asource' column exists
   411             # test if 'asource' column exists
   412             query = self.dbhelper.sql_add_limit_offset('SELECT asource FROM entities', 1)
   412             query = self.dbhelper.sql_add_limit_offset('SELECT asource FROM entities', 1)
   413             source_entity._cw.system_sql(query)
   413             source_entity._cw.system_sql(query)
   414         except Exception, ex:
   414         except Exception as ex:
   415             self.eid_type_source = self.eid_type_source_pre_131
   415             self.eid_type_source = self.eid_type_source_pre_131
   416 
   416 
   417     def shutdown(self):
   417     def shutdown(self):
   418         if self._eid_creation_cnx:
   418         if self._eid_creation_cnx:
   419             self._eid_creation_cnx.close()
   419             self._eid_creation_cnx.close()
   534                 raise
   534                 raise
   535             # FIXME: better detection of deconnection pb
   535             # FIXME: better detection of deconnection pb
   536             self.warning("trying to reconnect")
   536             self.warning("trying to reconnect")
   537             session.cnxset.reconnect(self)
   537             session.cnxset.reconnect(self)
   538             cursor = self.doexec(session, sql, args)
   538             cursor = self.doexec(session, sql, args)
   539         except (self.DbapiError,), exc:
   539         except self.DbapiError as exc:
   540             # We get this one with pyodbc and SQL Server when connection was reset
   540             # We get this one with pyodbc and SQL Server when connection was reset
   541             if exc.args[0] == '08S01' and session.mode != 'write':
   541             if exc.args[0] == '08S01' and session.mode != 'write':
   542                 self.warning("trying to reconnect")
   542                 self.warning("trying to reconnect")
   543                 session.cnxset.reconnect(self)
   543                 session.cnxset.reconnect(self)
   544                 cursor = self.doexec(session, sql, args)
   544                 cursor = self.doexec(session, sql, args)
   734             # instance
   734             # instance
   735             print 'exec', query, args, getattr(cnx, '_cnx', cnx)
   735             print 'exec', query, args, getattr(cnx, '_cnx', cnx)
   736         try:
   736         try:
   737             # str(query) to avoid error if it's an unicode string
   737             # str(query) to avoid error if it's an unicode string
   738             cursor.execute(str(query), args)
   738             cursor.execute(str(query), args)
   739         except Exception, ex:
   739         except Exception as ex:
   740             if self.repo.config.mode != 'test':
   740             if self.repo.config.mode != 'test':
   741                 # during test we get those message when trying to alter sqlite
   741                 # during test we get those message when trying to alter sqlite
   742                 # db schema
   742                 # db schema
   743                 self.critical("sql: %r\n args: %s\ndbms message: %r",
   743                 self.critical("sql: %r\n args: %s\ndbms message: %r",
   744                               query, args, ex.args[0])
   744                               query, args, ex.args[0])
   745             if rollback:
   745             if rollback:
   746                 try:
   746                 try:
   747                     session.cnxset.connection(self.uri).rollback()
   747                     session.cnxset.connection(self.uri).rollback()
   748                     if self.repo.config.mode != 'test':
   748                     if self.repo.config.mode != 'test':
   749                         self.critical('transaction has been rollbacked')
   749                         self.critical('transaction has been rollbacked')
   750                 except Exception, ex:
   750                 except Exception as ex:
   751                     pass
   751                     pass
   752             if ex.__class__.__name__ == 'IntegrityError':
   752             if ex.__class__.__name__ == 'IntegrityError':
   753                 # need string comparison because of various backends
   753                 # need string comparison because of various backends
   754                 for arg in ex.args:
   754                 for arg in ex.args:
   755                     mo = re.search('unique_cw_[^ ]+_idx', arg)
   755                     mo = re.search('unique_cw_[^ ]+_idx', arg)
   775             print 'execmany', query, 'with', len(args), 'arguments'
   775             print 'execmany', query, 'with', len(args), 'arguments'
   776         cursor = session.cnxset[self.uri]
   776         cursor = session.cnxset[self.uri]
   777         try:
   777         try:
   778             # str(query) to avoid error if it's an unicode string
   778             # str(query) to avoid error if it's an unicode string
   779             cursor.executemany(str(query), args)
   779             cursor.executemany(str(query), args)
   780         except Exception, ex:
   780         except Exception as ex:
   781             if self.repo.config.mode != 'test':
   781             if self.repo.config.mode != 'test':
   782                 # during test we get those message when trying to alter sqlite
   782                 # during test we get those message when trying to alter sqlite
   783                 # db schema
   783                 # db schema
   784                 self.critical("sql many: %r\n args: %s\ndbms message: %r",
   784                 self.critical("sql many: %r\n args: %s\ndbms message: %r",
   785                               query, args, ex.args[0])
   785                               query, args, ex.args[0])
   939         except (self.OperationalError, self.InterfaceError):
   939         except (self.OperationalError, self.InterfaceError):
   940             # FIXME: better detection of deconnection pb
   940             # FIXME: better detection of deconnection pb
   941             self.warning("trying to reconnect create eid connection")
   941             self.warning("trying to reconnect create eid connection")
   942             self._eid_creation_cnx = None
   942             self._eid_creation_cnx = None
   943             return self._create_eid() # pylint: disable=E1102
   943             return self._create_eid() # pylint: disable=E1102
   944         except (self.DbapiError,), exc:
   944         except self.DbapiError as exc:
   945             # We get this one with pyodbc and SQL Server when connection was reset
   945             # We get this one with pyodbc and SQL Server when connection was reset
   946             if exc.args[0] == '08S01':
   946             if exc.args[0] == '08S01':
   947                 self.warning("trying to reconnect create eid connection")
   947                 self.warning("trying to reconnect create eid connection")
   948                 self._eid_creation_cnx = None
   948                 self._eid_creation_cnx = None
   949                 return self._create_eid() # pylint: disable=E1102
   949                 return self._create_eid() # pylint: disable=E1102
  1307         """undo a relation removal"""
  1307         """undo a relation removal"""
  1308         errors = []
  1308         errors = []
  1309         subj, rtype, obj = action.eid_from, action.rtype, action.eid_to
  1309         subj, rtype, obj = action.eid_from, action.rtype, action.eid_to
  1310         try:
  1310         try:
  1311             sentity, oentity, rdef = _undo_rel_info(session, subj, rtype, obj)
  1311             sentity, oentity, rdef = _undo_rel_info(session, subj, rtype, obj)
  1312         except _UndoException, ex:
  1312         except _UndoException as ex:
  1313             errors.append(unicode(ex))
  1313             errors.append(unicode(ex))
  1314         else:
  1314         else:
  1315             for role, entity in (('subject', sentity),
  1315             for role, entity in (('subject', sentity),
  1316                                  ('object', oentity)):
  1316                                  ('object', oentity)):
  1317                 try:
  1317                 try:
  1318                     _undo_check_relation_target(entity, rdef, role)
  1318                     _undo_check_relation_target(entity, rdef, role)
  1319                 except _UndoException, ex:
  1319                 except _UndoException as ex:
  1320                     errors.append(unicode(ex))
  1320                     errors.append(unicode(ex))
  1321                     continue
  1321                     continue
  1322         if not errors:
  1322         if not errors:
  1323             self.repo.hm.call_hooks('before_add_relation', session,
  1323             self.repo.hm.call_hooks('before_add_relation', session,
  1324                                     eidfrom=subj, rtype=rtype, eidto=obj)
  1324                                     eidfrom=subj, rtype=rtype, eidto=obj)
  1390         """undo a relation addition"""
  1390         """undo a relation addition"""
  1391         errors = []
  1391         errors = []
  1392         subj, rtype, obj = action.eid_from, action.rtype, action.eid_to
  1392         subj, rtype, obj = action.eid_from, action.rtype, action.eid_to
  1393         try:
  1393         try:
  1394             sentity, oentity, rdef = _undo_rel_info(session, subj, rtype, obj)
  1394             sentity, oentity, rdef = _undo_rel_info(session, subj, rtype, obj)
  1395         except _UndoException, ex:
  1395         except _UndoException as ex:
  1396             errors.append(unicode(ex))
  1396             errors.append(unicode(ex))
  1397         else:
  1397         else:
  1398             rschema = rdef.rtype
  1398             rschema = rdef.rtype
  1399             if rschema.inlined:
  1399             if rschema.inlined:
  1400                 sql = 'SELECT 1 FROM cw_%s WHERE cw_eid=%s and cw_%s=%s'\
  1400                 sql = 'SELECT 1 FROM cw_%s WHERE cw_eid=%s and cw_%s=%s'\