mirror of
https://github.com/django/django.git
synced 2025-06-06 12:09:11 +00:00
Added introspection.table_exists() method to db instrospection; this is used all over the place in django.db.management and will break under database backends that don't support LIMIT.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2461 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
7e11865b84
commit
27bb553ba0
@ -10,4 +10,7 @@ def get_relations(cursor, table_name):
|
|||||||
def get_indexes(cursor, table_name):
|
def get_indexes(cursor, table_name):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def table_exists(cursor, table_name):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
DATA_TYPES_REVERSE = {}
|
DATA_TYPES_REVERSE = {}
|
||||||
|
@ -4,5 +4,6 @@ get_table_list = complain
|
|||||||
get_table_description = complain
|
get_table_description = complain
|
||||||
get_relations = complain
|
get_relations = complain
|
||||||
get_indexes = complain
|
get_indexes = complain
|
||||||
|
table_exists = complain
|
||||||
|
|
||||||
DATA_TYPES_REVERSE = {}
|
DATA_TYPES_REVERSE = {}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from django.db import transaction
|
||||||
from django.db.backends.mysql.base import quote_name
|
from django.db.backends.mysql.base import quote_name
|
||||||
from MySQLdb.constants import FIELD_TYPE
|
from MySQLdb.constants import FIELD_TYPE
|
||||||
|
|
||||||
@ -27,6 +28,16 @@ def get_indexes(cursor, table_name):
|
|||||||
indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
|
indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
|
def table_exists(cursor, table_name):
|
||||||
|
"""Returns True if the given table exists."""
|
||||||
|
try:
|
||||||
|
cursor.execute("SELECT 1 FROM %s LIMIT 1" % quote_name(table_name))
|
||||||
|
except:
|
||||||
|
transaction.rollback_unless_managed()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
DATA_TYPES_REVERSE = {
|
DATA_TYPES_REVERSE = {
|
||||||
FIELD_TYPE.BLOB: 'TextField',
|
FIELD_TYPE.BLOB: 'TextField',
|
||||||
FIELD_TYPE.CHAR: 'CharField',
|
FIELD_TYPE.CHAR: 'CharField',
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from django.db import transaction
|
||||||
from django.db.backends.postgresql.base import quote_name
|
from django.db.backends.postgresql.base import quote_name
|
||||||
|
|
||||||
def get_table_list(cursor):
|
def get_table_list(cursor):
|
||||||
@ -67,6 +68,16 @@ def get_indexes(cursor, table_name):
|
|||||||
indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
|
indexes[col_name] = {'primary_key': row[2], 'unique': row[1]}
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
|
def table_exists(cursor, table_name):
|
||||||
|
"""Returns True if the given table exists."""
|
||||||
|
try:
|
||||||
|
cursor.execute("SELECT 1 FROM %s LIMIT 1" % quote_name(table_name))
|
||||||
|
except:
|
||||||
|
transaction.rollback_unless_managed()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
# Maps type codes to Django Field types.
|
# Maps type codes to Django Field types.
|
||||||
DATA_TYPES_REVERSE = {
|
DATA_TYPES_REVERSE = {
|
||||||
16: 'BooleanField',
|
16: 'BooleanField',
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from django.db import transaction
|
||||||
from django.db.backends.sqlite3.base import quote_name
|
from django.db.backends.sqlite3.base import quote_name
|
||||||
|
|
||||||
def get_table_list(cursor):
|
def get_table_list(cursor):
|
||||||
@ -14,6 +15,16 @@ def get_relations(cursor, table_name):
|
|||||||
def get_indexes(cursor, table_name):
|
def get_indexes(cursor, table_name):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def table_exists(cursor, table_name):
|
||||||
|
"""Returns True if the given table exists."""
|
||||||
|
try:
|
||||||
|
cursor.execute("SELECT 1 FROM %s LIMIT 1" % quote_name(table_name))
|
||||||
|
except:
|
||||||
|
transaction.rollback_unless_managed()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
# Maps SQL types to Django Field types. Some of the SQL types have multiple
|
# Maps SQL types to Django Field types. Some of the SQL types have multiple
|
||||||
# entries here because SQLite allows for anything and doesn't normalize the
|
# entries here because SQLite allows for anything and doesn't normalize the
|
||||||
# field type; it uses whatever was given.
|
# field type; it uses whatever was given.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user