| 1 | Index: db/backends/mysql/introspection.py
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- db/backends/mysql/introspection.py (revisione 9082)
|
|---|
| 4 | +++ db/backends/mysql/introspection.py (copia locale)
|
|---|
| 5 | @@ -5,6 +5,24 @@
|
|---|
| 6 |
|
|---|
| 7 | foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
|
|---|
| 8 |
|
|---|
| 9 | +def visitDFS(g):
|
|---|
| 10 | + vertices_order =[]
|
|---|
| 11 | + def dfs(g):
|
|---|
| 12 | + seen = {}
|
|---|
| 13 | + def visit(g, v):
|
|---|
| 14 | +
|
|---|
| 15 | + seen[v] = 1
|
|---|
| 16 | + for u in g[v]:
|
|---|
| 17 | + if u not in seen:
|
|---|
| 18 | + visit(g, u)
|
|---|
| 19 | + vertices_order.append(v)
|
|---|
| 20 | +
|
|---|
| 21 | + for v in g:
|
|---|
| 22 | + if v not in seen:
|
|---|
| 23 | + visit(g, v)
|
|---|
| 24 | + dfs(g)
|
|---|
| 25 | + return vertices_order
|
|---|
| 26 | +
|
|---|
| 27 | class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|---|
| 28 | data_types_reverse = {
|
|---|
| 29 | FIELD_TYPE.BLOB: 'TextField',
|
|---|
| 30 | @@ -28,11 +46,28 @@
|
|---|
| 31 | FIELD_TYPE.VAR_STRING: 'CharField',
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | +
|
|---|
| 35 | def get_table_list(self, cursor):
|
|---|
| 36 | "Returns a list of table names in the current database."
|
|---|
| 37 | cursor.execute("SHOW TABLES")
|
|---|
| 38 | - return [row[0] for row in cursor.fetchall()]
|
|---|
| 39 | + tables = cursor.fetchall()
|
|---|
| 40 | + cursor.execute("""
|
|---|
| 41 | + SELECT referenced_table_name, table_name
|
|---|
| 42 | + FROM information_schema.key_column_usage
|
|---|
| 43 | + WHERE
|
|---|
| 44 | + table_schema = DATABASE()
|
|---|
| 45 | + AND constraint_name LIKE %s
|
|---|
| 46 | + AND referenced_column_name IS NOT NULL""", ['%fk%'])
|
|---|
| 47 | + list_adj ={}
|
|---|
| 48 | + listtables = cursor.fetchall()
|
|---|
| 49 | + for t in tables :
|
|---|
| 50 | + list_adj[str(t[0])] =[]
|
|---|
| 51 | + for row in listtables:
|
|---|
| 52 | + list_adj[str(row[1])].append(str(row[0]))
|
|---|
| 53 |
|
|---|
| 54 | + return visitDFS(list_adj)
|
|---|
| 55 | +
|
|---|
| 56 | +
|
|---|
| 57 | def get_table_description(self, cursor, table_name):
|
|---|
| 58 | "Returns a description of the table, with the DB-API cursor.description interface."
|
|---|
| 59 | cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
|
|---|