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