Skip to content

Commit

Permalink
Fixed #13630 -- Made __init__ methods of all DB backends' DatabaseOpe…
Browse files Browse the repository at this point in the history
…rations classes take a `connection` argument. Thanks calexium for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16016 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Apr 5, 2011
1 parent 71bf169 commit 30b3d51
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 32 deletions.
3 changes: 1 addition & 2 deletions django/contrib/gis/db/backends/oracle/operations.py
Expand Up @@ -133,8 +133,7 @@ class OracleOperations(DatabaseOperations, BaseSpatialOperations):
truncate_params = {'relate' : None}

def __init__(self, connection):
super(OracleOperations, self).__init__()
self.connection = connection
super(OracleOperations, self).__init__(connection)

def convert_extent(self, clob):
if clob:
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/gis/db/backends/spatialite/operations.py
Expand Up @@ -110,8 +110,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
geometry_functions.update(distance_functions)

def __init__(self, connection):
super(DatabaseOperations, self).__init__()
self.connection = connection
super(DatabaseOperations, self).__init__(connection)

# Determine the version of the SpatiaLite library.
try:
Expand Down
3 changes: 2 additions & 1 deletion django/db/backends/__init__.py
Expand Up @@ -388,7 +388,8 @@ class BaseDatabaseOperations(object):
"""
compiler_module = "django.db.models.sql.compiler"

def __init__(self):
def __init__(self, connection):
self.connection = connection
self._cache = None

def autoinc_sql(self, table, column):
Expand Down
2 changes: 1 addition & 1 deletion django/db/backends/dummy/base.py
Expand Up @@ -59,7 +59,7 @@ def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)

self.features = BaseDatabaseFeatures(self)
self.ops = DatabaseOperations()
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = BaseDatabaseCreation(self)
self.introspection = DatabaseIntrospection(self)
Expand Down
4 changes: 2 additions & 2 deletions django/db/backends/mysql/base.py
Expand Up @@ -23,7 +23,7 @@
raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)

from MySQLdb.converters import conversions
from MySQLdb.constants import FIELD_TYPE, FLAG, CLIENT
from MySQLdb.constants import FIELD_TYPE, CLIENT

from django.db import utils
from django.db.backends import *
Expand Down Expand Up @@ -279,7 +279,7 @@ def __init__(self, *args, **kwargs):

self.server_version = None
self.features = DatabaseFeatures(self)
self.ops = DatabaseOperations()
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self)
self.introspection = DatabaseIntrospection(self)
Expand Down
34 changes: 16 additions & 18 deletions django/db/backends/oracle/base.py
Expand Up @@ -84,8 +84,8 @@ class DatabaseOperations(BaseDatabaseOperations):
def autoinc_sql(self, table, column):
# To simulate auto-incrementing primary keys in Oracle, we have to
# create a sequence and a trigger.
sq_name = get_sequence_name(table)
tr_name = get_trigger_name(table)
sq_name = self._get_sequence_name(table)
tr_name = self._get_trigger_name(table)
tbl_name = self.quote_name(table)
col_name = self.quote_name(column)
sequence_sql = """
Expand Down Expand Up @@ -197,7 +197,7 @@ def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED"

def drop_sequence_sql(self, table):
return "DROP SEQUENCE %s;" % self.quote_name(get_sequence_name(table))
return "DROP SEQUENCE %s;" % self.quote_name(self._get_sequence_name(table))

def fetch_returned_insert_id(self, cursor):
return long(cursor._insert_id_var.getvalue())
Expand All @@ -209,7 +209,7 @@ def field_cast_sql(self, db_type):
return "%s"

def last_insert_id(self, cursor, table_name, pk_name):
sq_name = get_sequence_name(table_name)
sq_name = self._get_sequence_name(table_name)
cursor.execute('SELECT "%s".currval FROM dual' % sq_name)
return cursor.fetchone()[0]

