SQLite   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 36
c 1
b 0
f 0
dl 0
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getListDatabasesSqlAction() 0 11 2
A getDropUserSqlAction() 0 5 1
A getListUsersSqlAction() 0 7 1
A getDropDatabaseSqlAction() 0 11 3
A getListCollationsSqlAction() 0 7 1
A getCreateDatabaseSqlAction() 0 11 1
A getRetrieveVersionInfoSqlAction() 0 7 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
class SQLite extends BaseManager implements DatabaseManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SQLite
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
        $fileGlob = dirname($this->databaseConfiguration['path']) . '/*.sqlite';
19
        return new Command(
20
            null,
21
            function() use ($fileGlob) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
22
                $out = [];
23
                foreach (glob($fileGlob) as $filename) {
24
                    $out[] =  basename($filename);
25
                }
26
                return $out;
27
            }
28
        );
29
    }
30
31
    /**
32
     * Returns the sql 'action' used to create a new db and accompanying user
33
     * @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...
34
     * @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...
35
     * @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...
36
     * @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...
37
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
38
     * @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...
39
     */
40
    public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null)
41
    {
42
        //$collation = $this->getCollationName($charset);
43
44
        /// @todo throw if $charset is not supported
45
46
        /// @todo this does not support creation of the new db with a different character encoding...
47
        ///       see https://stackoverflow.com/questions/21348459/set-pragma-encoding-utf-16-for-main-database-in-sqlite
48
        $filename = dirname($this->databaseConfiguration['path']) . '/' . $dbName . '.sqlite';
49
        return new Command(
50
            "ATTACH '$filename' AS \"$dbName\";"
51
        );
52
    }
53
54
    /**
55
     * Returns the sql 'action' used to drop a db and associated user account
56
     * @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...
57
     * @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...
58
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
59
     * @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...
60
     * @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...
61
     */
62
    public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false)
63
    {
64
        $filename = dirname($this->databaseConfiguration['path']) . '/' . $dbName . '.sqlite';
65
        return new Command(
66
            null,
67
            function() use($filename, $dbName, $ifExists) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
68
                if (is_file($filename)) {
69
                    unlink($filename);
70
                } else {
71
                    if (!$ifExists) {
72
                        throw new \Exception("Can not drop database '$dbName': file not found");
73
                    }
74
                }
75
            }
76
        );
77
    }
78
79
    /**
80
     * Returns the sql 'action' used to list all existing db users
81
     * @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...
82
     */
83
    public function getListUsersSqlAction()
84
    {
85
        return new Command(
86
            null,
87
            function () {
88
                // since sqlite does not support users, null seems more appropriate than an empty array...
89
                return null;
90
            }
91
        );
92
    }
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
95
     * @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...
96
     * @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...
97
     * @return Command
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
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
        return new Command(
103
            null,
104
            function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
105
                /// @todo should we return something ?
106
            }
107
        );
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
            null,
118
            /// @todo list the supported utf16 variants as soon as allow using them
119
            function () {
120
                return [];
121
            }
122
        );
123
        /*return [
124
            // q: are these comparable to other databases ? we probably should instead list the values for https://www.sqlite.org/pragma.html#pragma_encoding
125
            'PRAGMA collation_list;',
126
            function ($output, $executor) {
127
                $out = [];
128
                foreach(explode("\n", $output) as $line) {
129
                    $out[] = explode("|", $line, 2)[1];
130
                }
131
                sort($out);
132
                return implode("\n", $out);
133
            }
134
        ];*/
135
    }
136
137
    /**
138
     * Returns the sql 'action' used to retrieve the db instance version info
139
     * @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...
140
     */
141
    public function getRetrieveVersionInfoSqlAction()
142
    {
143
        return new Command(
144
            "select sqlite_version();",
145
            function ($output, $executor) {
146
                /** @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...
147
                return $executor->resultSetToArray($output)[0];
148
            }
149
        );
150
    }
151
152
    /**
153
     * Transform collation name into a supported one
154
     * @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...
155
     * @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...
156
     * @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...
157
     *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names
158
     */
159
    /*protected function getCollationName($charset)
160
    {
161
        if ($charset == null) {
162
            return null;
163
        }
164
165
        $charset = trim(strtolower($charset));
166
167
        // accept official iana charset name, but most dbs prefer 'utf8'...
168
        if ($charset == 'utf-8') {
169
            $charset = 'utf8';
170
        }
171
172
        return $charset;
173
    }*/
174
}
175