Python client library  |  Google Cloud (original) (raw)

Database API

User friendly container for Cloud Spanner Database.

class google.cloud.spanner_v1.database.BatchCheckout(database, request_options=None)

Bases: object

Context manager for using a batch from a database.

Inside the context manager, checks out a session from the database, creates a batch from it, making the batch available.

Caller must not use the batch to perform API requests outside the scope of the context manager.

_ enter_()

Begin with block.

_ exit_(exc_type, exc_val, exc_tb)

End with block.

class google.cloud.spanner_v1.database.BatchSnapshot(database, read_timestamp=None, exact_staleness=None)

Bases: object

Wrapper for generating and processing read / query batches.

close()

Clean up underlying session.

NOTE: If the transaction has been shared across multiple machines, calling this on any machine would invalidate the transaction everywhere. Ideally this would be called when data has been read from all the partitions.

execute_sql(*args, **kw)

Convenience method: perform query operation via snapshot.

See execute_sql().

classmethod from_dict(database, mapping)

Reconstruct an instance from a mapping.

generate_query_batches(sql, params=None, param_types=None, partition_size_bytes=None, max_partitions=None, query_options=None, data_boost_enabled=False, *, retry=<_MethodDefault._DEFAULT_VALUE:

Start a partitioned query operation.

Uses the PartitionQuery API request to start a partitioned query operation. Returns a list of batch information needed to perform the actual queries.

generate_read_batches(table, columns, keyset, index='', partition_size_bytes=None, max_partitions=None, data_boost_enabled=False, *, retry=<_MethodDefault._DEFAULT_VALUE:

Start a partitioned batch read operation.

Uses the PartitionRead API request to initiate the partitioned read. Returns a list of batch information needed to perform the actual reads.

process(batch)

Process a single, partitioned query or read.

process_query_batch(batch, *, retry=<_MethodDefault._DEFAULT_VALUE:

Process a single, partitioned query.

process_read_batch(batch, *, retry=<_MethodDefault._DEFAULT_VALUE:

Process a single, partitioned read.

read(*args, **kw)

Convenience method: perform read operation via snapshot.

See read().

to_dict()

Return state as a dictionary.

Result can be used to serialize the instance and reconstitute it later using from_dict().

class google.cloud.spanner_v1.database.Database(database_id, instance, ddl_statements=(), pool=None, logger=None, encryption_config=None, database_dialect=<DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED: 0>, database_role=None, enable_drop_protection=False)

Bases: object

Representation of a Cloud Spanner Database.

We can use a Database to:

batch(request_options=None)

Return an object which wraps a batch.

The wrapper must be used as a context manager, with the batch as the value returned by the wrapper.

batch_snapshot(read_timestamp=None, exact_staleness=None)

Return an object which wraps a batch read / query.

create()

Create this database within its instance

Includes any configured schema assigned to ddl_statements.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.CreateDatabase

property create_time()

Create time of this database.

property database_dialect()

DDL Statements used to define database schema.

See cloud.google.com/spanner/docs/data-definition-language

property database_role()

User-assigned database_role for sessions created by the pool. :rtype: str :returns: a str with the name of the database role.

property ddl_statements()

DDL Statements used to define database schema.

See cloud.google.com/spanner/docs/data-definition-language

property default_leader()

The read-write region which contains the database’s leader replicas.

drop()

Drop this database.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.DropDatabase

property earliest_version_time()

The earliest time at which older versions of the data can be read.

property enable_drop_protection()

Whether the database has drop protection enabled.

property encryption_config()

Encryption config for this database. :rtype: EncryptionConfig:returns: an object representing the encryption config for this database

property encryption_info()

Encryption info for this database. :rtype: a list of EncryptionInfo:returns: a list of objects representing encryption info for this database

execute_partitioned_dml(dml, params=None, param_types=None, query_options=None, request_options=None)

Execute a partitionable DML statement.

exists()

Test whether this database exists.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.GetDatabaseDDL

classmethod from_pb(database_pb, instance, pool=None)

Creates an instance of this class from a protobuf.

get_iam_policy(policy_version=None)

Gets the access control policy for a database resource.

is_optimized()

Test whether this database has finished optimizing.

is_ready()

Test whether this database is ready for use.

list_database_operations(filter_='', page_size=None)

List database operations for the database.

list_database_roles(page_size=None)

Lists Cloud Spanner database roles.

list_tables()

List tables within the database.

property logger()

Logger used by the database.

The default logger will log commit stats at the log level INFO using sys.stderr.

property name()

Database name used in requests.

NOTE: This property will not change if database_id does not, but the return value is not cached.

The database name is of the form

"projects/../instances/../databases/{database_id}"

property reconciling()

Whether the database is currently reconciling.

reload()

Reload this database.

Refresh any configured schema into ddl_statements.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.GetDatabaseDDL

restore(source)

Restore from a backup to this database.

property restore_info()

Restore info for this database.

run_in_transaction(func, *args, **kw)

Perform a unit of work in a transaction, retrying on abort.

session(labels=None, database_role=None)

Factory to create a session for this database.

set_iam_policy(policy)

Sets the access control policy on a database resource. Replaces any existing policy.

snapshot(**kw)

Return an object which wraps a snapshot.

The wrapper must be used as a context manager, with the snapshot as the value returned by the wrapper.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.TransactionOptions.ReadOnly

property spanner_api()

Helper for session-related API calls.

property state()

State of this database.

table(table_id)

Factory to create a table object within this database.

Note: This method does not create a table in Cloud Spanner, but it can be used to check if a table exists.

my_table = database.table("my_table")
if my_table.exists():
    print("Table with ID 'my_table' exists.")
else:
    print("Table with ID 'my_table' does not exist.")

update(fields)

Update this database.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabase

NOTE: > Updates the specified fields of a Cloud Spanner database. Currently,

only the enable_drop_protection field supports updates. To change this value before updating, set it via

database.enable_drop_protection = True

before calling update().

update_ddl(ddl_statements, operation_id='')

Update DDL for this database.

Apply any configured schema from ddl_statements.

Seehttps://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabaseDdl

property version_retention_period()

The period in which Cloud Spanner retains all versions of data for the database.

class google.cloud.spanner_v1.database.SnapshotCheckout(database, **kw)

Bases: object

Context manager for using a snapshot from a database.

Inside the context manager, checks out a session from the database, creates a snapshot from it, making the snapshot available.

Caller must not use the snapshot to perform API requests outside the scope of the context manager.

_ enter_()

Begin with block.

_ exit_(exc_type, exc_val, exc_tb)

End with block.