Skip to content

Commit 30b3d51

Browse files
committed
Fixed #13630 -- Made __init__ methods of all DB backends' DatabaseOperations 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
1 parent 71bf169 commit 30b3d51

File tree

9 files changed

+34
-32
lines changed

9 files changed

+34
-32
lines changed

django/contrib/gis/db/backends/oracle/operations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ class OracleOperations(DatabaseOperations, BaseSpatialOperations):
133133
truncate_params = {'relate' : None}
134134

135135
def __init__(self, connection):
136-
super(OracleOperations, self).__init__()
137-
self.connection = connection
136+
super(OracleOperations, self).__init__(connection)
138137

139138
def convert_extent(self, clob):
140139
if clob:

django/contrib/gis/db/backends/spatialite/operations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
110110
geometry_functions.update(distance_functions)
111111

112112
def __init__(self, connection):
113-
super(DatabaseOperations, self).__init__()
114-
self.connection = connection
113+
super(DatabaseOperations, self).__init__(connection)
115114

116115
# Determine the version of the SpatiaLite library.
117116
try:

django/db/backends/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ class BaseDatabaseOperations(object):
388388
"""
389389
compiler_module = "django.db.models.sql.compiler"
390390

391-
def __init__(self):
391+
def __init__(self, connection):
392+
self.connection = connection
392393
self._cache = None
393394

394395
def autoinc_sql(self, table, column):

django/db/backends/dummy/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, *args, **kwargs):
5959
super(DatabaseWrapper, self).__init__(*args, **kwargs)
6060

6161
self.features = BaseDatabaseFeatures(self)
62-
self.ops = DatabaseOperations()
62+
self.ops = DatabaseOperations(self)
6363
self.client = DatabaseClient(self)
6464
self.creation = BaseDatabaseCreation(self)
6565
self.introspection = DatabaseIntrospection(self)

django/db/backends/mysql/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
raise ImproperlyConfigured("MySQLdb-1.2.1p2 or newer is required; you have %s" % Database.__version__)
2424

2525
from MySQLdb.converters import conversions
26-
from MySQLdb.constants import FIELD_TYPE, FLAG, CLIENT
26+
from MySQLdb.constants import FIELD_TYPE, CLIENT
2727

2828
from django.db import utils
2929
from django.db.backends import *
@@ -279,7 +279,7 @@ def __init__(self, *args, **kwargs):
279279

280280
self.server_version = None
281281
self.features = DatabaseFeatures(self)
282-
self.ops = DatabaseOperations()
282+
self.ops = DatabaseOperations(self)
283283
self.client = DatabaseClient(self)
284284
self.creation = DatabaseCreation(self)
285285
self.introspection = DatabaseIntrospection(self)

django/db/backends/oracle/base.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ class DatabaseOperations(BaseDatabaseOperations):
8484
def autoinc_sql(self, table, column):
8585
# To simulate auto-incrementing primary keys in Oracle, we have to
8686
# create a sequence and a trigger.
87-
sq_name = get_sequence_name(table)
88-
tr_name = get_trigger_name(table)
87+
sq_name = self._get_sequence_name(table)
88+
tr_name = self._get_trigger_name(table)
8989
tbl_name = self.quote_name(table)
9090
col_name = self.quote_name(column)
9191
sequence_sql = """
@@ -197,7 +197,7 @@ def deferrable_sql(self):
197197
return " DEFERRABLE INITIALLY DEFERRED"
198198

199199
def drop_sequence_sql(self, table):
200-
return "DROP SEQUENCE %s;" % self.quote_name(get_sequence_name(table))
200+
return "DROP SEQUENCE %s;" % self.quote_name(self._get_sequence_name(table))
201201

202202
def fetch_returned_insert_id(self, cursor):
203203
return long(cursor._insert_id_var.getvalue())
@@ -209,7 +209,7 @@ def field_cast_sql(self, db_type):
209209
return "%s"
210210

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

