Passed
Push — master ( c56107...76bdd0 )
by Gaetano
16:10
created

PostgreSQL::getListCollationsSqlAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Db3v4l\Core\DatabaseManager;
4
5
use Db3v4l\API\Interfaces\DatabaseManager;
6
use Db3v4l\API\Interfaces\SqlExecutor\Executor;
7
use Db3v4l\Core\SqlAction\Command;
8
9
class PostgreSQL extends BaseManager implements DatabaseManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PostgreSQL
Loading history...
10
{
11
    /**
12
     * Returns the sql 'action' used to list all available databases
13
     * @return Command
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
14
     * @todo for each database, retrieve the charset/collation
0 ignored issues
show
Coding Style introduced by
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
15
     */
16
    public function getListDatabasesSqlAction()
17
    {
18
        return new Command(
19
            'SELECT datname AS "Database" FROM pg_database ORDER BY datname;',
20
            function ($output, $executor) {
21
                /** @var Executor $executor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
                return $executor->resultSetToArray($output);
23
            }
24
        );
25
    }
26
27
    /**
28
     * Returns the sql 'action' used to create a new db and accompanying user
29
     * @param string $dbName Max 63 chars for Postgres
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
30
     * @param string $userName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
31
     * @param string $password
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @param string $charset charset/collation name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
34
     * @todo prevent sql injection!
0 ignored issues
show
Coding Style introduced by
Tag @todo cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
35
     */
36
    public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null)
37
    {
38
        $collation = $this->getCollationName($charset);
39
40
        $statements = [
41
            // q: do we need to add 'TEMPLATE template0' ?
42
            //    see f.e. https://www.vertabelo.com/blog/collations-in-postgresql/
43
            "CREATE DATABASE \"$dbName\"" . ($collation !== null ? " ENCODING $collation" : '') . ';',
44
        ];
45
        if ($userName != '') {
46
            $statements[] = "COMMIT;";
47
            $statements[] = "CREATE USER \"$userName\" WITH PASSWORD '$password'" . ';';
48
            $statements[] = "GRANT ALL ON DATABASE \"$dbName\" TO \"$userName\""; // q: should we avoid granting CREATE?
49
        }
50
        return new Command($statements);
51
    }
52
53
    /**
54
     * Returns the sql 'action' used to drop a db and associated user account
55
     * @param string $dbName
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
56
     * @param string $userName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
58
     * @param bool $ifExists
0 ignored issues
show
Coding Style introduced by
Tags must be grouped together in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
59
     * @todo prevent sql injection!
0 ignored issues
show
Coding Style introduced by
Tag @todo cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
60
     */
61
    public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false)
62
    {
63
        $ifClause = '';
64
        if ($ifExists) {
65
            $ifClause = 'IF EXISTS';
66
        }
67
68
        $statements = [
69
            "DROP DATABASE {$ifClause} \"$dbName\";",
70
        ];
71
        if ($userName != '') {
72
            $statements[] = "DROP USER {$ifClause} \"$userName\";";
73
        }
74
        return new Command($statements);
75
    }
76
77
    /**
78
     * Returns the sql 'action' used to list all existing db users
79
     * @return Command
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
80
     */
81
    public function getListUsersSqlAction()
82
    {
83
        return new Command(
84
            'SELECT usename AS "User" FROM pg_catalog.pg_user ORDER BY usename;',
85
            function ($output, $executor) {
86
                /** @var Executor $executor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
87
                return $executor->resultSetToArray($output);
88
            }
89
        );
90
    }
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @param string $userName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
94
     * @param bool $ifExists
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
95
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
96
     * @todo prevent sql injection!
0 ignored issues
show
Coding Style introduced by
Tag @todo cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
97
     */
98
    public function getDropUserSqlAction($userName, $ifExists = false)
99
    {
100
        $ifClause = '';
101
        if ($ifExists) {
102
            $ifClause = 'IF EXISTS';
103
        }
104
105
        return new Command([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
106
            "DROP USER {$ifClause} \"$userName\";"
107
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
108
    }
109
110
    /**
111
     * Returns the sql 'action' used to list all available collations
112
     * @return Command
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
113
     */
114
    public function getListCollationsSqlAction()
115
    {
116
        return new Command(
117
            'SELECT collname AS Collation FROM pg_collation ORDER BY collname',
118
            function ($output, $executor) {
119
                /** @var Executor $executor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
120
                return $executor->resultSetToArray($output);
121
            }
122
        );
123
    }
124
125
    /**
126
     * Returns the sql 'action' used to retrieve the db instance version info
127
     * @return Command
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
128
     */
129
    public function getRetrieveVersionInfoSqlAction()
130
    {
131
        return new Command(
132
            'SHOW server_version;',
133
            function ($output, $executor) {
134
                /** @var Executor $executor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
135
                return $executor->resultSetToArray($output)[0];
136
            }
137
        );
138
    }
139
140
    /**
141
     * Transform collation name into a supported one
142
     * @param null|string $charset so far only 'utf8' is supported...
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
143
     * @return null|string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
144
     * @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ?
0 ignored issues
show
Coding Style introduced by
Tag @todo cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
145
     *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names
146
     */
147
    protected function getCollationName($charset)
148
    {
149
        if ($charset == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $charset of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
150
            return null;
151
        }
152
153
        $charset = trim(strtolower($charset));
154
155
        // accept official iana charset name, but most dbs prefer 'utf8'...
156
        if ($charset == 'utf-8') {
157
            $charset = 'utf8';
158
        }
159
160
        return $charset;
161
    }
162
}
163