-
Notifications
You must be signed in to change notification settings - Fork 28
/
sqlalchemy_spanner.py
1008 lines (819 loc) · 31.7 KB
/
sqlalchemy_spanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pkg_resources
import re
from alembic.ddl.base import (
ColumnNullable,
ColumnType,
alter_column,
alter_table,
format_type,
)
from sqlalchemy import ForeignKeyConstraint, types, util
from sqlalchemy.engine.base import Engine
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.event import listens_for
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.pool import Pool
from sqlalchemy.sql.compiler import (
selectable,
DDLCompiler,
GenericTypeCompiler,
IdentifierPreparer,
SQLCompiler,
OPERATORS,
RESERVED_WORDS,
)
from sqlalchemy.sql.default_comparator import operator_lookup
from sqlalchemy.sql.operators import json_getitem_op
from google.cloud.spanner_v1.data_types import JsonObject
from google.cloud import spanner_dbapi
from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call
@listens_for(Pool, "reset")
def reset_connection(dbapi_conn, connection_record):
"""An event of returning a connection back to a pool."""
dbapi_conn.connection.staleness = None
# register a method to get a single value of a JSON object
OPERATORS[json_getitem_op] = operator_lookup["json_getitem_op"]
# Spanner-to-SQLAlchemy types map
_type_map = {
"BOOL": types.Boolean,
"BYTES": types.LargeBinary,
"DATE": types.DATE,
"DATETIME": types.DATETIME,
"FLOAT64": types.Float,
"INT64": types.BIGINT,
"NUMERIC": types.NUMERIC(precision=38, scale=9),
"STRING": types.String,
"TIME": types.TIME,
"TIMESTAMP": types.TIMESTAMP,
"ARRAY": types.ARRAY,
"JSON": types.JSON,
}
_type_map_inv = {
types.Boolean: "BOOL",
types.BINARY: "BYTES(MAX)",
types.LargeBinary: "BYTES(MAX)",
types.DATE: "DATE",
types.DATETIME: "DATETIME",
types.Float: "FLOAT64",
types.BIGINT: "INT64",
types.DECIMAL: "NUMERIC",
types.String: "STRING",
types.TIME: "TIME",
types.TIMESTAMP: "TIMESTAMP",
types.Integer: "INT64",
types.NullType: "INT64",
}
_compound_keywords = {
selectable.CompoundSelect.UNION: "UNION DISTINCT",
selectable.CompoundSelect.UNION_ALL: "UNION ALL",
selectable.CompoundSelect.EXCEPT: "EXCEPT DISTINCT",
selectable.CompoundSelect.EXCEPT_ALL: "EXCEPT ALL",
selectable.CompoundSelect.INTERSECT: "INTERSECT DISTINCT",
selectable.CompoundSelect.INTERSECT_ALL: "INTERSECT ALL",
}
_max_size = 2621440
def int_from_size(size_str):
"""Convert a string column length to an integer value.
Args:
size_str (str): The column length or the 'MAX' keyword.
Returns:
int: The column length value.
"""
return _max_size if size_str == "MAX" else int(size_str)
def engine_to_connection(function):
"""
Decorator to initiate a connection to a
database in case of an engine-related use.
"""
def wrapper(self, connection, *args, **kwargs):
"""
Args:
connection (Union[
sqlalchemy.engine.base.Connection,
sqlalchemy.engine.Engine
]):
SQLAlchemy connection or engine object.
"""
if isinstance(connection, Engine):
connection = connection.connect()
return function(self, connection, *args, **kwargs)
return wrapper
class SpannerExecutionContext(DefaultExecutionContext):
def pre_exec(self):
"""
Apply execution options to the DB API connection before
executing the next SQL operation.
"""
super(SpannerExecutionContext, self).pre_exec()
read_only = self.execution_options.get("read_only", None)
if read_only is not None:
self._dbapi_connection.connection.read_only = read_only
staleness = self.execution_options.get("staleness", None)
if staleness is not None:
self._dbapi_connection.connection.staleness = staleness
class SpannerIdentifierPreparer(IdentifierPreparer):
"""Identifiers compiler.
In Cloud Spanner backticks "`" are used for keywords escaping.
"""
reserved_words = RESERVED_WORDS.copy()
reserved_words.update(spanner_dbapi.parse_utils.SPANNER_RESERVED_KEYWORDS)
def __init__(self, dialect):
super(SpannerIdentifierPreparer, self).__init__(
dialect, initial_quote="`", final_quote="`"
)
def _requires_quotes(self, value):
"""Return True if the given identifier requires quoting."""
lc_value = value.lower()
if lc_value == '"unicode"': # don't escape default Spanner colation
return False
return (
lc_value in self.reserved_words
or value[0] in self.illegal_initial_characters
or not self.legal_characters.match(util.text_type(value))
or (lc_value != value)
)
class SpannerSQLCompiler(SQLCompiler):
"""Spanner SQL statements compiler."""
compound_keywords = _compound_keywords
def get_from_hint_text(self, _, text):
"""Return a hint text.
Overriden to avoid adding square brackets to the hint text.
Args:
text (str): The hint text.
"""
return text
def visit_empty_set_expr(self, type_):
"""Return an empty set expression of the given type.
Args:
type_ (sqlalchemy.sql.sqltypes.SchemaType):
A SQLAlchemy data type.
Returns:
str: A query to select an empty set of the given type.
"""
return "SELECT CAST(1 AS {}) FROM (SELECT 1) WHERE 1 != 1".format(
_type_map_inv[type(type_[0])]
)
def visit_like_op_binary(self, binary, operator, **kw):
"""Build a LIKE clause."""
if binary.modifiers.get("escape", None):
raise NotImplementedError("ESCAPE keyword is not supported by Spanner")
# TODO: use ternary here, not "and"/ "or"
return "%s LIKE %s" % (
binary.left._compiler_dispatch(self, **kw),
binary.right._compiler_dispatch(self, **kw),
)
def _generate_generic_binary(self, binary, opstring, eager_grouping=False, **kw):
"""The method is overriden to process JSON data type cases."""
_in_binary = kw.get("_in_binary", False)
kw["_in_binary"] = True
if isinstance(opstring, str):
text = (
binary.left._compiler_dispatch(
self, eager_grouping=eager_grouping, **kw
)
+ opstring
+ binary.right._compiler_dispatch(
self, eager_grouping=eager_grouping, **kw
)
)
if _in_binary and eager_grouping:
text = "(%s)" % text
else:
# got JSON data
right_value = getattr(
binary.right, "value", None
) or binary.right._compiler_dispatch(
self, eager_grouping=eager_grouping, **kw
)
text = (
binary.left._compiler_dispatch(
self, eager_grouping=eager_grouping, **kw
)
+ """, "$."""
+ str(right_value)
+ '"'
)
text = "JSON_VALUE(%s)" % text
return text
def visit_json_path_getitem_op_binary(self, binary, operator, **kw):
"""Build a JSON_VALUE() function call."""
expr = """JSON_VALUE(%s, "$.%s")"""
return expr % (
self.process(binary.left, **kw),
self.process(binary.right, **kw),
)
def render_literal_value(self, value, type_):
"""Render the value of a bind parameter as a quoted literal.
This is used for statement sections that do not accept bind parameters
on the target driver/database.
This should be implemented by subclasses using the quoting services
of the DBAPI.
Cloud spanner supports prefixed backslash to escape non-alphanumeric characters
in string. Override the method to add additional escape before using it to
generate a SQL statement.
"""
raw = ["\\", "'", '"', "\n", "\t", "\r"]
if type(value) == str and any(single in value for single in raw):
value = 'r"""{}"""'.format(value)
return value
else:
processor = type_._cached_literal_processor(self.dialect)
if processor:
return processor(value)
else:
raise NotImplementedError(
"Don't know how to literal-quote value %r" % value
)
def limit_clause(self, select, **kw):
"""Build LIMIT-OFFSET clause.
Spanner doesn't support using OFFSET without a LIMIT
clause. It also doesn't support negative LIMITs, while
SQLAlchemy support both.
The method builds LIMIT-OFFSET clauses as usual, with
only difference: when OFFSET is used without an explicit
LIMIT, the dialect compiles a statement with a LIMIT
set to the biggest integer value.
Args:
(sqlalchemy.sql.selectable.Select): Select clause object.
Returns:
str: LIMIT-OFFSET clause.
"""
text = ""
if select._limit_clause is not None:
text += "\n LIMIT " + self.process(select._limit_clause, **kw)
if select._offset_clause is not None:
if select._limit_clause is None:
text += f"\n LIMIT {9223372036854775807-select._offset}"
text += " OFFSET " + self.process(select._offset_clause, **kw)
return text
class SpannerDDLCompiler(DDLCompiler):
"""Spanner DDL statements compiler."""
def visit_computed_column(self, generated, **kw):
"""Computed column operator."""
text = "AS (%s) STORED" % self.sql_compiler.process(
generated.sqltext, include_table=False, literal_binds=True
)
return text
def visit_drop_table(self, drop_table):
"""
Cloud Spanner doesn't drop tables which have indexes
or foreign key constraints. This method builds several DDL
statements separated by semicolons to drop the indexes and
foreign keys constraints of the table before the DROP TABLE
statement.
Args:
(sqlalchemy.schema.DropTable): DROP TABLE statement object.
Returns:
str:
DDL statements separated by semicolons, which will
sequentially drop indexes, foreign keys constraints
and then the table itself.
"""
constrs = ""
for cons in drop_table.element.constraints:
if isinstance(cons, ForeignKeyConstraint) and cons.name:
constrs += "ALTER TABLE {table} DROP CONSTRAINT {constr};".format(
table=drop_table.element.name,
constr=self.preparer.quote(cons.name),
)
indexes = ""
for index in drop_table.element.indexes:
indexes += "DROP INDEX {};".format(self.preparer.quote(index.name))
return indexes + constrs + str(drop_table)
def visit_primary_key_constraint(self, constraint):
"""Build primary key definition.
Primary key in Spanner is defined outside of a table columns definition, see:
https://cloud.google.com/spanner/docs/getting-started/python#create_a_database
The method returns None to omit primary key in a table columns definition.
"""
return None
def visit_unique_constraint(self, constraint):
"""Unique constraints in Spanner are defined with indexes:
https://cloud.google.com/spanner/docs/secondary-indexes#unique-indexes
The method throws an exception to notify user that in
Spanner unique constraints are done with unique indexes.
"""
raise spanner_dbapi.exceptions.ProgrammingError(
"Spanner doesn't support direct UNIQUE constraints creation. "
"Create UNIQUE indexes instead."
)
def post_create_table(self, table):
"""Build statements to be executed after CREATE TABLE.
Includes "primary key" and "interleaved table" statements generation.
Args:
table (sqlalchemy.schema.Table): Table to create.
Returns:
str: primary key difinition to add to the table CREATE request.
"""
cols = [col.name for col in table.primary_key.columns]
post_cmds = " PRIMARY KEY ({})".format(", ".join(cols))
if "TEMPORARY" in table._prefixes:
raise NotImplementedError("Temporary tables are not supported.")
if table.kwargs.get("spanner_interleave_in"):
post_cmds += ",\nINTERLEAVE IN PARENT {}".format(
table.kwargs["spanner_interleave_in"]
)
if table.kwargs.get("spanner_interleave_on_delete_cascade"):
post_cmds += " ON DELETE CASCADE"
return post_cmds
class SpannerTypeCompiler(GenericTypeCompiler):
"""Spanner types compiler.
Maps SQLAlchemy types to Spanner data types.
"""
def visit_INTEGER(self, type_, **kw):
return "INT64"
def visit_FLOAT(self, type_, **kw):
return "FLOAT64"
def visit_TEXT(self, type_, **kw):
return "STRING({})".format(type_.length or "MAX")
def visit_ARRAY(self, type_, **kw):
return "ARRAY<{}>".format(self.process(type_.item_type, **kw))
def visit_BINARY(self, type_, **kw): # pragma: no cover
"""
The BINARY type is superseded by large_binary in
newer versions of SQLAlchemy (>1.4).
"""
return "BYTES({})".format(type_.length or "MAX")
def visit_large_binary(self, type_, **kw): # pragma: no cover
return "BYTES({})".format(type_.length or "MAX")
def visit_DECIMAL(self, type_, **kw):
return "NUMERIC"
def visit_VARCHAR(self, type_, **kw):
return "STRING({})".format(type_.length or "MAX")
def visit_CHAR(self, type_, **kw):
return "STRING({})".format(type_.length or "MAX")
def visit_BOOLEAN(self, type_, **kw):
return "BOOL"
def visit_DATETIME(self, type_, **kw):
return "TIMESTAMP"
def visit_NUMERIC(self, type_, **kw):
return "NUMERIC"
def visit_BIGINT(self, type_, **kw):
return "INT64"
def visit_JSON(self, type_, **kw):
return "JSON"
class SpannerDialect(DefaultDialect):
"""Cloud Spanner dialect.
Represents an API layer to control Cloud Spanner database with SQLAlchemy API.
"""
name = "spanner"
driver = "spanner"
positional = False
paramstyle = "format"
encoding = "utf-8"
max_identifier_length = 128
execute_sequence_format = list
supports_alter = True
supports_sane_rowcount = False
supports_sane_multi_rowcount = False
supports_default_values = False
supports_sequences = True
supports_native_enum = True
supports_native_boolean = True
supports_native_decimal = True
ddl_compiler = SpannerDDLCompiler
preparer = SpannerIdentifierPreparer
statement_compiler = SpannerSQLCompiler
type_compiler = SpannerTypeCompiler
execution_ctx_cls = SpannerExecutionContext
_json_serializer = JsonObject
_json_deserializer = JsonObject
@classmethod
def dbapi(cls):
"""A pointer to the Cloud Spanner DB API package.
Used to initiate connections to the Cloud Spanner databases.
"""
return spanner_dbapi
@property
def default_isolation_level(self):
"""Default isolation level name.
Returns:
str: default isolation level.
"""
return "SERIALIZABLE"
@default_isolation_level.setter
def default_isolation_level(self, value):
"""Default isolation level should not be changed."""
pass
def _check_unicode_returns(self, connection, additional_tests=None):
"""Ensure requests are returning Unicode responses."""
return True
def _get_default_schema_name(self, _):
"""Get default Cloud Spanner schema name.
Returns:
str: Schema name.
"""
return ""
def create_connect_args(self, url):
"""Parse connection args from the given URL.
The method prepares args suitable to send to the DB API `connect()` function.
The given URL follows the style:
`spanner:///projects/{project-id}/instances/{instance-id}/databases/{database-id}`
"""
match = re.match(
(
r"^projects/(?P<project>.+?)/instances/"
"(?P<instance>.+?)/databases/(?P<database>.+?)$"
),
url.database,
)
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
return (
[match.group("instance"), match.group("database"), match.group("project")],
{"user_agent": dist.project_name + "/" + dist.version},
)
@engine_to_connection
def get_columns(self, connection, table_name, schema=None, **kw):
"""Get the table columns description.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name
Returns:
list: The table every column dict-like description.
"""
sql = """
SELECT column_name, spanner_type, is_nullable, generation_expression
FROM information_schema.columns
WHERE
table_catalog = ''
AND table_schema = ''
AND table_name = '{}'
ORDER BY
table_catalog,
table_schema,
table_name,
ordinal_position
""".format(
table_name
)
cols_desc = []
with connection.connection.database.snapshot() as snap:
columns = snap.execute_sql(sql)
for col in columns:
col_desc = {
"name": col[0],
"type": self._designate_type(col[1]),
"nullable": col[2] == "YES",
"default": None,
}
if col[3] is not None:
col_desc["computed"] = {
"persisted": True,
"sqltext": col[3],
}
cols_desc.append(col_desc)
return cols_desc
def _designate_type(self, str_repr):
"""
Designate an SQLAlchemy data type from a Spanner
string representation.
Args:
str_repr (str): String representation of a type.
Returns:
An SQLAlchemy data type.
"""
if str_repr.startswith("STRING"):
end = str_repr.index(")")
size = int_from_size(str_repr[7:end])
return _type_map["STRING"](length=size)
# add test creating a table with bytes
elif str_repr.startswith("BYTES"):
end = str_repr.index(")")
size = int_from_size(str_repr[6:end])
return _type_map["BYTES"](length=size)
elif str_repr.startswith("ARRAY"):
inner_type_str = str_repr[6:-1]
inner_type = self._designate_type(inner_type_str)
return _type_map["ARRAY"](inner_type)
else:
return _type_map[str_repr]
@engine_to_connection
def get_indexes(self, connection, table_name, schema=None, **kw):
"""Get the table indexes.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name
Returns:
list: List with indexes description.
"""
sql = """
SELECT
i.index_name,
ARRAY_AGG(ic.column_name),
i.is_unique,
ARRAY_AGG(ic.column_ordering)
FROM information_schema.indexes as i
JOIN information_schema.index_columns AS ic
ON ic.index_name = i.index_name AND ic.table_name = i.table_name
WHERE
i.table_name="{table_name}"
AND i.index_type != 'PRIMARY_KEY'
GROUP BY i.index_name, i.is_unique
""".format(
table_name=table_name
)
ind_desc = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(sql)
for row in rows:
ind_desc.append(
{
"name": row[0],
"column_names": row[1],
"unique": row[2],
"column_sorting": {
col: order for col, order in zip(row[1], row[3])
},
}
)
return ind_desc
@engine_to_connection
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
"""Get the table primary key constraint.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name
Returns:
dict: Dict with the primary key constraint description.
"""
sql = """
SELECT ccu.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE tc.TABLE_NAME="{table_name}" AND tc.CONSTRAINT_TYPE = "PRIMARY KEY"
""".format(
table_name=table_name
)
cols = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(sql)
for row in rows:
cols.append(row[0])
return {"constrained_columns": cols}
@engine_to_connection
def get_schema_names(self, connection, **kw):
"""Get all the schemas in the database.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
Returns:
list: Schema names.
"""
schemas = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(
"SELECT schema_name FROM information_schema.schemata"
)
for row in rows:
schemas.append(row[0])
return schemas
@engine_to_connection
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
"""Get the table foreign key constraints.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name
Returns:
list: Dicts, each of which describes a foreign key constraint.
"""
sql = """
SELECT
tc.constraint_name,
ctu.table_name,
ctu.table_schema,
ARRAY_AGG(DISTINCT ccu.column_name),
ARRAY_AGG(kcu.column_name)
FROM information_schema.table_constraints AS tc
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
JOIN information_schema.constraint_table_usage AS ctu
ON ctu.constraint_name = tc.constraint_name
JOIN information_schema.key_column_usage AS kcu
ON kcu.constraint_name = tc.constraint_name
WHERE
tc.table_name="{table_name}"
AND tc.constraint_type = "FOREIGN KEY"
GROUP BY tc.constraint_name, ctu.table_name, ctu.table_schema
""".format(
table_name=table_name
)
keys = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(sql)
for row in rows:
keys.append(
{
"name": row[0],
"referred_table": row[1],
"referred_schema": row[2] or None,
"referred_columns": row[3],
"constrained_columns": row[4],
}
)
return keys
@engine_to_connection
def get_table_names(self, connection, schema=None, **kw):
"""Get all the tables from the given schema.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
schema (str): Optional. Schema name.
Returns:
list: Names of the tables within the given schema.
"""
sql = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '{}'
""".format(
schema or ""
)
table_names = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(sql)
for row in rows:
table_names.append(row[0])
return table_names
@engine_to_connection
def get_unique_constraints(self, connection, table_name, schema=None, **kw):
"""Get the table unique constraints.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name
Returns:
dict: Dict with the unique constraints' descriptions.
"""
sql = """
SELECT ccu.CONSTRAINT_NAME, ccu.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu
ON ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
WHERE tc.TABLE_NAME="{table_name}" AND tc.CONSTRAINT_TYPE = "UNIQUE"
""".format(
table_name=table_name
)
cols = []
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(sql)
for row in rows:
cols.append({"name": row[0], "column_names": [row[1]]})
return cols
@engine_to_connection
def has_table(self, connection, table_name, schema=None):
"""Check if the given table exists.
The method is used by SQLAlchemy introspection systems.
Args:
connection (sqlalchemy.engine.base.Connection):
SQLAlchemy connection or engine object.
table_name (str): Name of the table to introspect.
schema (str): Optional. Schema name.
Returns:
bool: True, if the given table exists, False otherwise.
"""
with connection.connection.database.snapshot() as snap:
rows = snap.execute_sql(
"""
SELECT true
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME="{table_name}"
LIMIT 1
""".format(
table_name=table_name
)
)
for _ in rows:
return True
return False
def set_isolation_level(self, conn_proxy, level):
"""Set the connection isolation level.
Args:
conn_proxy (
Union[
sqlalchemy.pool._ConnectionFairy,
spanner_dbapi.connection.Connection,
]
):
Database connection proxy object or the connection iself.
level (string): Isolation level.
"""
if isinstance(conn_proxy, spanner_dbapi.Connection):
conn = conn_proxy
else:
conn = conn_proxy.connection
conn.autocommit = level == "AUTOCOMMIT"
def get_isolation_level(self, conn_proxy):
"""Get the connection isolation level.
Args:
conn_proxy (
Union[
sqlalchemy.pool._ConnectionFairy,
spanner_dbapi.connection.Connection,
]
):
Database connection proxy object or the connection iself.
Returns:
str: the connection isolation level.
"""
if isinstance(conn_proxy, spanner_dbapi.Connection):
conn = conn_proxy
else:
conn = conn_proxy.connection
return "AUTOCOMMIT" if conn.autocommit else "SERIALIZABLE"
def do_rollback(self, dbapi_connection):
"""
To prevent rollback exception, don't rollback
committed/rolled back transactions.
"""
if not isinstance(dbapi_connection, spanner_dbapi.Connection):
dbapi_connection = dbapi_connection.connection
if dbapi_connection._transaction and (
dbapi_connection._transaction.rolled_back
or dbapi_connection._transaction.committed
):
pass
else:
trace_attributes = {"db.instance": dbapi_connection.database.name}
with trace_call("SpannerSqlAlchemy.Rollback", trace_attributes):
dbapi_connection.rollback()
def do_commit(self, dbapi_connection):
trace_attributes = {"db.instance": dbapi_connection.database.name}
with trace_call("SpannerSqlAlchemy.Commit", trace_attributes):
dbapi_connection.commit()
def do_close(self, dbapi_connection):
trace_attributes = {"db.instance": dbapi_connection.database.name}
with trace_call("SpannerSqlAlchemy.Close", trace_attributes):
dbapi_connection.close()
def do_executemany(self, cursor, statement, parameters, context=None):
trace_attributes = {
"db.statement": statement,
"db.params": parameters,
"db.instance": cursor.connection.database.name,
}
with trace_call("SpannerSqlAlchemy.ExecuteMany", trace_attributes):
cursor.executemany(statement, parameters)
def do_execute(self, cursor, statement, parameters, context=None):
trace_attributes = {
"db.statement": statement,
"db.params": parameters,
"db.instance": cursor.connection.database.name,
}
with trace_call("SpannerSqlAlchemy.Execute", trace_attributes):
cursor.execute(statement, parameters)
def do_execute_no_params(self, cursor, statement, context=None):
trace_attributes = {
"db.statement": statement,
"db.instance": cursor.connection.database.name,
}
with trace_call("SpannerSqlAlchemy.ExecuteNoParams", trace_attributes):
cursor.execute(statement)
# Alembic ALTER operation override
@compiles(ColumnNullable, "spanner")
def visit_column_nullable(
element: "ColumnNullable", compiler: "SpannerDDLCompiler", **kw
) -> str:
return "%s %s %s %s" % (
alter_table(compiler, element.table_name, element.schema),
alter_column(compiler, element.column_name),
format_type(compiler, element.existing_type),
"" if element.nullable else "NOT NULL",
)
# Alembic ALTER operation override
@compiles(ColumnType, "spanner")