Oracle   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 31
c 0
b 0
f 0
dl 0
loc 131
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRetrieveVersionInfoSqlAction() 0 7 1
A getDropDatabaseSqlAction() 0 12 3
A getCreateDatabaseSqlAction() 0 18 3
A getListCollationsSqlAction() 0 7 1
A getListDatabasesSqlAction() 0 7 1
A getDropUserSqlAction() 0 6 1
A getListUsersSqlAction() 0 8 1
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
/**
10
 * This DBManager uses schemas as 'database'.
11
 * NB: if used with Oracle >= 12, usernames have to be prefixed with c## to be 'common' (ie. in the CDB), so you might
12
 *     want to run everything inside a pdb...
13
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class Oracle extends BaseManager implements DatabaseManager
15
{
16
    /**
17
     * Returns the sql 'action' used to list all available databases
18
     * @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...
19
     * @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...
20
     */
21
    public function getListDatabasesSqlAction()
22
    {
23
        return new Command(
24
            "SELECT username AS Database FROM all_users WHERE oracle_maintained != 'Y' ORDER BY username;",
25
            function ($output, $executor) {
26
                /** @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...
27
                return $executor->resultSetToArray($output);
28
            }
29
        );
30
    }
31
32
    /**
33
     * Returns the sql 'action' used to create a new db and accompanying user
34
     * @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...
35
     * @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...
36
     * @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...
37
     * @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...
38
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
39
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
40
     * @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...
41
     */
42
    public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null)
43
    {
44
        //$collation = $this->getCollationName($charset);
45
46
        /// @todo throw if $charset is not the same as the db one
47
48
        if ($userName != '' && $userName != $dbName) {
49
            throw new \Exception("Can not create a database (schema) on Oracle with a different accompanying user");
50
        }
51
52
        // NB: if we use a quoted identifier for the username, logging in becomes more difficult, as it will require quotes too...
53
        $statements = [
54
            "CREATE USER $dbName IDENTIFIED BY \"$password\";",
55
            "GRANT CONNECT, RESOURCE TO $dbName;",
56
            "GRANT UNLIMITED TABLESPACE TO $dbName;",
57
        ];
58
59
        return new Command($statements);
60
    }
61
62
    /**
63
     * Returns the sql 'action' used to drop a db and associated user account
64
     * @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...
65
     * @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...
66
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
67
     * @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...
68
     * @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...
69
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
70
     * @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...
71
     */
72
    public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false)
73
    {
74
        if ($userName != '' && $userName != $dbName) {
75
            throw new \Exception("Can not drop a database (schema) on Oracle with a different accompanying user");
76
        }
77
78
        /// @todo check support for IF EXISTS
79
        $statements = [
80
            "DROP USER $dbName CASCADE;",
81
        ];
82
83
        return new Command($statements);
84
    }
85
86
    /**
87
     * Returns the sql 'action' used to list all existing db users
88
     * @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...
89
     */
90
    public function getListUsersSqlAction()
91
    {
92
        return new Command(
93
            // NB: we filter out 'system' users, as there are many...
94
            "SELECT username FROM sys.all_users WHERE oracle_maintained != 'Y' ORDER BY username;",
95
            function ($output, $executor) {
96
                /** @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...
97
                return $executor->resultSetToArray($output);
98
            }
99
        );
100
    }
101
102
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @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...
104
     * @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...
105
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
106
     * @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...
107
     * @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...
108
     * @todo we should somehow warn users that this destroys the schema too
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...
109
     */
110
    public function getDropUserSqlAction($userName, $ifExists = false)
111
    {
112
        /// @todo check support for IF EXISTS
113
114
        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...
115
            "DROP USER $userName CASCADE;",
116
        ]);
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...
117
    }
118
119
    /**
120
     * Returns the sql 'action' used to list all available collations
121
     * @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...
122
     */
123
    public function getListCollationsSqlAction()
124
    {
125
        return new Command(
126
            "SELECT value AS Collation FROM v\$nls_valid_values WHERE parameter = 'CHARACTERSET' ORDER BY value;",
127
            function ($output, $executor) {
128
                /** @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...
129
                return $executor->resultSetToArray($output);
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
            "SELECT version_full || ' (' || banner || ')' AS version FROM  v\$version, v\$instance;",
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
                return $executor->resultSetToArray($output)[0];
145
            }
146
        );
147
    }
148
149
    /**
150
     * Transform collation name into a supported one
151
     * @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...
152
     * @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...
153
     * @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...
154
     *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names
155
     */
156
    /*protected function getCollationName($charset)
157
    {
158
        if ($charset == null) {
159
            return null;
160
        }
161
162
        $charset = trim(strtolower($charset));
163
164
        // accept official iana charset name, but most dbs prefer 'utf8'...
165
        if ($charset == 'utf-8') {
166
            $charset = 'utf8';
167
        }
168
169
        if ($charset == 'utf8') {
170
            $charset = 'AL32UTF8';
171
        }
172
173
        return $charset;
174
    }*/
175
}
176