-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathexample_preferred.php
More file actions
69 lines (54 loc) · 2.2 KB
/
example_preferred.php
File metadata and controls
69 lines (54 loc) · 2.2 KB
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
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use Aws\AuroraDsql\PdoPgsql\AuroraDsql;
use Aws\AuroraDsql\PdoPgsql\DsqlConfig;
// Works with both admin and non-admin users:
// - Admin users operate in the default "public" schema
// - Non-admin users operate in a custom "myschema" schema
function main(): void
{
$clusterEndpoint = getenv('CLUSTER_ENDPOINT') ?: throw new RuntimeException(
'CLUSTER_ENDPOINT environment variable is required'
);
$clusterUser = getenv('CLUSTER_USER') ?: 'admin';
// Determine schema based on user type
$schema = $clusterUser === 'admin' ? 'public' : 'myschema';
$config = new DsqlConfig(
host: $clusterEndpoint,
user: $clusterUser,
occMaxRetries: 3,
);
$pdo = AuroraDsql::connect($config);
// Set search_path for the appropriate schema
$pdo->exec("SET search_path = '{$schema}'");
// Simple read
$stmt = $pdo->query('SELECT 1 AS result');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "Connected successfully. SELECT 1 = {$row['result']}\n";
// Create a test table — exec() is automatically retried on OCC conflict
$pdo->exec('CREATE TABLE IF NOT EXISTS example_test (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
)');
// Transactional write — transaction() is automatically retried on OCC conflict
$name = 'test-' . bin2hex(random_bytes(4));
$id = $pdo->transaction(function (PDO $conn) use ($name): string {
$stmt = $conn->prepare('INSERT INTO example_test (name) VALUES (?) RETURNING id');
$stmt->execute([$name]);
return $stmt->fetchColumn();
});
echo "Inserted row with id: {$id}\n";
// Read it back
$stmt = $pdo->prepare('SELECT name FROM example_test WHERE id = ?');
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "Read back: {$row['name']}\n";
// Cleanup — exec() retry handles OCC conflicts transparently
$pdo->exec('DELETE FROM example_test');
echo "Done.\n";
}
main();