Expand Down Expand Up @@ -285,7 +285,7 @@ def sql_flush(self, style, tables, sequences):
# Since we've just deleted all the rows, running our sequence
# ALTER code will reset the sequence to 0.
for sequence_info in sequences:
sequence_name = get_sequence_name(sequence_info['table'])
sequence_name = self._get_sequence_name(sequence_info['table'])
table_name = self.quote_name(sequence_info['table'])
column_name = self.quote_name(sequence_info['column'] or 'id')
query = _get_sequence_reset_sql() % {'sequence': sequence_name,
Expand All @@ -304,7 +304,7 @@ def sequence_reset_sql(self, style, model_list):
for f in model._meta.local_fields:
if isinstance(f, models.AutoField):
table_name = self.quote_name(model._meta.db_table)
sequence_name = get_sequence_name(model._meta.db_table)
sequence_name = self._get_sequence_name(model._meta.db_table)
column_name = self.quote_name(f.column)
output.append(query % {'sequence': sequence_name,
'table': table_name,
Expand All @@ -315,7 +315,7 @@ def sequence_reset_sql(self, style, model_list):
for f in model._meta.many_to_many:
if not f.rel.through:
table_name = self.quote_name(f.m2m_db_table())
sequence_name = get_sequence_name(f.m2m_db_table())
sequence_name = self._get_sequence_name(f.m2m_db_table())
column_name = self.quote_name('id')
output.append(query % {'sequence': sequence_name,
'table': table_name,
Expand Down Expand Up @@ -365,6 +365,14 @@ def combine_expression(self, connector, sub_expressions):
raise NotImplementedError("Bit-wise or is not supported in Oracle.")
return super(DatabaseOperations, self).combine_expression(connector, sub_expressions)

def _get_sequence_name(self, table):
name_length = self.max_name_length() - 3
return '%s_SQ' % util.truncate_name(table, name_length).upper()

def _get_trigger_name(self, table):
name_length = self.max_name_length() - 3
return '%s_TR' % util.truncate_name(table, name_length).upper()


class _UninitializedOperatorsDescriptor(object):

Expand Down Expand Up @@ -415,7 +423,7 @@ def __init__(self, *args, **kwargs):
self.features = DatabaseFeatures(self)
use_returning_into = self.settings_dict["OPTIONS"].get('use_returning_into', True)
self.features.can_return_id_from_insert = use_returning_into
self.ops = DatabaseOperations()
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self)
self.introspection = DatabaseIntrospection(self)
Expand Down Expand Up @@ -776,13 +784,3 @@ def _get_sequence_reset_sql():
END LOOP;
END;
/"""


def get_sequence_name(table):
name_length = DatabaseOperations().max_name_length() - 3
return '%s_SQ' % util.truncate_name(table, name_length).upper()


def get_trigger_name(table):
name_length = DatabaseOperations().max_name_length() - 3
return '%s_TR' % util.truncate_name(table, name_length).upper()
3 changes: 1 addition & 2 deletions django/db/backends/postgresql_psycopg2/operations.py
Expand Up @@ -5,9 +5,8 @@

class DatabaseOperations(BaseDatabaseOperations):
def __init__(self, connection):
super(DatabaseOperations, self).__init__()
super(DatabaseOperations, self).__init__(connection)
self._postgres_version = None
self.connection = connection

def _get_postgres_version(self):
if self._postgres_version is None:
Expand Down
8 changes: 4 additions & 4 deletions django/db/backends/sqlite3/base.py
Expand Up @@ -88,10 +88,10 @@ def date_interval_sql(self, sql, connector, timedelta):
# It would be more straightforward if we could use the sqlite strftime
# function, but it does not allow for keeping six digits of fractional
# second information, nor does it allow for formatting date and datetime
# values differently. So instead we register our own function that
# formats the datetime combined with the delta in a manner suitable
# values differently. So instead we register our own function that
# formats the datetime combined with the delta in a manner suitable
# for comparisons.
return u'django_format_dtdelta(%s, "%s", "%d", "%d", "%d")' % (sql,
return u'django_format_dtdelta(%s, "%s", "%d", "%d", "%d")' % (sql,
connector, timedelta.days, timedelta.seconds, timedelta.microseconds)

def date_trunc_sql(self, lookup_type, field_name):
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)

self.features = DatabaseFeatures(self)
self.ops = DatabaseOperations()
self.ops = DatabaseOperations(self)
self.client = DatabaseClient(self)
self.creation = DatabaseCreation(self)
self.introspection = DatabaseIntrospection(self)
Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/backends/tests.py
Expand Up @@ -232,6 +232,12 @@ def test_unicode_fetches(self):
self.assertEqual(list(cursor.fetchmany(2)), [(u'Jane', u'Doe'), (u'John', u'Doe')])
self.assertEqual(list(cursor.fetchall()), [(u'Mary', u'Agnelline'), (u'Peter', u'Parker')])

def test_database_operations_helper_class(self):
# Ticket #13630
self.assertTrue(hasattr(connection, 'ops'))
self.assertTrue(hasattr(connection.ops, 'connection'))
self.assertEqual(connection, connection.ops.connection)


# We don't make these tests conditional because that means we would need to
# check and differentiate between:
Expand Down

0 comments on commit 30b3d51

Please sign in to comment.