|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use function substr; |
|
6
|
|
|
use function strlen; |
|
7
|
|
|
use function preg_replace; |
|
8
|
|
|
|
|
9
|
|
|
trait AdminQueryTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Query printed after execution in the message |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $query Executed query |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
private function messageQuery(string $query/*, string $time*/): string |
|
19
|
|
|
{ |
|
20
|
|
|
if (strlen($query) > 1e6) { |
|
21
|
|
|
// [\x80-\xFF] - valid UTF-8, \n - can end by one-line comment |
|
22
|
|
|
$query = preg_replace('~[\x80-\xFF]+$~', '', substr($query, 0, 1e6)) . "\n…"; |
|
23
|
|
|
} |
|
24
|
|
|
return $query; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Execute query |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $query |
|
31
|
|
|
* @param bool $execute |
|
32
|
|
|
* @param bool $failed |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool |
|
35
|
|
|
* @throws Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
private function executeQuery(string $query, bool $execute = true, bool $failed = false/*, string $time = ''*/): bool |
|
38
|
|
|
{ |
|
39
|
|
|
if ($execute) { |
|
40
|
|
|
// $start = microtime(true); |
|
41
|
|
|
$failed = !$this->driver->execute($query); |
|
42
|
|
|
// $time = $this->trans->formatTime($start); |
|
43
|
|
|
} |
|
44
|
|
|
if ($failed) { |
|
45
|
|
|
$sql = ''; |
|
46
|
|
|
if ($query) { |
|
47
|
|
|
$sql = $this->messageQuery($query/*, $time*/); |
|
48
|
|
|
} |
|
49
|
|
|
throw new Exception($this->driver->error() . $sql); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Execute remembered queries |
|
56
|
|
|
* |
|
57
|
|
|
* @param bool $failed |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
* @throws Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
private function executeSavedQuery(bool $failed): bool |
|
63
|
|
|
{ |
|
64
|
|
|
list($queries/*, $time*/) = $this->driver->queries(); |
|
65
|
|
|
return $this->executeQuery($queries, false, $failed/*, $time*/); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Drop old object and create a new one |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $drop Drop old object query |
|
72
|
|
|
* @param string $create Create new object query |
|
73
|
|
|
* @param string $dropCreated Drop new object query |
|
74
|
|
|
* @param string $test Create test object query |
|
75
|
|
|
* @param string $dropTest Drop test object query |
|
76
|
|
|
* @param string $oldName |
|
77
|
|
|
* @param string $newName |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
*/ |
|
82
|
|
|
private function dropAndCreate(string $drop, string $create, string $dropCreated, |
|
83
|
|
|
string $test, string $dropTest, string $oldName, string $newName): string |
|
84
|
|
|
{ |
|
85
|
|
|
if ($oldName == '' && $newName == '') { |
|
86
|
|
|
$this->executeQuery($drop); |
|
87
|
|
|
return 'dropped'; |
|
88
|
|
|
} |
|
89
|
|
|
if ($oldName == '') { |
|
90
|
|
|
$this->executeQuery($create); |
|
91
|
|
|
return 'created'; |
|
92
|
|
|
} |
|
93
|
|
|
if ($oldName != $newName) { |
|
94
|
|
|
$created = $this->driver->execute($create); |
|
95
|
|
|
$dropped = $this->driver->execute($drop); |
|
96
|
|
|
// $this->executeSavedQuery(!($created && $this->driver->execute($drop))); |
|
97
|
|
|
if (!$dropped && $created) { |
|
98
|
|
|
$this->driver->execute($dropCreated); |
|
99
|
|
|
} |
|
100
|
|
|
return 'altered'; |
|
101
|
|
|
} |
|
102
|
|
|
$this->executeSavedQuery(!($this->driver->execute($test) && |
|
103
|
|
|
$this->driver->execute($dropTest) && |
|
104
|
|
|
$this->driver->execute($drop) && $this->driver->execute($create))); |
|
105
|
|
|
return 'altered'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|