Skip to content

Commit b648004

Browse files
ankiagagcf-owl-bot[bot]olavloite
authored
chore: Add support for named schema (#858)
* chore: Add support for named schema * Fix * Comments addressed * Comments addressed * Comments addressed * Comments addressed * Removing quote method for table name * Fix * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update django_spanner/introspection.py Co-authored-by: Knut Olav Løite <[email protected]> --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Knut Olav Løite <[email protected]>
1 parent b4dcd86 commit b648004

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

django_spanner/introspection.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
2929
TypeCode.NUMERIC: "DecimalField",
3030
TypeCode.JSON: "JSONField",
3131
}
32-
if USE_EMULATOR:
33-
# Emulator does not support table_type yet.
34-
# https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/issues/43
35-
LIST_TABLE_SQL = """
36-
SELECT
37-
t.table_name, t.table_name
38-
FROM
39-
information_schema.tables AS t
40-
WHERE
41-
t.table_catalog = '' and t.table_schema = ''
42-
"""
43-
else:
44-
LIST_TABLE_SQL = """
45-
SELECT
46-
t.table_name, t.table_type
47-
FROM
48-
information_schema.tables AS t
49-
WHERE
50-
t.table_catalog = '' and t.table_schema = ''
51-
"""
32+
LIST_TABLE_SQL = """
33+
SELECT
34+
t.table_name, t.table_type
35+
FROM
36+
information_schema.tables AS t
37+
WHERE
38+
t.table_catalog = '' and t.table_schema = @schema_name
39+
"""
5240

5341
def get_field_type(self, data_type, description):
5442
"""A hook for a Spanner database to use the cursor description to
@@ -76,7 +64,10 @@ def get_table_list(self, cursor):
7664
:rtype: list
7765
:returns: A list of table and view names in the current database.
7866
"""
79-
results = cursor.run_sql_in_snapshot(self.LIST_TABLE_SQL)
67+
schema_name = self._get_schema_name(cursor)
68+
results = cursor.run_sql_in_snapshot(
69+
self.LIST_TABLE_SQL, params={"schema_name": schema_name}
70+
)
8071
tables = []
8172
# The second TableInfo field is 't' for table or 'v' for view.
8273
for row in results:
@@ -159,8 +150,9 @@ def get_relations(self, cursor, table_name):
159150
:rtype: dict
160151
:returns: A dictionary representing column relationships to other tables.
161152
"""
153+
schema_name = self._get_schema_name(cursor)
162154
results = cursor.run_sql_in_snapshot(
163-
'''
155+
"""
164156
SELECT
165157
tc.COLUMN_NAME as col, ccu.COLUMN_NAME as ref_col, ccu.TABLE_NAME as ref_table
166158
FROM
@@ -174,8 +166,9 @@ def get_relations(self, cursor, table_name):
174166
ON
175167
rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME
176168
WHERE
177-
tc.TABLE_NAME="%s"'''
178-
% self.connection.ops.quote_name(table_name)
169+
tc.TABLE_SCHEMA=@schema_name AND tc.TABLE_NAME=@view_name
170+
""",
171+
params={"schema_name": schema_name, "view_name": table_name},
179172
)
180173
return {
181174
column: (referred_column, referred_table)
@@ -194,6 +187,7 @@ def get_primary_key_column(self, cursor, table_name):
194187
:rtype: str
195188
:returns: The name of the PK column.
196189
"""
190+
schema_name = self._get_schema_name(cursor)
197191
results = cursor.run_sql_in_snapshot(
198192
"""
199193
SELECT
@@ -205,9 +199,9 @@ def get_primary_key_column(self, cursor, table_name):
205199
AS
206200
ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME
207201
WHERE
208-
tc.TABLE_NAME="%s" AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=''
209-
"""
210-
% self.connection.ops.quote_name(table_name)
202+
tc.TABLE_NAME=@table_name AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND tc.TABLE_SCHEMA=@schema_name
203+
""",
204+
params={"schema_name": schema_name, "table_name": table_name},
211205
)
212206
return results[0][0] if results else None
213207

@@ -224,18 +218,17 @@ def get_constraints(self, cursor, table_name):
224218
:returns: A dictionary with constraints.
225219
"""
226220
constraints = {}
227-
quoted_table_name = self.connection.ops.quote_name(table_name)
221+
schema_name = self._get_schema_name(cursor)
228222

229223
# Firstly populate all available constraints and their columns.
230224
constraint_columns = cursor.run_sql_in_snapshot(
231-
'''
225+
"""
232226
SELECT
233227
CONSTRAINT_NAME, COLUMN_NAME
234228
FROM
235229
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
236-
WHERE TABLE_NAME="{table}"'''.format(
237-
table=quoted_table_name
238-
)
230+
WHERE TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name""",
231+
params={"table": table_name, "schema_name": schema_name},
239232
)
240233
for constraint, column_name in constraint_columns:
241234
if constraint not in constraints:
@@ -254,15 +247,14 @@ def get_constraints(self, cursor, table_name):
254247

255248
# Add the various constraints by type.
256249
constraint_types = cursor.run_sql_in_snapshot(
257-
'''
250+
"""
258251
SELECT
259252
CONSTRAINT_NAME, CONSTRAINT_TYPE
260253
FROM
261254
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
262255
WHERE
263-
TABLE_NAME="{table}"'''.format(
264-
table=quoted_table_name
265-
)
256+
TABLE_NAME=@table AND TABLE_SCHEMA=@schema_name""",
257+
params={"table": table_name, "schema_name": schema_name},
266258
)
267259
for constraint, constraint_type in constraint_types:
268260
already_added = constraint in constraints
@@ -303,14 +295,13 @@ def get_constraints(self, cursor, table_name):
303295
RIGHT JOIN
304296
INFORMATION_SCHEMA.INDEX_COLUMNS AS idx_col
305297
ON
306-
idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME="{table}"
298+
idx_col.INDEX_NAME = idx.INDEX_NAME AND idx_col.TABLE_NAME=@table AND idx_col.TABLE_SCHEMA=idx.TABLE_SCHEMA
307299
WHERE
308-
idx.TABLE_NAME="{table}"
300+
idx.TABLE_NAME=@table AND idx.TABLE_SCHEMA=@schema_name
309301
ORDER BY
310302
idx_col.ORDINAL_POSITION
311-
""".format(
312-
table=quoted_table_name
313-
)
303+
""",
304+
params={"table": table_name, "schema_name": schema_name},
314305
)
315306
for (
316307
index_name,
@@ -350,6 +341,7 @@ def get_key_columns(self, cursor, table_name):
350341
for all key columns in the given table.
351342
"""
352343
key_columns = []
344+
schema_name = self._get_schema_name(cursor)
353345
cursor.execute(
354346
"""SELECT
355347
tc.COLUMN_NAME as column_name,
@@ -366,10 +358,12 @@ def get_key_columns(self, cursor, table_name):
366358
ON
367359
rc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME
368360
WHERE
369-
tc.TABLE_NAME="{table}"
370-
""".format(
371-
table=self.connection.ops.quote_name(table_name)
372-
)
361+
tc.TABLE_NAME=@table AND tc.TABLE_SCHEMA=@schema_name
362+
""",
363+
params={"table": table_name, "schema_name": schema_name},
373364
)
374365
key_columns.extend(cursor.fetchall())
375366
return key_columns
367+
368+
def _get_schema_name(self, cursor):
369+
return cursor.connection.current_schema

0 commit comments

Comments
 (0)