-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.inc
455 lines (407 loc) · 13.7 KB
/
database.inc
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* @file
* Database interface code for PDO database servers.
*/
/**
* @addtogroup database
* @{
*/
class DatabaseConnection_pdo extends DatabaseConnection
{
/** @var \PDO */
protected $pdo;
/** @var \DatabaseHelper */
protected $helper;
public function __construct(array $connection_options = array())
{
$this->connectionOptions = $connection_options;
// Initialize and prepare the connection prefix.
$this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');
// Because the other methods don't seem to work right.
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
// Call PDO::__construct and PDO::setAttribute.
$this->pdo = $connection_options['pdo'];
$helper_class = 'DatabaseHelper_'. $this->driver();
$this->helper = new $helper_class($this);
// Set a Statement class, unless the driver opted out.
if (!empty($this->statementClass)) {
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));
}
}
/**
* {@inheritdocs}
*/
public function prepareQuery($query) {
$query = $this->prefixTables($query);
// Call PDO::prepare.
return $this->prepare($query);
}
/**
* {@inheritdocs}
*/
public function queryRange(
$query,
$from,
$count,
array $args = array(),
array $options = array()
) {
return $this->helper->queryRange($query, $from, $count, $args, $options);
}
/**
* {@inheritdocs}
*/
function queryTemporary(
$query,
array $args = array(),
array $options = array()
) {
return $this->helper->queryTemporary($query, $args, $options);
}
/**
* {@inheritdocs}
*/
public function driver()
{
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
/**
* {@inheritdocs}
*/
public function databaseType()
{
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
/**
* {@inheritdocs}
*/
public function mapConditionOperator($operator)
{
return $this->helper->mapConditionOperator($operator);
}
/**
* {@inheritdocs}
*/
public function nextId($existing_id = 0)
{
return $this->helper->nextId($existing_id);
}
/**
* {@inheritdocs}
*/
public function prepare($statement, $driver_options = array())
{
return $this->pdo->prepare($statement, $driver_options);
}
/**
* {@inheritdocs}
*/
public function beginTransaction()
{
return $this->pdo->beginTransaction();
}
/**
* {@inheritdocs}
*/
public function commit()
{
return $this->pdo->commit();
}
/**
* {@inheritdocs}
*/
public function rollback($savepoint_name = 'drupal_transaction')
{
if (!$this->supportsTransactions()) {
return;
}
if (!$this->inTransaction()) {
throw new DatabaseTransactionNoActiveException();
}
// A previous rollback to an earlier savepoint may mean that the savepoint
// in question has already been accidentally committed.
if (!isset($this->transactionLayers[$savepoint_name])) {
throw new DatabaseTransactionNoActiveException();
}
// We need to find the point we're rolling back to, all other savepoints
// before are no longer needed. If we rolled back other active savepoints,
// we need to throw an exception.
$rolled_back_other_active_savepoints = FALSE;
while ($savepoint = array_pop($this->transactionLayers)) {
if ($savepoint == $savepoint_name) {
// If it is the last the transaction in the stack, then it is not a
// savepoint, it is the transaction itself so we will need to roll back
// the transaction rather than a savepoint.
if (empty($this->transactionLayers)) {
break;
}
$this->query('ROLLBACK TO SAVEPOINT ' . $savepoint);
$this->popCommittableTransactions();
if ($rolled_back_other_active_savepoints) {
throw new DatabaseTransactionOutOfOrderException();
}
return;
}
else {
$rolled_back_other_active_savepoints = TRUE;
}
}
$this->pdo->rollBack();
if ($rolled_back_other_active_savepoints) {
throw new DatabaseTransactionOutOfOrderException();
}
}
/**
* {@inheritdocs}
*/
public function pushTransaction($name) {
if (!$this->supportsTransactions()) {
return;
}
if (isset($this->transactionLayers[$name])) {
throw new DatabaseTransactionNameNonUniqueException($name . " is already in use.");
}
// If we're already in a transaction then we want to create a savepoint
// rather than try to create another transaction.
if ($this->inTransaction()) {
$this->pdo->query('SAVEPOINT ' . $name);
}
else {
$this->pdo->beginTransaction();
}
$this->transactionLayers[$name] = $name;
}
/**
* {@inheritdocs}
*/
public function inTransaction()
{
return $this->pdo->inTransaction();
}
/**
* {@inheritdocs}
*/
public function setAttribute($attribute, $value)
{
return $this->pdo->setAttribute($attribute, $value);
}
/**
* {@inheritdocs}
*/
public function exec($statement)
{
return $this->pdo->exec($statement);
}
/**
* {@inheritdocs}
*/
public function lastInsertId($name = null)
{
return $this->pdo->lastInsertId($name);
}
/**
* {@inheritdocs}
*/
public function errorCode()
{
return $this->pdo->errorCode();
}
/**
* {@inheritdocs}
*/
public function errorInfo()
{
return $this->pdo->errorInfo();
}
/**
* {@inheritdocs}
*/
public function getAttribute($attribute)
{
return $this->pdo->getAttribute($attribute);
}
/**
* {@inheritdocs}
*/
public function quote($string, $parameter_type = PDO::PARAM_STR)
{
return $this->pdo->quote($string, $parameter_type);
}
/**
* {@inheritdocs}
*/
protected function popCommittableTransactions()
{
if ($this->helper instanceof DatabaseTransactionHelper) {
$this->helper->popCommittableTransactions($this->transactionLayers);
}
else {
parent::popCommittableTransactions();
}
}
}
interface DatabaseHelper
{
public function __construct(DatabaseConnection_pdo $conn);
public function queryRange($query, $from, $count, array $args = array(), array $options = array());
public function queryTemporary($query, array $args = array(), array $options = array());
public function mapConditionOperator($operator);
public function nextId($existing_id = 0);
}
interface DatabaseTransactionHelper
{
public function popCommittableTransactions(&$transactionLayers);
}
abstract class DatabaseHelper_pdo implements DatabaseHelper
{
/** @var DatabaseConnection_pdo */
protected $conn;
/**
* An index used to generate unique temporary table names.
*
* @var integer
*/
protected $temporaryNameIndex = 0;
public function __construct(\DatabaseConnection_pdo $conn)
{
$this->conn = $conn;
}
/**
* Generates a temporary table name.
*
* @return
* A table name.
*/
protected function generateTemporaryTableName() {
return "db_temporary_" . $this->temporaryNameIndex++;
}
}
class DatabaseHelper_mysql extends DatabaseHelper_pdo implements DatabaseTransactionHelper
{
/**
* Flag to indicate if the cleanup function in __destruct() should run.
*
* @var boolean
*/
protected $needsCleanup = FALSE;
public function queryRange(
$query,
$from,
$count,
array $args = array(),
array $options = array()
) {
return $this->conn->query($query . ' LIMIT ' . (int) $from . ', ' . (int) $count, $args, $options);
}
public function __destruct() {
if ($this->needsCleanup) {
$this->nextIdDelete();
}
}
public function queryTemporary(
$query,
array $args = array(),
array $options = array()
) {
$tablename = $this->generateTemporaryTableName();
$this->conn->query('CREATE TEMPORARY TABLE {' . $tablename . '} Engine=MEMORY ' . $query, $args, $options);
return $tablename;
}
public function mapConditionOperator($operator)
{
// We don't want to override any of the defaults.
return NULL;
}
public function nextId($existing_id = 0)
{
$new_id = $this->conn->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
// This should only happen after an import or similar event.
if ($existing_id >= $new_id) {
// If we INSERT a value manually into the sequences table, on the next
// INSERT, MySQL will generate a larger value. However, there is no way
// of knowing whether this value already exists in the table. MySQL
// provides an INSERT IGNORE which would work, but that can mask problems
// other than duplicate keys. Instead, we use INSERT ... ON DUPLICATE KEY
// UPDATE in such a way that the UPDATE does not do anything. This way,
// duplicate keys do not generate errors but everything else does.
$this->conn->query('INSERT INTO {sequences} (value) VALUES (:value) ON DUPLICATE KEY UPDATE value = value', array(':value' => $existing_id));
$new_id = $this->conn->query('INSERT INTO {sequences} () VALUES ()', array(), array('return' => Database::RETURN_INSERT_ID));
}
$this->needsCleanup = TRUE;
return $new_id;
}
public function nextIdDelete() {
// While we want to clean up the table to keep it up from occupying too
// much storage and memory, we must keep the highest value in the table
// because InnoDB uses an in-memory auto-increment counter as long as the
// server runs. When the server is stopped and restarted, InnoDB
// reinitializes the counter for each table for the first INSERT to the
// table based solely on values from the table so deleting all values would
// be a problem in this case. Also, TRUNCATE resets the auto increment
// counter.
try {
$max_id = $this->conn->query('SELECT MAX(value) FROM {sequences}')->fetchField();
// We know we are using MySQL here, no need for the slower db_delete().
$this->conn->query('DELETE FROM {sequences} WHERE value < :value', array(':value' => $max_id));
}
// During testing, this function is called from shutdown with the
// simpletest prefix stored in $this->connection, and those tables are gone
// by the time shutdown is called so we need to ignore the database
// errors. There is no problem with completely ignoring errors here: if
// these queries fail, the sequence will work just fine, just use a bit
// more database storage and memory.
catch (PDOException $e) {
}
}
/**
* Overridden to work around issues to MySQL not supporting transactional DDL.
* @param $transactionLayers
* @throws DatabaseTransactionCommitFailedException
*/
public function popCommittableTransactions(&$transactionLayers) {
// Commit all the committable layers.
foreach (array_reverse($transactionLayers) as $name => $active) {
// Stop once we found an active transaction.
if ($active) {
break;
}
// If there are no more layers left then we should commit.
unset($transactionLayers[$name]);
if (empty($transactionLayers)) {
if (!$this->conn->commit()) {
throw new DatabaseTransactionCommitFailedException();
}
}
else {
// Attempt to release this savepoint in the standard way.
try {
$this->conn->query('RELEASE SAVEPOINT ' . $name);
}
catch (PDOException $e) {
// However, in MySQL (InnoDB), savepoints are automatically committed
// when tables are altered or created (DDL transactions are not
// supported). This can cause exceptions due to trying to release
// savepoints which no longer exist.
//
// To avoid exceptions when no actual error has occurred, we silently
// succeed for MySQL error code 1305 ("SAVEPOINT does not exist").
if ($e->errorInfo[1] == '1305') {
// If one SAVEPOINT was released automatically, then all were.
// Therefore, clean the transaction stack.
$transactionLayers = array();
// We also have to explain to PDO that the transaction stack has
// been cleaned-up.
$this->conn->commit();
}
else {
throw $e;
}
}
}
}
}
}
/**
* @} End of "addtogroup database".
*/