Passed
Push — main ( a1a226...77b8c8 )
by Thierry
09:48 queued 01:16
created

Driver::postConnectConfig()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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