mirror of
https://github.com/django/django.git
synced 2025-06-03 02:29:13 +00:00
Made get_table_list return a TableInfo named tuple
This commit is contained in:
parent
463952d940
commit
b8cdc7dcc3
@ -23,6 +23,13 @@ from django.utils.functional import cached_property
|
|||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
# Structure returned by DatabaseIntrospection.get_table_list()
|
||||||
|
TableInfo = namedtuple('TableInfo', ['name', 'type'])
|
||||||
|
|
||||||
|
# Structure returned by the DB-API cursor.description interface (PEP 249)
|
||||||
|
FieldInfo = namedtuple('FieldInfo',
|
||||||
|
'name type_code display_size internal_size precision scale null_ok')
|
||||||
|
|
||||||
|
|
||||||
class BaseDatabaseWrapper(object):
|
class BaseDatabaseWrapper(object):
|
||||||
"""
|
"""
|
||||||
@ -1235,11 +1242,6 @@ class BaseDatabaseOperations(object):
|
|||||||
return self.integer_field_ranges[internal_type]
|
return self.integer_field_ranges[internal_type]
|
||||||
|
|
||||||
|
|
||||||
# Structure returned by the DB-API cursor.description interface (PEP 249)
|
|
||||||
FieldInfo = namedtuple('FieldInfo',
|
|
||||||
'name type_code display_size internal_size precision scale null_ok')
|
|
||||||
|
|
||||||
|
|
||||||
class BaseDatabaseIntrospection(object):
|
class BaseDatabaseIntrospection(object):
|
||||||
"""
|
"""
|
||||||
This class encapsulates all backend-specific introspection utilities
|
This class encapsulates all backend-specific introspection utilities
|
||||||
@ -1281,13 +1283,13 @@ class BaseDatabaseIntrospection(object):
|
|||||||
"""
|
"""
|
||||||
if cursor is None:
|
if cursor is None:
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
return sorted(self.get_table_list(cursor))
|
return sorted([ti.name for ti in self.get_table_list(cursor) if ti.type == 't'])
|
||||||
return sorted(self.get_table_list(cursor))
|
return sorted([ti.name for ti in self.get_table_list(cursor) if ti.type == 't'])
|
||||||
|
|
||||||
def get_table_list(self, cursor):
|
def get_table_list(self, cursor):
|
||||||
"""
|
"""
|
||||||
Returns an unsorted list of names of all tables that exist in the
|
Returns an unsorted list of TableInfo named tuples of all tables and
|
||||||
database.
|
views that exist in the database.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_table_list() method')
|
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_table_list() method')
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
from .base import FIELD_TYPE
|
from .base import FIELD_TYPE
|
||||||
from django.utils.datastructures import OrderedSet
|
from django.utils.datastructures import OrderedSet
|
||||||
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
|
from django.db.backends import BaseDatabaseIntrospection, FieldInfo, TableInfo
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
|
|
||||||
@ -33,9 +33,12 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def get_table_list(self, cursor):
|
def get_table_list(self, cursor):
|
||||||
"Returns a list of table names in the current database."
|
"""
|
||||||
cursor.execute("SHOW TABLES")
|
Returns a list of table and view names in the current database.
|
||||||
return [row[0] for row in cursor.fetchall()]
|
"""
|
||||||
|
cursor.execute("SHOW FULL TABLES")
|
||||||
|
return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
|
||||||
|
for row in cursor.fetchall()]
|
||||||
|
|
||||||
def get_table_description(self, cursor, table_name):
|
def get_table_description(self, cursor, table_name):
|
||||||
"""
|
"""
|
||||||
|
@ -2,7 +2,7 @@ import re
|
|||||||
|
|
||||||
import cx_Oracle
|
import cx_Oracle
|
||||||
|
|
||||||
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
|
from django.db.backends import BaseDatabaseIntrospection, FieldInfo, TableInfo
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
|
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
|
||||||
@ -48,9 +48,12 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||||||
return super(DatabaseIntrospection, self).get_field_type(data_type, description)
|
return super(DatabaseIntrospection, self).get_field_type(data_type, description)
|
||||||
|
|
||||||
def get_table_list(self, cursor):
|
def get_table_list(self, cursor):
|
||||||
"Returns a list of table names in the current database."
|
"""
|
||||||
cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
|
Returns a list of table and view names in the current database.
|
||||||
return [row[0].lower() for row in cursor.fetchall()]
|
"""
|
||||||
|
cursor.execute("SELECT TABLE_NAME, 't' FROM USER_TABLES UNION ALL "
|
||||||
|
"SELECT VIEW_NAME, 'v' FROM USER_VIEWS")
|
||||||
|
return [TableInfo(row[0].lower(), row[1]) for row in cursor.fetchall()]
|
||||||
|
|
||||||
def get_table_description(self, cursor, table_name):
|
def get_table_description(self, cursor, table_name):
|
||||||
"Returns a description of the table, with the DB-API cursor.description interface."
|
"Returns a description of the table, with the DB-API cursor.description interface."
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
|
from django.db.backends import BaseDatabaseIntrospection, FieldInfo, TableInfo
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
|
||||||
|
|
||||||
@ -29,15 +29,19 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||||||
ignored_tables = []
|
ignored_tables = []
|
||||||
|
|
||||||
def get_table_list(self, cursor):
|
def get_table_list(self, cursor):
|
||||||
"Returns a list of table names in the current database."
|
"""
|
||||||
|
Returns a list of table and view names in the current database.
|
||||||
|
"""
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT c.relname
|
SELECT c.relname, c.relkind
|
||||||
FROM pg_catalog.pg_class c
|
FROM pg_catalog.pg_class c
|
||||||
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||||
WHERE c.relkind IN ('r', 'v', '')
|
WHERE c.relkind IN ('r', 'v')
|
||||||
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
|
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
|
||||||
AND pg_catalog.pg_table_is_visible(c.oid)""")
|
AND pg_catalog.pg_table_is_visible(c.oid)""")
|
||||||
return [row[0] for row in cursor.fetchall() if row[0] not in self.ignored_tables]
|
return [TableInfo(row[0], {'r':'t', 'v': 'v'}.get(row[1]))
|
||||||
|
for row in cursor.fetchall()
|
||||||
|
if row[0] not in self.ignored_tables]
|
||||||
|
|
||||||
def get_table_description(self, cursor, table_name):
|
def get_table_description(self, cursor, table_name):
|
||||||
"Returns a description of the table, with the DB-API cursor.description interface."
|
"Returns a description of the table, with the DB-API cursor.description interface."
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from django.db.backends import BaseDatabaseIntrospection, FieldInfo
|
from django.db.backends import BaseDatabaseIntrospection, FieldInfo, TableInfo
|
||||||
|
|
||||||
|
|
||||||
field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
|
field_size_re = re.compile(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$')
|
||||||
@ -54,14 +54,16 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||||||
data_types_reverse = FlexibleFieldLookupDict()
|
data_types_reverse = FlexibleFieldLookupDict()
|
||||||
|
|
||||||
def get_table_list(self, cursor):
|
def get_table_list(self, cursor):
|
||||||
"Returns a list of table names in the current database."
|
"""
|
||||||
|
Returns a list of table and view names in the current database.
|
||||||
|
"""
|
||||||
# Skip the sqlite_sequence system table used for autoincrement key
|
# Skip the sqlite_sequence system table used for autoincrement key
|
||||||
# generation.
|
# generation.
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT name FROM sqlite_master
|
SELECT name, type FROM sqlite_master
|
||||||
WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
|
WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
|
||||||
ORDER BY name""")
|
ORDER BY name""")
|
||||||
return [row[0] for row in cursor.fetchall()]
|
return [TableInfo(row[0], row[1][0]) for row in cursor.fetchall()]
|
||||||
|
|
||||||
def get_table_description(self, cursor, table_name):
|
def get_table_description(self, cursor, table_name):
|
||||||
"Returns a description of the table, with the DB-API cursor.description interface."
|
"Returns a description of the table, with the DB-API cursor.description interface."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user