2015-01-28 20:35:27 +08:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from MySQLdb.constants import FIELD_TYPE
|
2015-01-13 04:20:40 +08:00
|
|
|
|
|
|
|
from django.db.backends.base.introspection import (
|
2019-01-09 21:12:45 +08:00
|
|
|
BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo,
|
2015-01-13 04:20:40 +08:00
|
|
|
)
|
2017-02-16 02:33:55 +08:00
|
|
|
from django.db.models.indexes import Index
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.datastructures import OrderedSet
|
2012-08-20 16:46:21 +08:00
|
|
|
|
2019-01-09 21:12:45 +08:00
|
|
|
FieldInfo = namedtuple('FieldInfo', BaseFieldInfo._fields + ('extra', 'is_unsigned'))
|
2017-03-08 23:56:00 +08:00
|
|
|
InfoLine = namedtuple('InfoLine', 'col_name data_type max_len num_prec num_scale extra column_default is_unsigned')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
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',
|
2014-08-26 13:22:55 +08:00
|
|
|
FIELD_TYPE.SHORT: 'SmallIntegerField',
|
2008-08-11 20:11:25 +08:00
|
|
|
FIELD_TYPE.STRING: 'CharField',
|
2013-02-01 03:40:53 +08:00
|
|
|
FIELD_TYPE.TIME: 'TimeField',
|
2008-08-11 20:11:25 +08:00
|
|
|
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
|
|
|
|
2014-11-15 00:48:00 +08:00
|
|
|
def get_field_type(self, data_type, description):
|
2017-01-21 21:13:44 +08:00
|
|
|
field_type = super().get_field_type(data_type, description)
|
2015-07-02 16:43:15 +08:00
|
|
|
if 'auto_increment' in description.extra:
|
|
|
|
if field_type == 'IntegerField':
|
|
|
|
return 'AutoField'
|
|
|
|
elif field_type == 'BigIntegerField':
|
|
|
|
return 'BigAutoField'
|
2017-03-08 23:56:00 +08:00
|
|
|
if description.is_unsigned:
|
|
|
|
if field_type == 'IntegerField':
|
|
|
|
return 'PositiveIntegerField'
|
|
|
|
elif field_type == 'SmallIntegerField':
|
|
|
|
return 'PositiveSmallIntegerField'
|
2014-11-15 00:48:00 +08:00
|
|
|
return field_type
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_table_list(self, cursor):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return a list of table and view names in the current database."""
|
2014-09-21 03:34:23 +08:00
|
|
|
cursor.execute("SHOW FULL TABLES")
|
|
|
|
return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
|
|
|
|
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):
|
2012-08-31 01:28:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a description of the table with the DB-API cursor.description
|
|
|
|
interface."
|
2012-08-31 01:28:13 +08:00
|
|
|
"""
|
2014-11-15 00:48:00 +08:00
|
|
|
# information_schema database gives more accurate results for some figures:
|
|
|
|
# - varchar length returned by cursor.description is an internal length,
|
|
|
|
# not visible length (#5725)
|
|
|
|
# - precision and scale (for decimal fields) (#5014)
|
|
|
|
# - auto_increment is not available in cursor.description
|
2013-04-02 00:17:00 +08:00
|
|
|
cursor.execute("""
|
2017-03-08 23:56:00 +08:00
|
|
|
SELECT
|
|
|
|
column_name, data_type, character_maximum_length,
|
|
|
|
numeric_precision, numeric_scale, extra, column_default,
|
|
|
|
CASE
|
|
|
|
WHEN column_type LIKE '%% unsigned' THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END AS is_unsigned
|
2014-11-15 00:48:00 +08:00
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_name = %s AND table_schema = DATABASE()""", [table_name])
|
2014-12-07 05:00:09 +08:00
|
|
|
field_info = {line[0]: InfoLine(*line) for line in cursor.fetchall()}
|
2013-04-02 00:17:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
|
2016-01-24 00:47:07 +08:00
|
|
|
|
|
|
|
def to_int(i):
|
|
|
|
return int(i) if i is not None else i
|
|
|
|
|
2014-11-15 00:48:00 +08:00
|
|
|
fields = []
|
|
|
|
for line in cursor.description:
|
2017-12-22 10:05:23 +08:00
|
|
|
info = field_info[line[0]]
|
|
|
|
fields.append(FieldInfo(
|
|
|
|
*line[:3],
|
|
|
|
to_int(info.max_len) or line[3],
|
|
|
|
to_int(info.num_prec) or line[4],
|
|
|
|
to_int(info.num_scale) or line[5],
|
|
|
|
line[6],
|
|
|
|
info.column_default,
|
|
|
|
info.extra,
|
|
|
|
info.is_unsigned,
|
|
|
|
))
|
2014-11-15 00:48:00 +08:00
|
|
|
return fields
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2017-09-14 02:12:32 +08:00
|
|
|
def get_sequences(self, cursor, table_name, table_fields=()):
|
|
|
|
for field_info in self.get_table_description(cursor, table_name):
|
|
|
|
if 'auto_increment' in field_info.extra:
|
|
|
|
# MySQL allows only one auto-increment column per table.
|
|
|
|
return [{'table': table_name, 'column': field_info.name}]
|
|
|
|
return []
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_relations(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a dictionary of {field_name: (field_name_other_table, other_table)}
|
2015-01-11 03:27:30 +08:00
|
|
|
representing all relationships to the given table.
|
2008-08-11 20:11:25 +08:00
|
|
|
"""
|
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:
|
2015-01-11 03:27:30 +08:00
|
|
|
relations[my_fieldname] = (other_field, other_table)
|
2011-08-07 08:43:26 +08:00
|
|
|
return relations
|
|
|
|
|
|
|
|
def get_key_columns(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a list of (column_name, referenced_table_name, referenced_column_name)
|
|
|
|
for all key columns in the given table.
|
2011-08-07 08:43:26 +08:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2014-09-24 02:13:59 +08:00
|
|
|
def get_storage_engine(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Retrieve the storage engine for a given table. Return the default
|
2014-12-24 22:55:57 +08:00
|
|
|
storage engine if the table doesn't exist.
|
2014-09-24 02:13:59 +08:00
|
|
|
"""
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT engine "
|
|
|
|
"FROM information_schema.tables "
|
|
|
|
"WHERE table_name = %s", [table_name])
|
2014-12-24 22:55:57 +08:00
|
|
|
result = cursor.fetchone()
|
|
|
|
if not result:
|
|
|
|
return self.connection.features._mysql_storage_engine
|
|
|
|
return result[0]
|
2014-09-24 02:13:59 +08:00
|
|
|
|
2012-08-18 19:29:31 +08:00
|
|
|
def get_constraints(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Retrieve any constraints or keys (unique, pk, fk, check, index) across
|
|
|
|
one or more columns.
|
2012-08-18 19:29:31 +08:00
|
|
|
"""
|
|
|
|
constraints = {}
|
2012-08-18 20:48:54 +08:00
|
|
|
# Get the actual constraint names and columns
|
|
|
|
name_query = """
|
|
|
|
SELECT kc.`constraint_name`, kc.`column_name`,
|
|
|
|
kc.`referenced_table_name`, kc.`referenced_column_name`
|
|
|
|
FROM information_schema.key_column_usage AS kc
|
|
|
|
WHERE
|
2016-02-26 04:10:03 +08:00
|
|
|
kc.table_schema = DATABASE() AND
|
2012-08-18 20:48:54 +08:00
|
|
|
kc.table_name = %s
|
2018-06-29 22:43:53 +08:00
|
|
|
ORDER BY kc.`ordinal_position`
|
2012-08-18 20:48:54 +08:00
|
|
|
"""
|
2016-02-26 04:10:03 +08:00
|
|
|
cursor.execute(name_query, [table_name])
|
2012-08-18 20:48:54 +08:00
|
|
|
for constraint, column, ref_table, ref_column in cursor.fetchall():
|
|
|
|
if constraint not in constraints:
|
|
|
|
constraints[constraint] = {
|
2013-08-11 03:00:12 +08:00
|
|
|
'columns': OrderedSet(),
|
2012-08-18 20:48:54 +08:00
|
|
|
'primary_key': False,
|
|
|
|
'unique': False,
|
2012-08-31 06:11:56 +08:00
|
|
|
'index': False,
|
|
|
|
'check': False,
|
2012-08-18 20:48:54 +08:00
|
|
|
'foreign_key': (ref_table, ref_column) if ref_column else None,
|
|
|
|
}
|
|
|
|
constraints[constraint]['columns'].add(column)
|
|
|
|
# Now get the constraint types
|
|
|
|
type_query = """
|
|
|
|
SELECT c.constraint_name, c.constraint_type
|
|
|
|
FROM information_schema.table_constraints AS c
|
|
|
|
WHERE
|
2016-02-26 04:10:03 +08:00
|
|
|
c.table_schema = DATABASE() AND
|
2012-08-18 20:48:54 +08:00
|
|
|
c.table_name = %s
|
|
|
|
"""
|
2016-02-26 04:10:03 +08:00
|
|
|
cursor.execute(type_query, [table_name])
|
2012-08-18 20:48:54 +08:00
|
|
|
for constraint, kind in cursor.fetchall():
|
|
|
|
if kind.lower() == "primary key":
|
|
|
|
constraints[constraint]['primary_key'] = True
|
|
|
|
constraints[constraint]['unique'] = True
|
|
|
|
elif kind.lower() == "unique":
|
|
|
|
constraints[constraint]['unique'] = True
|
2012-08-31 06:11:56 +08:00
|
|
|
# Now add in the indexes
|
|
|
|
cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
|
2016-08-25 15:12:17 +08:00
|
|
|
for table, non_unique, index, colseq, column, type_ in [x[:5] + (x[10],) for x in cursor.fetchall()]:
|
2012-08-31 06:11:56 +08:00
|
|
|
if index not in constraints:
|
|
|
|
constraints[index] = {
|
2013-08-11 03:00:12 +08:00
|
|
|
'columns': OrderedSet(),
|
2012-08-31 06:11:56 +08:00
|
|
|
'primary_key': False,
|
|
|
|
'unique': False,
|
|
|
|
'check': False,
|
|
|
|
'foreign_key': None,
|
|
|
|
}
|
|
|
|
constraints[index]['index'] = True
|
2017-02-16 02:33:55 +08:00
|
|
|
constraints[index]['type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
|
2012-08-31 06:11:56 +08:00
|
|
|
constraints[index]['columns'].add(column)
|
2013-07-03 01:02:20 +08:00
|
|
|
# Convert the sorted sets to lists
|
|
|
|
for constraint in constraints.values():
|
|
|
|
constraint['columns'] = list(constraint['columns'])
|
2012-08-18 19:29:31 +08:00
|
|
|
return constraints
|