Example #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 Django would prefer the more logical place.
         if e.args[0] in self.codes_for_integrityerror:
             six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
         raise
Example #2
0
 def execute(self, query, params=None):
     query, params = self._fix_for_params(query, params)
     self._guess_input_sizes([params])
     try:
         return self.cursor.execute(query, self._param_generator(params))
     except Database.DatabaseError as e:
         # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400.
         if hasattr(e.args[0], 'code') and e.args[0].code == 1400 and not isinstance(e, IntegrityError):
             six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
         raise
Example #3
0
    def check_constraints(self, table_names=None):
        """
        Checks 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.

        Raises an IntegrityError on the first invalid foreign key reference
        encountered (if any) and provides detailed information about the
        invalid reference in the error message.

        Backends can override this method if they can more directly apply
        constraint checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE")
        """
        cursor = self.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,
                        ))
Example #4
0
 def _commit(self):
     if self.connection is not None:
         try:
             return self.connection.commit()
         except Database.DatabaseError as e:
             # cx_Oracle 5.0.4 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_DJANGOTEST.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:
                 six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
             raise
Example #5
0
 def executemany(self, query, params=None):
     if not params:
         # No params given, nothing to do
         return None
     # uniform treatment for sequences and iterables
     params_iter = iter(params)
     query, firstparams = self._fix_for_params(query, next(params_iter))
     # we build a list of formatted params; as we're going to traverse it
     # more than once, we can't make it lazy by using a generator
     formatted = [firstparams] + [self._format_params(p) for p in params_iter]
     self._guess_input_sizes(formatted)
     try:
         return self.cursor.executemany(query, [self._param_generator(p) for p in formatted])
     except Database.DatabaseError as e:
         # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400.
         if hasattr(e.args[0], 'code') and e.args[0].code == 1400 and not isinstance(e, IntegrityError):
             six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
         raise