Passed
Push — main ( af7fe2...46179f )
by Thierry
01:51 queued 14s
created

Driver::operators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver;
4
5
use Lagdo\DbAdmin\Driver\Entity\ConfigEntity;
6
7
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
8
use Lagdo\DbAdmin\Driver\Db\ServerInterface;
9
use Lagdo\DbAdmin\Driver\Db\TableInterface;
10
use Lagdo\DbAdmin\Driver\Db\QueryInterface;
11
use Lagdo\DbAdmin\Driver\Db\GrammarInterface;
12
13
use Lagdo\DbAdmin\Driver\Exception\AuthException;
14
15
abstract class Driver implements DriverInterface
16
{
17
    use ServerTrait;
18
    use TableTrait;
19
    use QueryTrait;
20
    use GrammarTrait;
21
    use ConnectionTrait;
22
23
    /**
24
     * @var UtilInterface
25
     */
26
    protected $util;
27
28
    /**
29
     * @var TranslatorInterface
30
     */
31
    protected $trans;
32
33
    /**
34
     * @var ServerInterface
35
     */
36
    protected $server;
37
38
    /**
39
     * @var TableInterface
40
     */
41
    protected $table;
42
43
    /**
44
     * @var QueryInterface
45
     */
46
    protected $query;
47
48
    /**
49
     * @var GrammarInterface
50
     */
51
    protected $grammar;
52
53
    /**
54
     * @var ConnectionInterface
55
     */
56
    protected $connection;
57
58
    /**
59
     * @var ConfigEntity
60
     */
61
    protected $config;
62
63
    /**
64
     * @var array
65
     */
66
    protected $options;
67
68
    /**
69
     * From bootstrap.inc.php
70
     * @var string
71
     */
72
    public $onActions = "RESTRICT|NO ACTION|CASCADE|SET NULL|SET DEFAULT"; ///< @var string used in foreignKeys()
73
74
    /**
75
     * From index.php
76
     * @var string
77
     */
78
    public $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'";
79
80
    /**
81
     * From index.php
82
     * @var string
83
     */
84
    public $inout = "IN|OUT|INOUT";
85
86
    /**
87
     * @var string
88
     */
89
    protected $database = '';
90
91
    /**
92
     * @var string
93
     */
94
    protected $schema = '';
95
96
    /**
97
     * The constructor
98
     *
99
     * @param UtilInterface $util
100
     * @param TranslatorInterface $trans
101
     * @param array $options
102
     */
103
    public function __construct(UtilInterface $util, TranslatorInterface $trans, array $options)
104
    {
105
        $this->util = $util;
106
        $this->util->setDriver($this);
107
        $this->trans = $trans;
108
        $this->options = $options;
109
        $this->config = new ConfigEntity();
110
        $this->initConfig();
111
        $this->createConnection();
112
    }
113
114
    /**
115
     * Set driver config
116
     *
117
     * @return void
118
     */
119
    abstract protected function initConfig();
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function version()
125
    {
126
        return "4.8.1-dev";
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function options(string $name = '')
133
    {
134
        if (!($name = trim($name))) {
135
            return $this->options;
136
        }
137
        if (\array_key_exists($name, $this->options)) {
138
            return $this->options[$name];
139
        }
140
        if ($name === 'server') {
141
            $server = $this->options['host'] ?? '';
142
            $port = $this->options['port'] ?? ''; // Optional
143
            // Append the port to the host if it is defined.
144
            if (($port)) {
145
                $server .= ":$port";
146
            }
147
            return $server;
148
        }
149
        // if ($name === 'ssl') {
150
        //     return false; // No SSL options yet
151
        // }
152
        // Option not found
153
        return '';
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function connect(string $database, string $schema)
160
    {
161
        if (!$this->connection->open($database, $schema)) {
162
            throw new AuthException($this->error());
163
        }
164
        $this->database = $database;
165
        $this->schema = $schema;
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171
    public function numberRegex()
172
    {
173
        return '((?<!o)int(?!er)|numeric|real|float|double|decimal|money)'; // not point, not interval
174
    }
175
176
    /**
177
     * @inheritDoc
178
     */
179
    public function database()
180
    {
181
        return $this->database;
182
    }
183
184
    /**
185
     * @inheritDoc
186
     */
187
    public function schema()
188
    {
189
        return $this->schema;
190
    }
191
192
    /**
193
     * @inheritDoc
194
     */
195
    public function config()
196
    {
197
        return $this->config;
198
    }
199
200
    /**
201
     * @inheritDoc
202
     */
203
    public function minVersion(string $version, string $mariaDb = "", ConnectionInterface $connection = null)
204
    {
205
        if (!$connection) {
206
            $connection = $this->connection;
207
        }
208
        $info = $connection->serverInfo();
209
        if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) {
210
            $info = $match[1];
211
            $version = $mariaDb;
212
        }
213
        return (version_compare($info, $version) >= 0);
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219
    public function charset()
220
    {
221
        // SHOW CHARSET would require an extra query
222
        return ($this->minVersion("5.5.3", 0) ? "utf8mb4" : "utf8");
223
    }
224
225
    /**
226
     * @inheritDoc
227
     */
228
    public function setUtf8mb4(string $create)
229
    {
230
        static $set = false;
231
        // possible false positive
232
        if (!$set && preg_match('~\butf8mb4~i', $create)) {
233
            $set = true;
234
            return "SET NAMES " . $this->charset() . ";\n\n";
235
        }
236
        return '';
237
    }
238
239
    /**
240
     * @return string
241
     */
242
    public function actions()
243
    {
244
        return $this->onActions;
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function onActions()
251
    {
252
        return \explode('|', $this->onActions);
253
    }
254
255
    /**
256
     * Get the server jush
257
     *
258
     * @return string
259
     */
260
    public function jush()
261
    {
262
        return $this->config->jush;
263
    }
264
265
    /**
266
     * @return array
267
     */
268
    public function unsigned()
269
    {
270
        return $this->config->unsigned;
271
    }
272
273
    /**
274
     * @return array
275
     */
276
    public function functions()
277
    {
278
        return $this->config->functions;
279
    }
280
281
    /**
282
     * @return array
283
     */
284
    public function grouping()
285
    {
286
        return $this->config->grouping;
287
    }
288
289
    /**
290
     * @return array
291
     */
292
    public function operators()
293
    {
294
        return $this->config->operators;
295
    }
296
297
    /**
298
     * @return array
299
     */
300
    public function editFunctions()
301
    {
302
        return $this->config->editFunctions;
303
    }
304
305
    /**
306
     * @return array
307
     */
308
    public function types()
309
    {
310
        return $this->config->types;
311
    }
312
313
    /**
314
     * @param string $type
315
     *
316
     * @return bool
317
     */
318
    public function typeExists(string $type)
319
    {
320
        return isset($this->config->types[$type]);
321
    }
322
323
    /**
324
     * @param string $type
325
     *
326
     * @return mixed
327
     */
328
    public function type(string $type)
329
    {
330
        return $this->config->types[$type];
331
    }
332
333
    /**
334
     * @return array
335
     */
336
    public function structuredTypes()
337
    {
338
        return $this->config->structuredTypes;
339
    }
340
341
    /**
342
     * @param string $key
343
     * @param mixed $value
344
     *
345
     * @return void
346
     */
347
    public function setStructuredType(string $key, $value)
348
    {
349
        $this->config->structuredTypes[$key] = $value;
350
    }
351
}
352