Passed
Push — main ( 7a9b6d...37b0c3 )
by Thierry
13:48 queued 11:37
created

Driver::initConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\Sqlite;
4
5
use Lagdo\DbAdmin\Driver\Exception\AuthException;
6
use Lagdo\DbAdmin\Driver\Driver as AbstractDriver;
7
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection;
8
9
use function in_array;
10
use function class_exists;
11
use function extension_loaded;
12
13
class Driver extends AbstractDriver
14
{
15
    /**
16
     * Driver features
17
     *
18
     * @var array
19
     */
20
    private $features = ['columns', 'database', 'drop_col', 'dump', 'indexes', 'descidx',
21
        'move_col', 'sql', 'status', 'table', 'trigger', 'variables', 'view', 'view_trigger'];
22
23
    /**
24
     * Data types
25
     *
26
     * @var array
27
     */
28
    private $types = [ //! arrays
29
        'Numbers' => ["integer" => 0, "real" => 0, "numeric" => 0],
30
        'Strings' => ["text" => 0],
31
        'Binary' => ["blob" => 0],
32
    ];
33
34
    /**
35
     * Number variants
36
     *
37
     * @var array
38
     */
39
    // private $unsigned = [];
40
41
    /**
42
     * Operators used in select
43
     *
44
     * @var array
45
     */
46
    private $operators = ["=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%",
47
        "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL"]; // REGEXP can be user defined function;
48
49
    /**
50
     * Functions used in select
51
     *
52
     * @var array
53
     */
54
    private $functions = ["hex", "length", "lower", "round", "unixepoch", "upper"];
55
56
    /**
57
     * Grouping functions used in select
58
     *
59
     * @var array
60
     */
61
    private $grouping = ["avg", "count", "count distinct", "group_concat", "max", "min", "sum"];
62
63
    /**
64
     * Functions used to edit data
65
     *
66
     * @var array
67
     */
68
    private $editFunctions = [[
69
        // "text" => "date('now')/time('now')/datetime('now')",
70
    ],[
71
        "integer|real|numeric" => "+/-",
72
        // "text" => "date/time/datetime",
73
        "text" => "||",
74
    ]];
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function name()
80
    {
81
        return "SQLite 3";
82
    }
83
84
    /**
85
     * Initialize a new connection
86
     *
87
     * @param AbstractConnection $connection
88
     *
89
     * @return AbstractConnection
90
     */
91
    private function initConnection(AbstractConnection $connection)
92
    {
93
        if ($this->connection === null) {
94
            $this->connection = $connection;
95
            $this->server = new Db\Server($this, $this->util, $this->trans);
96
            $this->database = new Db\Database($this, $this->util, $this->trans);
97
            $this->table = new Db\Table($this, $this->util, $this->trans);
98
            $this->query = new Db\Query($this, $this->util, $this->trans);
99
            $this->grammar = new Db\Grammar($this, $this->util, $this->trans);
100
        }
101
        return $connection;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     * @throws AuthException
107
     */
108
    public function createConnection()
109
    {
110
        if (!$this->options('prefer_pdo', false) && class_exists("SQLite3")) {
0 ignored issues
show
Unused Code introduced by
The call to Lagdo\DbAdmin\Driver\Driver::options() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        if (!$this->/** @scrutinizer ignore-call */ options('prefer_pdo', false) && class_exists("SQLite3")) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
111
            $connection = new Db\Sqlite\Connection($this, $this->util, $this->trans, 'SQLite3');
112
            return $this->initConnection($connection);
113
        }
114
        if (extension_loaded("pdo_sqlite")) {
115
            $connection = new Db\Pdo\Connection($this, $this->util, $this->trans, 'PDO_SQLite');
116
            return $this->initConnection($connection);
117
        }
118
        throw new AuthException($this->trans->lang('No package installed to open a Sqlite database.'));
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function connect(string $database, string $schema)
125
    {
126
        parent::connect($database, $schema);
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function support(string $feature)
133
    {
134
        return in_array($feature, $this->features);
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    protected function initConfig()
141
    {
142
        $this->config->jush = 'sqlite';
143
        $this->config->drivers = ["SQLite3", "PDO_SQLite"];
144
        $this->config->setTypes($this->types, $this->trans);
145
        // $this->config->unsigned = [];
146
        $this->config->operators = $this->operators;
147
        $this->config->functions = $this->functions;
148
        $this->config->grouping = $this->grouping;
149
        $this->config->editFunctions = $this->editFunctions;
150
    }
151
}
152