-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlite3db.php
265 lines (245 loc) · 6.66 KB
/
sqlite3db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/*
*============================================================
* Program Name: Easy Jia Content Management System (EJCMS)
* Web Address: http://www.ejcms.com
* Copyright: 2011 EJCMS.com. All rights reserved
* Author: TengLin
* File Name: SQLite3DB.php
*============================================================
*/
class SQLite3DB extends SQLite3 {
public $dbfile = "";
public $prefix = "";
protected $columns = "*";
protected $_join = [];
protected $_where = [];
protected $_orderby = [];
public function __construct($dbfile = "", $prefix = "") {
if (empty($this->dbfile)) {
if (empty($dbfile)) {
exit("Please write sqlite database address!");
} else {
$this->open($dbfile);
}
} else {
$this->open($this->dbfile);
}
if (!empty($prefix)) {
$this->prefix = $prefix;
}
}
public function create($table, $data) {
if (count($data) == count($data, 1)) {
$id = $this->insert($table, $data);
} else {
$id = $this->insertMulti($table, $data);
}
$this->reset();
if ($id) {
return $id;
} else {
return $this->lastErrorMsg();
}
}
public function update($table, $data, $array = []) {
$set = [];
if (!empty($array)) {
foreach ($array as $k => $v) {
$this->where($k, $v);
}
}
$query = "UPDATE " . $this->prefix . $table . " SET ";
foreach ($data as $k => $v) {
$set[] = "[" . $k . "] = '" . $v . "'";
}
$query .= implode(", ", $set) . $this->_where() . ";";
$result = $this->exec($query);
$this->reset();
return $result;
}
public function delete($table, $array = []) {
if (!empty($array)) {
foreach ($array as $k => $v) {
$this->where($k, $v);
}
}
$query = "DELETE FROM [" . $this->prefix . $table . "]" . $this->_where() . ";";
$result = $this->exec($query);
$this->reset();
return $result;
}
public function detail($table, $array = []) {
if (!empty($array)) {
foreach ($array as $k => $v) {
$this->where($k, $v);
}
}
if (is_array($table)) {
if ($table["join"]["table"]) {
$this->_db->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]);
} else {
foreach ($table["join"] as $v) {
$this->_db->join($v["table"], $v["condition"], $v["type"]);
}
}
$tables["table"] = $table["table"];
} else {
$tables["table"] = $table;
}
$result = $this->querySingle($this->_select($tables["table"]) . $this->_join() . $this->_where(), true);
$this->reset();
return $result;
}
public function items($table, $limit = null, &$callback = [], $columns = "*") {
if (is_array($table)) {
if ($table["join"]["table"]) {
$this->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]);
} else {
foreach ($table["join"] as $v) {
$this->join($v["table"], $v["condition"], $v["type"]);
}
}
$tables["table"] = $table["table"];
} else {
$tables["table"] = $table;
}
$this->columns = $columns;
$result = $this->query($this->_select($tables["table"]) . $this->_join() . $this->_where() . $this->_orderby() . $this->_limit($limit));
$totalCount = $this->querySingle($this->_select($tables["table"], "count(*)") . $this->_join() . $this->_where());
$this->reset();
$callback = $this->_pageinfo($totalCount, $limit);
$row = [];
while ($res = $result->fetchArray(SQLITE3_ASSOC)) {
$row[] = $res;
}
return $row;
}
public function join($table, $condition, $type = "INNER") {
$_join[] = $type . " JOIN " . $table . " ON " . $condition;
}
public function where($prop, $value = "", $operator = "=", $cond = "AND") {
$where = [];
if (count($this->_where) == 0) {
$cond = "";
}
$this->_where[] = [$cond, $prop, $operator, $value];
return $this;
}
public function orwhere($prop, $value = "", $operator = "=") {
return $this->where($prop, $value, $operator, "OR");
}
public function orderby($field, $direction = "DESC") {
$this->_orderby[$field] = $direction;
}
protected function reset() {
$this->columns = "*";
$this->_join = [];
$this->_where = [];
$this->_orderby = [];
}
protected function insert($table, $data, &$error = "") {
$sql = "INSERT INTO [" . $this->prefix . $table . "] ";
$sql .= "(" . implode(", ", array_keys($data)) . ")";
$sql .= " VALUES ";
$sql .= "('" . implode("', '", array_values($data)) . "');";
if ($this->exec($sql)) {
return $this->lastInsertRowID();
}
$error = $this->lastErrorMsg();
return false;
}
protected function insertMulti($table, $data) {
$ids = [];
$this->exec("begin;");
foreach ($data as $v) {
$ids[] = $this->insert($table, $v);
}
$this->exec("commit;");
return $ids;
}
protected function _select($table, $columns = "") {
if (empty($columns)) {
$columns = $this->columns;
}
return "SELECT " . $columns . " FROM [" . $this->prefix . $table . "]";
}
protected function _join() {
if (empty($this->_join)) {
return;
}
return " " . implode(" ", $this->_join);
}
protected function _where() {
if (empty($this->_where)) {
return;
}
$build = " WHERE";
foreach ($this->_where as $cond) {
list($concat, $varName, $operator, $val) = $cond;
$build .= " " . $concat . " " . $varName . " ";
switch (strtolower($operator)) {
case "not in":
case "in":
$build .= $operator . " ('" . implode("', '", $val) . "')";
break;
case "not between":
case "between":
$build .= $operator . " " . sprintf("%u AND %u", $val[0], $val[1]);
break;
case "not exists":
case "exists":
$build .= $operator . " (" . $val . ")";
break;
default:
$build .= $operator . " '" . $val . "'";
}
}
return $build;
}
protected function _orderby() {
if (empty($this->_orderby)) {
return;
}
$build = " ORDER BY ";
foreach ($this->_orderby as $prop => $value) {
if (strtolower(str_replace(" ", "", $prop)) == "rand()") {
$build .= "rand(), ";
} else {
$build .= $prop . " " . $value . ", ";
}
}
return rtrim($build, ", ") . " ";
}
protected function _limit($numRows) {
if (empty($numRows)) {
return;
}
if (is_array($numRows)) {
if ($numRows[0] < 1) {
$numRows[0] = 1;
}
return " LIMIT " . (int) $numRows[0] . ", " . (int) $numRows[1];
} else {
return " LIMIT " . (int) $numRows;
}
}
protected function _pageinfo($totalCount, $numRows) {
$pageinfo = [];
if (empty($numRows)) {
$pageinfo["page"] = 1;
} else {
if (is_array($limit)) {
$pageinfo["page"] = $numRows[0];
$pageinfo["pageSize"] = $numRows[1];
$pageinfo["totalPages"] = ceil($totalCount / $numRows[1]);
} else {
$pageinfo["page"] = 1;
$pageinfo["pageSize"] = $numRows;
$pageinfo["totalPages"] = ceil($totalCount / $numRows);
}
}
$pageinfo["totalCount"] = $totalCount;
return $pageinfo;
}
}