Passed
Push — develop ( d2d625...405251 )
by nguereza
03:36
created

SQLite::getPrimaryKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 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   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
    /**
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 getTypeTimestamp(BaseColumn $column): string
165
    {
166
        return 'DATETIME';
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    protected function getModifierAutoincrement(BaseColumn $column): string
173
    {
174
        $modifier = parent::getModifierAutoincrement($column);
175
176
        if ($modifier !== '') {
177
            $this->noPrimaryKey = true;
178
            $modifier = 'PRIMARY KEY ' . $modifier;
179
        }
180
181
        return $modifier;
182
    }
183
184
    /**
185
     * @inheritDoc
186
     */
187
    protected function getPrimaryKey(CreateTable $schema): string
188
    {
189
        if ($this->noPrimaryKey) {
190
            return '';
191
        }
192
        return parent::getPrimaryKey($schema);
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    protected function getEngine(CreateTable $schema): string
199
    {
200
        return '';
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206
    protected function getAddUnique(AlterTable $schema, $data): string
207
    {
208
        return sprintf(
209
            'CREATE UNIQUE INDEX %s ON %s (%s)',
210
            $this->quoteIdentifier($data['name']),
211
            $this->quoteIdentifier($schema->getTableName()),
212
            $this->quoteIdentifiers($data['columns'])
213
        );
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219
    protected function getAddIndex(AlterTable $schema, $data): string
220
    {
221
        return sprintf(
222
            'CREATE INDEX %s ON %s (%s)',
223
            $this->quoteIdentifier($data['name']),
224
            $this->quoteIdentifier($schema->getTableName()),
225
            $this->quoteIdentifiers($data['columns'])
226
        );
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232
    public function truncate(string $table): array
233
    {
234
        //TODO add a way to delete the table sequence information in
235
        //"sqlite_sequence table"
236
        //DELETE FROM `sqlite_sequence` WHERE `name` = 'TABLE_NAME';
237
        return [
238
            'sql' => 'DELETE FROM ' . $this->quoteIdentifier($table),
239
            'params' => []
240
        ];
241
    }
242
}
243