mirror of
https://github.com/django/django.git
synced 2025-07-06 02:39:12 +00:00
[multi-db] Updated ansi sql SchemaBuilder to track pendings as dicts
keyed by the model they depend on. git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3289 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f043b395ba
commit
0d9dee02bb
@ -4,6 +4,10 @@ import os
|
|||||||
import re
|
import re
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
# For Python 2.3
|
||||||
|
if not hasattr(__builtins__, 'set'):
|
||||||
|
from sets import Set as set
|
||||||
|
|
||||||
# default dummy style
|
# default dummy style
|
||||||
class dummy:
|
class dummy:
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
@ -39,22 +43,20 @@ class SchemaBuilder(object):
|
|||||||
or other constraints.
|
or other constraints.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.models_already_seen = []
|
self.models_already_seen = set()
|
||||||
|
|
||||||
def get_create_table(self, model, style=None):
|
def get_create_table(self, model, style=None):
|
||||||
"""Construct and return the SQL expression(s) needed to create the
|
"""Construct and return the SQL expression(s) needed to create the
|
||||||
table for the given model, and any constraints on that
|
table for the given model, and any constraints on that
|
||||||
table. The return value is a 2-tuple. The first element of the tuple
|
table. The return value is a 2-tuple. The first element of the tuple
|
||||||
is a list of BoundStatements that may be executed immediately. The
|
is a list of BoundStatements that may be executed immediately. The
|
||||||
second is a list of BoundStatements representing constraints that
|
second is a dict of BoundStatements representing constraints that
|
||||||
can't be executed immediately because (for instance) the referent
|
can't be executed immediately because (for instance) the referent
|
||||||
table does not exist.
|
table does not exist, keyed by the model class they reference.
|
||||||
"""
|
"""
|
||||||
if style is None:
|
if style is None:
|
||||||
style = default_style
|
style = default_style
|
||||||
if model in self.models_already_seen:
|
self.models_already_seen.add(model)
|
||||||
return ([], [])
|
|
||||||
self.models_already_seen.append(model)
|
|
||||||
|
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
info = opts.connection_info
|
info = opts.connection_info
|
||||||
@ -62,8 +64,14 @@ class SchemaBuilder(object):
|
|||||||
quote_name = backend.quote_name
|
quote_name = backend.quote_name
|
||||||
data_types = info.get_creation_module().DATA_TYPES
|
data_types = info.get_creation_module().DATA_TYPES
|
||||||
table_output = []
|
table_output = []
|
||||||
|
|
||||||
|
# pending field references, keyed by the model class
|
||||||
|
# they reference
|
||||||
pending_references = {}
|
pending_references = {}
|
||||||
pending = [] # actual pending statements to execute
|
|
||||||
|
# pending statements to execute, keyed by
|
||||||
|
# the model class they reference
|
||||||
|
pending = {}
|
||||||
for f in opts.fields:
|
for f in opts.fields:
|
||||||
if isinstance(f, models.ForeignKey):
|
if isinstance(f, models.ForeignKey):
|
||||||
rel_field = f.rel.get_related_field()
|
rel_field = f.rel.get_related_field()
|
||||||
@ -128,7 +136,8 @@ class SchemaBuilder(object):
|
|||||||
(quote_name(table),
|
(quote_name(table),
|
||||||
quote_name('%s_referencing_%s_%s' % (r_col, r_table, col)),
|
quote_name('%s_referencing_%s_%s' % (r_col, r_table, col)),
|
||||||
quote_name(r_col), quote_name(r_table), quote_name(col))
|
quote_name(r_col), quote_name(r_table), quote_name(col))
|
||||||
pending.append(BoundStatement(sql, opts.connection))
|
pending.setdefault(rel_class, []).append(
|
||||||
|
BoundStatement(sql, opts.connection))
|
||||||
return (create, pending)
|
return (create, pending)
|
||||||
|
|
||||||
def get_create_indexes(self, model, style=None):
|
def get_create_indexes(self, model, style=None):
|
||||||
@ -173,7 +182,9 @@ class SchemaBuilder(object):
|
|||||||
connection = info.connection
|
connection = info.connection
|
||||||
data_types = info.get_creation_module().DATA_TYPES
|
data_types = info.get_creation_module().DATA_TYPES
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
output = []
|
|
||||||
|
# statements to execute, keyed by the other model
|
||||||
|
output = {}
|
||||||
for f in opts.many_to_many:
|
for f in opts.many_to_many:
|
||||||
if not isinstance(f.rel, models.GenericRel):
|
if not isinstance(f.rel, models.GenericRel):
|
||||||
table_output = [
|
table_output = [
|
||||||
@ -200,8 +211,8 @@ class SchemaBuilder(object):
|
|||||||
style.SQL_FIELD(quote_name(f.m2m_column_name())),
|
style.SQL_FIELD(quote_name(f.m2m_column_name())),
|
||||||
style.SQL_FIELD(quote_name(f.m2m_reverse_name()))))
|
style.SQL_FIELD(quote_name(f.m2m_reverse_name()))))
|
||||||
table_output.append(');')
|
table_output.append(');')
|
||||||
output.append(BoundStatement('\n'.join(table_output),
|
output.setdefault(f.rel.to, []).append(
|
||||||
connection))
|
BoundStatement('\n'.join(table_output), connection))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def get_drop_table(self, model, cascade=False, style=None):
|
def get_drop_table(self, model, cascade=False, style=None):
|
||||||
@ -271,3 +282,4 @@ class SchemaBuilder(object):
|
|||||||
'PositiveSmallIntegerField')) \
|
'PositiveSmallIntegerField')) \
|
||||||
and 'IntegerField' \
|
and 'IntegerField' \
|
||||||
or f.get_internal_type()
|
or f.get_internal_type()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user