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
This commit is contained in:
Adrian Holovaty 2007-08-19 23:53:39 +00:00
parent d3e69c3a47
commit e4b7e369dd
11 changed files with 18 additions and 31 deletions

View File

@ -148,7 +148,7 @@ def sql_delete(app, style):
(style.SQL_KEYWORD('ALTER TABLE'), (style.SQL_KEYWORD('ALTER TABLE'),
style.SQL_TABLE(backend.quote_name(table)), style.SQL_TABLE(backend.quote_name(table)),
style.SQL_KEYWORD(connection.ops.drop_foreignkey_sql()), 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] del references_to_delete[model]
if model._meta.has_auto_field and hasattr(backend, 'get_drop_sequence'): if model._meta.has_auto_field and hasattr(backend, 'get_drop_sequence'):
output.append(backend.get_drop_sequence(model._meta.db_table)) 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. # So we are careful with character usage here.
r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table)))) 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;' % \ 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), backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col),
connection.ops.deferrable_sql())) connection.ops.deferrable_sql()))
del pending_references[model] del pending_references[model]

View File

@ -119,3 +119,10 @@ class BaseDatabaseOperations(object):
if offset and offset != 0: if offset and offset != 0:
sql += " OFFSET %s" % offset sql += " OFFSET %s" % offset
return sql 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

View File

@ -106,9 +106,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return None
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -45,7 +45,6 @@ dictfetchmany = complain
dictfetchall = complain dictfetchall = complain
get_random_function_sql = complain get_random_function_sql = complain
get_pk_default_value = complain get_pk_default_value = complain
get_max_name_length = ignore
get_start_transaction_sql = complain get_start_transaction_sql = complain
get_sql_flush = complain get_sql_flush = complain
get_sql_sequence_reset = complain get_sql_sequence_reset = complain

View File

@ -168,9 +168,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return None;
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -187,9 +187,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return None;
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -58,7 +58,7 @@ class DatabaseOperations(BaseDatabaseOperations):
return " DEFERRABLE INITIALLY DEFERRED" return " DEFERRABLE INITIALLY DEFERRED"
def last_insert_id(self, cursor, table_name, pk_name): 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) cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name)
return cursor.fetchone()[0] return cursor.fetchone()[0]
@ -67,6 +67,9 @@ class DatabaseOperations(BaseDatabaseOperations):
# Instead, they are handled in django/db/backends/oracle/query.py. # Instead, they are handled in django/db/backends/oracle/query.py.
return "" return ""
def max_name_length(self):
return 30
class DatabaseWrapper(BaseDatabaseWrapper): class DatabaseWrapper(BaseDatabaseWrapper):
ops = DatabaseOperations() ops = DatabaseOperations()
@ -170,7 +173,7 @@ def quote_name(name):
# always defaults to uppercase. # always defaults to uppercase.
# We simplify things by making Oracle identifiers always uppercase. # We simplify things by making Oracle identifiers always uppercase.
if not name.startswith('"') and not name.endswith('"'): 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() return name.upper()
dictfetchone = util.dictfetchone dictfetchone = util.dictfetchone
@ -189,9 +192,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return 30
def get_start_transaction_sql(): def get_start_transaction_sql():
return None return None
@ -249,7 +249,7 @@ def get_sql_flush(style, tables, sequences):
return [] return []
def get_sequence_name(table): 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() return '%s_SQ' % util.truncate_name(table, name_length).upper()
def get_sql_sequence_reset(style, model_list): def get_sql_sequence_reset(style, model_list):
@ -271,7 +271,7 @@ def get_sql_sequence_reset(style, model_list):
return output return output
def get_trigger_name(table): 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() return '%s_TR' % util.truncate_name(table, name_length).upper()
def get_query_set_class(DefaultQuerySet): def get_query_set_class(DefaultQuerySet):

View File

@ -137,9 +137,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return None
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -91,9 +91,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "DEFAULT" return "DEFAULT"
def get_max_name_length():
return None
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -121,9 +121,6 @@ def get_random_function_sql():
def get_pk_default_value(): def get_pk_default_value():
return "NULL" return "NULL"
def get_max_name_length():
return None
def get_start_transaction_sql(): def get_start_transaction_sql():
return "BEGIN;" return "BEGIN;"

View File

@ -63,7 +63,7 @@ class Options(object):
del self.meta del self.meta
def _prepare(self, model): def _prepare(self, model):
from django.db import backend from django.db import connection
from django.db.backends.util import truncate_name from django.db.backends.util import truncate_name
if self.order_with_respect_to: if self.order_with_respect_to:
self.order_with_respect_to = self.get_field(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 the db_table wasn't provided, use the app_label + module_name.
if not self.db_table: if not self.db_table:
self.db_table = "%s_%s" % (self.app_label, self.module_name) self.db_table = "%s_%s" % (self.app_label, self.module_name)
self.db_table = truncate_name(self.db_table, self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
backend.get_max_name_length())
def add_field(self, field): def add_field(self, field):
# Insert the given field in the order in which it was created, using # Insert the given field in the order in which it was created, using