|
36 | 36 | from django.db.backends.base.introspection import (
|
37 | 37 | BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo,
|
38 | 38 | )
|
39 |
| -from django.db.backends.mysql.introspection import ( |
40 |
| - DatabaseIntrospection as MySQLDatabaseIntrospection, |
41 |
| -) |
42 | 39 | from django.db.models import Index
|
43 | 40 | from django.utils.datastructures import OrderedSet
|
44 | 41 |
|
|
60 | 57 | )
|
61 | 58 |
|
62 | 59 |
|
63 |
| -class DatabaseIntrospection(MySQLDatabaseIntrospection): |
| 60 | +class DatabaseIntrospection(BaseDatabaseIntrospection): |
| 61 | + |
| 62 | + data_types_reverse = { |
| 63 | + FieldType.BLOB: 'TextField', |
| 64 | + FieldType.DECIMAL: 'DecimalField', |
| 65 | + FieldType.NEWDECIMAL: 'DecimalField', |
| 66 | + FieldType.DATE: 'DateField', |
| 67 | + FieldType.DATETIME: 'DateTimeField', |
| 68 | + FieldType.DOUBLE: 'FloatField', |
| 69 | + FieldType.FLOAT: 'FloatField', |
| 70 | + FieldType.INT24: 'IntegerField', |
| 71 | + FieldType.LONG: 'IntegerField', |
| 72 | + FieldType.LONGLONG: 'BigIntegerField', |
| 73 | + FieldType.SHORT: 'SmallIntegerField', |
| 74 | + FieldType.STRING: 'CharField', |
| 75 | + FieldType.TIME: 'TimeField', |
| 76 | + FieldType.TIMESTAMP: 'DateTimeField', |
| 77 | + FieldType.TINY: 'IntegerField', |
| 78 | + FieldType.TINY_BLOB: 'TextField', |
| 79 | + FieldType.MEDIUM_BLOB: 'TextField', |
| 80 | + FieldType.LONG_BLOB: 'TextField', |
| 81 | + FieldType.VAR_STRING: 'CharField', |
| 82 | + } |
| 83 | + |
| 84 | + def get_field_type(self, data_type, description): |
| 85 | + field_type = super().get_field_type(data_type, description) |
| 86 | + if 'auto_increment' in description.extra: |
| 87 | + if field_type == 'IntegerField': |
| 88 | + return 'AutoField' |
| 89 | + elif field_type == 'BigIntegerField': |
| 90 | + return 'BigAutoField' |
| 91 | + elif field_type == 'SmallIntegerField': |
| 92 | + return 'SmallAutoField' |
| 93 | + if description.is_unsigned: |
| 94 | + if field_type == 'BigIntegerField': |
| 95 | + return 'PositiveBigIntegerField' |
| 96 | + elif field_type == 'IntegerField': |
| 97 | + return 'PositiveIntegerField' |
| 98 | + elif field_type == 'SmallIntegerField': |
| 99 | + return 'PositiveSmallIntegerField' |
| 100 | + # JSON data type is an alias for LONGTEXT in MariaDB, use check |
| 101 | + # constraints clauses to introspect JSONField. |
| 102 | + if description.has_json_constraint: |
| 103 | + return 'JSONField' |
| 104 | + return field_type |
| 105 | + |
| 106 | + def get_table_list(self, cursor): |
| 107 | + """Return a list of table and view names in the current database.""" |
| 108 | + cursor.execute("SHOW FULL TABLES") |
| 109 | + return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1])) |
| 110 | + for row in cursor.fetchall()] |
64 | 111 |
|
65 | 112 | def get_table_description(self, cursor, table_name):
|
66 | 113 | """
|
@@ -181,6 +228,54 @@ def get_primary_key_column(self, cursor, table_name):
|
181 | 228 | return column[0]
|
182 | 229 | return None
|
183 | 230 |
|
| 231 | + def get_sequences(self, cursor, table_name, table_fields=()): |
| 232 | + for field_info in self.get_table_description(cursor, table_name): |
| 233 | + if 'auto_increment' in field_info.extra: |
| 234 | + # MySQL allows only one auto-increment column per table. |
| 235 | + return [{'table': table_name, 'column': field_info.name}] |
| 236 | + return [] |
| 237 | + |
| 238 | + def get_relations(self, cursor, table_name): |
| 239 | + """ |
| 240 | + Return a dictionary of {field_name: (field_name_other_table, other_table)} |
| 241 | + representing all relationships to the given table. |
| 242 | + """ |
| 243 | + constraints = self.get_key_columns(cursor, table_name) |
| 244 | + relations = {} |
| 245 | + for my_fieldname, other_table, other_field in constraints: |
| 246 | + relations[my_fieldname] = (other_field, other_table) |
| 247 | + return relations |
| 248 | + |
| 249 | + def get_key_columns(self, cursor, table_name): |
| 250 | + """ |
| 251 | + Return a list of (column_name, referenced_table_name, referenced_column_name) |
| 252 | + for all key columns in the given table. |
| 253 | + """ |
| 254 | + key_columns = [] |
| 255 | + cursor.execute(""" |
| 256 | + SELECT column_name, referenced_table_name, referenced_column_name |
| 257 | + FROM information_schema.key_column_usage |
| 258 | + WHERE table_name = %s |
| 259 | + AND table_schema = DATABASE() |
| 260 | + AND referenced_table_name IS NOT NULL |
| 261 | + AND referenced_column_name IS NOT NULL""", [table_name]) |
| 262 | + key_columns.extend(cursor.fetchall()) |
| 263 | + return key_columns |
| 264 | + |
| 265 | + def get_storage_engine(self, cursor, table_name): |
| 266 | + """ |
| 267 | + Retrieve the storage engine for a given table. Return the default |
| 268 | + storage engine if the table doesn't exist. |
| 269 | + """ |
| 270 | + cursor.execute( |
| 271 | + "SELECT engine " |
| 272 | + "FROM information_schema.tables " |
| 273 | + "WHERE table_name = %s", [table_name]) |
| 274 | + result = cursor.fetchone() |
| 275 | + if not result: |
| 276 | + return self.connection.features._mysql_storage_engine |
| 277 | + return result[0] |
| 278 | + |
184 | 279 | def get_constraints(self, cursor, table_name):
|
185 | 280 | """
|
186 | 281 | Retrieve any constraints or keys (unique, pk, fk, check, index) across
|
|
0 commit comments