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, ) )
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_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: raise utils.IntegrityError(*tuple(e.args)) raise
def _execute_wrapper(self, method, query, args): """Wrapper around execute() and executemany()""" try: return method(query, args) except (mysql.connector.ProgrammingError) as err: six.reraise(utils.ProgrammingError, utils.ProgrammingError(err.msg), sys.exc_info()[2]) except (mysql.connector.IntegrityError) as err: six.reraise(utils.IntegrityError, utils.IntegrityError(err.msg), sys.exc_info()[2]) except mysql.connector.OperationalError as err: six.reraise(utils.DatabaseError, utils.DatabaseError(err.msg), sys.exc_info()[2]) except mysql.connector.DatabaseError as err: six.reraise(utils.DatabaseError, utils.DatabaseError(err.msg), sys.exc_info()[2])
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, Database.IntegrityError): six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise
def execute(self, query, params=None): if params is None: params = [] else: params = self._format_params(params) args = [(':arg%d' % i) for i in range(len(params))] # cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it # it does want a trailing ';' but not a trailing '/'. However, these # characters must be included in the original query in case the query # is being passed to SQL*Plus. if query.endswith(';') or query.endswith('/'): query = query[:-1] query = convert_unicode(query % tuple(args), self.charset) self._guess_input_sizes([params]) try: return self.cursor.execute(query, self._param_generator(params)) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
def executemany(self, sql, params_list): sql = self.format_sql(sql) # pyodbc's cursor.executemany() doesn't support an empty param_list if not params_list: if '?' in sql: return else: raw_pll = params_list params_list = [self.format_params(p) for p in raw_pll] try: return self.cursor.executemany(sql, params_list) except IntegrityError: e = sys.exc_info()[1] raise utils.IntegrityError(*e.args) except DatabaseError: e = sys.exc_info()[1] raise utils.DatabaseError(*e.args)
def executemany(self, operation, seq_parameters): try: if operation.count("db2regexExtraField(%s)") > 0: raise ValueError("Regex not supported in this operation") if operation.count("%s") > 0: operation = operation % (tuple("?" * operation.count("%s"))) seq_parameters = [self._format_parameters(parameters) for parameters in seq_parameters] try: return self.cursor.executemany(operation, seq_parameters) except IntegrityError as e: raise utils.IntegrityError(*e.args) from e except DatabaseError as e: raise utils.DatabaseError(*e.args) from e except (IndexError, TypeError): return None
def execute(self, query, args=None): #from sys import stderr #q = query % args if args is not None else query #print >>stderr, "QUERY\n", q try: try: return self.cursor.execute(query, args) except Exception, e: from sys import stderr print >> stderr, "Q", query print >> stderr, "A", args print >> stderr, "E", e from traceback import print_stack print_stack(file=stderr) stderr.flush() raise except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2]
def execute(self, query, args=None): try: # args is None means no string interpolation # sql_statment = str(query).lower() # c_time = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S') # if 'select' not in sql_statment and 'homepublishedvideo' in sql_statment: # print 'search_sql', c_time, query, args return self.cursor.execute(query, args) except Database.OperationalError as e: # CR_SERVER_GONE_ERROR, CR_SERVER_LOST if e.args[0] in (2006, 2013): from django.db import close_old_connections close_old_connections() # 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
def executemany(self, query, params=None): try: args = [(':arg%d' % i) for i in range(len(params[0]))] except (IndexError, TypeError): # No params given, nothing to do return None # cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it # it does want a trailing ';' but not a trailing '/'. However, these # characters must be included in the original query in case the query # is being passed to SQL*Plus. if query.endswith(';') or query.endswith('/'): query = query[:-1] query = convert_unicode(query % tuple(args), self.charset) formatted = [self._format_params(i) for i in params] self._guess_input_sizes(formatted) try: return self.cursor.executemany(query, [self._param_generator(p) for p in formatted]) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
def _wrap_execute(self, execute): try: result = execute() except Database.Error as e: # iaccess seems to be sending incorrect sqlstate for some errors # reraise "referential constraint violation" errors as IntegrityError if e.args[0] == 'HY000' and SQLCODE_0530_REGEX.match(e.args[1]): raise utils.IntegrityError(*e.args, execute.func, *execute.args) elif e.args[0] == 'HY000' and SQLCODE_0910_REGEX.match(e.args[1]): # file in use error (likely in the same transaction) query, _params, *_ = execute.args if query.startswith('ALTER TABLE') and 'RESTART WITH' in query: raise utils.ProgrammingError( *e.args, execute.func, execute.args, "Db2 for iSeries cannot reset a table's primary key sequence during same " "transaction as insert/update on that table") raise type(e)(*e.args, execute.func, execute.args) if result == self.cursor: return self return result
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, ) )
def execute(self, query, params=None): # cx_Oracle wants no trailing ';' for SQL statements. For PL/SQL, it # it does want a trailing ';' but not a trailing '/'. However, these # characters must be included in the original query in case the query # is being passed to SQL*Plus. if query.endswith(';') or query.endswith('/'): query = query[:-1] if params is None: params = [] query = convert_unicode(query, self.charset) else: params = self._format_params(params) args = [(':arg%d' % i) for i in range(len(params))] query = convert_unicode(query % tuple(args), self.charset) 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
def executemany(self, operation, seq_parameters): try: if operation.count("db2regexExtraField(%s)") > 0: raise ValueError("Regex not supported in this operation") return_only_param = True seq_parameters = [ self._format_parameters(parameters, operation, return_only_param) for parameters in seq_parameters ] if operation.count("%s") > 0: operation = operation % (tuple("?" * operation.count("%s"))) if (djangoVersion[0:2] <= (1, 1)): return super(DB2CursorWrapper, self).executemany(operation, seq_parameters) else: try: return super(DB2CursorWrapper, self).executemany(operation, seq_parameters) except IntegrityError as e: six.reraise( utils.IntegrityError, utils.IntegrityError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise except DatabaseError as e: six.reraise( utils.DatabaseError, utils.DatabaseError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise except (IndexError, TypeError): return None
class SafeCursorWrapper(django_backend_module.CursorWrapper): """ Override Cursor Execution to prevent Connection Errors Errors with retrial: 1205, 2006, 2013 """ codes_for_connectionerror = ('1205', '2006', '2013') def execute(self, query, args=None): try: return self.cursor.execute(query, args) except django_backend_module.Database.IntegrityError, e: if e[0] in self.codes_for_integrityerror: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise except django_backend_module.Database.OperationalError, e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. if e[0] in self.codes_for_connectionerror: # Refresh cursor with a new connection and retry connection = _get_pool().connect(**POOL_SETTINGS) connection.encoders[SafeText] = connection.encoders[ six.text_type] connection.encoders[SafeBytes] = connection.encoders[bytes] django_backend_module.connection_created.send( sender=self.__class__, connection=self) self.cursor = connection.cursor() return self.cursor.execute(query, args) elif e[0] in self.codes_for_integrityerror: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise
class CursorWrapper(object): """ A thin wrapper around MySQLdb's normal cursor class so that we can catch particular exception instances and reraise them with the right types. Implemented as a wrapper, rather than a subclass, so that we aren't stuck to the particular underlying representation returned by Connection.cursor(). """ codes_for_integrityerror = (1048,) def __init__(self, cursor): self.cursor = cursor def execute(self, query, args=None): try: return self.cursor.execute(query, args) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] except Database.OperationalError, e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. if e[0] in self.codes_for_integrityerror: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] raise
def validate_constraints(self, cursor, table_name): cursor.execute("SELECT ANALYZE_CONSTRAINTS(%s)", [table_name]) if cursor.rowcount > 0: raise utils.IntegrityError('Constraints failed', {"row_count": cursor.rowcount, "first_row": cursor.fetchone()})
def execute(self, operation, parameters=()): try: if operation.find('ALTER TABLE') == 0 and getattr( self.connection, dbms_name) != 'DB2': doReorg = 1 else: doReorg = 0 if operation.count("db2regexExtraField(%s)") > 0: operation = operation.replace("db2regexExtraField(%s)", "") operation = operation % parameters parameters = () if operation.count("%s") > 0: operation = operation % (tuple("?" * operation.count("%s"))) if (djangoVersion[0:2] >= (1, 4)): parameters = self._format_parameters(parameters) if (djangoVersion[0:2] <= (1, 1)): if (doReorg == 1): super(DB2CursorWrapper, self).execute(operation, parameters) return self._reorg_tables() else: return super(DB2CursorWrapper, self).execute(operation, parameters) else: try: if (doReorg == 1): super(DB2CursorWrapper, self).execute(operation, parameters) return self._reorg_tables() else: return super(DB2CursorWrapper, self).execute(operation, parameters) except IntegrityError as e: if (djangoVersion[0:2] >= (1, 5)): six.reraise( utils.IntegrityError, utils.IntegrityError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise else: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] except ProgrammingError as e: if (djangoVersion[0:2] >= (1, 5)): six.reraise( utils.ProgrammingError, utils.ProgrammingError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise else: raise utils.ProgrammingError, utils.ProgrammingError( *tuple(e)), sys.exc_info()[2] except DatabaseError as e: if (djangoVersion[0:2] >= (1, 5)): six.reraise( utils.DatabaseError, utils.DatabaseError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise else: raise utils.DatabaseError, utils.DatabaseError( *tuple(e)), sys.exc_info()[2] except (TypeError): return None
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_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: <<<<<<< HEAD six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) ======= raise utils.IntegrityError(*tuple(e.args)) >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 raise # Oracle doesn't support releasing savepoints. But we fake them when query # logging is enabled to keep query counts consistent with other backends. def _savepoint_commit(self, sid): if self.queries_logged: self.queries_log.append({ 'sql': '-- RELEASE SAVEPOINT %s (faked)' % self.ops.quote_name(sid), 'time': '0.000', }) def _set_autocommit(self, autocommit):
def _commit(self): if self.connection is not None: try: return self.connection.commit() except Database.IntegrityError as e: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
def execute(self, operation, parameters=()): if (djangoVersion[0:2] >= (2, 0)): operation = str(operation) try: if operation == "''": operation = "SELECT NULL FROM SYSIBM.DUAL FETCH FIRST 0 ROW ONLY" if operation.find('ALTER TABLE') == 0 and getattr( self.connection, dbms_name) != 'DB2': doReorg = 1 else: doReorg = 0 if operation.count("db2regexExtraField(%s)") > 0: operation = operation.replace("db2regexExtraField(%s)", "") operation = operation % parameters parameters = () if operation.count("%s") > 0 and parameters: parameters, operation = self._resolve_parameters_in_aggregator_func( parameters, operation) parameters, operation = self._format_parameters( parameters, operation) parameters, operation = self._resolve_parameters_in_expression_func( parameters, operation) if operation.count("%s") > 0: operation = operation.replace("%s", "?") if operation.count("%%") > 0: operation = operation.replace("%%", "%") if (djangoVersion[0:2] <= (1, 1)): if (doReorg == 1): super(DB2CursorWrapper, self).execute(operation, parameters) return self._reorg_tables() else: return super(DB2CursorWrapper, self).execute(operation, parameters) else: try: if (doReorg == 1): super(DB2CursorWrapper, self).execute(operation, parameters) return self._reorg_tables() else: return super(DB2CursorWrapper, self).execute(operation, parameters) except IntegrityError as e: six.reraise( utils.IntegrityError, utils.IntegrityError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise except ProgrammingError as e: six.reraise( utils.ProgrammingError, utils.ProgrammingError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise except DatabaseError as e: six.reraise( utils.DatabaseError, utils.DatabaseError( *tuple(six.PY3 and e.args or (e._message, ))), sys.exc_info()[2]) raise except (TypeError): return None
def execute(self, query, args=None): try: return self.cursor.execute(query, args) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2]
def executemany(self, query, param_list): query = self.convert_query(query) try: return Database.Cursor.executemany(self, query, param_list) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
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. """ if self.features.supports_pragma_foreign_key_check: with self.cursor() as cursor: if table_names is None: violations = self.cursor().execute( 'PRAGMA foreign_key_check').fetchall() else: violations = chain.from_iterable( cursor.execute('PRAGMA foreign_key_check(%s)' % table_name).fetchall() for table_name in table_names) # See https://www.sqlite.org/pragma.html#pragma_foreign_key_check for table_name, rowid, referenced_table_name, foreign_key_index in violations: foreign_key = cursor.execute( 'PRAGMA foreign_key_list(%s)' % table_name).fetchall()[foreign_key_index] column_name, referenced_column_name = foreign_key[3:5] primary_key_column_name = self.introspection.get_primary_key_column( cursor, table_name) primary_key_value, bad_value = cursor.execute( 'SELECT %s, %s FROM %s WHERE rowid = %%s' % (primary_key_column_name, column_name, table_name), (rowid, ), ).fetchone() 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, primary_key_value, table_name, column_name, bad_value, referenced_table_name, referenced_column_name)) else: 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, ))
def executemany(self, sql, params): try: return self.cursor.executemany(sql, params) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] raise def executemany(self, query, args, **kwargs): query = self._replace_params(query) try: return self.cursor.executemany(query, args, **kwargs) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] except Database.OperationalError, e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. if e[0] in self.codes_for_integrityerror: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] raise def __getattr__(self, attr): if attr in self.__dict__: return self.__dict__[attr] else: return getattr(self.cursor, attr) def __iter__(self): return iter(self.cursor) class DatabaseFeatures(BaseDatabaseFeatures): empty_fetchmany_value = [] update_can_self_select = False
connection.encoders[SafeBytes] = connection.encoders[bytes] django_backend_module.connection_created.send( sender=self.__class__, connection=self) self.cursor = connection.cursor() return self.cursor.execute(query, args) elif e[0] in self.codes_for_integrityerror: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise except django_backend_module.Database.DatabaseError, e: if e[0] in self.codes_for_integrityerror: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise def executemany(self, query, args): try: return self.cursor.executemany(query, args) except django_backend_module.Database.IntegrityError, e: if e[0] in self.codes_for_integrityerror: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) raise except django_backend_module.Database.OperationalError, e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place.