Passed
Push — main ( 64ae32...ac06bb )
by Thierry
02:16
created

Server::renameDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\Server as AbstractServer;
6
use Lagdo\DbAdmin\Driver\Db\StatementInterface;
7
8
use function array_key_exists;
9
use function is_a;
10
use function intval;
11
use function preg_match;
12
13
class Server extends AbstractServer
14
{
15
    /**
16
     * @inheritDoc
17
     */
18
    public function databases(bool $flush)
19
    {
20
        // !!! Caching and slow query handling are temporarily disabled !!!
21
        $query = $this->driver->minVersion(5) ?
22
            "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME" :
23
            "SHOW DATABASES";
24
        return $this->driver->values($query);
25
26
        // SHOW DATABASES can take a very long time so it is cached
27
        // $databases = get_session("dbs");
28
        // if ($databases === null) {
29
        //     $query = ($this->driver->minVersion(5)
30
        //         ? "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME"
31
        //         : "SHOW DATABASES"
32
        //     ); // SHOW DATABASES can be disabled by skip_show_database
33
        //     $databases = ($flush ? slow_query($query) : $this->driver->values($query));
34
        //     restart_session();
35
        //     set_session("dbs", $databases);
36
        //     stop_session();
37
        // }
38
        // return $databases;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function databaseSize(string $database)
45
    {
46
        $statement = $this->driver->execute("SELECT SUM(data_length + index_length) " .
47
            "FROM information_schema.tables where table_schema=" . $this->driver->quote($database));
48
        if (is_a($statement, StatementInterface::class) && ($row = $statement->fetchRow())) {
49
            return intval($row[0]);
50
        }
51
        return 0;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function databaseCollation(string $database, array $collations)
58
    {
59
        $collation = null;
60
        $create = $this->driver->result("SHOW CREATE DATABASE " . $this->driver->escapeId($database), 1);
61
        if (preg_match('~ COLLATE ([^ ]+)~', $create, $match)) {
62
            $collation = $match[1];
63
        } elseif (preg_match('~ CHARACTER SET ([^ ]+)~', $create, $match)) {
64
            // default collation
65
            $collation = $collations[$match[1]][-1];
66
        }
67
        return $collation;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function engines()
74
    {
75
        $engines = [];
76
        foreach ($this->driver->rows("SHOW ENGINES") as $row) {
77
            if (preg_match("~YES|DEFAULT~", $row["Support"])) {
78
                $engines[] = $row["Engine"];
79
            }
80
        }
81
        return $engines;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function collations()
88
    {
89
        $collations = [];
90
        foreach ($this->driver->rows("SHOW COLLATION") as $row) {
91
            if ($row["Default"]) {
92
                $collations[$row["Charset"]][-1] = $row["Collation"];
93
            } else {
94
                $collations[$row["Charset"]][] = $row["Collation"];
95
            }
96
        }
97
        ksort($collations);
98
        foreach ($collations as $key => $val) {
99
            asort($collations[$key]);
100
        }
101
        return $collations;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function isInformationSchema(string $database)
108
    {
109
        return ($this->driver->minVersion(5) && $database == "information_schema")
110
            || ($this->driver->minVersion(5.5) && $database == "performance_schema");
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function createDatabase(string $database, string $collation)
117
    {
118
        $result = $this->driver->execute("CREATE DATABASE " . $this->driver->escapeId($database) .
119
            ($collation ? " COLLATE " . $this->driver->quote($collation) : ""));
120
        return $result !== false;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function dropDatabases(array $databases)
127
    {
128
        return $this->driver->applyQueries("DROP DATABASE", $databases, function ($database) {
129
            return $this->driver->escapeId($database);
130
        });
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function renameDatabase(string $name, string $collation)
137
    {
138
        // The feature is not natively provided by latest MySQL versions, thus it is disabled here.
139
        return false;
140
        /*$renamed = false;
141
        if ($this->createDatabase($name, $collation)) {
142
            $tables = [];
143
            $views = [];
144
            foreach ($this->driver->tables() as $table => $type) {
145
                if ($type == 'VIEW') {
146
                    $views[] = $table;
147
                } else {
148
                    $tables[] = $table;
149
                }
150
            }
151
            $renamed = (!$tables && !$views) || $this->driver->moveTables($tables, $views, $name);
152
            $this->dropDatabases($renamed ? [$this->driver->database()] : []);
153
        }
154
        return $renamed;*/
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function routineLanguages()
161
    {
162
        return []; // "SQL" not required
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function variables()
169
    {
170
        return $this->driver->keyValues("SHOW VARIABLES");
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function processes()
177
    {
178
        return $this->driver->rows("SHOW FULL PROCESSLIST");
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184
    public function processAttr(array $process, string $key, string $val): string
185
    {
186
        $match = array_key_exists('Command', $process) && preg_match("~Query|Killed~", $process["Command"]);
187
        if ($key == "Info" && $match && $val != "") {
188
            return '<code>' . $this->util->shortenUtf8($val, 50) . '</code>' . $this->trans->lang('Clone');
189
        }
190
        return parent::processAttr($process, $key, $val);
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196
    public function statusVariables()
197
    {
198
        return $this->driver->keyValues("SHOW STATUS");
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    // public function killProcess($val)
205
    // {
206
    //     return $this->driver->execute("KILL " . $this->util->number($val));
207
    // }
208
209
    /**
210
     * @inheritDoc
211
     */
212
    // public function maxConnections()
213
    // {
214
    //     return $this->driver->result("SELECT @@max_connections");
215
    // }
216
}
217