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 combine_duration_expression(self, connector, sub_expressions): if 'NULL' in sub_expressions: # django expects that adding a null duration will return null # db2 does not allow untyped columns return 'CAST(NULL AS TIMESTAMP)' lhs, rhs = sub_expressions if connector not in '+-': raise utils.DatabaseError('Invalid connector for timedelta: %s.' % connector) if 'MICROSECONDS' not in lhs: lhs = f'CAST({lhs} as TIMESTAMP)' if 'MICROSECONDS' not in rhs: rhs = f'CAST({rhs} as TIMESTAMP)' lhs, rhs = rhs, lhs if connector == '-' and 'MICROSECONDS' in rhs: rhs = rhs.replace('+', '-') return f'{lhs} {connector} {rhs}'
class CursorWrapper(object): """ A thin wrapper around adsdb'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 __del__(self): if self.cursor: self.cursor.close() self.cursor = None def convert_query(self, query, num_params): """ Django uses "format" style placeholders, but Advantage uses "qmark" style. This fixes it -- but note that if you want to use a literal "%s" in a query, you'll need to use "%%s". """ return query % tuple("?" * num_params) def execute(self, query, args=()): try: try: if args != None: query = self.convert_query(query, len(args)) ret = self.cursor.execute(query, args) return ret 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 Database.IntegrityError(tuple(e)) raise except Database.IntegrityError, e: raise utils.IntegrityError(e) except Database.DatabaseError, e: raise utils.DatabaseError(e)
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, sql, params=()): """ execute with replaced placeholders """ try: self.cursor.execute(self._replace_params(sql), params) except Database.IntegrityError as e: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) except Database.Error as 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: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
def executemany(self, sql, param_list): try: self.cursor.executemany( self._replace_params( sql, len(param_list[0]) if param_list and len(param_list) > 0 else 0), param_list) except Database.IntegrityError as e: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) except Database.Error as 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: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
def _commit(self): if self.connection is not None: try: return self.connection.commit() except Database.IntegrityError, e: # In case cx_Oracle implements (now or in a future version) # raising this specific exception raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] except Database.DatabaseError, 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: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
class CursorWrapper(object): def __init__(self, cursor): self.cursor = cursor def __enter__(self): return self def __exit__(self, type, value, traceback): self.cursor.close() def execute(self, sql, params = ()): try: return self.cursor.execute(sql, params) except Database.IntegrityError, e: if not self.cursor.connection.mars_enabled: self.cursor.cancel() raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] except Database.DatabaseError, e: if not self.cursor.connection.mars_enabled: self.cursor.cancel() raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
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
def set_search_path(self, cursor): """ Actual search_path modification for the cursor. Database will search schemata from left to right when looking for the object (table, index, sequence, etc.). """ from django.db import connection if self.schema_name is None: raise ImproperlyConfigured("Database schema not set. Did your forget " "to call set_schema() or set_tenant()?") _check_identifier(self.schema_name) #connection.enter_transaction_management() try: if self.schema_name == 'public': cursor.execute('SET search_path = public') else: cursor.execute('SET search_path = %s', [self.schema_name]) #, public except utils.DatabaseError, e: #connection.rollback() raise utils.DatabaseError(e.message)
def set_search_path(self, cursor): """ Actual search_path modification for the cursor. Database will search schemata from left to right when looking for the object (table, index, sequence, etc.). """ if self.schema_name is None: raise ImproperlyConfigured("Database schema not set. Did your forget " "to call set_schema() or set_tenant()?") _check_identifier(self.schema_name) try: public_schema_name = get_public_schema_name() if self.schema_name == public_schema_name: cursor.execute('SET search_path = %s' % public_schema_name) elif self.include_public_schema: cursor.execute('SET search_path = %s,%s', [self.schema_name, public_schema_name]) else: cursor.execute('SET search_path = %s', [self.schema_name]) except utils.DatabaseError, e: raise utils.DatabaseError(e.message)
def executemany(self, query, param_list): try: q = self.convert_query(query, len(param_list[0])) return self.cursor.executemany(q, param_list) except Database.IntegrityError as e: six.reraise( utils.IntegrityError, utils.IntegrityError( *self.error_info(e, query, param_list[0])), sys.exc_info()[2]) except Database.DatabaseError as 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: six.reraise( utils.IntegrityError, utils.IntegrityError( *self.error_info(e, query, param_list[0])), sys.exc_info()[2]) six.reraise( utils.DatabaseError, utils.DatabaseError(*self.error_info(e, query, param_list[0])), sys.exc_info()[2])
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 as e: raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] 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): raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
class UnicodeCursorWrapper(object): """ A thin wrapper around psycopg cursors that allows them to accept Unicode strings as params. This is necessary because psycopg doesn't apply any DB quoting to parameters that are Unicode strings. If a param is Unicode, this will convert it to a bytestring using database client's encoding before passing it to psycopg. All results retrieved from the database are converted into Unicode strings before being returned to the caller. """ def __init__(self, cursor, charset): self.cursor = cursor self.charset = charset def format_params(self, params): if isinstance(params, dict): result = {} charset = self.charset for key, value in params.items(): result[smart_str(key, charset)] = smart_str(value, charset) return result else: return tuple([smart_str(p, self.charset, True) for p in params]) def execute(self, sql, params=()): try: return self.cursor.execute(smart_str(sql, self.charset), self.format_params(params)) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] except Database.DatabaseError, e: raise utils.DatabaseError, utils.DatabaseError( *tuple(e)), sys.exc_info()[2]
def execute(self, query, params=None): if params is None: params = [] try: q = self.convert_query(query, len(params)) return self.cursor.execute(q, params) except Database.IntegrityError as e: six.reraise( utils.IntegrityError, utils.IntegrityError(*self.error_info(e, query, params)), sys.exc_info()[2]) except Database.DatabaseError as e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. # fdb: raise exception as tuple with (error_msg, sqlcode, error_code) if e.args[1] in self.codes_for_integrityerror: six.reraise( utils.IntegrityError, utils.IntegrityError(*self.error_info(e, query, params)), sys.exc_info()[2]) six.reraise( utils.DatabaseError, utils.DatabaseError(*self.error_info(e, query, params)), sys.exc_info()[2])
class CursorWrapper(object): """ A thin wrapper around PyGreSQL's normal cursor class so that we can catch particular exception instances and reraise them with the right types. """ def __init__(self, cursor): self.cursor = cursor self.query = None def execute(self, query, args=None): try: self.query = query try: res = self.cursor.execute(query, args) self.cursor.connection.commit() except: self.cursor.connection.rollback() return res except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] except Database.DatabaseError, e: raise utils.DatabaseError, utils.DatabaseError( *tuple(e)), sys.exc_info()[2]
def executemany(self, query, params=None): # cx_Oracle doesn't support iterators, convert them to lists if params is not None and not isinstance(params, (list, tuple)): params = list(params) 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 as e: six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) 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]) six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
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 utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]
*tuple(e)), sys.exc_info()[2] except Database.DatabaseError, e: raise utils.DatabaseError, utils.DatabaseError( *tuple(e)), sys.exc_info()[2] def executemany(self, sql, param_list): try: new_param_list = [ self.format_params(params) for params in param_list ] return self.cursor.executemany(sql, new_param_list) except Database.IntegrityError, e: raise utils.IntegrityError, utils.IntegrityError( *tuple(e)), sys.exc_info()[2] except Database.DatabaseError, e: raise utils.DatabaseError, utils.DatabaseError( *tuple(e)), sys.exc_info()[2] 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): uses_savepoints = True
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, 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 len(args) > 0: query = self.convert_query(query, len(args[0])) ret = self.cursor.executemany(query, args) return ret else: return None 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 Database.IntegrityError(tuple(e)) raise except Database.IntegrityError, e: raise utils.IntegrityError(e) except Database.DatabaseError, e: raise utils.DatabaseError(e) 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.fetchall()) class DatabaseFeatures(BaseDatabaseFeatures): empty_fetchmany_value = [] update_can_self_select = False allows_group_by_pk = False related_fields_match_type = True
class DB2CursorWrapper( Database.Cursor ): """ This is the wrapper around IBM_DB_DBI in order to support format parameter style IBM_DB_DBI supports qmark, where as Django support format style, hence this conversion is required. """ def __init__( self, connection ): super( DB2CursorWrapper, self ).__init__( connection.conn_handler, connection ) def __iter__( self ): return self def next( self ): row = self.fetchone() if row == None: raise StopIteration return row def _format_parameters( self, parameters ): parameters = list( parameters ) for index in range( len( parameters ) ): # With raw SQL queries, datetimes can reach this function # without being converted by DateTimeField.get_db_prep_value. if settings.USE_TZ and isinstance( parameters[index], datetime.datetime ): param = parameters[index] if timezone.is_naive( param ): warnings.warn(u"Received a naive datetime (%s)" u" while time zone support is active." % param, RuntimeWarning) default_timezone = timezone.get_default_timezone() param = timezone.make_aware( param, default_timezone ) param = param.astimezone(timezone.utc).replace(tzinfo=None) parameters[index] = param return tuple( parameters ) # Over-riding this method to modify SQLs which contains format parameter to qmark. def execute( self, operation, parameters = () ): try: 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 ) ): return super( DB2CursorWrapper, self ).execute( operation, parameters ) else: try: return super( DB2CursorWrapper, self ).execute( operation, parameters ) except IntegrityError, 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, 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, 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]