Passed
Push — main ( 217991...a337bd )
by Thierry
01:33
created

Driver::support()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
8
class Driver extends AbstractDriver
9
{
10
    /**
11
     * Features not available
12
     *
13
     * @var array
14
     */
15
    private $features = ['scheme', 'sequence', 'type', 'view_trigger', 'materializedview'];
16
17
    /**
18
     * Data types
19
     *
20
     * @var array
21
     */
22
    private $types = [
23
        'Numbers' => ["tinyint" => 3, "smallint" => 5, "mediumint" => 8, "int" => 10,
24
            "bigint" => 20, "decimal" => 66, "float" => 12, "double" => 21],
25
        'Date and time' => ["date" => 10, "datetime" => 19, "timestamp" => 19, "time" => 10, "year" => 4],
26
        'Strings' => ["char" => 255, "varchar" => 65535, "tinytext" => 255,
27
            "text" => 65535, "mediumtext" => 16777215, "longtext" => 4294967295],
28
        'Lists' => ["enum" => 65535, "set" => 64],
29
        'Binary' => ["bit" => 20, "binary" => 255, "varbinary" => 65535, "tinyblob" => 255,
30
            "blob" => 65535, "mediumblob" => 16777215, "longblob" => 4294967295],
31
        'Geometry' => ["geometry" => 0, "point" => 0, "linestring" => 0, "polygon" => 0,
32
            "multipoint" => 0, "multilinestring" => 0, "multipolygon" => 0, "geometrycollection" => 0],
33
    ];
34
35
    /**
36
     * Number variants
37
     *
38
     * @var array
39
     */
40
    private $unsigned = ["unsigned", "zerofill", "unsigned zerofill"];
41
42
    /**
43
     * Operators used in select
44
     *
45
     * @var array
46
     */
47
    private $operators = ["=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%",
48
        "REGEXP", "IN", "FIND_IN_SET", "IS NULL", "NOT LIKE", "NOT REGEXP",
49
        "NOT IN", "IS NOT NULL", "SQL"];
50
51
    /**
52
     * Functions used in select
53
     *
54
     * @var array
55
     */
56
    private $functions = ["char_length", "date", "from_unixtime", "lower",
57
        "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"];
58
59
    /**
60
     * Grouping functions used in select
61
     *
62
     * @var array
63
     */
64
    private $grouping = ["avg", "count", "count distinct", "group_concat", "max", "min", "sum"];
65
66
    /**
67
     * Functions used to edit data
68
     *
69
     * @var array
70
     */
71
    private $editFunctions = [[
72
        "char" => "md5/sha1/password/encrypt/uuid",
73
        "binary" => "md5/sha1",
74
        "date|time" => "now",
75
    ],[
76
        // $this->numberRegex() => "+/-",
77
        "date" => "+ interval/- interval",
78
        "time" => "addtime/subtime",
79
        "char|text" => "concat",
80
    ]];
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function name()
86
    {
87
        return "MySQL";
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function createConnection()
94
    {
95
        if (extension_loaded("mysqli")) {
96
            $connection = new Db\MySqli\Connection($this, $this->util, $this->trans, 'MySQLi');
97
        }
98
        elseif (extension_loaded("pdo_mysql")) {
99
            $connection = new Db\Pdo\Connection($this, $this->util, $this->trans, 'PDO_MySQL');
100
        }
101
        else {
102
            throw new AuthException($this->trans->lang('No package installed to connect to a MySQL server.'));
103
        }
104
105
        if ($this->connection === null) {
106
            $this->connection = $connection;
107
            $this->server = new Db\Server($this, $this->util, $this->trans, $connection);
108
            $this->database = new Db\Database($this, $this->util, $this->trans, $connection);
109
            $this->table = new Db\Table($this, $this->util, $this->trans, $connection);
110
            $this->query = new Db\Query($this, $this->util, $this->trans, $connection);
111
            $this->grammar = new Db\Grammar($this, $this->util, $this->trans, $connection);
112
        }
113
114
        // if (!$connection->open($this->options('server'), $this->options())) {
115
        //     $error = $this->error();
116
        //     // windows-1250 - most common Windows encoding
117
        //     if (function_exists('iconv') && !$this->util->isUtf8($error) &&
118
        //         strlen($s = iconv("windows-1250", "utf-8", $error)) > strlen($error)) {
119
        //         $error = $s;
120
        //     }
121
        //     throw new AuthException($error);
122
        // }
123
124
        return $connection;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function connect(string $database, string $schema)
131
    {
132
        parent::connect($database, $schema);
133
134
        if (!$this->minVersion(8)) {
135
            $this->features[] = 'descidx';
136
            if (!$this->minVersion(5.1)) {
137
                $this->features[] = 'event';
138
                $this->features[] = 'partitioning';
139
                if (!$this->minVersion(5)) {
140
                    $this->features[] = 'routine';
141
                    $this->features[] = 'trigger';
142
                    $this->features[] = 'view';
143
                }
144
            }
145
        }
146
147
        if ($this->minVersion('5.7.8', 10.2)) {
148
            $this->config->structuredTypes[$this->trans->lang('Strings')][] = "json";
149
            $this->config->types["json"] = 4294967295;
150
        }
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function support(string $feature)
157
    {
158
        // $this->features contains features that are not available.
159
        return !in_array($feature, $this->features);
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    protected function initConfig()
166
    {
167
        $this->config->jush = 'sql';
168
        $this->config->drivers = ["MySQLi", "PDO_MySQL"];
169
        $this->config->setTypes($this->types, $this->trans);
170
        $this->config->unsigned = $this->unsigned;
171
        $this->config->operators = $this->operators;
172
        $this->config->functions = $this->functions;
173
        $this->config->grouping = $this->grouping;
174
        $this->config->editFunctions = $this->editFunctions;
175
        $this->config->editFunctions[1][$this->numberRegex()] = "+/-";
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function error()
182
    {
183
        $error = preg_replace('~^You have an error.*syntax to use~U', 'Syntax error', parent::error());
184
        return $this->util->html($error);
185
    }
186
}
187