1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Query |
4
|
|
|
* |
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
6
|
|
|
* |
7
|
|
|
* PHP version 7.1 |
8
|
|
|
* |
9
|
|
|
* @package Query |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2012 - 2018 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query |
14
|
|
|
*/ |
15
|
|
|
namespace Query\Drivers\Sqlite; |
16
|
|
|
|
17
|
|
|
use Query\Drivers\AbstractSQL; |
18
|
|
|
use Query\Exception\NotImplementedException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SQLite Specific SQL |
22
|
|
|
*/ |
23
|
|
|
class SQL extends AbstractSQL { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the query plan for the sql query |
27
|
|
|
* |
28
|
|
|
* @param string $sql |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function explain(string $sql): string |
32
|
|
|
{ |
33
|
|
|
return "EXPLAIN QUERY PLAN {$sql}"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Random ordering keyword |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function random(): string |
42
|
|
|
{ |
43
|
|
|
return ' RANDOM()'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns sql to list other databases |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function dbList(): string |
52
|
|
|
{ |
53
|
|
|
return 'PRAGMA database_list'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns sql to list tables |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function tableList(): string |
62
|
|
|
{ |
63
|
|
|
return <<<SQL |
64
|
|
|
SELECT "name" FROM ( |
65
|
|
|
SELECT * FROM "sqlite_master" UNION ALL |
66
|
|
|
SELECT * FROM "sqlite_temp_master" |
67
|
|
|
) |
68
|
|
|
WHERE "type"='table' |
69
|
|
|
AND "name" NOT LIKE "sqlite_%" |
70
|
|
|
ORDER BY "name" |
71
|
|
|
SQL; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* List the system tables |
76
|
|
|
* |
77
|
|
|
* @return string[] |
78
|
|
|
*/ |
79
|
|
|
public function systemTableList(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'sqlite_master', |
83
|
|
|
'sqlite_temp_master', |
84
|
|
|
'sqlite_sequence' |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns sql to list views |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function viewList(): string |
94
|
|
|
{ |
95
|
|
|
return <<<SQL |
96
|
|
|
SELECT "name" FROM "sqlite_master" WHERE "type" = 'view' |
97
|
|
|
SQL; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns sql to list triggers |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function triggerList(): string |
106
|
|
|
{ |
107
|
|
|
return <<<SQL |
108
|
|
|
SELECT "name" FROM "sqlite_master" WHERE "type"='trigger' |
109
|
|
|
SQL; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return sql to list functions |
114
|
|
|
* |
115
|
|
|
* @throws NotImplementedException |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function functionList(): string |
119
|
|
|
{ |
120
|
|
|
throw new NotImplementedException('Functionality does not exist in SQLite'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return sql to list stored procedures |
125
|
|
|
* |
126
|
|
|
* @throws NotImplementedException |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function procedureList(): string |
130
|
|
|
{ |
131
|
|
|
throw new NotImplementedException('Functionality does not exist in SQLite'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return sql to list sequences |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function sequenceList(): string |
140
|
|
|
{ |
141
|
|
|
return 'SELECT "name" FROM "sqlite_sequence"'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* SQL to show list of field types |
146
|
|
|
* |
147
|
|
|
* @return string[] |
148
|
|
|
*/ |
149
|
|
|
public function typeList(): array |
150
|
|
|
{ |
151
|
|
|
return ['INTEGER', 'REAL', 'TEXT', 'BLOB', 'NULL']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* SQL to show information about columns in a table |
156
|
|
|
* |
157
|
|
|
* @param string $table |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function columnList(string $table): string |
161
|
|
|
{ |
162
|
|
|
return <<<SQL |
163
|
|
|
PRAGMA table_info("$table") |
164
|
|
|
SQL; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the list of foreign keys for the current |
169
|
|
|
* table |
170
|
|
|
* |
171
|
|
|
* @param string $table |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function fkList(string $table): string |
175
|
|
|
{ |
176
|
|
|
return <<<SQL |
177
|
|
|
PRAGMA foreign_key_list("$table") |
178
|
|
|
SQL; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the list of indexes for the current table |
184
|
|
|
* |
185
|
|
|
* @param string $table |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function indexList(string $table): string |
189
|
|
|
{ |
190
|
|
|
return <<<SQL |
191
|
|
|
PRAGMA index_list("$table") |
192
|
|
|
SQL; |
193
|
|
|
} |
194
|
|
|
} |