|
0
|
1 |
from django.db.backends import BaseDatabaseIntrospection |
|
|
2 |
import cx_Oracle |
|
|
3 |
import re |
|
|
4 |
|
|
|
5 |
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)") |
|
|
6 |
|
|
|
7 |
class DatabaseIntrospection(BaseDatabaseIntrospection): |
|
|
8 |
# Maps type objects to Django Field types. |
|
|
9 |
data_types_reverse = { |
|
|
10 |
cx_Oracle.CLOB: 'TextField', |
|
|
11 |
cx_Oracle.DATETIME: 'DateField', |
|
|
12 |
cx_Oracle.FIXED_CHAR: 'CharField', |
|
|
13 |
cx_Oracle.NCLOB: 'TextField', |
|
|
14 |
cx_Oracle.NUMBER: 'DecimalField', |
|
|
15 |
cx_Oracle.STRING: 'CharField', |
|
|
16 |
cx_Oracle.TIMESTAMP: 'DateTimeField', |
|
|
17 |
} |
|
|
18 |
|
|
|
19 |
try: |
|
|
20 |
data_types_reverse[cx_Oracle.NATIVE_FLOAT] = 'FloatField' |
|
|
21 |
except AttributeError: |
|
|
22 |
pass |
|
|
23 |
|
|
|
24 |
try: |
|
|
25 |
data_types_reverse[cx_Oracle.UNICODE] = 'CharField' |
|
|
26 |
except AttributeError: |
|
|
27 |
pass |
|
|
28 |
|
|
|
29 |
def get_field_type(self, data_type, description): |
|
|
30 |
# If it's a NUMBER with scale == 0, consider it an IntegerField |
|
|
31 |
if data_type == cx_Oracle.NUMBER and description[5] == 0: |
|
29
|
32 |
if description[4] > 11: |
|
|
33 |
return 'BigIntegerField' |
|
|
34 |
else: |
|
|
35 |
return 'IntegerField' |
|
0
|
36 |
else: |
|
|
37 |
return super(DatabaseIntrospection, self).get_field_type( |
|
|
38 |
data_type, description) |
|
|
39 |
|
|
|
40 |
def get_table_list(self, cursor): |
|
|
41 |
"Returns a list of table names in the current database." |
|
|
42 |
cursor.execute("SELECT TABLE_NAME FROM USER_TABLES") |
|
|
43 |
return [row[0].lower() for row in cursor.fetchall()] |
|
|
44 |
|
|
|
45 |
def get_table_description(self, cursor, table_name): |
|
|
46 |
"Returns a description of the table, with the DB-API cursor.description interface." |
|
|
47 |
cursor.execute("SELECT * FROM %s WHERE ROWNUM < 2" % self.connection.ops.quote_name(table_name)) |
|
|
48 |
description = [] |
|
|
49 |
for desc in cursor.description: |
|
|
50 |
description.append((desc[0].lower(),) + desc[1:]) |
|
|
51 |
return description |
|
|
52 |
|
|
|
53 |
def table_name_converter(self, name): |
|
|
54 |
"Table name comparison is case insensitive under Oracle" |
|
|
55 |
return name.lower() |
|
|
56 |
|
|
|
57 |
def _name_to_index(self, cursor, table_name): |
|
|
58 |
""" |
|
|
59 |
Returns a dictionary of {field_name: field_index} for the given table. |
|
|
60 |
Indexes are 0-based. |
|
|
61 |
""" |
|
|
62 |
return dict([(d[0], i) for i, d in enumerate(self.get_table_description(cursor, table_name))]) |
|
|
63 |
|
|
|
64 |
def get_relations(self, cursor, table_name): |
|
|
65 |
""" |
|
|
66 |
Returns a dictionary of {field_index: (field_index_other_table, other_table)} |
|
|
67 |
representing all relationships to the given table. Indexes are 0-based. |
|
|
68 |
""" |
|
|
69 |
cursor.execute(""" |
|
|
70 |
SELECT ta.column_id - 1, tb.table_name, tb.column_id - 1 |
|
|
71 |
FROM user_constraints, USER_CONS_COLUMNS ca, USER_CONS_COLUMNS cb, |
|
|
72 |
user_tab_cols ta, user_tab_cols tb |
|
|
73 |
WHERE user_constraints.table_name = %s AND |
|
|
74 |
ta.table_name = %s AND |
|
|
75 |
ta.column_name = ca.column_name AND |
|
|
76 |
ca.table_name = %s AND |
|
|
77 |
user_constraints.constraint_name = ca.constraint_name AND |
|
|
78 |
user_constraints.r_constraint_name = cb.constraint_name AND |
|
|
79 |
cb.table_name = tb.table_name AND |
|
|
80 |
cb.column_name = tb.column_name AND |
|
|
81 |
ca.position = cb.position""", [table_name, table_name, table_name]) |
|
|
82 |
|
|
|
83 |
relations = {} |
|
|
84 |
for row in cursor.fetchall(): |
|
|
85 |
relations[row[0]] = (row[2], row[1]) |
|
|
86 |
return relations |
|
|
87 |
|
|
|
88 |
def get_indexes(self, cursor, table_name): |
|
|
89 |
""" |
|
|
90 |
Returns a dictionary of fieldname -> infodict for the given table, |
|
|
91 |
where each infodict is in the format: |
|
|
92 |
{'primary_key': boolean representing whether it's the primary key, |
|
|
93 |
'unique': boolean representing whether it's a unique index} |
|
|
94 |
""" |
|
|
95 |
# This query retrieves each index on the given table, including the |
|
|
96 |
# first associated field name |
|
|
97 |
# "We were in the nick of time; you were in great peril!" |
|
|
98 |
sql = """\ |
|
|
99 |
SELECT LOWER(all_tab_cols.column_name) AS column_name, |
|
|
100 |
CASE user_constraints.constraint_type |
|
|
101 |
WHEN 'P' THEN 1 ELSE 0 |
|
|
102 |
END AS is_primary_key, |
|
|
103 |
CASE user_indexes.uniqueness |
|
|
104 |
WHEN 'UNIQUE' THEN 1 ELSE 0 |
|
|
105 |
END AS is_unique |
|
|
106 |
FROM all_tab_cols, user_cons_columns, user_constraints, user_ind_columns, user_indexes |
|
|
107 |
WHERE all_tab_cols.column_name = user_cons_columns.column_name (+) |
|
|
108 |
AND all_tab_cols.table_name = user_cons_columns.table_name (+) |
|
|
109 |
AND user_cons_columns.constraint_name = user_constraints.constraint_name (+) |
|
|
110 |
AND user_constraints.constraint_type (+) = 'P' |
|
|
111 |
AND user_ind_columns.column_name (+) = all_tab_cols.column_name |
|
|
112 |
AND user_ind_columns.table_name (+) = all_tab_cols.table_name |
|
|
113 |
AND user_indexes.uniqueness (+) = 'UNIQUE' |
|
|
114 |
AND user_indexes.index_name (+) = user_ind_columns.index_name |
|
|
115 |
AND all_tab_cols.table_name = UPPER(%s) |
|
|
116 |
""" |
|
|
117 |
cursor.execute(sql, [table_name]) |
|
|
118 |
indexes = {} |
|
|
119 |
for row in cursor.fetchall(): |
|
|
120 |
indexes[row[0]] = {'primary_key': row[1], 'unique': row[2]} |
|
|
121 |
return indexes |