Passed
Push — develop ( e333c2...b3554d )
by nguereza
02:25
created

SQLite   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 193
rs 10
c 1
b 0
f 0
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeInteger() 0 3 1
A getTables() 0 13 1
A getColumns() 0 10 1
A getTypeTime() 0 3 1
A getDatabaseName() 0 5 1
A renameTable() 0 6 1
A getEngine() 0 3 1
A truncate() 0 8 1
A getModifierAutoincrement() 0 10 2
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
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
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
59
60
    /**
61
     * @inheritdoc
62
     * @var string
63
     */
64
    protected string $identifier = '`%s`';
65
66
    /**
67
     * @inheritDoc
68
     */
69
    protected array $modifiers = [
70
        'nullable',
71
        'default',
72
        'autoincrement',
73
        'description'
74
    ];
75
76
    /**
77
     * @inheritDoc
78
     */
79
    protected string $autoincrement = 'AUTOINCREMENT';
80
81
    /**
82
     *
83
     * @var bool
84
     */
85
    private bool $noPrimaryKey = false;
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getDatabaseName(): array
91
    {
92
        $dsn = $this->connection->getDsn();
93
        return [
94
            'result' => substr($dsn, strpos($dsn, ':') + 1)
95
        ];
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function getTables(string $database): array
102
    {
103
        $sql = sprintf(
104
            'SELECT %s FROM %s WHERE type = ? '
105
                . ' ORDER BY %s ASC',
106
            $this->quoteIdentifier('name'),
107
            $this->quoteIdentifier('sqlite_master'),
108
            $this->quoteIdentifier('name'),
109
        );
110
111
        return [
112
            'sql' => $sql,
113
            'params' => ['table']
114
        ];
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function getColumns(string $database, string $table): array
121
    {
122
        $sql = sprintf(
123
            'PRAGMA table_info(%s)',
124
            $this->quoteIdentifier($table)
125
        );
126
127
        return [
128
            'sql' => $sql,
129
            'params' => []
130
        ];
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function renameTable(string $current, string $new): array
137
    {
138
        return [
139
            'sql' => 'ALTER TABLE ' . $this->quoteIdentifier($current)
140
            . ' RENAME TO ' . $this->quoteIdentifier($new),
141
            'params' => []
142
        ];
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    protected function getTypeInteger(BaseColumn $column): string
149
    {
150
        return 'INTEGER';
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    protected function getTypeTime(BaseColumn $column): string
157
    {
158
        return 'DATETIME';
159
    }
160
    
161
    /**
162
     * @inheritdoc
163
     */
164
    protected function getTypeEnum(BaseColumn $column): string
165
    {
166
        // TODO
167
168
        return '';
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    protected function getTypeTimestamp(BaseColumn $column): string
175
    {
176
        return 'DATETIME';
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    protected function getModifierAutoincrement(BaseColumn $column): string
183
    {
184
        $modifier = parent::getModifierAutoincrement($column);
185
186
        if ($modifier !== '') {
187
            $this->noPrimaryKey = true;
188
            $modifier = 'PRIMARY KEY ' . $modifier;
189
        }
190
191
        return $modifier;
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    protected function getPrimaryKey(CreateTable $schema): string
198
    {
199
        if ($this->noPrimaryKey) {
200
            return '';
201
        }
202
        return parent::getPrimaryKey($schema);
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    protected function getEngine(CreateTable $schema): string
209
    {
210
        return '';
211
    }
212
213
    /**
214
     * @inheritdoc
215
     */
216
    protected function getAddUnique(AlterTable $schema, $data): string
217
    {
218
        return sprintf(
219
            'CREATE UNIQUE INDEX %s ON %s (%s)',
220
            $this->quoteIdentifier($data['name']),
221
            $this->quoteIdentifier($schema->getTableName()),
222
            $this->quoteIdentifiers($data['columns'])
223
        );
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229
    protected function getAddIndex(AlterTable $schema, $data): string
230
    {
231
        return sprintf(
232
            'CREATE INDEX %s ON %s (%s)',
233
            $this->quoteIdentifier($data['name']),
234
            $this->quoteIdentifier($schema->getTableName()),
235
            $this->quoteIdentifiers($data['columns'])
236
        );
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    public function truncate(string $table): array
243
    {
244
        //TODO add a way to delete the table sequence information in
245
        //"sqlite_sequence table"
246
        //DELETE FROM `sqlite_sequence` WHERE `name` = 'TABLE_NAME';
247
        return [
248
            'sql' => 'DELETE FROM ' . $this->quoteIdentifier($table),
249
            'params' => []
250
        ];
251
    }
252
}
253