Passed
Branch main (217991)
by Thierry
02:18
created

Server::statusVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\RoutineEntity;
6
7
use Lagdo\DbAdmin\Driver\Db\Server as AbstractServer;
8
9
class Server extends AbstractServer
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function databases(bool $flush)
15
    {
16
        // !!! Caching and slow query handling are temporarily disabled !!!
17
        $query = $this->driver->minVersion(5) ?
18
            "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME" :
19
            "SHOW DATABASES";
20
        return $this->driver->values($query);
21
22
        // SHOW DATABASES can take a very long time so it is cached
23
        // $databases = get_session("dbs");
24
        // if ($databases === null) {
25
        //     $query = ($this->driver->minVersion(5)
26
        //         ? "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME"
27
        //         : "SHOW DATABASES"
28
        //     ); // SHOW DATABASES can be disabled by skip_show_database
29
        //     $databases = ($flush ? slow_query($query) : $this->driver->values($query));
30
        //     restart_session();
31
        //     set_session("dbs", $databases);
32
        //     stop_session();
33
        // }
34
        // return $databases;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function databaseSize(string $database)
41
    {
42
        $statement = $this->connection->query("SELECT SUM(data_length + index_length) " .
43
            "FROM information_schema.tables where table_schema=" . $this->driver->quote($database));
44
        if (is_object($statement) && ($row = $statement->fetchRow())) {
45
            return intval($row[0]);
46
        }
47
        return 0;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function databaseCollation(string $database, array $collations)
54
    {
55
        $collation = null;
56
        $create = $this->connection->result("SHOW CREATE DATABASE " . $this->driver->escapeId($database), 1);
57
        if (preg_match('~ COLLATE ([^ ]+)~', $create, $match)) {
58
            $collation = $match[1];
59
        } elseif (preg_match('~ CHARACTER SET ([^ ]+)~', $create, $match)) {
60
            // default collation
61
            $collation = $collations[$match[1]][-1];
62
        }
63
        return $collation;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function engines()
70
    {
71
        $engines = [];
72
        foreach ($this->driver->rows("SHOW ENGINES") as $row) {
73
            if (preg_match("~YES|DEFAULT~", $row["Support"])) {
74
                $engines[] = $row["Engine"];
75
            }
76
        }
77
        return $engines;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function collations()
84
    {
85
        $collations = [];
86
        foreach ($this->driver->rows("SHOW COLLATION") as $row) {
87
            if ($row["Default"]) {
88
                $collations[$row["Charset"]][-1] = $row["Collation"];
89
            } else {
90
                $collations[$row["Charset"]][] = $row["Collation"];
91
            }
92
        }
93
        ksort($collations);
94
        foreach ($collations as $key => $val) {
95
            asort($collations[$key]);
96
        }
97
        return $collations;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function isInformationSchema(string $database)
104
    {
105
        return ($this->driver->minVersion(5) && $database == "information_schema")
106
            || ($this->driver->minVersion(5.5) && $database == "performance_schema");
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function createDatabase(string $database, string $collation)
113
    {
114
        $result = $this->driver->execute("CREATE DATABASE " . $this->driver->escapeId($database) .
115
            ($collation ? " COLLATE " . $this->driver->quote($collation) : ""));
116
        return $result !== false;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function dropDatabases(array $databases)
123
    {
124
        return $this->driver->applyQueries("DROP DATABASE", $databases, function ($database) {
125
            return $this->driver->escapeId($database);
126
        });
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function renameDatabase(string $name, string $collation)
133
    {
134
        $renamed = false;
135
        if ($this->createDatabase($name, $collation)) {
136
            $tables = [];
137
            $views = [];
138
            foreach ($this->tables() as $table => $type) {
0 ignored issues
show
Bug introduced by
The method tables() does not exist on Lagdo\DbAdmin\Driver\MySql\Db\Server. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
            foreach ($this->/** @scrutinizer ignore-call */ tables() as $table => $type) {

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.

Loading history...
139
                if ($type == 'VIEW') {
140
                    $views[] = $table;
141
                } else {
142
                    $tables[] = $table;
143
                }
144
            }
145
            $renamed = (!$tables && !$views) || $this->driver->moveTables($tables, $views, $name);
146
            $this->dropDatabases($renamed ? [$this->driver->database()] : []);
147
        }
148
        return $renamed;
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function routineLanguages()
155
    {
156
        return []; // "SQL" not required
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function variables()
163
    {
164
        return $this->driver->keyValues("SHOW VARIABLES");
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170
    public function processes()
171
    {
172
        return $this->driver->rows("SHOW FULL PROCESSLIST");
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    public function statusVariables()
179
    {
180
        return $this->driver->keyValues("SHOW STATUS");
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    // public function killProcess($val)
187
    // {
188
    //     return $this->driver->execute("KILL " . $this->util->number($val));
189
    // }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    // public function maxConnections()
195
    // {
196
    //     return $this->connection->result("SELECT @@max_connections");
197
    // }
198
}
199