Passed
Push — main ( c04b9d...0045ac )
by Thierry
09:23 queued 07:24
created

Driver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 68
c 11
b 0
f 0
dl 0
loc 186
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createConnection() 0 11 4
A name() 0 3 1
A initConnection() 0 20 2
A error() 0 4 1
A connect() 0 20 5
A initConfig() 0 11 1
A support() 0 4 1
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql;
4
5
use Lagdo\DbAdmin\Driver\Driver as AbstractDriver;
6
use Lagdo\DbAdmin\Driver\Exception\AuthException;
7
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection;
8
9
class Driver extends AbstractDriver
10
{
11
    /**
12
     * Features not available
13
     *
14
     * @var array
15
     */
16
    private $features = ['scheme', 'sequence', 'type', 'view_trigger', 'materializedview'];
17
18
    /**
19
     * Data types
20
     *
21
     * @var array
22
     */
23
    private $types = [
24
        'Numbers' => ["tinyint" => 3, "smallint" => 5, "mediumint" => 8, "int" => 10,
25
            "bigint" => 20, "decimal" => 66, "float" => 12, "double" => 21],
26
        'Date and time' => ["date" => 10, "datetime" => 19, "timestamp" => 19, "time" => 10, "year" => 4],
27
        'Strings' => ["char" => 255, "varchar" => 65535, "tinytext" => 255,
28
            "text" => 65535, "mediumtext" => 16777215, "longtext" => 4294967295],
29
        'Lists' => ["enum" => 65535, "set" => 64],
30
        'Binary' => ["bit" => 20, "binary" => 255, "varbinary" => 65535, "tinyblob" => 255,
31
            "blob" => 65535, "mediumblob" => 16777215, "longblob" => 4294967295],
32
        'Geometry' => ["geometry" => 0, "point" => 0, "linestring" => 0, "polygon" => 0,
33
            "multipoint" => 0, "multilinestring" => 0, "multipolygon" => 0, "geometrycollection" => 0],
34
    ];
35
36
    /**
37
     * Number variants
38
     *
39
     * @var array
40
     */
41
    private $unsigned = ["unsigned", "zerofill", "unsigned zerofill"];
42
43
    /**
44
     * Operators used in select
45
     *
46
     * @var array
47
     */
48
    private $operators = ["=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%",
49
        "REGEXP", "IN", "FIND_IN_SET", "IS NULL", "NOT LIKE", "NOT REGEXP",
50
        "NOT IN", "IS NOT NULL", "SQL"];
51
52
    /**
53
     * Functions used in select
54
     *
55
     * @var array
56
     */
57
    private $functions = ["char_length", "date", "from_unixtime", "lower",
58
        "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"];
59
60
    /**
61
     * Grouping functions used in select
62
     *
63
     * @var array
64
     */
65
    private $grouping = ["avg", "count", "count distinct", "group_concat", "max", "min", "sum"];
66
67
    /**
68
     * Functions used to edit data
69
     *
70
     * @var array
71
     */
72
    private $editFunctions = [[
73
        "char" => "md5/sha1/password/encrypt/uuid",
74
        "binary" => "md5/sha1",
75
        "date|time" => "now",
76
    ],[
77
        // $this->numberRegex() => "+/-",
78
        "date" => "+ interval/- interval",
79
        "time" => "addtime/subtime",
80
        "char|text" => "concat",
81
    ]];
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function name()
87
    {
88
        return "MySQL";
89
    }
90
91
    /**
92
     * Initialize a new connection
93
     *
94
     * @param AbstractConnection $connection
95
     *
96
     * @return AbstractConnection
97
     */
98
    private function initConnection(AbstractConnection $connection)
99
    {
100
        if ($this->connection === null) {
101
            $this->connection = $connection;
102
            $this->server = new Db\Server($this, $this->util, $this->trans);
103
            $this->database = new Db\Database($this, $this->util, $this->trans);
104
            $this->table = new Db\Table($this, $this->util, $this->trans);
105
            $this->query = new Db\Query($this, $this->util, $this->trans);
106
            $this->grammar = new Db\Grammar($this, $this->util, $this->trans);
107
        }
108
        // if (!$connection->open($this->options('server'), $this->options())) {
109
        //     $error = $this->error();
110
        //     // windows-1250 - most common Windows encoding
111
        //     if (function_exists('iconv') && !$this->util->isUtf8($error) &&
112
        //         strlen($s = iconv("windows-1250", "utf-8", $error)) > strlen($error)) {
113
        //         $error = $s;
114
        //     }
115
        //     throw new AuthException($error);
116
        // }
117
        return $connection;
118
    }
119
120
    /**
121
     * @inheritDoc
122
     * @throws AuthException
123
     */
124
    public function createConnection()
125
    {
126
        if (!$this->options('prefer_pdo', false) && extension_loaded("mysqli")) {
0 ignored issues
show
Unused Code introduced by
The call to Lagdo\DbAdmin\Driver\Driver::options() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        if (!$this->/** @scrutinizer ignore-call */ options('prefer_pdo', false) && extension_loaded("mysqli")) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
127
            $connection = new Db\MySqli\Connection($this, $this->util, $this->trans, 'MySQLi');
128
            return $this->initConnection($connection);
129
        }
130
        if (extension_loaded("pdo_mysql")) {
131
            $connection = new Db\Pdo\Connection($this, $this->util, $this->trans, 'PDO_MySQL');
132
            return $this->initConnection($connection);
133
        }
134
        throw new AuthException($this->trans->lang('No package installed to connect to a MySQL server.'));
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function connect(string $database, string $schema)
141
    {
142
        parent::connect($database, $schema);
143
144
        if (!$this->minVersion(8)) {
145
            $this->features[] = 'descidx';
146
            if (!$this->minVersion(5.1)) {
147
                $this->features[] = 'event';
148
                $this->features[] = 'partitioning';
149
                if (!$this->minVersion(5)) {
150
                    $this->features[] = 'routine';
151
                    $this->features[] = 'trigger';
152
                    $this->features[] = 'view';
153
                }
154
            }
155
        }
156
157
        if ($this->minVersion('5.7.8', 10.2)) {
158
            $this->config->structuredTypes[$this->trans->lang('Strings')][] = "json";
159
            $this->config->types["json"] = 4294967295;
160
        }
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function support(string $feature)
167
    {
168
        // $this->features contains features that are not available.
169
        return !in_array($feature, $this->features);
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    protected function initConfig()
176
    {
177
        $this->config->jush = 'sql';
178
        $this->config->drivers = ["MySQLi", "PDO_MySQL"];
179
        $this->config->setTypes($this->types, $this->trans);
180
        $this->config->unsigned = $this->unsigned;
181
        $this->config->operators = $this->operators;
182
        $this->config->functions = $this->functions;
183
        $this->config->grouping = $this->grouping;
184
        $this->config->editFunctions = $this->editFunctions;
185
        $this->config->editFunctions[1][$this->numberRegex()] = "+/-";
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function error()
192
    {
193
        $error = preg_replace('~^You have an error.*syntax to use~U', 'Syntax error', parent::error());
194
        return $this->util->html($error);
195
    }
196
}
197