|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Query |
|
4
|
|
|
* |
|
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7.1 |
|
8
|
|
|
* |
|
9
|
|
|
* @package Query |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2012 - 2018 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace Query\Drivers\Pgsql; |
|
16
|
|
|
|
|
17
|
|
|
use Query\Drivers\AbstractDriver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PostgreSQL specific class |
|
21
|
|
|
*/ |
|
22
|
|
|
class Driver extends AbstractDriver { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Connect to a PosgreSQL database |
|
26
|
|
|
* |
|
27
|
|
|
* @codeCoverageIgnore |
|
28
|
|
|
* @param string $dsn |
|
29
|
|
|
* @param string $username |
|
30
|
|
|
* @param string $password |
|
31
|
|
|
* @param array $options |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $options=[]) |
|
34
|
|
|
{ |
|
35
|
|
|
if (strpos($dsn, 'pgsql') === FALSE) |
|
36
|
|
|
{ |
|
37
|
|
|
$dsn = 'pgsql:'.$dsn; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($dsn, $username, $password, $options); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get a list of schemas for the current connection |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getSchemas(): ?array |
|
49
|
|
|
{ |
|
50
|
|
|
$sql = <<<SQL |
|
51
|
|
|
SELECT DISTINCT "schemaname" FROM "pg_tables" |
|
52
|
|
|
WHERE "schemaname" NOT LIKE 'pg\_%' |
|
53
|
|
|
AND "schemaname" != 'information_schema' |
|
54
|
|
|
SQL; |
|
55
|
|
|
|
|
56
|
|
|
return $this->driverQuery($sql); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retrieve foreign keys for the table |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $table |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getFks($table): array |
|
66
|
|
|
{ |
|
67
|
|
|
$valueMap = [ |
|
68
|
|
|
'c' => 'CASCADE', |
|
69
|
|
|
'r' => 'RESTRICT', |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
$keys = parent::getFks($table); |
|
73
|
|
|
|
|
74
|
|
|
foreach($keys as &$key) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
foreach(['update', 'delete'] AS $type) |
|
77
|
|
|
{ |
|
78
|
|
|
if ( ! isset($valueMap[$key[$type]])) continue; |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$key[$type] = $valueMap[$key[$type]]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $keys; |
|
86
|
|
|
} |
|
87
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.