Passed
Push — main ( 32fdaa...f87e5b )
by Thierry
13:22 queued 04:56
created

Driver::initConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 11
rs 9.9666
c 3
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
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection;
8
9
class Driver extends AbstractDriver
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    protected function initDriver()
15
    {
16
        // Init config
17
        $this->config->jush = 'sql';
18
        $this->config->drivers = ["MySQLi", "PDO_MySQL"];
19
        $this->config->setTypes([
20
            'Numbers' => ["tinyint" => 3, "smallint" => 5, "mediumint" => 8, "int" => 10,
21
                "bigint" => 20, "decimal" => 66, "float" => 12, "double" => 21],
22
            'Date and time' => ["date" => 10, "datetime" => 19, "timestamp" => 19, "time" => 10, "year" => 4],
23
            'Strings' => ["char" => 255, "varchar" => 65535, "tinytext" => 255,
24
                "text" => 65535, "mediumtext" => 16777215, "longtext" => 4294967295],
25
            'Lists' => ["enum" => 65535, "set" => 64],
26
            'Binary' => ["bit" => 20, "binary" => 255, "varbinary" => 65535, "tinyblob" => 255,
27
                "blob" => 65535, "mediumblob" => 16777215, "longblob" => 4294967295],
28
            'Geometry' => ["geometry" => 0, "point" => 0, "linestring" => 0, "polygon" => 0,
29
                "multipoint" => 0, "multilinestring" => 0, "multipolygon" => 0, "geometrycollection" => 0],
30
        ]);
31
        $this->config->unsigned = ["unsigned", "zerofill", "unsigned zerofill"];
32
        $this->config->operators = ["=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%",
33
            "REGEXP", "IN", "FIND_IN_SET", "IS NULL", "NOT LIKE", "NOT REGEXP",
34
            "NOT IN", "IS NOT NULL", "SQL"];
35
        $this->config->functions = ["char_length", "date", "from_unixtime", "lower",
36
            "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper"];
37
        $this->config->grouping = ["avg", "count", "count distinct", "group_concat", "max", "min", "sum"];
38
        $this->config->editFunctions = [[
39
            "char" => "md5/sha1/password/encrypt/uuid",
40
            "binary" => "md5/sha1",
41
            "date|time" => "now",
42
        ],[
43
            $this->numberRegex() => "+/-",
44
            "date" => "+ interval/- interval",
45
            "time" => "addtime/subtime",
46
            "char|text" => "concat",
47
        ]];
48
        /**
49
         * Features not available
50
         *
51
         * @var array
52
         */
53
        $this->config->features = ['database', 'table', 'columns', 'sql', 'indexes', 'descidx',
54
            'comment', 'processlist', 'variables', 'drop_col', 'kill', 'dump', 'fkeys_sql'];
55
56
        $this->server = new Db\Server($this, $this->util, $this->trans);
57
        $this->database = new Db\Database($this, $this->util, $this->trans);
58
        $this->table = new Db\Table($this, $this->util, $this->trans);
59
        $this->query = new Db\Query($this, $this->util, $this->trans);
60
        $this->grammar = new Db\Grammar($this, $this->util, $this->trans);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function name()
67
    {
68
        return "MySQL";
69
    }
70
71
    /**
72
     * Initialize a new connection
73
     *
74
     * @param AbstractConnection $connection
75
     *
76
     * @return AbstractConnection
77
     */
78
    private function initConnection(AbstractConnection $connection)
79
    {
80
        // if (!$connection->open($this->options('server'), $this->options())) {
81
        //     $error = $this->error();
82
        //     // windows-1250 - most common Windows encoding
83
        //     if (function_exists('iconv') && !$this->util->isUtf8($error) &&
84
        //         strlen($s = iconv("windows-1250", "utf-8", $error)) > strlen($error)) {
85
        //         $error = $s;
86
        //     }
87
        //     throw new AuthException($error);
88
        // }
89
        return $this->connection = $connection;
90
    }
91
92
    /**
93
     * @inheritDoc
94
     * @throws AuthException
95
     */
96
    public function createConnection()
97
    {
98
        if (!$this->options('prefer_pdo', false) && extension_loaded("mysqli")) {
99
            $connection = new Db\MySqli\Connection($this, $this->util, $this->trans, 'MySQLi');
100
            return $this->initConnection($connection);
101
        }
102
        if (extension_loaded("pdo_mysql")) {
103
            $connection = new Db\Pdo\Connection($this, $this->util, $this->trans, 'PDO_MySQL');
104
            return $this->initConnection($connection);
105
        }
106
        throw new AuthException($this->trans->lang('No package installed to connect to a MySQL server.'));
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function connect(string $database, string $schema)
113
    {
114
        parent::connect($database, $schema);
115
116
        if ($this->minVersion(5)) {
117
            $this->config->features[] = 'routine';
118
            $this->config->features[] = 'trigger';
119
            $this->config->features[] = 'view';
120
            if ($this->minVersion(5.1)) {
121
                $this->config->features[] = 'event';
122
                $this->config->features[] = 'partitioning';
123
            }
124
            if ($this->minVersion(8)) {
125
                $this->config->features[] = 'descidx';
126
            }
127
        }
128
        if ($this->minVersion('5.7.8', 10.2)) {
129
            $this->config->structuredTypes[$this->trans->lang('Strings')][] = "json";
130
            $this->config->types["json"] = 4294967295;
131
        }
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function error()
138
    {
139
        $error = preg_replace('~^You have an error.*syntax to use~U', 'Syntax error', parent::error());
140
        return $this->util->html($error);
141
    }
142
}
143