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