SQLite   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 71
c 2
b 0
f 0
dl 0
loc 227
rs 10
wmc 18

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getEngine() 0 3 1
A getTypeInteger() 0 3 1
A getColumns() 0 10 1
A getTables() 0 13 1
A getTypeTime() 0 3 1
A getDatabaseName() 0 5 1
A getModifierAutoincrement() 0 10 2
A getViewColumns() 0 10 1
A getAddUnique() 0 7 1
A getTypeEnum() 0 5 1
A getTypeTimestamp() 0 3 1
A getAddIndex() 0 7 1
A getPrimaryKey() 0 6 2
A renameTable() 0 6 1
A getViews() 0 13 1
A truncate() 0 9 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file SQLite.php
33
 *
34
 *  The SQLite Driver class
35
 *
36
 *  @package    Platine\Database\Driver
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Driver;
48
49
use Platine\Database\Schema\AlterTable;
50
use Platine\Database\Schema\BaseColumn;
51
use Platine\Database\Schema\CreateTable;
52
53
/**
54
 * @class SQLite
55
 * @package Platine\Database\Driver
56
 */
57
class SQLite extends Driver
58
{
59
    /**
60
     * @inheritdoc
61
     * @var string
62
     */
63
    protected string $identifier = '`%s`';
64
65
    /**
66
     * @inheritDoc
67
     */
68
    protected array $modifiers = [
69
        'nullable',
70
        'default',
71
        'autoincrement',
72
    ];
73
74
    /**
75
     * @inheritDoc
76
     */
77
    protected string $autoincrement = 'AUTOINCREMENT';
78
79
    /**
80
     *
81
     * @var bool
82
     */
83
    private bool $noPrimaryKey = false;
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function getDatabaseName(): array
89
    {
90
        $dsn = $this->connection->getDsn();
91
        return [
92
            'result' => substr($dsn, strpos($dsn, ':') + 1)
93
        ];
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getTables(string $database): array
100
    {
101
        $sql = sprintf(
102
            'SELECT %s FROM %s WHERE type = ? '
103
                . ' ORDER BY %s ASC',
104
            $this->quoteIdentifier('name'),
105
            $this->quoteIdentifier('sqlite_master'),
106
            $this->quoteIdentifier('name'),
107
        );
108
109
        return [
110
            'sql' => $sql,
111
            'params' => ['table']
112
        ];
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function getViews(string $database): array
119
    {
120
        $sql = sprintf(
121
            'SELECT %s FROM %s WHERE type = ? '
122
                . ' ORDER BY %s ASC',
123
            $this->quoteIdentifier('name'),
124
            $this->quoteIdentifier('sqlite_master'),
125
            $this->quoteIdentifier('name'),
126
        );
127
128
        return [
129
            'sql' => $sql,
130
            'params' => ['view']
131
        ];
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function getColumns(string $database, string $table): array
138
    {
139
        $sql = sprintf(
140
            'PRAGMA table_info(%s)',
141
            $this->quoteIdentifier($table)
142
        );
143
144
        return [
145
            'sql' => $sql,
146
            'params' => []
147
        ];
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function getViewColumns(string $database, string $view): array
154
    {
155
        $sql = sprintf(
156
            'PRAGMA table_info(%s)',
157
            $this->quoteIdentifier($view)
158
        );
159
160
        return [
161
            'sql' => $sql,
162
            'params' => []
163
        ];
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169
    public function renameTable(string $current, string $new): array
170
    {
171
        return [
172
            'sql' => 'ALTER TABLE ' . $this->quoteIdentifier($current)
173
            . ' RENAME TO ' . $this->quoteIdentifier($new),
174
            'params' => []
175
        ];
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    protected function getTypeInteger(BaseColumn $column): string
182
    {
183
        return 'INTEGER';
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    protected function getTypeTime(BaseColumn $column): string
190
    {
191
        return 'DATETIME';
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    protected function getTypeEnum(BaseColumn $column): string
198
    {
199
        // TODO
200
201
        return '';
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    protected function getTypeTimestamp(BaseColumn $column): string
208
    {
209
        return 'DATETIME';
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    protected function getModifierAutoincrement(BaseColumn $column): string
216
    {
217
        $modifier = parent::getModifierAutoincrement($column);
218
219
        if ($modifier !== '') {
220
            $this->noPrimaryKey = true;
221
            $modifier = 'PRIMARY KEY ' . $modifier;
222
        }
223
224
        return $modifier;
225
    }
226
227
    /**
228
     * @inheritDoc
229
     */
230
    protected function getPrimaryKey(CreateTable $schema): string
231
    {
232
        if ($this->noPrimaryKey) {
233
            return '';
234
        }
235
        return parent::getPrimaryKey($schema);
236
    }
237
238
    /**
239
     * @inheritdoc
240
     */
241
    protected function getEngine(CreateTable $schema): string
242
    {
243
        return '';
244
    }
245
246
    /**
247
     * @inheritdoc
248
     */
249
    protected function getAddUnique(AlterTable $schema, mixed $data): string
250
    {
251
        return sprintf(
252
            'CREATE UNIQUE INDEX %s ON %s (%s)',
253
            $this->quoteIdentifier($data['name']),
254
            $this->quoteIdentifier($schema->getTableName()),
255
            $this->quoteIdentifiers($data['columns'])
256
        );
257
    }
258
259
    /**
260
     * @inheritdoc
261
     */
262
    protected function getAddIndex(AlterTable $schema, mixed $data): string
263
    {
264
        return sprintf(
265
            'CREATE INDEX %s ON %s (%s)',
266
            $this->quoteIdentifier($data['name']),
267
            $this->quoteIdentifier($schema->getTableName()),
268
            $this->quoteIdentifiers($data['columns'])
269
        );
270
    }
271
272
    /**
273
     * @inheritdoc
274
     */
275
    public function truncate(string $table): array
276
    {
277
        //TODO add a way to delete the table sequence information in
278
        //"sqlite_sequence table"
279
        //DELETE FROM `sqlite_sequence` WHERE `name` = 'TABLE_NAME';
280
281
        return [
282
            'sql' => 'DELETE FROM ' . $this->quoteIdentifier($table),
283
            'params' => []
284
        ];
285
    }
286
}
287