|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\MySql\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 count; |
|
13
|
|
|
use function array_keys; |
|
14
|
|
|
use function implode; |
|
15
|
|
|
use function strlen; |
|
16
|
|
|
use function preg_match; |
|
17
|
|
|
use function preg_replace; |
|
18
|
|
|
|
|
19
|
|
|
class Query extends AbstractQuery |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritDoc |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function limitToOne(string $table, string $query, string $where, string $separator = "\n") |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->driver->limit($query, $where, 1, 0, $separator); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function insert(string $table, array $set) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!empty($set)) { |
|
35
|
|
|
return parent::insert($table, $set); |
|
36
|
|
|
} |
|
37
|
|
|
$result = $this->execute('INSERT INTO ' . $this->driver->table($table) . ' () VALUES ()'); |
|
|
|
|
|
|
38
|
|
|
return $result !== false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritDoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function insertOrUpdate(string $table, array $rows, array $primary) |
|
45
|
|
|
{ |
|
46
|
|
|
$columns = array_keys(reset($rows)); |
|
47
|
|
|
$prefix = 'INSERT INTO ' . $this->driver->table($table) . ' (' . implode(', ', $columns) . ') VALUES '; |
|
48
|
|
|
$values = []; |
|
49
|
|
|
foreach ($columns as $key) { |
|
50
|
|
|
$values[$key] = "$key = VALUES($key)"; |
|
51
|
|
|
} |
|
52
|
|
|
$suffix = ' ON DUPLICATE KEY UPDATE ' . implode(', ', $values); |
|
53
|
|
|
$values = []; |
|
54
|
|
|
$length = 0; |
|
55
|
|
|
foreach ($rows as $set) { |
|
56
|
|
|
$value = '(' . implode(', ', $set) . ')'; |
|
57
|
|
|
if (!empty($values) && (strlen($prefix) + $length + strlen($value) + strlen($suffix) > 1e6)) { |
|
58
|
|
|
// 1e6 - default max_allowed_packet |
|
59
|
|
|
if (!$this->execute($prefix . implode(",\n", $values) . $suffix)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
$values = []; |
|
63
|
|
|
$length = 0; |
|
64
|
|
|
} |
|
65
|
|
|
$values[] = $value; |
|
66
|
|
|
$length += strlen($value) + 2; // 2 - strlen(",\n") |
|
67
|
|
|
} |
|
68
|
|
|
$result = $this->execute($prefix . implode(",\n", $values) . $suffix); |
|
69
|
|
|
return $result !== false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritDoc |
|
74
|
|
|
*/ |
|
75
|
|
|
public function slowQuery(string $query, int $timeout) |
|
76
|
|
|
{ |
|
77
|
|
|
// $this->connection->timeout = $timeout; |
|
78
|
|
|
if ($this->driver->minVersion('5.7.8', '10.1.2')) { |
|
79
|
|
|
if (preg_match('~MariaDB~', $this->connection->serverInfo())) { |
|
80
|
|
|
return "SET STATEMENT max_statement_time=$timeout FOR $query"; |
|
81
|
|
|
} elseif (preg_match('~^(SELECT\b)(.+)~is', $query, $match)) { |
|
82
|
|
|
return "$match[1] /*+ MAX_EXECUTION_TIME(" . ($timeout * 1000) . ") */ $match[2]"; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc |
|
90
|
|
|
*/ |
|
91
|
|
|
public function convertSearch(string $idf, array $val, TableFieldEntity $field) |
|
92
|
|
|
{ |
|
93
|
|
|
return (preg_match('~char|text|enum|set~', $field->type) && |
|
94
|
|
|
!preg_match('~^utf8~', $field->collation) && preg_match('~[\x80-\xFF]~', $val['val']) ? |
|
95
|
|
|
"CONVERT($idf USING " . $this->driver->charset() . ')' : $idf |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritDoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function user() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->connection->result('SELECT USER()'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritDoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function view(string $name) |
|
111
|
|
|
{ |
|
112
|
|
|
return [ |
|
113
|
|
|
'name' => $name, |
|
114
|
|
|
'type' => 'VIEW', |
|
115
|
|
|
'materialized' => false, |
|
116
|
|
|
'select' => preg_replace('~^(?:[^`]|`[^`]*`)*\s+AS\s+~isU', '', |
|
117
|
|
|
$this->connection->result('SHOW CREATE VIEW ' . $this->driver->table($name), 1)), |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritDoc |
|
123
|
|
|
*/ |
|
124
|
|
|
public function lastAutoIncrementId() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->connection->result('SELECT LAST_INSERT_ID()'); // mysql_insert_id() truncates bigint |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritDoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function explain(ConnectionInterface $connection, string $query) |
|
133
|
|
|
{ |
|
134
|
|
|
return $connection->query('EXPLAIN ' . ($this->driver->minVersion(5.1) && |
|
135
|
|
|
!$this->driver->minVersion(5.7) ? 'PARTITIONS ' : '') . $query); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritDoc |
|
140
|
|
|
*/ |
|
141
|
|
|
public function countRows(TableEntity $tableStatus, array $where) |
|
142
|
|
|
{ |
|
143
|
|
|
return (!empty($where) || $tableStatus->engine != 'InnoDB' ? null : count($tableStatus->rows)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritDoc |
|
148
|
|
|
*/ |
|
149
|
|
|
public function error() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->util->html(preg_replace('~^You have an error.*syntax to use~U', 'Syntax error', $this->error)); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.