From e4b7e369ddc511dac78e1bf735e927c5fe0afe68 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 19 Aug 2007 23:53:39 +0000 Subject: [PATCH] Refactored get_max_name_length() to DatabaseOperations.max_name_length(). Refs #5106 git-svn-id: http://code.djangoproject.com/svn/django/trunk@5960 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/sql.py | 4 ++-- django/db/backends/__init__.py | 7 +++++++ django/db/backends/ado_mssql/base.py | 3 --- django/db/backends/dummy/base.py | 1 - django/db/backends/mysql/base.py | 3 --- django/db/backends/mysql_old/base.py | 3 --- django/db/backends/oracle/base.py | 14 +++++++------- django/db/backends/postgresql/base.py | 3 --- django/db/backends/postgresql_psycopg2/base.py | 3 --- django/db/backends/sqlite3/base.py | 3 --- django/db/models/options.py | 5 ++--- 11 files changed, 18 insertions(+), 31 deletions(-) diff --git a/django/core/management/sql.py b/django/core/management/sql.py index b34b6d3dcc..09713d278d 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -148,7 +148,7 @@ def sql_delete(app, style): (style.SQL_KEYWORD('ALTER TABLE'), style.SQL_TABLE(backend.quote_name(table)), style.SQL_KEYWORD(connection.ops.drop_foreignkey_sql()), - style.SQL_FIELD(truncate_name(r_name, backend.get_max_name_length())))) + style.SQL_FIELD(truncate_name(r_name, connection.ops.max_name_length())))) del references_to_delete[model] if model._meta.has_auto_field and hasattr(backend, 'get_drop_sequence'): output.append(backend.get_drop_sequence(model._meta.db_table)) @@ -297,7 +297,7 @@ def sql_for_pending_references(model, style, pending_references): # So we are careful with character usage here. r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table)))) final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \ - (backend.quote_name(r_table), truncate_name(r_name, backend.get_max_name_length()), + (backend.quote_name(r_table), truncate_name(r_name, connection.ops.max_name_length()), backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col), connection.ops.deferrable_sql())) del pending_references[model] diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index e6776a19b7..5df24eb3c8 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -119,3 +119,10 @@ class BaseDatabaseOperations(object): if offset and offset != 0: sql += " OFFSET %s" % offset return sql + + def max_name_length(self): + """ + Returns the maximum length of table and column names, or None if there + is no limit. + """ + return None diff --git a/django/db/backends/ado_mssql/base.py b/django/db/backends/ado_mssql/base.py index 19dd7278d2..f42bc7e2af 100644 --- a/django/db/backends/ado_mssql/base.py +++ b/django/db/backends/ado_mssql/base.py @@ -106,9 +106,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return None - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index 334e5146b7..1cee9687f7 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -45,7 +45,6 @@ dictfetchmany = complain dictfetchall = complain get_random_function_sql = complain get_pk_default_value = complain -get_max_name_length = ignore get_start_transaction_sql = complain get_sql_flush = complain get_sql_sequence_reset = complain diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 259034b4bb..545ae3dcdf 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -168,9 +168,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return None; - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/mysql_old/base.py b/django/db/backends/mysql_old/base.py index 5de2cbfda8..f445dcb8d6 100644 --- a/django/db/backends/mysql_old/base.py +++ b/django/db/backends/mysql_old/base.py @@ -187,9 +187,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return None; - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index df153378f0..9908518d47 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -58,7 +58,7 @@ class DatabaseOperations(BaseDatabaseOperations): return " DEFERRABLE INITIALLY DEFERRED" def last_insert_id(self, cursor, table_name, pk_name): - sq_name = util.truncate_name(table_name, get_max_name_length()-3) + sq_name = util.truncate_name(table_name, self.max_name_length() - 3) cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) return cursor.fetchone()[0] @@ -67,6 +67,9 @@ class DatabaseOperations(BaseDatabaseOperations): # Instead, they are handled in django/db/backends/oracle/query.py. return "" + def max_name_length(self): + return 30 + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -170,7 +173,7 @@ def quote_name(name): # always defaults to uppercase. # We simplify things by making Oracle identifiers always uppercase. if not name.startswith('"') and not name.endswith('"'): - name = '"%s"' % util.truncate_name(name.upper(), get_max_name_length()) + name = '"%s"' % util.truncate_name(name.upper(), DatabaseOperations().max_name_length()) return name.upper() dictfetchone = util.dictfetchone @@ -189,9 +192,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return 30 - def get_start_transaction_sql(): return None @@ -249,7 +249,7 @@ def get_sql_flush(style, tables, sequences): return [] def get_sequence_name(table): - name_length = get_max_name_length() - 3 + name_length = DatabaseOperations().max_name_length() - 3 return '%s_SQ' % util.truncate_name(table, name_length).upper() def get_sql_sequence_reset(style, model_list): @@ -271,7 +271,7 @@ def get_sql_sequence_reset(style, model_list): return output def get_trigger_name(table): - name_length = get_max_name_length() - 3 + name_length = DatabaseOperations().max_name_length() - 3 return '%s_TR' % util.truncate_name(table, name_length).upper() def get_query_set_class(DefaultQuerySet): diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 4926ddf8d1..9193e532c8 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -137,9 +137,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return None - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index f8d8765e92..b1611c5848 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -91,9 +91,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "DEFAULT" -def get_max_name_length(): - return None - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 75c753ef22..ad792e1fa0 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -121,9 +121,6 @@ def get_random_function_sql(): def get_pk_default_value(): return "NULL" -def get_max_name_length(): - return None - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/models/options.py b/django/db/models/options.py index 8cd9b58ec8..ad813ae0f7 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -63,7 +63,7 @@ class Options(object): del self.meta def _prepare(self, model): - from django.db import backend + from django.db import connection from django.db.backends.util import truncate_name if self.order_with_respect_to: self.order_with_respect_to = self.get_field(self.order_with_respect_to) @@ -79,8 +79,7 @@ class Options(object): # If the db_table wasn't provided, use the app_label + module_name. if not self.db_table: self.db_table = "%s_%s" % (self.app_label, self.module_name) - self.db_table = truncate_name(self.db_table, - backend.get_max_name_length()) + self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) def add_field(self, field): # Insert the given field in the order in which it was created, using