2017-06-01 17:51:02 +08:00
|
|
|
from collections import namedtuple
|
2016-08-20 18:14:02 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
import cx_Oracle
|
|
|
|
|
2017-09-14 02:12:32 +08:00
|
|
|
from django.db import models
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.introspection import (
|
2017-06-01 17:51:02 +08:00
|
|
|
BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo,
|
2015-01-13 04:20:40 +08:00
|
|
|
)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2017-06-01 17:51:02 +08:00
|
|
|
FieldInfo = namedtuple('FieldInfo', BaseFieldInfo._fields + ('is_autofield',))
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|
|
|
# Maps type objects to Django Field types.
|
|
|
|
data_types_reverse = {
|
2012-12-18 05:35:35 +08:00
|
|
|
cx_Oracle.BLOB: 'BinaryField',
|
2008-08-11 20:11:25 +08:00
|
|
|
cx_Oracle.CLOB: 'TextField',
|
2008-10-14 03:38:18 +08:00
|
|
|
cx_Oracle.DATETIME: 'DateField',
|
2008-08-11 20:11:25 +08:00
|
|
|
cx_Oracle.FIXED_CHAR: 'CharField',
|
2017-02-09 21:57:34 +08:00
|
|
|
cx_Oracle.FIXED_NCHAR: 'CharField',
|
|
|
|
cx_Oracle.NATIVE_FLOAT: 'FloatField',
|
|
|
|
cx_Oracle.NCHAR: 'CharField',
|
2008-08-11 20:11:25 +08:00
|
|
|
cx_Oracle.NCLOB: 'TextField',
|
|
|
|
cx_Oracle.NUMBER: 'DecimalField',
|
|
|
|
cx_Oracle.STRING: 'CharField',
|
|
|
|
cx_Oracle.TIMESTAMP: 'DateTimeField',
|
|
|
|
}
|
|
|
|
|
2015-02-10 07:54:51 +08:00
|
|
|
cache_bust_counter = 1
|
|
|
|
|
2009-08-22 05:42:39 +08:00
|
|
|
def get_field_type(self, data_type, description):
|
2014-01-30 01:18:32 +08:00
|
|
|
if data_type == cx_Oracle.NUMBER:
|
|
|
|
precision, scale = description[4:6]
|
|
|
|
if scale == 0:
|
|
|
|
if precision > 11:
|
2017-06-01 17:51:02 +08:00
|
|
|
return 'BigAutoField' if description.is_autofield else 'BigIntegerField'
|
2014-01-30 01:18:32 +08:00
|
|
|
elif precision == 1:
|
|
|
|
return 'BooleanField'
|
2017-06-01 17:51:02 +08:00
|
|
|
elif description.is_autofield:
|
|
|
|
return 'AutoField'
|
2014-01-30 01:18:32 +08:00
|
|
|
else:
|
|
|
|
return 'IntegerField'
|
|
|
|
elif scale == -127:
|
|
|
|
return 'FloatField'
|
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_field_type(data_type, description)
|
2009-08-22 05:42:39 +08:00
|
|
|
|
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("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()]
|
2006-05-26 13:37:21 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def get_table_description(self, cursor, table_name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
|
|
|
Return a description of the table with the DB-API cursor.description
|
|
|
|
interface.
|
|
|
|
"""
|
2016-12-01 00:21:57 +08:00
|
|
|
# user_tab_columns gives data default for columns
|
|
|
|
cursor.execute("""
|
2016-12-19 19:13:31 +08:00
|
|
|
SELECT
|
|
|
|
column_name,
|
|
|
|
data_default,
|
|
|
|
CASE
|
|
|
|
WHEN char_used IS NULL THEN data_length
|
|
|
|
ELSE char_length
|
2017-06-01 17:51:02 +08:00
|
|
|
END as internal_size,
|
|
|
|
CASE
|
|
|
|
WHEN identity_column = 'YES' THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END as is_autofield
|
2016-12-01 00:21:57 +08:00
|
|
|
FROM user_tab_cols
|
|
|
|
WHERE table_name = UPPER(%s)""", [table_name])
|
2016-12-19 19:13:31 +08:00
|
|
|
field_map = {
|
2017-06-01 17:51:02 +08:00
|
|
|
column: (internal_size, default if default != 'NULL' else None, is_autofield)
|
|
|
|
for column, default, internal_size, is_autofield in cursor.fetchall()
|
2016-12-19 19:13:31 +08:00
|
|
|
}
|
2015-02-10 07:54:51 +08:00
|
|
|
self.cache_bust_counter += 1
|
|
|
|
cursor.execute("SELECT * FROM {} WHERE ROWNUM < 2 AND {} > 0".format(
|
|
|
|
self.connection.ops.quote_name(table_name),
|
|
|
|
self.cache_bust_counter))
|
2009-05-12 04:55:40 +08:00
|
|
|
description = []
|
|
|
|
for desc in cursor.description:
|
2017-04-17 20:22:00 +08:00
|
|
|
name = desc[0]
|
2017-06-01 17:51:02 +08:00
|
|
|
internal_size, default, is_autofield = field_map[name]
|
2013-11-03 05:02:56 +08:00
|
|
|
name = name % {} # cx_Oracle, for some reason, doubles percent signs.
|
2017-12-22 10:05:23 +08:00
|
|
|
description.append(FieldInfo(
|
|
|
|
name.lower(), *desc[1:3], internal_size, desc[4] or 0,
|
|
|
|
desc[5] or 0, *desc[6:], default, is_autofield,
|
|
|
|
))
|
2009-05-12 04:55:40 +08:00
|
|
|
return description
|
2006-05-26 13:37:21 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def table_name_converter(self, name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Table name comparison is case insensitive under Oracle."""
|
2009-05-12 04:55:40 +08:00
|
|
|
return name.lower()
|
|
|
|
|
2017-09-14 02:12:32 +08:00
|
|
|
def get_sequences(self, cursor, table_name, table_fields=()):
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
|
|
|
user_tab_identity_cols.sequence_name,
|
|
|
|
user_tab_identity_cols.column_name
|
|
|
|
FROM
|
|
|
|
user_tab_identity_cols,
|
|
|
|
user_constraints,
|
|
|
|
user_cons_columns cols
|
|
|
|
WHERE
|
|
|
|
user_constraints.constraint_name = cols.constraint_name
|
|
|
|
AND user_constraints.table_name = user_tab_identity_cols.table_name
|
|
|
|
AND cols.column_name = user_tab_identity_cols.column_name
|
|
|
|
AND user_constraints.constraint_type = 'P'
|
|
|
|
AND user_tab_identity_cols.table_name = UPPER(%s)
|
|
|
|
""", [table_name])
|
|
|
|
# Oracle allows only one identity column per table.
|
|
|
|
row = cursor.fetchone()
|
|
|
|
if row:
|
|
|
|
return [{'name': row[0].lower(), 'table': table_name, 'column': row[1].lower()}]
|
|
|
|
# To keep backward compatibility for AutoFields that aren't Oracle
|
|
|
|
# identity columns.
|
|
|
|
for f in table_fields:
|
|
|
|
if isinstance(f, models.AutoField):
|
|
|
|
return [{'table': table_name, 'column': f.column}]
|
|
|
|
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-03-10 03:40:44 +08:00
|
|
|
table_name = table_name.upper()
|
2008-08-11 20:11:25 +08:00
|
|
|
cursor.execute("""
|
2017-07-06 19:39:01 +08:00
|
|
|
SELECT ca.column_name, cb.table_name, cb.column_name
|
|
|
|
FROM user_constraints, USER_CONS_COLUMNS ca, USER_CONS_COLUMNS cb
|
2008-08-11 20:11:25 +08:00
|
|
|
WHERE user_constraints.table_name = %s AND
|
|
|
|
user_constraints.constraint_name = ca.constraint_name AND
|
|
|
|
user_constraints.r_constraint_name = cb.constraint_name AND
|
2012-04-30 19:05:30 +08:00
|
|
|
ca.position = cb.position""", [table_name])
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
relations = {}
|
|
|
|
for row in cursor.fetchall():
|
2015-01-13 06:57:52 +08:00
|
|
|
relations[row[0].lower()] = (row[2].lower(), row[1].lower())
|
2008-08-11 20:11:25 +08:00
|
|
|
return relations
|
2006-05-26 13:37:21 +08:00
|
|
|
|
2013-05-28 02:05:59 +08:00
|
|
|
def get_key_columns(self, cursor, table_name):
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT ccol.column_name, rcol.table_name AS referenced_table, rcol.column_name AS referenced_column
|
|
|
|
FROM user_constraints c
|
|
|
|
JOIN user_cons_columns ccol
|
2013-07-08 08:39:54 +08:00
|
|
|
ON ccol.constraint_name = c.constraint_name
|
2013-05-28 02:05:59 +08:00
|
|
|
JOIN user_cons_columns rcol
|
2013-07-08 08:39:54 +08:00
|
|
|
ON rcol.constraint_name = c.r_constraint_name
|
|
|
|
WHERE c.table_name = %s AND c.constraint_type = 'R'""", [table_name.upper()])
|
|
|
|
return [tuple(cell.lower() for cell in row)
|
2013-05-28 02:05:59 +08:00
|
|
|
for row in cursor.fetchall()]
|
|
|
|
|
2013-08-14 03:54:57 +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.
|
2013-08-14 03:54:57 +08:00
|
|
|
"""
|
|
|
|
constraints = {}
|
2016-10-15 01:59:13 +08:00
|
|
|
# Loop over the constraints, getting PKs, uniques, and checks
|
2013-08-14 03:54:57 +08:00
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
|
|
|
user_constraints.constraint_name,
|
2017-05-31 19:54:13 +08:00
|
|
|
LISTAGG(LOWER(cols.column_name), ',') WITHIN GROUP (ORDER BY cols.position),
|
2013-08-14 03:54:57 +08:00
|
|
|
CASE user_constraints.constraint_type
|
|
|
|
WHEN 'P' THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END AS is_primary_key,
|
2016-10-15 01:59:13 +08:00
|
|
|
CASE
|
2017-05-31 19:54:13 +08:00
|
|
|
WHEN user_constraints.constraint_type IN ('P', 'U') THEN 1
|
2013-08-14 03:54:57 +08:00
|
|
|
ELSE 0
|
|
|
|
END AS is_unique,
|
|
|
|
CASE user_constraints.constraint_type
|
|
|
|
WHEN 'C' THEN 1
|
|
|
|
ELSE 0
|
2017-05-31 19:54:13 +08:00
|
|
|
END AS is_check_constraint
|
2013-08-14 03:54:57 +08:00
|
|
|
FROM
|
|
|
|
user_constraints
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
user_cons_columns cols ON user_constraints.constraint_name = cols.constraint_name
|
|
|
|
WHERE
|
2016-10-15 01:59:13 +08:00
|
|
|
user_constraints.constraint_type = ANY('P', 'U', 'C')
|
2013-08-14 03:54:57 +08:00
|
|
|
AND user_constraints.table_name = UPPER(%s)
|
2017-05-31 19:54:13 +08:00
|
|
|
GROUP BY user_constraints.constraint_name, user_constraints.constraint_type
|
2013-08-14 03:54:57 +08:00
|
|
|
""", [table_name])
|
2017-05-31 19:54:13 +08:00
|
|
|
for constraint, columns, pk, unique, check in cursor.fetchall():
|
|
|
|
constraints[constraint] = {
|
|
|
|
'columns': columns.split(','),
|
|
|
|
'primary_key': pk,
|
|
|
|
'unique': unique,
|
|
|
|
'foreign_key': None,
|
|
|
|
'check': check,
|
|
|
|
'index': unique, # All uniques come with an index
|
|
|
|
}
|
2013-08-14 03:54:57 +08:00
|
|
|
# Foreign key constraints
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
|
|
|
cons.constraint_name,
|
2017-05-31 19:54:13 +08:00
|
|
|
LISTAGG(LOWER(cols.column_name), ',') WITHIN GROUP (ORDER BY cols.position),
|
2016-12-06 08:12:37 +08:00
|
|
|
LOWER(rcols.table_name),
|
2013-08-14 03:54:57 +08:00
|
|
|
LOWER(rcols.column_name)
|
|
|
|
FROM
|
|
|
|
user_constraints cons
|
|
|
|
INNER JOIN
|
2017-05-31 19:54:13 +08:00
|
|
|
user_cons_columns rcols ON rcols.constraint_name = cons.r_constraint_name AND rcols.position = 1
|
2013-08-14 03:54:57 +08:00
|
|
|
LEFT OUTER JOIN
|
|
|
|
user_cons_columns cols ON cons.constraint_name = cols.constraint_name
|
|
|
|
WHERE
|
|
|
|
cons.constraint_type = 'R' AND
|
|
|
|
cons.table_name = UPPER(%s)
|
2017-05-31 19:54:13 +08:00
|
|
|
GROUP BY cons.constraint_name, rcols.table_name, rcols.column_name
|
2013-08-14 03:54:57 +08:00
|
|
|
""", [table_name])
|
2017-05-31 19:54:13 +08:00
|
|
|
for constraint, columns, other_table, other_column in cursor.fetchall():
|
|
|
|
constraints[constraint] = {
|
|
|
|
'primary_key': False,
|
|
|
|
'unique': False,
|
|
|
|
'foreign_key': (other_table, other_column),
|
|
|
|
'check': False,
|
|
|
|
'index': False,
|
|
|
|
'columns': columns.split(','),
|
|
|
|
}
|
2013-08-14 03:54:57 +08:00
|
|
|
# Now get indexes
|
|
|
|
cursor.execute("""
|
|
|
|
SELECT
|
2017-05-31 19:54:13 +08:00
|
|
|
ind.index_name,
|
|
|
|
LOWER(ind.index_type),
|
|
|
|
LISTAGG(LOWER(cols.column_name), ',') WITHIN GROUP (ORDER BY cols.column_position),
|
|
|
|
LISTAGG(cols.descend, ',') WITHIN GROUP (ORDER BY cols.column_position)
|
2013-08-14 03:54:57 +08:00
|
|
|
FROM
|
2016-08-25 15:12:17 +08:00
|
|
|
user_ind_columns cols, user_indexes ind
|
2013-08-14 03:54:57 +08:00
|
|
|
WHERE
|
2016-08-25 15:12:17 +08:00
|
|
|
cols.table_name = UPPER(%s) AND
|
2013-08-14 03:54:57 +08:00
|
|
|
NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM user_constraints cons
|
2017-05-31 19:54:13 +08:00
|
|
|
WHERE ind.index_name = cons.index_name
|
2016-08-25 15:12:17 +08:00
|
|
|
) AND cols.index_name = ind.index_name
|
2017-05-31 19:54:13 +08:00
|
|
|
GROUP BY ind.index_name, ind.index_type
|
2013-08-14 03:54:57 +08:00
|
|
|
""", [table_name])
|
2017-05-31 19:54:13 +08:00
|
|
|
for constraint, type_, columns, orders in cursor.fetchall():
|
|
|
|
constraints[constraint] = {
|
|
|
|
'primary_key': False,
|
|
|
|
'unique': False,
|
|
|
|
'foreign_key': None,
|
|
|
|
'check': False,
|
|
|
|
'index': True,
|
|
|
|
'type': 'idx' if type_ == 'normal' else type_,
|
|
|
|
'columns': columns.split(','),
|
|
|
|
'orders': orders.split(','),
|
|
|
|
}
|
2013-08-14 03:54:57 +08:00
|
|
|
return constraints
|