PHP code example of colopl / laravel-spanner
1. Go to this page and download the library: Download colopl/laravel-spanner library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
colopl / laravel-spanner example snippets
[
'connections' => [
'spanner' => [
'driver' => 'spanner',
'instance' => '<Cloud Spanner instanceId here>',
'database' => '<Cloud Spanner database name here>',
]
]
];
$conn = DB::connection('spanner');
$conn->...
[
'connections' => [
'spanner' => [
'driver' => 'spanner',
'instance' => '<Cloud Spanner instanceId here>',
'database' => '<Cloud Spanner database name here>',
// Spanner Client configurations
'client' => [
'projectId' => 'xxx',
...
],
// CacheSessionPool options
'session_pool' => [
'minSessions' => 10,
'maxSessions' => 500,
],
]
]
];
// BAD: Do not use transactions manually!!
try {
DB::beginTransaction();
...
DB::commit();
} catch (\Throwable $ex) {
DB::rollBack();
}
// GOOD: You should always use transaction method
DB::transaction(function() {
...
});
// implicit transaction (Read-only transaction)
$conn->select('SELECT ...');
// explicit transaction (Read-write transaction)
$conn->transaction(function() {
$conn->select('SELECT ...');
});
// implicit transaction (Read-write transaction)
$conn->insert('INSERT ...');
// explicit transaction (Read-write transaction)
$conn->transaction(function() {
$conn->insert('INSERT ...');
});
// There are four types of timestamp bounds: ExactStaleness, MaxStaleness, MinReadTimestamp and ReadTimestamp.
$timestampBound = new ExactStaleness(10);
// by Connection
$connection->selectWithTimestampBound('SELECT ...', $bindings, $timestampBound);
// by Query Builder
$queryBuilder
->withStaleness($timestampBound)
->get();
// Using Connection
$connection->selectWithOptions('SELECT ...', $bindings, ['dataBoostEnabled' => true]);
// Using Query Builder
$queryBuilder
->useDataBoost()
->setRequestTimeoutSeconds(60)
->get();
$requestPath = request()->path();
$tag = 'url=' . $requestPath;
$connection->setRequestTag($tag);
$connection->setTransactionTag($tag);
// by Connection
$connection->runPartitionedDml('UPDATE ...');
// by Query Builder
$queryBuilder->partitionedUpdate($values);
$queryBuilder->partitionedDelete();
$schemaBuilder->create('user_items', function (Blueprint $table) {
$table->uuid('user_id');
$table->uuid('id');
$table->uuid('item_id');
$table->integer('count');
$table->timestamps();
$table->primary(['user_id', 'id']);
// interleaved table
$table->interleaveInParent('users')->cascadeOnDelete();
// interleaved index
$table->index(['userId', 'created_at'])->interleaveIn('users');
});
$schemaBuilder->create('user', function (Blueprint $table) {
$table->uuid('user_id');
$table->timestamps();
// create a policy
$table->deleteRowsOlderThan(['updated_at'], 365);
});
$schemaBuilder->table('user', function (Blueprint $table) {
// add policy
$table->addRowDeletionPolicy('udpated_at', 100);
// replace policy
$table->replaceRowDeletionPolicy('udpated_at', 100);
// drop policy
$table->dropRowDeletionPolicy();
});
$schemaBuilder->create('user', function (Blueprint $table) {
$table->integer('id')->useSequence();
});
$schemaBuilder->create('user_items', function (Blueprint $table) {
$table->createSequence('sequence_name');
$table->integer('id')->useSequence('sequence_name');
$table->alterSequence('sequence_name')
->startWithCounter(100)
->skipRangeMin(1)
->skipRangeMax(10);
$table->dropSequence('sequence_name');
});
$schemaBuilder->create('user_items', function (Blueprint $table) {
$table->createChangeStream('stream_name')
->for('user_items', ['userId', 'userItemId'])
->retentionPeriod('7d')
->valueCaptureType(ChangeStreamValueCaptureType::NewValues)
->excludeTtlDeletes(true);
$table->createChangeStream('stream_name')
->excludeInsert(true)
->excludeUpdate(true)
->excludeDelete(true);
$table->dropChangeStream('stream_name');
});
$schemaBuilder->table('user_items', function (Blueprint $table) {
$table->index('userId')
// Interleave in parent table
->interleaveIn('user')
// Add null filtering
->nullFiltered()
// Add storing
->storing(['itemId', 'count']);
});
$queryBuilder->insertUsingMutation($values);
$queryBuilder->updateUsingMutation($values);
$queryBuilder->insertOrUpdateUsingMutation($values);
$queryBuilder->deleteUsingMutation($values);
ini
grpc.enable_fork_support=1