-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path05-savepoints.php
More file actions
269 lines (205 loc) · 7.94 KB
/
05-savepoints.php
File metadata and controls
269 lines (205 loc) · 7.94 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
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
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
use tommyknocker\pdodb\PdoDb;
echo "=== Savepoints and Nested Transactions Examples ===\n\n";
$db = createExampleDb();
// Ensure users table exists
$schema = $db->schema();
recreateTable($db, 'users', [
'id' => $schema->primaryKey(),
'name' => $schema->string(100),
'age' => $schema->integer(),
]);
// Example 1: Basic savepoint usage
echo "Example 1: Basic savepoint usage\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
// Insert first record
$userId = $db->find()->table('users')->insert([
'name' => 'Alice',
'age' => 25
]);
echo " Inserted user ID: {$userId}\n";
// Create savepoint
$db->savepoint('sp1');
echo " Created savepoint 'sp1'\n";
// Insert second record
$postId = $db->find()->table('users')->insert([
'name' => 'Bob',
'age' => 30
]);
echo " Inserted user ID: {$postId}\n";
// Verify both records exist
$count = count($db->find()->from('users')->whereIn('id', [$userId, $postId])->get());
echo " Total records before rollback: {$count}\n";
// Rollback to savepoint (undoes second insert)
$db->rollbackToSavepoint('sp1');
echo " Rolled back to savepoint 'sp1'\n";
// Verify only first record exists
$exists1 = $db->find()->from('users')->where('id', $userId)->exists();
$exists2 = $db->find()->from('users')->where('id', $postId)->exists();
echo " First record exists: " . ($exists1 ? 'yes' : 'no') . "\n";
echo " Second record exists: " . ($exists2 ? 'yes' : 'no') . "\n";
$db->rollback();
echo " Transaction rolled back\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Example 2: Savepoint with release
echo "Example 2: Savepoint with release\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
$userId = $db->find()->table('users')->insert([
'name' => 'Charlie',
'age' => 35
]);
echo " Inserted user ID: {$userId}\n";
$db->savepoint('sp1');
echo " Created savepoint 'sp1'\n";
$postId = $db->find()->table('users')->insert([
'name' => 'David',
'age' => 40
]);
echo " Inserted user ID: {$postId}\n";
// Release savepoint (keeps changes, removes savepoint)
$db->releaseSavepoint('sp1');
echo " Released savepoint 'sp1'\n";
// Both records should still exist
$exists1 = $db->find()->from('users')->where('id', $userId)->exists();
$exists2 = $db->find()->from('users')->where('id', $postId)->exists();
echo " First record exists: " . ($exists1 ? 'yes' : 'no') . "\n";
echo " Second record exists: " . ($exists2 ? 'yes' : 'no') . "\n";
$db->rollback();
echo " Transaction rolled back\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Example 3: Nested savepoints
echo "Example 3: Nested savepoints\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
$id1 = $db->find()->table('users')->insert(['name' => 'Eve', 'age' => 28]);
echo " Inserted user ID: {$id1}\n";
$db->savepoint('sp1');
echo " Created savepoint 'sp1'\n";
$id2 = $db->find()->table('users')->insert(['name' => 'Frank', 'age' => 32]);
echo " Inserted user ID: {$id2}\n";
$db->savepoint('sp2');
echo " Created savepoint 'sp2'\n";
$id3 = $db->find()->table('users')->insert(['name' => 'Grace', 'age' => 36]);
echo " Inserted user ID: {$id3}\n";
echo " Active savepoints: " . implode(', ', $db->getSavepoints()) . "\n";
// Rollback to first savepoint (removes records 2 and 3)
$db->rollbackToSavepoint('sp1');
echo " Rolled back to savepoint 'sp1'\n";
$exists1 = $db->find()->from('users')->where('id', $id1)->exists();
$exists2 = $db->find()->from('users')->where('id', $id2)->exists();
$exists3 = $db->find()->from('users')->where('id', $id3)->exists();
echo " Record 1 exists: " . ($exists1 ? 'yes' : 'no') . "\n";
echo " Record 2 exists: " . ($exists2 ? 'yes' : 'no') . "\n";
echo " Record 3 exists: " . ($exists3 ? 'yes' : 'no') . "\n";
$db->rollback();
echo " Transaction rolled back\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Example 4: Error handling with savepoints
echo "Example 4: Error handling with savepoints\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
$userId = $db->find()->table('users')->insert([
'name' => 'Henry',
'age' => 45
]);
echo " Inserted user ID: {$userId}\n";
$db->savepoint('sp1');
echo " Created savepoint 'sp1'\n";
try {
// Simulate an error (e.g., constraint violation)
// This would fail if there's a unique constraint on name
$db->find()->table('users')->insert([
'name' => 'Henry', // Duplicate name might cause error
'age' => 50
]);
echo " Second insert succeeded\n";
} catch (\Exception $e) {
echo " Second insert failed: " . $e->getMessage() . "\n";
// Rollback to savepoint to undo the failed operation
$db->rollbackToSavepoint('sp1');
echo " Rolled back to savepoint 'sp1'\n";
}
// First record should still exist
$exists = $db->find()->from('users')->where('id', $userId)->exists();
echo " First record exists: " . ($exists ? 'yes' : 'no') . "\n";
$db->rollback();
echo " Transaction rolled back\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Example 5: Savepoint stack management
echo "Example 5: Savepoint stack management\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
echo " Initial savepoints: " . count($db->getSavepoints()) . "\n";
$db->savepoint('sp1');
echo " After creating sp1: " . implode(', ', $db->getSavepoints()) . "\n";
echo " Has sp1: " . ($db->hasSavepoint('sp1') ? 'yes' : 'no') . "\n";
$db->savepoint('sp2');
echo " After creating sp2: " . implode(', ', $db->getSavepoints()) . "\n";
echo " Has sp2: " . ($db->hasSavepoint('sp2') ? 'yes' : 'no') . "\n";
$db->rollbackToSavepoint('sp1');
echo " After rollback to sp1: " . implode(', ', $db->getSavepoints()) . "\n";
echo " Has sp1: " . ($db->hasSavepoint('sp1') ? 'yes' : 'no') . "\n";
echo " Has sp2: " . ($db->hasSavepoint('sp2') ? 'yes' : 'no') . "\n";
$db->releaseSavepoint('sp1');
echo " After release sp1: " . implode(', ', $db->getSavepoints()) . "\n";
$db->rollback();
echo " Transaction rolled back\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Example 6: Savepoint with commit
echo "Example 6: Savepoint with commit\n";
echo str_repeat('-', 50) . "\n";
$db->startTransaction();
try {
$id1 = $db->find()->table('users')->insert(['name' => 'Iris', 'age' => 50]);
echo " Inserted user ID: {$id1}\n";
$db->savepoint('sp1');
echo " Created savepoint 'sp1'\n";
$id2 = $db->find()->table('users')->insert(['name' => 'Jack', 'age' => 55]);
echo " Inserted user ID: {$id2}\n";
// Commit transaction (commits everything, including savepoint changes)
$db->commit();
echo " Transaction committed\n";
// Both records should exist
$exists1 = $db->find()->from('users')->where('id', $id1)->exists();
$exists2 = $db->find()->from('users')->where('id', $id2)->exists();
echo " Record 1 exists: " . ($exists1 ? 'yes' : 'no') . "\n";
echo " Record 2 exists: " . ($exists2 ? 'yes' : 'no') . "\n";
// Cleanup
$db->find()->table('users')->whereIn('id', [$id1, $id2])->delete();
echo " Cleaned up test data\n";
} catch (\Exception $e) {
$db->rollback();
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
echo "=== Examples completed ===\n";