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