3434from google .cloud .spanner_admin_database_v1 import RestoreDatabaseEncryptionConfig
3535from google .cloud .spanner_admin_database_v1 import RestoreDatabaseRequest
3636from google .cloud .spanner_admin_database_v1 import UpdateDatabaseDdlRequest
37+ from google .cloud .spanner_admin_database_v1 .types import DatabaseDialect
3738from google .cloud .spanner_v1 import ExecuteSqlRequest
3839from google .cloud .spanner_v1 import TransactionSelector
3940from google .cloud .spanner_v1 import TransactionOptions
6869
6970_LIST_TABLES_QUERY = """SELECT TABLE_NAME
7071FROM INFORMATION_SCHEMA.TABLES
71- WHERE SPANNER_STATE = 'COMMITTED'
72+ {}
7273"""
7374
7475DEFAULT_RETRY_BACKOFF = Retry (initial = 0.02 , maximum = 32 , multiplier = 1.3 )
@@ -114,6 +115,11 @@ class Database(object):
114115 If a dict is provided, it must be of the same form as either of the protobuf
115116 messages :class:`~google.cloud.spanner_admin_database_v1.types.EncryptionConfig`
116117 or :class:`~google.cloud.spanner_admin_database_v1.types.RestoreDatabaseEncryptionConfig`
118+ :type database_dialect:
119+ :class:`~google.cloud.spanner_admin_database_v1.types.DatabaseDialect`
120+ :param database_dialect:
121+ (Optional) database dialect for the database
122+
117123 """
118124
119125 _spanner_api = None
@@ -126,6 +132,7 @@ def __init__(
126132 pool = None ,
127133 logger = None ,
128134 encryption_config = None ,
135+ database_dialect = DatabaseDialect .DATABASE_DIALECT_UNSPECIFIED ,
129136 ):
130137 self .database_id = database_id
131138 self ._instance = instance
@@ -141,6 +148,7 @@ def __init__(
141148 self .log_commit_stats = False
142149 self ._logger = logger
143150 self ._encryption_config = encryption_config
151+ self ._database_dialect = database_dialect
144152
145153 if pool is None :
146154 pool = BurstyPool ()
@@ -294,6 +302,18 @@ def ddl_statements(self):
294302 """
295303 return self ._ddl_statements
296304
305+ @property
306+ def database_dialect (self ):
307+ """DDL Statements used to define database schema.
308+
309+ See
310+ cloud.google.com/spanner/docs/data-definition-language
311+
312+ :rtype: :class:`google.cloud.spanner_admin_database_v1.types.DatabaseDialect`
313+ :returns: the dialect of the database
314+ """
315+ return self ._database_dialect
316+
297317 @property
298318 def logger (self ):
299319 """Logger used by the database.
@@ -364,7 +384,10 @@ def create(self):
364384 metadata = _metadata_with_prefix (self .name )
365385 db_name = self .database_id
366386 if "-" in db_name :
367- db_name = "`%s`" % (db_name ,)
387+ if self ._database_dialect == DatabaseDialect .POSTGRESQL :
388+ db_name = f'"{ db_name } "'
389+ else :
390+ db_name = f"`{ db_name } `"
368391 if type (self ._encryption_config ) == dict :
369392 self ._encryption_config = EncryptionConfig (** self ._encryption_config )
370393
@@ -373,6 +396,7 @@ def create(self):
373396 create_statement = "CREATE DATABASE %s" % (db_name ,),
374397 extra_statements = list (self ._ddl_statements ),
375398 encryption_config = self ._encryption_config ,
399+ database_dialect = self ._database_dialect ,
376400 )
377401 future = api .create_database (request = request , metadata = metadata )
378402 return future
@@ -418,6 +442,7 @@ def reload(self):
418442 self ._encryption_config = response .encryption_config
419443 self ._encryption_info = response .encryption_info
420444 self ._default_leader = response .default_leader
445+ self ._database_dialect = response .database_dialect
421446
422447 def update_ddl (self , ddl_statements , operation_id = "" ):
423448 """Update DDL for this database.
@@ -778,7 +803,11 @@ def list_tables(self):
778803 resources within the current database.
779804 """
780805 with self .snapshot () as snapshot :
781- results = snapshot .execute_sql (_LIST_TABLES_QUERY )
806+ if self ._database_dialect == DatabaseDialect .POSTGRESQL :
807+ where_clause = "WHERE TABLE_SCHEMA = 'public'"
808+ else :
809+ where_clause = "WHERE SPANNER_STATE = 'COMMITTED'"
810+ results = snapshot .execute_sql (_LIST_TABLES_QUERY .format (where_clause ))
782811 for row in results :
783812 yield self .table (row [0 ])
784813
0 commit comments