1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\DB; |
9
|
|
|
use |
10
|
|
|
SQLite3Result; |
11
|
|
|
|
12
|
|
|
class SQLite extends _Abstract { |
13
|
|
|
/** |
14
|
|
|
* @var \SQLite3 Instance of DB connection |
15
|
|
|
*/ |
16
|
|
|
protected $instance; |
17
|
|
|
/** |
18
|
|
|
* @param string $database Ignored for SQLite |
19
|
|
|
* @param string $user Ignored for SQLite |
20
|
|
|
* @param string $password Ignored for SQLite |
21
|
|
|
* @param string $host Path to database file, relatively to website root or absolute |
22
|
|
|
* @param string $prefix |
23
|
|
|
*/ |
24
|
18 |
|
function __construct ($database, $user = '', $password = '', $host = '', $prefix = '') { |
25
|
18 |
|
$start = microtime(true); |
26
|
|
|
try { |
27
|
18 |
|
$this->instance = new \SQLite3($host); |
28
|
18 |
|
$this->database = $database; |
29
|
18 |
|
$this->connected = true; |
30
|
18 |
|
$this->connecting_time = microtime(true) - $start; |
31
|
18 |
|
$this->db_type = 'sqlite'; |
32
|
18 |
|
$this->prefix = $prefix; |
33
|
1 |
|
} catch (\Exception $e) { |
34
|
|
|
} |
35
|
18 |
|
} |
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
18 |
|
function q ($query, $params = [], ...$param) { |
40
|
|
|
// Hack to convert small subset of MySQL queries into SQLite-compatible syntax |
41
|
18 |
|
$query = str_replace('INSERT IGNORE', 'INSERT OR IGNORE', $query); |
42
|
18 |
|
return parent::q(...([$query] + func_get_args())); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
* |
47
|
|
|
* @return bool|SQLite3Result |
|
|
|
|
48
|
|
|
*/ |
49
|
18 |
|
protected function q_internal ($query) { |
50
|
18 |
|
if (!$query) { |
51
|
1 |
|
return false; |
52
|
|
|
} |
53
|
18 |
|
return $this->instance->query($query); |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
5 |
|
protected function q_multi_internal ($query) { |
59
|
5 |
|
$result = true; |
60
|
5 |
|
foreach ($query as $q) { |
61
|
5 |
|
$result = $result && $this->q_internal($q); |
62
|
|
|
} |
63
|
5 |
|
return $result; |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
* |
68
|
|
|
* @param false|SQLite3Result $query_result |
69
|
|
|
*/ |
70
|
17 |
|
function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
71
|
17 |
|
if (!($query_result instanceof SQLite3Result)) { |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
17 |
|
$result_type = $single_column || $indexed ? SQLITE3_NUM : SQLITE3_ASSOC; |
75
|
17 |
|
if ($array) { |
76
|
16 |
|
$result = []; |
77
|
16 |
|
while ($current = $query_result->fetchArray($result_type)) { |
78
|
16 |
|
$result[] = $single_column ? $current[0] : $current; |
79
|
|
|
} |
80
|
16 |
|
$this->free($query_result); |
81
|
16 |
|
return $result; |
82
|
|
|
} |
83
|
16 |
|
$result = $query_result->fetchArray($result_type); |
84
|
16 |
|
return $single_column && $result ? $result[0] : $result; |
85
|
|
|
} |
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
13 |
|
function id () { |
90
|
13 |
|
return $this->instance->lastInsertRowID(); |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
1 |
|
function affected () { |
96
|
1 |
|
return $this->instance->changes(); |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
* |
101
|
|
|
* @param false|SQLite3Result $query_result |
102
|
|
|
*/ |
103
|
16 |
|
function free ($query_result) { |
104
|
16 |
|
if ($query_result instanceof SQLite3Result) { |
105
|
16 |
|
return $query_result->finalize(); |
106
|
|
|
} |
107
|
1 |
|
return true; |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
2 |
|
function columns ($table, $like = false) { |
113
|
2 |
|
if (!$table) { |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
2 |
|
$columns = $this->qfa("PRAGMA table_info(`$table`)") ?: []; |
117
|
2 |
|
foreach ($columns as &$column) { |
118
|
2 |
|
$column = $column['name']; |
119
|
|
|
} |
120
|
|
|
/** |
121
|
|
|
* Only support the most common cases |
122
|
|
|
*/ |
123
|
2 |
|
if ($like) { |
124
|
1 |
|
if (substr($like, -1) == '%') { |
125
|
1 |
|
$like = substr($like, 0, -1); |
126
|
1 |
|
return array_values( |
127
|
|
|
array_filter( |
128
|
|
|
$columns, |
129
|
1 |
|
function ($column) use ($like) { |
130
|
1 |
|
return strpos($column, $like) === 0; |
131
|
1 |
|
} |
132
|
|
|
) |
133
|
|
|
); |
134
|
1 |
|
} elseif (strpos($like, '%') === false) { |
135
|
1 |
|
return in_array($like, $columns) ? [$like] : []; |
136
|
|
|
} else { |
137
|
1 |
|
trigger_error("Can't get columns like $like, SQLite engine doesn't support such conditions", E_USER_WARNING); |
138
|
1 |
|
return []; |
139
|
|
|
} |
140
|
|
|
} |
141
|
2 |
|
return $columns; |
142
|
|
|
} |
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
1 |
|
function tables ($like = false) { |
147
|
1 |
|
if ($like) { |
148
|
1 |
|
$like = $this->s($like); |
149
|
1 |
|
return $this->qfas( |
150
|
|
|
"SELECT `name` |
151
|
|
|
FROM `sqlite_master` |
152
|
|
|
WHERE |
153
|
|
|
`type` = 'table' AND |
154
|
|
|
`name` != 'sqlite_sequence' AND |
155
|
1 |
|
`name` LIKE $like |
156
|
1 |
|
ORDER BY `name` ASC" |
157
|
1 |
|
) ?: []; |
158
|
|
|
} else { |
159
|
1 |
|
return $this->qfas( |
160
|
|
|
"SELECT `name` |
161
|
|
|
FROM `sqlite_master` |
162
|
|
|
WHERE |
163
|
|
|
`type` = 'table' AND |
164
|
|
|
`name` != 'sqlite_sequence' |
165
|
1 |
|
ORDER BY `name` ASC" |
166
|
1 |
|
) ?: []; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
18 |
|
protected function s_internal ($string, $single_quotes_around) { |
173
|
18 |
|
$return = \SQLite3::escapeString($string); |
174
|
18 |
|
return $single_quotes_around ? "'$return'" : $return; |
175
|
|
|
} |
176
|
|
|
/** |
177
|
|
|
* @inheritdoc |
178
|
|
|
*/ |
179
|
1 |
|
function server () { |
180
|
1 |
|
return \SQLite3::version()['versionString']; |
181
|
|
|
} |
182
|
|
|
/** |
183
|
|
|
* @inheritdoc |
184
|
|
|
*/ |
185
|
1 |
|
function __destruct () { |
186
|
1 |
|
if ($this->connected && is_object($this->instance)) { |
187
|
1 |
|
$this->instance->close(); |
188
|
1 |
|
$this->connected = false; |
189
|
|
|
} |
190
|
1 |
|
} |
191
|
|
|
} |
192
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.