@@ -285,7 +285,7 @@ def sql_flush(self, style, tables, sequences):
285285
# Since we've just deleted all the rows, running our sequence
286286
# ALTER code will reset the sequence to 0.
287287
for sequence_info in sequences:
288-
sequence_name = get_sequence_name(sequence_info['table'])
288+
sequence_name = self._get_sequence_name(sequence_info['table'])
289289
table_name = self.quote_name(sequence_info['table'])
290290
column_name = self.quote_name(sequence_info['column'] or 'id')
291291
query = _get_sequence_reset_sql() % {'sequence': sequence_name,
@@ -304,7 +304,7 @@ def sequence_reset_sql(self, style, model_list):
304304
for f in model._meta.local_fields:
305305
if isinstance(f, models.AutoField):
306306
table_name = self.quote_name(model._meta.db_table)
307-
sequence_name = get_sequence_name(model._meta.db_table)
307+
sequence_name = self._get_sequence_name(model._meta.db_table)
308308
column_name = self.quote_name(f.column)
309309
output.append(query % {'sequence': sequence_name,
310310
'table': table_name,
@@ -315,7 +315,7 @@ def sequence_reset_sql(self, style, model_list):
315315
for f in model._meta.many_to_many:
316316
if not f.rel.through:
317317
table_name = self.quote_name(f.m2m_db_table())
318-
sequence_name = get_sequence_name(f.m2m_db_table())
318+
sequence_name = self._get_sequence_name(f.m2m_db_table())
319319
column_name = self.quote_name('id')
320320
output.append(query % {'sequence': sequence_name,
321321
'table': table_name,
@@ -365,6 +365,14 @@ def combine_expression(self, connector, sub_expressions):
365365
raise NotImplementedError("Bit-wise or is not supported in Oracle.")
366366
return super(DatabaseOperations, self).combine_expression(connector, sub_expressions)
367367

368+
def _get_sequence_name(self, table):
369+
name_length = self.max_name_length() - 3
370+
return '%s_SQ' % util.truncate_name(table, name_length).upper()
371+
372+
def _get_trigger_name(self, table):
373+
name_length = self.max_name_length() - 3
374+
return '%s_TR' % util.truncate_name(table, name_length).upper()
375+
368376

369377
class _UninitializedOperatorsDescriptor(object):
370378

@@ -415,7 +423,7 @@ def __init__(self, *args, **kwargs):
415423
self.features = DatabaseFeatures(self)
416424
use_returning_into = self.settings_dict["OPTIONS"].get('use_returning_into', True)
417425
self.features.can_return_id_from_insert = use_returning_into
418-
self.ops = DatabaseOperations()
426+
self.ops = DatabaseOperations(self)
419427
self.client = DatabaseClient(self)
420428
self.creation = DatabaseCreation(self)
421429
self.introspection = DatabaseIntrospection(self)
@@ -776,13 +784,3 @@ def _get_sequence_reset_sql():
776784
END LOOP;
777785
END;
778786
/"""
779-
780-
781-
def get_sequence_name(table):
782-
name_length = DatabaseOperations().max_name_length() - 3
783-
return '%s_SQ' % util.truncate_name(table, name_length).upper()
784-
785-
786-
def get_trigger_name(table):
787-
name_length = DatabaseOperations().max_name_length() - 3
788-
return '%s_TR' % util.truncate_name(table, name_length).upper()

django/db/backends/postgresql_psycopg2/operations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
class DatabaseOperations(BaseDatabaseOperations):
77
def __init__(self, connection):
8-
super(DatabaseOperations, self).__init__()
8+
super(DatabaseOperations, self).__init__(connection)
99
self._postgres_version = None
10-
self.connection = connection
1110

1211
def _get_postgres_version(self):
1312
if self._postgres_version is None:

django/db/backends/sqlite3/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ def date_interval_sql(self, sql, connector, timedelta):
8888
# It would be more straightforward if we could use the sqlite strftime
8989
# function, but it does not allow for keeping six digits of fractional
9090
# second information, nor does it allow for formatting date and datetime
91-
# values differently. So instead we register our own function that
92-
# formats the datetime combined with the delta in a manner suitable
91+
# values differently. So instead we register our own function that
92+
# formats the datetime combined with the delta in a manner suitable
9393
# for comparisons.
94-
return u'django_format_dtdelta(%s, "%s", "%d", "%d", "%d")' % (sql,
94+
return u'django_format_dtdelta(%s, "%s", "%d", "%d", "%d")' % (sql,
9595
connector, timedelta.days, timedelta.seconds, timedelta.microseconds)
9696

9797
def date_trunc_sql(self, lookup_type, field_name):
@@ -179,7 +179,7 @@ def __init__(self, *args, **kwargs):
179179
super(DatabaseWrapper, self).__init__(*args, **kwargs)
180180

181181
self.features = DatabaseFeatures(self)
182-
self.ops = DatabaseOperations()
182+
self.ops = DatabaseOperations(self)
183183
self.client = DatabaseClient(self)
184184
self.creation = DatabaseCreation(self)
185185
self.introspection = DatabaseIntrospection(self)

tests/regressiontests/backends/tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ def test_unicode_fetches(self):
232232
self.assertEqual(list(cursor.fetchmany(2)), [(u'Jane', u'Doe'), (u'John', u'Doe')])
233233
self.assertEqual(list(cursor.fetchall()), [(u'Mary', u'Agnelline'), (u'Peter', u'Parker')])
234234

235+
def test_database_operations_helper_class(self):
236+
# Ticket #13630
237+
self.assertTrue(hasattr(connection, 'ops'))
238+
self.assertTrue(hasattr(connection.ops, 'connection'))
239+
self.assertEqual(connection, connection.ops.connection)
240+
235241

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

0 commit comments

Comments
 (0)