-
Notifications
You must be signed in to change notification settings - Fork 673
/
Database.php
616 lines (525 loc) · 16 KB
/
Database.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
namespace Elgg;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Result;
use Elgg\Cache\QueryCache;
use Elgg\Database\DbConfig;
use Elgg\Database\QueryBuilder;
use Elgg\Exceptions\DatabaseException;
use Elgg\Exceptions\RuntimeException;
use Elgg\Traits\Debug\Profilable;
use Elgg\Traits\Loggable;
use Psr\Log\LogLevel;
/**
* The Elgg database
*
* @internal
*
* @property-read string $prefix Elgg table prefix (read only)
*/
class Database {
use Profilable;
use Loggable;
const DELAYED_QUERY = 'q';
const DELAYED_HANDLER = 'h';
/**
* @var string $table_prefix Prefix for database tables
*/
private $table_prefix;
/**
* @var Connection[]
*/
private array $connections = [];
/**
* @var int $query_count The number of queries made
*/
private int $query_count = 0;
/**
* Queries are saved as an array with the DELAYED_* constants as keys.
*
* @see registerDelayedQuery()
*
* @var array $delayed_queries Queries to be run during shutdown
*/
protected $delayed_queries = [];
/**
* @var \Elgg\Database\DbConfig $config Database configuration
*/
private $db_config;
/**
* Constructor
*
* @param DbConfig $db_config DB configuration
* @param QueryCache $query_cache Query Cache
* @param Config $config Elgg config
*/
public function __construct(DbConfig $db_config, protected QueryCache $query_cache, protected Config $config) {
$this->resetConnections($db_config);
}
/**
* Reset the connections with new credentials
*
* @param DbConfig $config DB config
*
* @return void
*/
public function resetConnections(DbConfig $config) {
$this->closeConnections();
$this->db_config = $config;
$this->table_prefix = $config->getTablePrefix();
$this->query_cache->enable();
$this->query_cache->clear();
}
/**
* Close all database connections
*
* Note: this is only meant to be used in the PHPUnit test suites
*
* @return void
* @since 4.1
*/
public function closeConnections(): void {
foreach ($this->connections as $connection) {
$connection->close();
}
$this->connections = [];
}
/**
* Gets (if required, also creates) a DB connection.
*
* @param string $type The type of link we want: "read", "write" or "readwrite".
*
* @return Connection
*/
public function getConnection(string $type): Connection {
if (isset($this->connections[$type])) {
return $this->connections[$type];
} else if (isset($this->connections['readwrite'])) {
return $this->connections['readwrite'];
}
$this->setupConnections();
return $this->getConnection($type);
}
/**
* Establish database connections
*
* If the configuration has been set up for multiple read/write databases, set those
* links up separately; otherwise just create the one database link.
*
* @return void
*/
public function setupConnections(): void {
if ($this->db_config->isDatabaseSplit()) {
$this->connect('read');
$this->connect('write');
} else {
$this->connect('readwrite');
}
}
/**
* Establish a connection to the database server
*
* Connect to the database server and use the Elgg database for a particular database link
*
* @param string $type The type of database connection. "read", "write", or "readwrite".
*
* @return void
* @throws DatabaseException
*/
public function connect(string $type = 'readwrite'): void {
$conf = $this->db_config->getConnectionConfig($type);
$params = [
'dbname' => $conf['database'],
'user' => $conf['user'],
'password' => $conf['password'],
'host' => $conf['host'],
'port' => $conf['port'],
'charset' => $conf['encoding'],
'driver' => 'pdo_mysql',
];
try {
$this->connections[$type] = DriverManager::getConnection($params);
// https://github.com/Elgg/Elgg/issues/8121
$sub_query = "SELECT REPLACE(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY', '')";
$this->connections[$type]->executeStatement("SET SESSION sql_mode=($sub_query);");
} catch (\Exception $e) {
// http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html
$this->log(LogLevel::ERROR, $e);
if ($e->getCode() == 1102 || $e->getCode() == 1049) {
$msg = "Elgg couldn't select the database '{$conf['database']}'. Please check that the database is created and you have access to it.";
} else {
$msg = "Elgg couldn't connect to the database using the given credentials. Check the settings file.";
}
throw new DatabaseException($msg);
}
}
/**
* Retrieve rows from the database.
*
* Queries are executed with {@link \Elgg\Database::executeQuery()} and results
* are retrieved with {@link \PDO::fetchObject()}. If a callback
* function $callback is defined, each row will be passed as a single
* argument to $callback. If no callback function is defined, the
* entire result set is returned as an array.
*
* @param QueryBuilder $query The query being passed.
* @param callable $callback Optionally, the name of a function to call back to on each row
*
* @return array An array of database result objects or callback function results. If the query
* returned nothing, an empty array.
*/
public function getData(QueryBuilder $query, $callback = null) {
return $this->getResults($query, $callback, false);
}
/**
* Retrieve a single row from the database.
*
* Similar to {@link \Elgg\Database::getData()} but returns only the first row
* matched. If a callback function $callback is specified, the row will be passed
* as the only argument to $callback.
*
* @param QueryBuilder $query The query to execute.
* @param callable $callback A callback function to apply to the row
*
* @return mixed A single database result object or the result of the callback function.
*/
public function getDataRow(QueryBuilder $query, $callback = null) {
return $this->getResults($query, $callback, true);
}
/**
* Insert a row into the database.
*
* @note Altering the DB invalidates all queries in the query cache.
*
* @param QueryBuilder $query The query to execute.
*
* @return int The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not
*/
public function insertData(QueryBuilder $query): int {
$params = $query->getParameters();
$sql = $query->getSQL();
$this->getLogger()->info("DB insert query {$sql} (params: " . print_r($params, true) . ')');
$this->query_cache->clear();
$this->executeQuery($query);
try {
return (int) $query->getConnection()->lastInsertId();
} catch (DriverException $e) {
if ($e->getPrevious() instanceof NoIdentityValue) {
return 0;
}
throw $e;
}
}
/**
* Update the database.
*
* @note Altering the DB invalidates all queries in the query cache.
*
* @param QueryBuilder $query The query to run.
* @param bool $get_num_rows Return the number of rows affected (default: false).
*
* @return bool|int
*/
public function updateData(QueryBuilder $query, bool $get_num_rows = false) {
$params = $query->getParameters();
$sql = $query->getSQL();
$this->getLogger()->info("DB update query {$sql} (params: " . print_r($params, true) . ')');
$this->query_cache->clear();
$result = $this->executeQuery($query);
if (!$get_num_rows) {
return true;
}
return ($result instanceof Result) ? $result->rowCount() : $result;
}
/**
* Delete data from the database
*
* @note Altering the DB invalidates all queries in query cache.
*
* @param QueryBuilder $query The SQL query to run
*
* @return int The number of affected rows
*/
public function deleteData(QueryBuilder $query): int {
$params = $query->getParameters();
$sql = $query->getSQL();
$this->getLogger()->info("DB delete query {$sql} (params: " . print_r($params, true) . ')');
$this->query_cache->clear();
$result = $this->executeQuery($query);
return ($result instanceof Result) ? $result->rowCount() : $result;
}
/**
* Get a string that uniquely identifies a callback during the current request.
*
* This is used to cache queries whose results were transformed by the callback. If the callback involves
* object method calls of the same class, different instances will return different values.
*
* @param callable $callback The callable value to fingerprint
*
* @return string A string that is unique for each callable passed in
* @since 1.9.4
*/
protected function fingerprintCallback($callback): string {
if (is_string($callback)) {
return $callback;
}
if (is_object($callback)) {
return spl_object_hash($callback) . '::__invoke';
}
if (is_array($callback)) {
if (is_string($callback[0])) {
return "{$callback[0]}::{$callback[1]}";
}
return spl_object_hash($callback[0]) . "::{$callback[1]}";
}
// this should not happen
return '';
}
/**
* Handles queries that return results, running the results through a
* an optional callback function. This is for R queries (from CRUD).
*
* @param QueryBuilder $query The select query to execute
* @param callable $callback An optional callback function to run on each row
* @param bool $single Return only a single result?
*
* @return array|\stdClass An array of database result objects or callback function results. If the query
* returned nothing, an empty array.
* @throws RuntimeException
*/
protected function getResults(QueryBuilder $query, $callback = null, bool $single = false) {
$params = $query->getParameters();
$sql = $query->getSQL();
// Since we want to cache results of running the callback, we need to
// namespace the query with the callback and single result request.
// https://github.com/elgg/elgg/issues/4049
$extras = (int) $single . '|';
if ($callback) {
if (!is_callable($callback)) {
throw new RuntimeException('$callback must be a callable function. Given ' . _elgg_services()->handlers->describeCallable($callback));
}
$extras .= $this->fingerprintCallback($callback);
}
$hash = $this->query_cache->getHash($sql, $params, $extras);
$cached_results = $this->query_cache->get($hash);
if (isset($cached_results)) {
return $cached_results;
}
$this->getLogger()->info("DB select query {$sql} (params: " . print_r($params, true) . ')');
$return = [];
$stmt = $this->executeQuery($query);
while ($row = $stmt->fetchAssociative()) {
$row_obj = (object) $row;
if ($callback) {
$row_obj = call_user_func($callback, $row_obj);
}
if ($single) {
$return = $row_obj;
break;
} else {
$return[] = $row_obj;
}
}
// Cache result
$this->query_cache->set($hash, $return);
return $return;
}
/**
* Execute a query.
*
* $query is executed via {@link Connection::query}. If there is an SQL error,
* a {@link DatabaseException} is thrown.
*
* @param QueryBuilder $query The query
*
* @return Result|int The result of the query
* @throws DatabaseException
*/
protected function executeQuery(QueryBuilder $query) {
try {
$result = $this->trackQuery($query, function() use ($query) {
if ($query instanceof \Elgg\Database\Select) {
return $query->executeQuery();
} else {
return $query->executeStatement();
}
});
} catch (\Exception $e) {
$ex = new DatabaseException($e->getMessage(), 0, $e);
$ex->setParameters($query->getParameters());
$ex->setQuery($query->getSQL());
throw $ex;
}
return $result;
}
/**
* Tracks the query count and timers for a given query
*
* @param QueryBuilder $query The query
* @param callable $callback Callback to execyte during query execution
*
* @return mixed
*/
public function trackQuery(QueryBuilder $query, callable $callback) {
$params = $query->getParameters();
$sql = $query->getSQL();
$this->query_count++;
$timer_key = preg_replace('~\\s+~', ' ', trim($sql . '|' . serialize($params)));
$this->beginTimer(['SQL', $timer_key]);
$stop_timer = function() use ($timer_key) {
$this->endTimer(['SQL', $timer_key]);
};
try {
$result = $callback();
} catch (\Exception $e) {
$stop_timer();
throw $e;
}
$stop_timer();
return $result;
}
/**
* Queue a query for execution upon shutdown.
*
* You can specify a callback if you care about the result. This function will always
* be passed a \Doctrine\DBAL\Driver\Statement.
*
* @param QueryBuilder $query The query to execute
* @param callable $callback A callback function to pass the results array to
*
* @return void
*/
public function registerDelayedQuery(QueryBuilder $query, $callback = null): void {
if (Application::isCli() && !$this->config->testing_mode) {
// during CLI execute delayed queries immediately (unless in testing mode, during PHPUnit)
// this should prevent OOM during long-running jobs
// @see Database::executeDelayedQueries()
try {
$stmt = $this->executeQuery($query);
if (is_callable($callback)) {
call_user_func($callback, $stmt);
}
} catch (\Throwable $t) {
// Suppress all exceptions to not allow the application to crash
$this->getLogger()->error($t);
}
return;
}
$this->delayed_queries[] = [
self::DELAYED_QUERY => $query,
self::DELAYED_HANDLER => $callback,
];
}
/**
* Trigger all queries that were registered as "delayed" queries. This is
* called by the system automatically on shutdown.
*
* @return void
*/
public function executeDelayedQueries(): void {
foreach ($this->delayed_queries as $set) {
$query = $set[self::DELAYED_QUERY];
$handler = $set[self::DELAYED_HANDLER];
try {
$stmt = $this->executeQuery($query);
if (is_callable($handler)) {
call_user_func($handler, $stmt);
}
} catch (\Throwable $t) {
// Suppress all exceptions since page already sent to requestor
$this->getLogger()->error($t);
}
}
$this->delayed_queries = [];
}
/**
* Enable the query cache
*
* This does not take precedence over the \Elgg\Database\Config setting.
*
* @return void
*/
public function enableQueryCache(): void {
$this->query_cache->enable();
}
/**
* Disable the query cache
*
* This is useful for special scripts that pull large amounts of data back
* in single queries.
*
* @return void
*/
public function disableQueryCache(): void {
$this->query_cache->disable();
}
/**
* Get the number of queries made to the database
*
* @return int
*/
public function getQueryCount(): int {
return $this->query_count;
}
/**
* Get the server version number
*
* @param string $type Connection type (Config constants, e.g. Config::READ_WRITE)
*
* @return string Empty if version cannot be determined
*/
public function getServerVersion(string $type = DbConfig::READ_WRITE): string {
return $this->getConnection($type)->getServerVersion();
}
/**
* Is the database MariaDB
*
* @param string $type Connection type (Config constants, e.g. Config::READ_WRITE)
*
* @return bool if MariaDB is detected
*/
public function isMariaDB(string $type = DbConfig::READ_WRITE): bool {
return $this->getConnection($type)->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MariaDBPlatform;
}
/**
* Is the database MySQL
*
* @param string $type Connection type (Config constants, e.g. Config::READ_WRITE)
*
* @return bool if MySQL is detected
*
* @since 6.0
*/
public function isMySQL(string $type = DbConfig::READ_WRITE): bool {
return $this->getConnection($type)->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySQLPlatform;
}
/**
* Handle magic property reads
*
* @param string $name Property name
*
* @return mixed
* @throws RuntimeException
*/
public function __get($name) {
if ($name === 'prefix') {
return $this->table_prefix;
}
throw new RuntimeException("Cannot read property '{$name}'");
}
/**
* Handle magic property writes
*
* @param string $name Property name
* @param mixed $value Value
*
* @return void
* @throws RuntimeException
*/
public function __set($name, $value): void {
throw new RuntimeException("Cannot write property '{$name}'");
}
}