Passed
Push — develop ( cd2c29...4f7ba8 )
by nguereza
02:28
created

SQLite::getTypeEnum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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   http://www.iacademy.cf
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
        'description'
73
    ];
74
75
    /**
76
     * @inheritDoc
77
     */
78
    protected string $autoincrement = 'AUTOINCREMENT';
79
80
    /**
81
     *
82
     * @var bool
83
     */
84
    private bool $noPrimaryKey = false;
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function getDatabaseName(): array
90
    {
91
        $dsn = $this->connection->getDsn();
92
        return [
93
            'result' => substr($dsn, strpos($dsn, ':') + 1)
94
        ];
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function getTables(string $database): array
101
    {
102
        $sql = sprintf(
103
            'SELECT %s FROM %s WHERE type = ? '
104
                . ' ORDER BY %s ASC',
105
            $this->quoteIdentifier('name'),
106
            $this->quoteIdentifier('sqlite_master'),
107
            $this->quoteIdentifier('name'),
108
        );
109
110
        return [
111
            'sql' => $sql,
112
            'params' => ['table']
113
        ];
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function getViews(string $database): array
120
    {
121
        $sql = sprintf(
122
            'SELECT %s FROM %s WHERE type = ? '
123
                . ' ORDER BY %s ASC',
124
            $this->quoteIdentifier('name'),
125
            $this->quoteIdentifier('sqlite_master'),
126
            $this->quoteIdentifier('name'),
127
        );
128
129
        return [
130
            'sql' => $sql,
131
            'params' => ['view']
132
        ];
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function getColumns(string $database, string $table): array
139
    {
140
        $sql = sprintf(
141
            'PRAGMA table_info(%s)',
142
            $this->quoteIdentifier($table)
143
        );
144
145
        return [
146
            'sql' => $sql,
147
            'params' => []
148
        ];
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function getViewColumns(string $database, string $view): array
155
    {
156
        $sql = sprintf(
157
            'PRAGMA table_info(%s)',
158
            $this->quoteIdentifier($view)
159
        );
160
161
        return [
162
            'sql' => $sql,
163
            'params' => []
164
        ];
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public function renameTable(string $current, string $new): array
171
    {
172
        return [
173
            'sql' => 'ALTER TABLE ' . $this->quoteIdentifier($current)
174
            . ' RENAME TO ' . $this->quoteIdentifier($new),
175
            'params' => []
176
        ];
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    protected function getTypeInteger(BaseColumn $column): string
183
    {
184
        return 'INTEGER';
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    protected function getTypeTime(BaseColumn $column): string
191
    {
192
        return 'DATETIME';
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    protected function getTypeEnum(BaseColumn $column): string
199
    {
200
        // TODO
201
202
        return '';
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    protected function getTypeTimestamp(BaseColumn $column): string
209
    {
210
        return 'DATETIME';
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216
    protected function getModifierAutoincrement(BaseColumn $column): string
217
    {
218
        $modifier = parent::getModifierAutoincrement($column);
219
220
        if ($modifier !== '') {
221
            $this->noPrimaryKey = true;
222
            $modifier = 'PRIMARY KEY ' . $modifier;
223
        }
224
225
        return $modifier;
226
    }
227
228
    /**
229
     * @inheritDoc
230
     */
231
    protected function getPrimaryKey(CreateTable $schema): string
232
    {
233
        if ($this->noPrimaryKey) {
234
            return '';
235
        }
236
        return parent::getPrimaryKey($schema);
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    protected function getEngine(CreateTable $schema): string
243
    {
244
        return '';
245
    }
246
247
    /**
248
     * @inheritdoc
249
     */
250
    protected function getAddUnique(AlterTable $schema, $data): string
251
    {
252
        return sprintf(
253
            'CREATE UNIQUE INDEX %s ON %s (%s)',
254
            $this->quoteIdentifier($data['name']),
255
            $this->quoteIdentifier($schema->getTableName()),
256
            $this->quoteIdentifiers($data['columns'])
257
        );
258
    }
259
260
    /**
261
     * @inheritdoc
262
     */
263
    protected function getAddIndex(AlterTable $schema, $data): string
264
    {
265
        return sprintf(
266
            'CREATE INDEX %s ON %s (%s)',
267
            $this->quoteIdentifier($data['name']),
268
            $this->quoteIdentifier($schema->getTableName()),
269
            $this->quoteIdentifiers($data['columns'])
270
        );
271
    }
272
273
    /**
274
     * @inheritdoc
275
     */
276
    public function truncate(string $table): array
277
    {
278
        //TODO add a way to delete the table sequence information in
279
        //"sqlite_sequence table"
280
        //DELETE FROM `sqlite_sequence` WHERE `name` = 'TABLE_NAME';
281
        return [
282
            'sql' => 'DELETE FROM ' . $this->quoteIdentifier($table),
283
            'params' => []
284
        ];
285
    }
286
}
287