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

OraclePDB::getListDatabasesSqlAction()   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
/**
10
 * This DBManager uses PDB as 'database'. As such, it does not support oracle <= 11
11
 * Also, Oracle XE has a fairly stringent limit on PDBS that can be concurrently created (3 total in 18c...)
12
 */
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...
13
class OraclePDB extends BaseManager implements DatabaseManager
14
{
15
    /**
16
     * Returns the sql 'action' used to list all available databases
17
     * @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...
18
     * @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...
19
     */
20
    public function getListDatabasesSqlAction()
21
    {
22
        return new Command(
23
            "SELECT pdb_name AS Database FROM dba_pdbs ORDER BY pdb_name;",
24
            function ($output, $executor) {
25
                /** @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...
26
                return $executor->resultSetToArray($output);
27
            }
28
        );
29
    }
30
31
    /**
32
     * Returns the sql 'action' used to create a new db and accompanying user.
33
     * NB: the 'new database' is created as pdb and the new user is created as local to the pdb
34
     * NB: needs SYS connection...
35
     * @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...
36
     * @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...
37
     * @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...
38
     * @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...
39
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
40
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
41
     * @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...
42
     */
43
    public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null)
44
    {
45
        //$collation = $this->getCollationName($charset);
46
47
        /// @todo throw if $charset is not the same as the db one
48
49
        if ($userName == '') {
50
            throw new \Exception("Can not create a PDB database on Oracle without an accompanying user");
51
        }
52
53
        $statements = [
54
            "CREATE PLUGGABLE DATABASE \"$dbName\" ADMIN USER $userName IDENTIFIED BY \"$password\" CREATE_FILE_DEST='/opt/oracle/oradata';",
55
            "ALTER PLUGGABLE DATABASE \"$dbName\" OPEN READ WRITE;"
56
        ];
57
58
        return new Command($statements);
59
    }
60
61
    /**
62
     * Returns the sql 'action' used to drop a db and associated user account
63
     * @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...
64
     * @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...
65
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
66
     * @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...
67
     * @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...
68
     */
69
    public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false)
70
    {
71
        if ($userName != '') {
72
            /// @todo check if $userName is not local to $dbName
73
            ///       select pdb_name from cdb_users, dba_pdbs where dba_pdbs.pdb_id = cdb_users.con_id and username = xxx
74
        }
75
76
        /// @todo check support for IF EXISTS
77
        $statements = [
78
            "ALTER PLUGGABLE DATABASE \"$dbName\" CLOSE;",
79
            "DROP PLUGGABLE DATABASE \"$dbName\" INCLUDING DATAFILES;",
80
        ];
81
82
        return new Command($statements);
83
    }
84
85
    /**
86
     * Returns the sql 'action' used to list all existing db users
87
     * @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...
88
     */
89
    public function getListUsersSqlAction()
90
    {
91
        return new Command(
92
            // NB: we filter out 'system' users, as there are many...
93
            "SELECT username FROM cdb_users WHERE oracle_maintained != 'Y' ORDER BY username;",
94
            function ($output, $executor) {
95
                /** @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...
96
                return $executor->resultSetToArray($output);
97
            }
98
        );
99
    }
100
101
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
102
     * @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...
103
     * @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...
104
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
105
     * @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...
106
     */
107
    public function getDropUserSqlAction($userName, $ifExists = false)
108
    {
109
        /// @todo check which pdb the user is in, and switch to it
110
        ///       select pdb_name from cdb_users, dba_pdbs where dba_pdbs.pdb_id = cdb_users.con_id and username = xxx
111
        ///       alter session set container = xxx;
112
        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...
113
            "DROP USER $userName CASCADE;",
114
        ]);
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...
115
    }
116
117
    /**
118
     * Returns the sql 'action' used to list all available ('valid') collations
119
     * @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...
120
     */
121
    public function getListCollationsSqlAction()
122
    {
123
        return new Command(
124
            "SELECT value AS Collation FROM v\$nls_valid_values WHERE parameter = 'CHARACTERSET' ORDER BY value;",
125
            function ($output, $executor) {
126
                /** @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...
127
                return $executor->resultSetToArray($output);
128
            }
129
        );
130
    }
131
132
    /**
133
     * Returns the sql 'action' used to retrieve the db instance version info
134
     * @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...
135
     */
136
    public function getRetrieveVersionInfoSqlAction()
137
    {
138
        return new Command(
139
            "SELECT version_full || ' (' || banner || ')' AS version FROM  v\$version, v\$instance;",
140
            function ($output, $executor) {
141
                /** @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...
142
                return $executor->resultSetToArray($output)[0];
143
            }
144
        );
145
    }
146
147
    /**
148
     * Transform collation name into a supported one
149
     * @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...
150
     * @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...
151
     * @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...
152
     *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names
153
     */
154
    /*protected function getCollationName($charset)
155
    {
156
        if ($charset == null) {
157
            return null;
158
        }
159
160
        $charset = trim(strtolower($charset));
161
162
        // accept official iana charset name, but most dbs prefer 'utf8'...
163
        if ($charset == 'utf-8') {
164
            $charset = 'utf8';
165
        }
166
167
        if ($charset == 'utf8') {
168
            $charset = 'AL32UTF8';
169
        }
170
171
        return $charset;
172
    }*/
173
}
174