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

MySQL::getListCollationsSqlAction()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 2
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 MySQL extends BaseManager implements DatabaseManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class MySQL
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
        /// @todo use 'SHOW DATABASES' for versions < 5
20
            "SELECT SCHEMA_NAME AS 'Database' FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME;",
21
            function ($output, $executor) {
22
                /** @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...
23
                return $executor->resultSetToArray($output);
24
            }
25
        );
26
    }
27
28
    /**
29
     * Returns the sql 'action' used to create a new db and accompanying user
30
     * @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...
31
     * @param string $userName Max 16 chars for MySQL 5.5
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @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...
33
     * @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...
34
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
35
     * @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...
36
     */
37
    public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null)
38
    {
39
        $collation = $this->getCollationName($charset);
40
41
        /// @todo if mysql version is bigger than 8.0, add `WITH mysql_native_password`
42
        $statements = [
43
            "CREATE DATABASE `$dbName`" . ($collation !== null ? " CHARACTER SET $collation" : '') . ';'
44
        ];
45
        if ($userName != '') {
46
            $statements[] = "CREATE USER '$userName'@'%' IDENTIFIED BY '$password';";
47
            $statements[] = "GRANT ALL PRIVILEGES ON `$dbName`.* TO '$userName'@'%';";
48
        }
49
        return new Command($statements);
50
    }
51
52
    /**
53
     * Returns the sql 'action' used to drop a db and associated user account
54
     * @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...
55
     * @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...
56
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
57
     * @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...
58
     * @bug $ifExists = true not fully supported
0 ignored issues
show
Coding Style introduced by
Tag @bug cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @bug tag indented incorrectly; expected 4 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
            /// @todo since mysql 5.7, 'DROP USER IF EXISTS' is supported. We could use it...
73
            $statements[] = "DROP USER '$userName'@'%';";
74
        }
75
        return new Command($statements);
76
    }
77
78
    /**
79
     * Returns the sql 'action' used to list all existing db users
80
     * @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...
81
     */
82
    public function getListUsersSqlAction()
83
    {
84
        return new Command(
85
            'SELECT DISTINCT User FROM mysql.user ORDER BY User;',
86
            function ($output, $executor) {
87
                /** @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...
88
                return $executor->resultSetToArray($output);
89
            }
90
        );
91
    }
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @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...
95
     * @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...
96
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
97
     * @bug $ifExists = true not supported
0 ignored issues
show
Coding Style introduced by
Tag @bug cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @bug tag indented incorrectly; expected 4 spaces but found 1
Loading history...
98
     * @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...
99
     */
100
    public function getDropUserSqlAction($userName, $ifExists = false)
101
    {
102
        /*$ifClause = '';
103
        if ($ifExists) {
104
            $ifClause = 'IF EXISTS';
105
        }*/
106
107
        /// @todo since mysql 5.7, 'DROP USER IF EXISTS' is supported. We could use it...
108
        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...
109
            "DROP USER '$userName'@'%';"
110
        ]);
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...
111
    }
112
113
    /**
114
     * Returns the sql 'action' used to list all available collations
115
     * @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...
116
     */
117
    public function getListCollationsSqlAction()
118
    {
119
        return new Command(
120
            'SHOW COLLATION;',
121
            function ($output, $executor) {
122
                /** @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...
123
                $lines = $executor->resultSetToArray($output);
124
                $out = [];
125
                foreach($lines as $line) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
126
                    $parts = explode("|", $line, 3);
127
                    $out[] = trim($parts[0]) . ' (' . trim($parts[1]) .')';
128
                }
129
                return $out;
130
            }
131
        );
132
    }
133
134
    /**
135
     * Returns the sql 'action' used to retrieve the db instance version info
136
     * @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...
137
     */
138
    public function getRetrieveVersionInfoSqlAction()
139
    {
140
        return new Command(
141
            'SHOW VARIABLES LIKE "version";',
142
            function ($output, $executor) {
143
                /** @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...
144
                $line = $executor->resultSetToArray($output)[0];
145
                $parts = explode('|', $line);
146
                return trim($parts[1]);
147
            }
148
        );
149
    }
150
151
    /**
152
     * Transform collation name into a supported one
153
     * @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...
154
     * @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...
155
     * @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...
156
     *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names
157
     */
158
    protected function getCollationName($charset)
159
    {
160
        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...
161
            return null;
162
        }
163
164
        $charset = trim(strtolower($charset));
165
166
        // accept official iana charset name, but most dbs prefer 'utf8'...
167
        if ($charset == 'utf-8') {
168
            $charset = 'utf8';
169
        }
170
171
        return $charset;
172
    }
173
}
174