Driver   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 117
rs 10
c 3
b 0
f 0
wmc 15

6 Methods

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