Passed
Push — main ( 372952...84cd49 )
by Thierry
04:34 queued 02:54
created

Server::maxConnections()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\PgSql\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\Server as AbstractServer;
6
7
class Server extends AbstractServer
8
{
9
    /**
10
     * @inheritDoc
11
     */
12
    public function databases(bool $flush)
13
    {
14
        return $this->driver->values("SELECT datname FROM pg_database WHERE " .
15
            "has_database_privilege(datname, 'CONNECT') ORDER BY datname");
16
    }
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function databaseSize(string $database)
22
    {
23
        $statement = $this->connection->query("SELECT pg_database_size(" . $this->driver->quote($database) . ")");
24
        if (is_object($statement) && ($row = $statement->fetchRow())) {
25
            return intval($row[0]);
26
        }
27
        return 0;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function databaseCollation(string $database, array $collations)
34
    {
35
        return $this->connection->result("SELECT datcollate FROM pg_database WHERE datname = " . $this->driver->quote($database));
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function collations()
42
    {
43
        //! supported in CREATE DATABASE
44
        return [];
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function isInformationSchema(string $database)
51
    {
52
        return ($database == "information_schema");
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function createDatabase(string $database, string $collation)
59
    {
60
        $result = $this->driver->execute("CREATE DATABASE " . $this->driver->escapeId($database) .
61
            ($collation ? " ENCODING " . $this->driver->escapeId($collation) : ""));
62
        return $result !== false;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function dropDatabases(array $databases)
69
    {
70
        $this->connection->close();
71
        return $this->driver->applyQueries("DROP DATABASE", $databases, function($database) {
72
            return $this->driver->escapeId($database);
73
        });
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function renameDatabase(string $name, string $collation)
80
    {
81
        //! current database cannot be renamed
82
        $currName = $this->driver->escapeId($this->driver->database());
83
        $nextName = $this->driver->escapeId($name);
84
        $result = $this->driver->execute("ALTER DATABASE $currName RENAME TO $nextName");
85
        return $result !== false;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function routineLanguages()
92
    {
93
        return $this->driver->values("SELECT LOWER(lanname) FROM pg_catalog.pg_language");
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function variables()
100
    {
101
        return $this->driver->keyValues("SHOW ALL");
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function processes()
108
    {
109
        return $this->driver->rows("SELECT * FROM pg_stat_activity ORDER BY " . ($this->driver->minVersion(9.2) ? "pid" : "procpid"));
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function processAttr(array $process, string $key, string $val): string
116
    {
117
        if ($key == "current_query" && $val != "<IDLE>") {
118
            return '<code>' . $this->shortenUtf8($val, 50) . '</code>' . $this->lang('Clone');
0 ignored issues
show
Bug introduced by
The method lang() does not exist on Lagdo\DbAdmin\Driver\PgSql\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

118
            return '<code>' . $this->shortenUtf8($val, 50) . '</code>' . $this->/** @scrutinizer ignore-call */ lang('Clone');

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...
Bug introduced by
The method shortenUtf8() does not exist on Lagdo\DbAdmin\Driver\PgSql\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

118
            return '<code>' . $this->/** @scrutinizer ignore-call */ shortenUtf8($val, 50) . '</code>' . $this->lang('Clone');

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...
119
        }
120
        return parent::processAttr($process, $key, $val);
0 ignored issues
show
Bug introduced by
The method processAttr() does not exist on Lagdo\DbAdmin\Driver\Db\Server. Did you maybe mean processes()? ( Ignorable by Annotation )

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

120
        return parent::/** @scrutinizer ignore-call */ processAttr($process, $key, $val);

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...
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function statusVariables()
127
    {
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    // public function killProcess($val)
134
    // {
135
    //     return $this->driver->execute("SELECT pg_terminate_backend(" . $this->util->number($val) . ")");
136
    // }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    // public function connectionId()
142
    // {
143
    //     return "SELECT pg_backend_pid()";
144
    // }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    // public function maxConnections()
150
    // {
151
    //     return $this->connection->result("SHOW max_connections");
152
    // }
153
}
154