2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends import BaseDatabaseIntrospection
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2006-05-02 09:31:56 +08:00
|
|
|
from MySQLdb import ProgrammingError, OperationalError
|
|
|
|
from MySQLdb.constants import FIELD_TYPE
|
|
|
|
import re
|
|
|
|
|
|
|
|
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|
|
|
data_types_reverse = {
|
|
|
|
FIELD_TYPE.BLOB: 'TextField',
|
|
|
|
FIELD_TYPE.CHAR: 'CharField',
|
|
|
|
FIELD_TYPE.DECIMAL: 'DecimalField',
|
2008-08-23 12:14:23 +08:00
|
|
|
FIELD_TYPE.NEWDECIMAL: 'DecimalField',
|
2008-08-11 20:11:25 +08:00
|
|
|
FIELD_TYPE.DATE: 'DateField',
|
|
|
|
FIELD_TYPE.DATETIME: 'DateTimeField',
|
|
|
|
FIELD_TYPE.DOUBLE: 'FloatField',
|
|
|
|
FIELD_TYPE.FLOAT: 'FloatField',
|
|
|
|
FIELD_TYPE.INT24: 'IntegerField',
|
|
|
|
FIELD_TYPE.LONG: 'IntegerField',
|
2009-12-17 23:10:38 +08:00
|
|
|
FIELD_TYPE.LONGLONG: 'BigIntegerField',
|
2008-08-11 20:11:25 +08:00
|
|
|
FIELD_TYPE.SHORT: 'IntegerField',
|
|
|
|
FIELD_TYPE.STRING: 'CharField',
|
|
|
|
FIELD_TYPE.TIMESTAMP: 'DateTimeField',
|
|
|
|
FIELD_TYPE.TINY: 'IntegerField',
|
|
|
|
FIELD_TYPE.TINY_BLOB: 'TextField',
|
|
|
|
FIELD_TYPE.MEDIUM_BLOB: 'TextField',
|
|
|
|
FIELD_TYPE.LONG_BLOB: 'TextField',
|
|
|
|
FIELD_TYPE.VAR_STRING: 'CharField',
|
|
|
|
}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_table_list(self, cursor):
|
|
|
|
"Returns a list of table names in the current database."
|
|
|
|
cursor.execute("SHOW TABLES")
|
|
|
|
return [row[0] for row in cursor.fetchall()]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_table_description(self, cursor, table_name):
|
|
|
|
"Returns a description of the table, with the DB-API cursor.description interface."
|
|
|
|
cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
|
|
|
|
return cursor.description
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def _name_to_index(self, cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns a dictionary of {field_name: field_index} for the given table.
|
|
|
|
Indexes are 0-based.
|
|
|
|
"""
|
|
|
|
return dict([(d[0], i) for i, d in enumerate(self.get_table_description(cursor, table_name))])
|
|
|
|
|
|
|
|
def get_relations(self, cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns a dictionary of {field_index: (field_index_other_table, other_table)}
|
|
|
|
representing all relationships to the given table. Indexes are 0-based.
|
|
|
|
"""
|
|
|
|
my_field_dict = self._name_to_index(cursor, table_name)
|
2011-08-07 08:43:26 +08:00
|
|
|
constraints = self.get_key_columns(cursor, table_name)
|
2008-08-11 20:11:25 +08:00
|
|
|
relations = {}
|
2011-08-07 08:43:26 +08:00
|
|
|
for my_fieldname, other_table, other_field in constraints:
|
|
|
|
other_field_index = self._name_to_index(cursor, other_table)[other_field]
|
|
|
|
my_field_index = my_field_dict[my_fieldname]
|
|
|
|
relations[my_field_index] = (other_field_index, other_table)
|
|
|
|
return relations
|
|
|
|
|
|
|
|
def get_key_columns(self, cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns a list of (column_name, referenced_table_name, referenced_column_name) for all
|
|
|
|
key columns in given table.
|
|
|
|
"""
|
|
|
|
key_columns = []
|
2012-04-21 11:04:10 +08:00
|
|
|
cursor.execute("""
|
|
|
|
SELECT column_name, referenced_table_name, referenced_column_name
|
|
|
|
FROM information_schema.key_column_usage
|
|
|
|
WHERE table_name = %s
|
|
|
|
AND table_schema = DATABASE()
|
|
|
|
AND referenced_table_name IS NOT NULL
|
|
|
|
AND referenced_column_name IS NOT NULL""", [table_name])
|
|
|
|
key_columns.extend(cursor.fetchall())
|
2011-08-07 08:43:26 +08:00
|
|
|
return key_columns
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-08-07 08:43:26 +08:00
|
|
|
def get_primary_key_column(self, cursor, table_name):
|
|
|
|
"""
|
|
|
|
Returns the name of the primary key column for the given table
|
|
|
|
"""
|
2012-07-21 03:14:27 +08:00
|
|
|
for column in six.iteritems(self.get_indexes(cursor, table_name)):
|
2011-08-07 08:43:26 +08:00
|
|
|
if column[1]['primary_key']:
|
|
|
|
return column[0]
|
|
|
|
return None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_indexes(self, cursor, table_name):
|
|
|
|
cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
|
2012-04-30 19:05:30 +08:00
|
|
|
# Do a two-pass search for indexes: on first pass check which indexes
|
|
|
|
# are multicolumn, on second pass check which single-column indexes
|
|
|
|
# are present.
|
|
|
|
rows = list(cursor.fetchall())
|
|
|
|
multicol_indexes = set()
|
|
|
|
for row in rows:
|
|
|
|
if row[3] > 1:
|
|
|
|
multicol_indexes.add(row[2])
|
2008-08-11 20:11:25 +08:00
|
|
|
indexes = {}
|
2012-04-30 19:05:30 +08:00
|
|
|
for row in rows:
|
|
|
|
if row[2] in multicol_indexes:
|
|
|
|
continue
|
2008-08-11 20:11:25 +08:00
|
|
|
indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
|
|
|
|
return indexes
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-08-18 19:29:31 +08:00
|
|
|
def get_constraints(self, cursor, table_name):
|
|
|
|
"""
|
|
|
|
Retrieves any constraints (unique, pk, fk, check) across one or more columns.
|
|
|
|
Returns {'cnname': {'columns': set(columns), 'primary_key': bool, 'unique': bool}}
|
|
|
|
"""
|
|
|
|
constraints = {}
|
|
|
|
# Loop over the constraint tables, collecting things as constraints
|
|
|
|
ifsc_tables = ["constraint_column_usage", "key_column_usage"]
|
|
|
|
for ifsc_table in ifsc_tables:
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT kc.constraint_name, kc.column_name, c.constraint_type
|
|
|
|
FROM information_schema.%s AS kc
|
|
|
|
JOIN information_schema.table_constraints AS c ON
|
|
|
|
kc.table_schema = c.table_schema AND
|
|
|
|
kc.table_name = c.table_name AND
|
|
|
|
kc.constraint_name = c.constraint_name
|
|
|
|
WHERE
|
|
|
|
kc.table_schema = %%s AND
|
|
|
|
kc.table_name = %%s
|
|
|
|
""" % ifsc_table, [self.connection.settings_dict['NAME'], table_name])
|
|
|
|
for constraint, column, kind in cursor.fetchall():
|
|
|
|
# If we're the first column, make the record
|
|
|
|
if constraint not in constraints:
|
|
|
|
constraints[constraint] = {
|
|
|
|
"columns": set(),
|
|
|
|
"primary_key": kind.lower() == "primary key",
|
|
|
|
"foreign_key": kind.lower() == "foreign key",
|
|
|
|
"unique": kind.lower() in ["primary key", "unique"],
|
|
|
|
}
|
|
|
|
# Record the details
|
|
|
|
constraints[constraint]['columns'].add(column)
|
|
|
|
return constraints
|