Passed
Push — main ( 7f4df5...64ca2e )
by Thierry
18:15 queued 09:46
created

Driver::initConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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