1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of dimtrovich/db-dumper". |
5
|
|
|
* |
6
|
|
|
* (c) 2024 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dimtrovich\DbDumper\Adapters; |
13
|
|
|
|
14
|
|
|
use Dimtrovich\DbDumper\Exceptions\Exception; |
15
|
|
|
|
16
|
|
|
class SqliteAdapter extends Factory |
17
|
|
|
{ |
18
|
|
|
// Numerical SQLITE types |
19
|
|
|
public $sqliteTypes = [ |
20
|
|
|
'numerical' => [ |
21
|
|
|
'INT', |
22
|
|
|
'INTEGER', |
23
|
|
|
'TINYINT', |
24
|
|
|
'SMALLINT', |
25
|
|
|
'MEDIUMINT', |
26
|
|
|
'BIGINT', |
27
|
|
|
'UNSIGNED BIG INT', |
28
|
|
|
'INT2', |
29
|
|
|
'INT8', |
30
|
|
|
'REAL', |
31
|
|
|
'DOUBLE', |
32
|
|
|
'DOUBLE PRECISION', |
33
|
|
|
'FLOAT', |
34
|
|
|
'NUMERIC', |
35
|
|
|
], |
36
|
|
|
'blob' => [ |
37
|
|
|
'BLOB', |
38
|
|
|
], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function showCreateTable(string $tableName): string |
45
|
|
|
{ |
46
|
|
|
return "SELECT tbl_name as 'Table', sql as 'Create Table' " . |
47
|
|
|
'FROM sqlite_master ' . |
48
|
|
|
"WHERE type='table' AND tbl_name='{$tableName}'"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function createTable(array $row): string |
55
|
|
|
{ |
56
|
|
|
if (! isset($row['Create Table'])) { |
57
|
|
|
throw new Exception('Error getting table code, unknown output'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$createTable = $row['Create Table']; |
61
|
|
|
|
62
|
|
|
if ($this->option->reset_auto_increment) { |
63
|
|
|
$createTable = 'DELETE FROM sqlite_sequence WHERE name=\'' . $row['Table'] . '\'' . PHP_EOL . $createTable; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->option->if_not_exists) { |
67
|
|
|
$createTable = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $createTable); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $createTable . ';' . PHP_EOL; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function showCreateView(string $viewName): string |
77
|
|
|
{ |
78
|
|
|
return "SELECT tbl_name as 'View', sql as 'Create View' " . |
79
|
|
|
'FROM sqlite_master ' . |
80
|
|
|
"WHERE type='view' AND tbl_name='{$viewName}'"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function showTables(string $database = ''): string |
87
|
|
|
{ |
88
|
|
|
return "SELECT tbl_name FROM sqlite_master WHERE type='table' AND tbl_name NOT LIKE 'sqlite_%'"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
|
|
public function showViews(string $database = ''): string |
95
|
|
|
{ |
96
|
|
|
return "SELECT tbl_name FROM sqlite_master WHERE type='view' AND tbl_name NOT LIKE 'sqlite_%'"; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
public function showTriggers(string $database = ''): string |
103
|
|
|
{ |
104
|
|
|
return "SELECT name FROM sqlite_master WHERE type='trigger' AND tbl_name NOT LIKE 'sqlite_%'"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function showColumns(string $table): string |
111
|
|
|
{ |
112
|
|
|
return "pragma table_info({$table})"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
|
|
public function startTransaction(): string |
119
|
|
|
{ |
120
|
|
|
return 'BEGIN EXCLUSIVE'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function commitTransaction(): string |
127
|
|
|
{ |
128
|
|
|
return 'COMMIT'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
public function startDisableForeignKeysCheck(): string |
135
|
|
|
{ |
136
|
|
|
return 'PRAGMA foreign_keys = OFF;'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
|
|
public function endDisableForeignKeysCheck(): string |
143
|
|
|
{ |
144
|
|
|
return 'PRAGMA foreign_keys = ON;'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
*/ |
150
|
|
|
public function parseColumnType(array $colType): array |
151
|
|
|
{ |
152
|
|
|
return parent::_parseColumnType($colType, $this->sqliteTypes); |
153
|
|
|
// for virtual columns that are of type 'Extra', column type |
154
|
|
|
// could by "STORED GENERATED" or "VIRTUAL GENERATED" |
155
|
|
|
// MySQL reference: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html |
156
|
|
|
// $colInfo['is_virtual'] = str_contains($colType['Extra'], 'VIRTUAL GENERATED') || str_contains($colType['Extra'], 'STORED GENERATED'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|