Passed
Push — main ( 52510a...0ffc81 )
by Thierry
29:26 queued 21:03
created

Driver::initDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 38
rs 9.44
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\PgSql;
4
5
use Lagdo\DbAdmin\Driver\Exception\AuthException;
6
use Lagdo\DbAdmin\Driver\Driver as AbstractDriver;
7
8
class Driver extends AbstractDriver
9
{
10
    /**
11
     * @inheritDoc
12
     */
13
    protected function initDriver()
14
    {
15
        // Init config
16
        $this->config->jush = 'pgsql';
17
        $this->config->drivers = ["PgSQL", "PDO_PgSQL"];
18
        $this->config->setTypes([ //! arrays
19
            'Numbers' => ["smallint" => 5, "integer" => 10, "bigint" => 19, "boolean" => 1,
20
                "numeric" => 0, "real" => 7, "double precision" => 16, "money" => 20],
21
            'Date and time' => ["date" => 13, "time" => 17, "timestamp" => 20, "timestamptz" => 21, "interval" => 0],
22
            'Strings' => ["character" => 0, "character varying" => 0, "text" => 0,
23
                "tsquery" => 0, "tsvector" => 0, "uuid" => 0, "xml" => 0],
24
            'Binary' => ["bit" => 0, "bit varying" => 0, "bytea" => 0],
25
            'Network' => ["cidr" => 43, "inet" => 43, "macaddr" => 17, "txid_snapshot" => 0],
26
            'Geometry' => ["box" => 0, "circle" => 0, "line" => 0, "lseg" => 0,
27
                "path" => 0, "point" => 0, "polygon" => 0],
28
        ]);
29
        // $this->config->unsigned = [];
30
        $this->config->operators = ["=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE",
31
            "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"]; // no "SQL" to avoid CSRF
32
        $this->config->functions = ["char_length", "lower", "round", "to_hex", "to_timestamp", "upper"];
33
        $this->config->grouping = ["avg", "count", "count distinct", "max", "min", "sum"];
34
        $this->config->editFunctions = [[
35
            "char" => "md5",
36
            "date|time" => "now",
37
        ],[
38
            $this->numberRegex() => "+/-",
39
            "date|time" => "+ interval/- interval", //! escape
40
            "char|text" => "||",
41
        ]];
42
        $this->config->features = ['database', 'table', 'columns', 'sql', 'indexes', 'descidx',
43
            'comment', 'view', 'scheme', 'routine', 'processlist', 'sequence', 'trigger',
44
            'type', 'variables', 'drop_col', 'kill', 'dump', 'fkeys_sql'];
45
46
        $this->server = new Db\Server($this, $this->util, $this->trans);
47
        $this->database = new Db\Database($this, $this->util, $this->trans);
48
        $this->table = new Db\Table($this, $this->util, $this->trans);
49
        $this->query = new Db\Query($this, $this->util, $this->trans);
50
        $this->grammar = new Db\Grammar($this, $this->util, $this->trans);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function name()
57
    {
58
        return "PostgreSQL";
59
    }
60
61
    /**
62
     * @inheritDoc
63
     * @throws AuthException
64
     */
65
    public function createConnection()
66
    {
67
        if (!$this->options('prefer_pdo', false) && extension_loaded("pgsql")) {
68
            $connection = new Db\PgSql\Connection($this, $this->util, $this->trans, 'PgSQL');
69
            return $this->connection = $connection;
70
        }
71
        if (extension_loaded("pdo_pgsql")) {
72
            $connection = new Db\Pdo\Connection($this, $this->util, $this->trans, 'PDO_PgSQL');
73
            return $this->connection = $connection;
74
        }
75
        throw new AuthException($this->trans->lang('No package installed to connect to a PostgreSQL server.'));
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function connect(string $database, string $schema)
82
    {
83
        parent::connect($database, $schema);
84
85
        if ($this->minVersion(9.3)) {
86
            $this->config->features[] = 'materializedview';
87
        }
88
        if ($this->minVersion(9.2)) {
89
            $this->config->structuredTypes[$this->trans->lang('Strings')][] = "json";
90
            $this->config->types["json"] = 4294967295;
91
            if ($this->minVersion(9.4)) {
92
                $this->config->structuredTypes[$this->trans->lang('Strings')][] = "jsonb";
93
                $this->config->types["jsonb"] = 4294967295;
94
            }
95
        }
96
        foreach ($this->userTypes() as $type) { //! get types from current_schemas('t')
97
            if (!isset($this->config->types[$type])) {
98
                $this->config->types[$type] = 0;
99
                $this->config->structuredTypes[$this->trans->lang('User types')][] = $type;
100
            }
101
        }
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function error()
108
    {
109
        $message = parent::error();
110
        if (preg_match('~^(.*\n)?([^\n]*)\n( *)\^(\n.*)?$~s', $message, $match)) {
111
            $match = array_pad($match, 5, '');
112
            $message = $match[1] . preg_replace('~((?:[^&]|&[^;]*;){' .
113
                strlen($match[3]) . '})(.*)~', '\1<b>\2</b>', $match[2]) . $match[4];
114
        }
115
        return $message;
116
    }
117
}
118