1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\PgSql\Db; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
6
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
7
|
|
|
|
8
|
|
|
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface; |
9
|
|
|
|
10
|
|
|
use Lagdo\DbAdmin\Driver\Db\Query as AbstractQuery; |
11
|
|
|
|
12
|
|
|
use function strtoupper; |
13
|
|
|
|
14
|
|
|
class Query extends AbstractQuery |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
|
|
protected function limitToOne(string $table, string $query, string $where) |
20
|
|
|
{ |
21
|
|
|
return (preg_match('~^INTO~', $query) ? $this->driver->getLimitClause($query, $where, 1, 0) : |
22
|
|
|
" $query" . ($this->driver->isView($this->driver->tableStatusOrName($table)) ? $where : |
23
|
|
|
" WHERE ctid = (SELECT ctid FROM " . $this->driver->escapeTableName($table) . $where . ' LIMIT 1)')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function insertOrUpdate(string $table, array $rows, array $primary) |
30
|
|
|
{ |
31
|
|
|
foreach ($rows as $set) { |
32
|
|
|
$update = []; |
33
|
|
|
$where = []; |
34
|
|
|
foreach ($set as $key => $val) { |
35
|
|
|
$update[] = "$key = $val"; |
36
|
|
|
if (isset($primary[$this->driver->unescapeId($key)])) { |
37
|
|
|
$where[] = "$key = $val"; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
if (!( |
41
|
|
|
($where && $this->driver->execute("UPDATE " . $this->driver->escapeTableName($table) . |
42
|
|
|
" SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && |
43
|
|
|
$this->driver->affectedRows()) || |
44
|
|
|
$this->driver->execute("INSERT INTO " . $this->driver->escapeTableName($table) . |
45
|
|
|
" (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")") |
46
|
|
|
)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function lastAutoIncrementId() |
57
|
|
|
{ |
58
|
|
|
return '0'; // there can be several sequences |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function convertSearch(string $idf, array $val, TableFieldEntity $field) |
65
|
|
|
{ |
66
|
|
|
return (preg_match('~char|text' . (!preg_match('~LIKE~', $val["op"]) ? |
67
|
|
|
'|date|time(stamp)?|boolean|uuid|' . $this->driver->numberRegex() : '') . |
68
|
|
|
'~', $field->type) ? $idf : "CAST($idf AS text)" |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function countRows(TableEntity $tableStatus, array $where) |
76
|
|
|
{ |
77
|
|
|
$query = "EXPLAIN SELECT * FROM " . $this->driver->escapeId($tableStatus->name) . |
78
|
|
|
($where ? " WHERE " . implode(" AND ", $where) : ""); |
79
|
|
|
if (preg_match("~ rows=([0-9]+)~", $this->driver->result($query), $regs)) |
80
|
|
|
{ |
81
|
|
|
return $regs[1]; |
82
|
|
|
} |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function view(string $name) |
90
|
|
|
{ |
91
|
|
|
$status = $this->driver->tableStatus($name); |
92
|
|
|
$type = strtoupper($status->engine); |
93
|
|
|
return [ |
94
|
|
|
'name' => $name, |
95
|
|
|
'type' => $type, |
96
|
|
|
'materialized' => ($type != 'VIEW'), |
97
|
|
|
'select' => trim($this->driver->result("SELECT pg_get_viewdef(" . |
98
|
|
|
$this->driver->result("SELECT oid FROM pg_class WHERE relnamespace = " . |
99
|
|
|
"(SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . |
100
|
|
|
$this->driver->quote($name)) . ")")) |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc |
106
|
|
|
*/ |
107
|
|
|
public function slowQuery(string $query, int $timeout) |
108
|
|
|
{ |
109
|
|
|
// $this->connection->timeout = 1000 * $timeout; |
110
|
|
|
$this->driver->execute("SET statement_timeout = " . (1000 * $timeout)); |
111
|
|
|
return $query; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function explain(ConnectionInterface $connection, string $query) |
118
|
|
|
{ |
119
|
|
|
return $connection->query("EXPLAIN $query"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function user() |
126
|
|
|
{ |
127
|
|
|
return $this->driver->result("SELECT user"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
|
|
public function schema() |
134
|
|
|
{ |
135
|
|
|
return $this->driver->result("SELECT current_schema()"); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|