-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathPDOConnector.php
593 lines (509 loc) · 17.2 KB
/
PDOConnector.php
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
namespace SilverStripe\ORM\Connect;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\Deprecation;
use PDO;
use PDOStatement;
use InvalidArgumentException;
use PDOException;
/**
* PDO driver database connector
*/
class PDOConnector extends DBConnector implements TransactionManager
{
/**
* Should ATTR_EMULATE_PREPARES flag be used to emulate prepared statements?
*
* @config
* @var boolean
*/
private static $emulate_prepare = false;
/**
* Should we return everything as a string in order to allow transaction savepoints?
* This preserves the behaviour of <= 4.3, including some bugs.
*
* @config
* @var boolean
*/
private static $legacy_types = false;
/**
* Default strong SSL cipher to be used
*
* @config
* @var string
*/
private static $ssl_cipher_default = 'DHE-RSA-AES256-SHA';
/**
* The PDO connection instance
*
* @var PDO
*/
protected $pdoConnection = null;
/**
* Name of the currently selected database
*
* @var string
*/
protected $databaseName = null;
/**
* If available, the row count of the last executed statement
*
* @var int|null
*/
protected $rowCount = null;
/**
* Error generated by the errorInfo() method of the last PDOStatement
*
* @var array|null
*/
protected $lastStatementError = null;
/**
* List of prepared statements, cached by SQL string
*
* @var array
*/
protected $cachedStatements = [];
/**
* Driver
* @var string
*/
protected $driver = null;
/*
* Is a transaction currently active?
* @var bool
*/
protected $inTransaction = false;
/**
* Flush all prepared statements
*/
public function flushStatements()
{
$this->cachedStatements = [];
}
/**
* Retrieve a prepared statement for a given SQL string, or return an already prepared version if
* one exists for the given query
*
* @param string $sql
* @return PDOStatementHandle|false
*/
public function getOrPrepareStatement($sql)
{
// Return cached statements
if (isset($this->cachedStatements[$sql])) {
return $this->cachedStatements[$sql];
}
// Generate new statement
try {
$statement = $this->pdoConnection->prepare(
$sql,
[PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]
);
} catch (PDOException $e) {
$statement = false;
$this->databaseError($e->getMessage(), E_USER_ERROR, $sql);
}
// Wrap in a PDOStatementHandle, to cache column metadata
$statementHandle = ($statement === false) ? false : new PDOStatementHandle($statement);
// Only cache select statements
if (preg_match('/^(\s*)select\b/i', $sql ?? '')) {
$this->cachedStatements[$sql] = $statementHandle;
}
return $statementHandle;
}
/**
* Is PDO running in emulated mode
*
* @return boolean
*/
public static function is_emulate_prepare()
{
return self::config()->get('emulate_prepare');
}
public function connect($parameters, $selectDB = false)
{
Deprecation::notice('4.5', 'Use native database drivers in favour of PDO. '
. 'https://github.com/silverstripe/silverstripe-framework/issues/8598');
$this->flushStatements();
// Note that we don't select the database here until explicitly
// requested via selectDatabase
$this->driver = $parameters['driver'];
// Build DSN string
$dsn = [];
// Typically this is false, but some drivers will request this
if ($selectDB) {
// Specify complete file path immediately following driver (SQLLite3)
if (!empty($parameters['filepath'])) {
$dsn[] = $parameters['filepath'];
} elseif (!empty($parameters['database'])) {
// Some databases require a selected database at connection (SQLite3, Azure)
if ($parameters['driver'] === 'sqlsrv') {
$dsn[] = "Database={$parameters['database']}";
} else {
$dsn[] = "dbname={$parameters['database']}";
}
}
}
// Syntax for sql server is slightly different
if ($parameters['driver'] === 'sqlsrv') {
$server = $parameters['server'];
if (!empty($parameters['port'])) {
$server .= ",{$parameters['port']}";
}
$dsn[] = "Server=$server";
} elseif ($parameters['driver'] === 'dblib') {
$server = $parameters['server'];
if (!empty($parameters['port'])) {
$server .= ":{$parameters['port']}";
}
$dsn[] = "host={$server}";
} else {
if (!empty($parameters['server'])) {
// Use Server instead of host for sqlsrv
$dsn[] = "host={$parameters['server']}";
}
if (!empty($parameters['port'])) {
$dsn[] = "port={$parameters['port']}";
}
}
// Connection charset and collation
$connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset');
$connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation');
// Set charset if given and not null. Can explicitly set to empty string to omit
if (!in_array($parameters['driver'], ['sqlsrv', 'pgsql'])) {
$charset = isset($parameters['charset'])
? $parameters['charset']
: $connCharset;
if (!empty($charset)) {
$dsn[] = "charset=$charset";
}
}
// Connection commands to be run on every re-connection
if (!isset($charset)) {
$charset = $connCharset;
}
$options = [];
if ($parameters['driver'] === 'mysql') {
$options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset . ' COLLATE ' . $connCollation;
}
// Set SSL options if they are defined
if (array_key_exists('ssl_key', $parameters ?? []) &&
array_key_exists('ssl_cert', $parameters ?? [])
) {
$options[PDO::MYSQL_ATTR_SSL_KEY] = $parameters['ssl_key'];
$options[PDO::MYSQL_ATTR_SSL_CERT] = $parameters['ssl_cert'];
if (array_key_exists('ssl_ca', $parameters ?? [])) {
$options[PDO::MYSQL_ATTR_SSL_CA] = $parameters['ssl_ca'];
}
// use default cipher if not provided
$options[PDO::MYSQL_ATTR_SSL_CIPHER] =
array_key_exists('ssl_cipher', $parameters ?? []) ?
$parameters['ssl_cipher'] :
self::config()->get('ssl_cipher_default');
}
if (static::config()->get('legacy_types')) {
$options[PDO::ATTR_STRINGIFY_FETCHES] = true;
$options[PDO::ATTR_EMULATE_PREPARES] = true;
} else {
// Set emulate prepares (unless null / default)
$isEmulatePrepares = self::is_emulate_prepare();
if (isset($isEmulatePrepares)) {
$options[PDO::ATTR_EMULATE_PREPARES] = (bool)$isEmulatePrepares;
}
// Disable stringified fetches
$options[PDO::ATTR_STRINGIFY_FETCHES] = false;
}
// May throw a PDOException if fails
$this->pdoConnection = new PDO(
$this->driver . ':' . implode(';', $dsn),
empty($parameters['username']) ? '' : $parameters['username'],
empty($parameters['password']) ? '' : $parameters['password'],
$options
);
// Show selected DB if requested
if ($this->pdoConnection && $selectDB && !empty($parameters['database'])) {
$this->databaseName = $parameters['database'];
}
}
/**
* Return the driver for this connector
* E.g. 'mysql', 'sqlsrv', 'pgsql'
*
* @return string
*/
public function getDriver()
{
return $this->driver;
}
public function getVersion()
{
return $this->pdoConnection->getAttribute(PDO::ATTR_SERVER_VERSION);
}
public function escapeString($value)
{
$value = $this->quoteString($value);
// Since the PDO library quotes the value, we should remove this to maintain
// consistency with MySQLDatabase::escapeString
if (preg_match('/^\'(?<value>.*)\'$/', $value ?? '', $matches)) {
$value = $matches['value'];
}
return $value;
}
public function quoteString($value)
{
return $this->pdoConnection->quote($value ?? '');
}
/**
* Invoked before any query is executed
*
* @param string $sql
*/
protected function beforeQuery($sql)
{
// Reset state
$this->rowCount = 0;
$this->lastStatementError = null;
// Flush if necessary
if ($this->isQueryDDL($sql)) {
$this->flushStatements();
}
}
/**
* Executes a query that doesn't return a resultset
*
* @param string $sql The SQL query to execute
* @param integer $errorLevel For errors to this query, raise PHP errors
* using this error level.
* @return int
*/
public function exec($sql, $errorLevel = E_USER_ERROR)
{
$this->beforeQuery($sql);
// Directly exec this query
$result = $this->pdoConnection->exec($sql);
// Check for errors
if ($result !== false) {
return $this->rowCount = $result;
}
$this->databaseError($this->getLastError(), $errorLevel, $sql);
return null;
}
public function query($sql, $errorLevel = E_USER_ERROR)
{
$this->beforeQuery($sql);
// Directly query against connection
$statement = $this->pdoConnection->query($sql);
// Generate results
if ($statement === false) {
$this->databaseError($this->getLastError(), $errorLevel, $sql);
} else {
return $this->prepareResults(new PDOStatementHandle($statement), $errorLevel, $sql);
}
}
/**
* Determines the PDO::PARAM_* type for a given PHP type string
* @param string $phpType Type of object in PHP
* @return integer PDO Parameter constant value
*/
public function getPDOParamType($phpType)
{
switch ($phpType) {
case 'boolean':
return PDO::PARAM_BOOL;
case 'NULL':
return PDO::PARAM_NULL;
case 'integer':
return PDO::PARAM_INT;
case 'object': // Allowed if the object or resource has a __toString method
case 'resource':
case 'float': // Not actually returnable from get_type
case 'double':
case 'string':
return PDO::PARAM_STR;
case 'blob':
return PDO::PARAM_LOB;
case 'array':
case 'unknown type':
default:
throw new InvalidArgumentException("Cannot bind parameter as it is an unsupported type ($phpType)");
}
}
/**
* Bind all parameters to a PDOStatement
*
* @param PDOStatement $statement
* @param array $parameters
*/
public function bindParameters(PDOStatement $statement, $parameters)
{
// Bind all parameters
$parameterCount = count($parameters ?? []);
for ($index = 0; $index < $parameterCount; $index++) {
$value = $parameters[$index];
$phpType = gettype($value);
// Allow overriding of parameter type using an associative array
if ($phpType === 'array') {
$phpType = $value['type'];
$value = $value['value'];
}
// Check type of parameter
$type = $this->getPDOParamType($phpType);
if ($type === PDO::PARAM_STR) {
$value = (string) $value;
}
// Bind this value
$statement->bindValue($index+1, $value, $type);
}
}
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
{
$this->beforeQuery($sql);
// Fetch cached statement, or create it
$statementHandle = $this->getOrPrepareStatement($sql);
// Error handling
if ($statementHandle === false) {
$this->databaseError($this->getLastError(), $errorLevel, $sql, $this->parameterValues($parameters));
return null;
}
// Bind parameters
$this->bindParameters($statementHandle->getPDOStatement(), $parameters);
$statementHandle->execute($parameters);
// Generate results
return $this->prepareResults($statementHandle, $errorLevel, $sql);
}
/**
* Given a PDOStatement that has just been executed, generate results
* and report any errors
*
* @param PDOStatementHandle $statement
* @param int $errorLevel
* @param string $sql
* @param array $parameters
* @return PDOQuery
*/
protected function prepareResults(PDOStatementHandle $statement, $errorLevel, $sql, $parameters = [])
{
// Catch error
if ($this->hasError($statement)) {
$this->lastStatementError = $statement->errorInfo();
$statement->closeCursor();
$this->databaseError($this->getLastError(), $errorLevel, $sql, $this->parameterValues($parameters));
return null;
}
// Count and return results
$this->rowCount = $statement->rowCount();
return new PDOQuery($statement);
}
/**
* Determine if a resource has an attached error
*
* @param PDOStatement|PDO $resource the resource to check
* @return boolean Flag indicating true if the resource has an error
*/
protected function hasError($resource)
{
// No error if no resource
if (empty($resource)) {
return false;
}
// If the error code is empty the statement / connection has not been run yet
$code = $resource->errorCode();
if (empty($code)) {
return false;
}
// Skip 'ok' and undefined 'warning' types.
// @see http://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm
return $code !== '00000' && $code !== '01000';
}
public function getLastError()
{
$error = null;
if ($this->lastStatementError) {
$error = $this->lastStatementError;
} elseif ($this->hasError($this->pdoConnection)) {
$error = $this->pdoConnection->errorInfo();
}
if ($error) {
return sprintf("%s-%s: %s", $error[0], $error[1], $error[2]);
}
return null;
}
public function getGeneratedID($table)
{
return (int) $this->pdoConnection->lastInsertId();
}
public function affectedRows()
{
return $this->rowCount;
}
public function selectDatabase($name)
{
$this->exec("USE \"{$name}\"");
$this->databaseName = $name;
return true;
}
public function getSelectedDatabase()
{
return $this->databaseName;
}
public function unloadDatabase()
{
$this->databaseName = null;
}
public function isActive()
{
return $this->databaseName && $this->pdoConnection;
}
public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
{
$this->inTransaction = true;
if ($transactionMode) {
$this->query("SET TRANSACTION $transactionMode");
}
if ($this->pdoConnection->beginTransaction()) {
if ($sessionCharacteristics) {
$this->query("SET SESSION CHARACTERISTICS AS TRANSACTION $sessionCharacteristics");
}
return true;
}
return false;
}
public function transactionEnd()
{
$this->inTransaction = false;
return $this->pdoConnection->commit();
}
public function transactionRollback($savepoint = null)
{
if ($savepoint) {
if ($this->supportsSavepoints()) {
$this->exec("ROLLBACK TO SAVEPOINT $savepoint");
} else {
throw new DatabaseException("Savepoints not supported on this PDO connection");
}
}
// Note: $this->inTransaction may not match the 'in-transaction' state in PDO
$this->inTransaction = false;
if ($this->pdoConnection->inTransaction()) {
return $this->pdoConnection->rollBack();
}
// return false because it did not rollback.
return false;
}
public function transactionDepth()
{
return (int)$this->inTransaction;
}
public function transactionSavepoint($savepoint = null)
{
if ($this->supportsSavepoints()) {
$this->exec("SAVEPOINT $savepoint");
} else {
throw new DatabaseException("Savepoints not supported on this PDO connection");
}
}
public function supportsSavepoints()
{
return static::config()->get('legacy_types');
}
}