Beispiel #1
0
 def executemany(self, query, args):
     try:
         return self.cursor.executemany(query, args)
     except Database.OperationalError as e:
         # Map some error codes to IntegrityError, since they seem to be
         # misclassified and Server would prefer the more logical place.
         if e.args[0] in self.codes_for_integrityerror:
             raise utils.IntegrityError(*tuple(e.args))
         raise
Beispiel #2
0
 def check_constraints(self, table_names=None):
     """
     Check each table name in `table_names` for rows with invalid foreign
     key references. This method is intended to be used in conjunction with
     `disable_constraint_checking()` and `enable_constraint_checking()`, to
     determine if rows with invalid references were entered while constraint
     checks were off.
     """
     with self.cursor() as cursor:
         if table_names is None:
             table_names = self.introspection.table_names(cursor)
         for table_name in table_names:
             primary_key_column_name = self.introspection.get_primary_key_column(
                 cursor, table_name)
             if not primary_key_column_name:
                 continue
             key_columns = self.introspection.get_key_columns(
                 cursor, table_name)
             for column_name, referenced_table_name, referenced_column_name in key_columns:
                 cursor.execute("""
                     SELECT REFERRING.`%s`, REFERRING.`%s` FROM `%s` as REFERRING
                     LEFT JOIN `%s` as REFERRED
                     ON (REFERRING.`%s` = REFERRED.`%s`)
                     WHERE REFERRING.`%s` IS NOT NULL AND REFERRED.`%s` IS NULL
                     """ % (
                     primary_key_column_name,
                     column_name,
                     table_name,
                     referenced_table_name,
                     column_name,
                     referenced_column_name,
                     column_name,
                     referenced_column_name,
                 ))
                 for bad_row in cursor.fetchall():
                     raise utils.IntegrityError(
                         "The row in table '%s' with primary key '%s' has an "
                         "invalid foreign key: %s.%s contains a value '%s' that "
                         "does not have a corresponding value in %s.%s." % (
                             table_name,
                             bad_row[0],
                             table_name,
                             column_name,
                             bad_row[1],
                             referenced_table_name,
                             referenced_column_name,
                         ))
Beispiel #3
0
 def _commit(self):
     if self.connection is not None:
         try:
             return self.connection.commit()
         except Database.DatabaseError as e:
             # cx_Oracle raises a cx_Oracle.DatabaseError exception
             # with the following attributes and values:
             #  code = 2091
             #  message = 'ORA-02091: transaction rolled back
             #            'ORA-02291: integrity constraint (TEST_SERVERTEST.SYS
             #               _C00102056) violated - parent key not found'
             # We convert that particular case to our IntegrityError exception
             x = e.args[0]
             if hasattr(x, 'code') and hasattr(x, 'message') \
                and x.code == 2091 and 'ORA-02291' in x.message:
                 raise utils.IntegrityError(*tuple(e.args))
             raise