Skip to content

Commit 024d2ec

Browse files
committed
support for non sqluser tables
1 parent 736b7c7 commit 024d2ec

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

django_iris/introspection.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
'FieldInfo', BaseFieldInfo._fields + ('auto_increment', ))
1111

1212

13+
def schema_name(table_name):
14+
table_schema = 'SQLUser'
15+
if '.' in table_name:
16+
[table_schema, table_name] = table_name.split('.')
17+
return [table_schema, table_name, ]
18+
19+
1320
class DatabaseIntrospection(BaseDatabaseIntrospection):
1421
data_types_reverse = {
1522
'bigint': 'BigIntegerField',
@@ -48,11 +55,12 @@ def get_sequences(self, cursor, table_name, table_fields=()):
4855

4956
def get_table_list(self, cursor):
5057
cursor.execute("""
51-
SELECT TABLE_NAME
58+
SELECT TABLE_SCHEMA,TABLE_NAME
5259
FROM information_schema.tables
53-
WHERE TABLE_SCHEMA = 'SQLUser'
60+
WHERE TABLE_TYPE = 'BASE TABLE'
61+
AND NOT (TABLE_SCHEMA %STARTSWITH 'Ens')
5462
""")
55-
return [TableInfo(row[0], 't') for row in cursor.fetchall()]
63+
return [TableInfo(row[1] if row[0] == 'SQLUser' else row[0] + '.' + row[1], 't') for row in cursor.fetchall()]
5664

5765
def get_table_description(self, cursor, table_name):
5866
"""
@@ -74,7 +82,7 @@ def get_table_description(self, cursor, table_name):
7482
AND TABLE_NAME = %s
7583
ORDER BY ORDINAL_POSITION
7684
""",
77-
['SQLUser', table_name]
85+
schema_name(table_name)
7886
)
7987

8088
description = [
@@ -101,6 +109,7 @@ def get_relations(self, cursor, table_name):
101109
Return a dictionary of {field_name: (field_name_other_table, other_table)}
102110
representing all relationships to the given table.
103111
"""
112+
104113
# Dictionary of relations to return
105114
cursor.execute("""
106115
SELECT column_name, referenced_column_name, referenced_table_name
@@ -109,7 +118,7 @@ def get_relations(self, cursor, table_name):
109118
AND table_name = %s
110119
AND referenced_table_name IS NOT NULL
111120
AND referenced_column_name IS NOT NULL
112-
""", ['SQLUser', table_name])
121+
""", schema_name(table_name))
113122
return {
114123
field_name: (other_field, other_table)
115124
for field_name, other_field, other_table in cursor.fetchall()
@@ -151,13 +160,13 @@ def get_constraints(self, cursor, table_name):
151160
information_schema.table_constraints AS c
152161
WHERE
153162
kc.table_schema = %s AND
163+
kc.table_name = %s AND
154164
c.table_schema = kc.table_schema AND
155165
c.constraint_name = kc.constraint_name AND
156-
c.constraint_type != 'CHECK' AND
157-
kc.table_name = %s
166+
c.constraint_type != 'CHECK'
158167
ORDER BY kc.ordinal_position
159168
"""
160-
cursor.execute(name_query, ['SQLUser', table_name])
169+
cursor.execute(name_query, schema_name(table_name))
161170
for constraint, column, ref_table, ref_column, kind in cursor.fetchall():
162171
if constraint not in constraints:
163172
constraints[constraint] = {
@@ -188,7 +197,7 @@ def get_constraints(self, cursor, table_name):
188197
tc.constraint_type = 'CHECK' AND
189198
tc.table_name = %s
190199
"""
191-
cursor.execute(type_query, ['SQLUser', table_name])
200+
cursor.execute(type_query, schema_name(table_name))
192201
for constraint, check_clause in cursor.fetchall():
193202
constraint_columns = self._parse_constraint_columns(
194203
check_clause, columns)
@@ -219,7 +228,7 @@ def get_constraints(self, cursor, table_name):
219228
AND TABLE_NAME = %s
220229
ORDER BY ORDINAL_POSITION
221230
"""
222-
cursor.execute(index_query, ['SQLUser', table_name])
231+
cursor.execute(index_query, schema_name(table_name))
223232
for index, column, primary, non_unique, order in cursor.fetchall():
224233
if index not in constraints:
225234
constraints[index] = {

0 commit comments

Comments
 (0)