-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_sqlite.php
545 lines (428 loc) · 10.3 KB
/
db_sqlite.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
date_default_timezone_set('PRC');
class db_sqlite{
//private db_path = "";
//打开数据库
function connect_sqlite(){
$dbPath = dirname(__FILE__).'\db\weixin.db';
$dbConnet = 'sqlite:'.$dbPath;
$conn =null;
if(file_exists($dbPath)){
try{
$conn = new PDO($dbConnet);
//$conn = new PDO("sqlite:student.db");
$conn->beginTransaction();
}
catch(PDOException $e){
error_logger("connect database fail .Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $conn;
}else{
error_logger('database path is wrong');
//exit(header('location:http://www.baidu.com/'));
}
}
function query_sqlite($conn,$sql){
$result = array();
try {
$sth=$conn->prepare($sql);
$sth->execute();
$result=$sth->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $result;
}
function query_count($conn,$sql){
$result = null;
try {
$sth=$conn->prepare($sql);
$sth->execute();
$vec=$sth->fetchAll();
$result = $vec['0'];
return ($vec['0']['count(*)']);
} catch (PDOException $e) {
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $result;
}
//获取某张表的总记录数
function query_total($conn,$table){
$result = 0;
$sql = 'select count(*) as c from '.$table;
try {
$sth=$conn->prepare($sql);
$sth->execute();
$vec=$sth->fetchAll();
//$result = $vec['0'];
return ($vec['0']['c']);
} catch (PDOException $e) {
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $result;
}
//获取最大的id
function query_maxid($conn,$table=''){
$maxid = 0;
try {
$sql = 'select max(id) as maxid from '.$table;
$sth=$conn->prepare($sql);
$sth->execute();
$vec=$sth->fetchAll();
$maxid = $vec['0']['maxid'];
return $maxid;
} catch (PDOException $e) {
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $maxid;
}
function exec_sqlite($conn,$sql){
$count = 0;
try {
$conn->exec($sql);
} catch (PDOException $e) {
$conn->rollBack();
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
// $conn->commit();
return $count;
}
function commit_sqlite($conn){
$conn->commit();
}
function close_sqlite($conn){
$conn = null;
}
}
/*Actions*/
class Actions{
function _count($sql){
sqlite_logger(' count sql '.$sql);
$result = null;
try {
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$sth=$conn->prepare($sql);
$sth->execute();
$vec=$sth->fetchAll();
$result = $vec['0']['count(*)'];
$result = intval($result);
//return ($vec['0']['count(*)']);
} catch (PDOException $e) {
error_logger("sql:".$sql." Exception is".$e->getMessage());
//echo "Exception is".$e->getMessage();
}
return $result;
}
function _list($sql){
sqlite_logger(' list sql '.$sql);
$array = array();
try{
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$array = $db->query_sqlite($conn,$sql);
$db->close_sqlite($conn);
$db = null;
//sqlite_logger('ok list result '.var_dump($array));
}catch(Exception $e){
//sqlite_logger('fail list result '.var_dump($array));
return null;
}
return $array;
}
function _update($sql){
sqlite_logger(' update sql '.$sql);
try{
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$db->exec_sqlite($conn,$sql);
$db->commit_sqlite($conn);
$db->close_sqlite($conn);
$db = null;
}
catch(Exception $e){
return false;
}
return true;
}
function _delete($sql){
sqlite_logger(' delete sql '.$sql);
try{
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$db->exec_sqlite($conn,$sql);
$db->commit_sqlite($conn);
$db->close_sqlite($conn);
$db = null;
}
catch(Exception $e){
return false;
}
return true;
}
function _bulkdelete($sql){
sqlite_logger(' bulkdelete sql '.$sql);
try{
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$db->exec_sqlite($conn,$sql);
$db->commit_sqlite($conn);
$db->close_sqlite($conn);
$db = null;
}
catch(Exception $e){
return false;
}
return true;
}
function _create($sql,$table){
sqlite_logger(' create sql '.$sql);
try{
$db = new db_sqlite();
$conn = $db->connect_sqlite();
$db->exec_sqlite($conn,$sql);
$db->commit_sqlite($conn);
$id = $db->query_maxid($conn,$table);
$db->close_sqlite($conn);
$db = null;
return $id;
}catch(Exception $e){
return null;
}
}
}
class Dao{
function Dao(){
$this->table = '';
}
function getSearchSql($params){
$sql = ' where 1=1 ';
return $sql;
}
function getListParams(){
}
function getUpdateParams(){
}
function getDeleteParams(){
$record = json_decode('{}');
$id = $_GET['id'];
$record->id = $id;
if( empty($id) ){
$record = json_decode('{"code":500}');
$record->msg = 'id miss';
}
return $record;
}
function getCreateParams(){
}
//获取批量删除ids
function getBulkDeleteParams(){
$record = json_decode('{}');
$ids = $_GET['ids'];
$record->ids = $ids;
if( empty($ids) ){
$record = json_decode('{"code":500}');
$record->msg = 'ids miss';
}
return $record;
}
function ajax_list(){
$ret = json_decode('{"code":0,"msg":"success"}');
$params = $this->getListParams();
$array = $this->_list($params);
$count = $this->_count($params);
if( gettype($array) == 'array' ){
$ret->data = $array;
$ret->total = $count;
return json_encode($ret);
}else{
return '{"code":500,"msg":"there are something wrong "}';
}
}
function ajax_update(){
$record = $this->getUpdateParams();
if($record->code){
return json_encode($record);
}
$result = $this->_update($record);
if($result){
return '{"code":200,"msg":"success"}';
}else{
return '{"code":500,"msg":"update 好像出错了"}';
}
}
function ajax_create(){
$record = $this->getCreateParams();
sqlite_logger(' ajax_create '.json_encode($record));
if($record->code){
return json_encode($record);
}
$id = $this->_create($record);
if($id){
return '{"code":200,"msg":"success","data":"'.$id.'"}';
}else{
return '{"code":500,"msg":"create 好像出错了"}';
}
}
function ajax_delete(){
$record = $this->getDeleteParams();
if($record->code){
return json_encode($record);
}
$id = $record->id;
$result = $this->_delete($id);
if($result){
return '{"code":200,"msg":"success"}';
}else{
return '{"code":500,"msg":"delete 好像出错了"}';
}
}
function ajax_bulkdelete(){
$record = $this->getBulkDeleteParams();
if($record->code){
return json_encode($record);
}
$ids = $record->ids;
$result = $this->_bulkdelete($ids);
if($result){
return '{"code":200,"msg":"success"}';
}else{
return '{"code":500,"msg":"bulkdelete 好像出错了"}';
}
}
//select * from keyword where msgtypes like '2,%' or msgtypes like '%,2,%' or msgtypes like '%,2';
//待改进 $condition
function _count($opts){
$table = $this->table;
$result = null;
$condition = $this->getSearchSql($opts);
$sql = "select count(*) from $table ".$condition;
try{
$actions = new Actions();
$result = $actions->_count($sql);
//return $result;
}catch(Exception $e){
return null;
}
return $result;
}
//待改进 $condition
function _list($opts){
$table = $this->table;
$array = null;
$condition = $this->getSearchSql($opts);
$page = $opts->_page?$opts->_page:1;
$pagesize = $opts->_pagesize?$opts->_pagesize:$this->_pagesize;
$page = intval($page);
$page--;
$pagesize = intval($pagesize);
$index = $page*$pagesize;
$limit = " limit $index,$pagesize";
$sql = "select * from $table ".$condition." order by id desc ".$limit;
try{
$actions = new Actions();
$array = $actions->_list($sql);
return $array;
}catch(Exception $e){
return null;
}
return $array;
}
function _update($record){
$table = $this->table;
$id=$record->id ;
$array =array();
foreach ($record as $key => $value) {
if($key == 'id'){
}else{
if(gettype($value) == 'integer'){
$array[] = "$key = $value";
}else if(gettype($value) == 'string'){
$array[] = "$key = '$value'";
}
}
}
$str = implode(',', $array);
if($id and count($array)>0){
$sql = "update $table set ".$str." where id = $id ";
try{
$actions = new Actions();
$actions->_update($sql);
}
catch(Exception $e){
return false;
}
}else{
return false;
}
return true;
}
function _delete($id){
$table = $this->table;
$sql = "delete from $table where id = $id ";
try{
$actions = new Actions();
$actions->_delete($sql);
return true;
}
catch(Exception $e){
return false;
}
}
//$ids string 1,2,3
function _bulkdelete($ids){
$table = $this->table;
$sql = "delete from $table where id in ($ids) ";
try{
$actions = new Actions();
$actions->_bulkdelete($sql);
return true;
}
catch(Exception $e){
return false;
}
}
function _create($record){
$id = null;
$table = $this->table;
$arr_name =array();
$arr_value =array();
foreach ($record as $key => $value) {
$arr_name[] = $key;
if(gettype($value) == 'integer'){
$arr_value[] = $value;
}else if(gettype($value) == 'string'){
$arr_value[] = "'$value'";
}
}
$str_name = implode(',', $arr_name);
$str_value = implode(',', $arr_value);
$sql = "insert into $table($str_name) values($str_value)";
try{
$actions = new Actions();
$id = $actions->_create($sql,$table);
}catch(Exception $e){
return null;
}
return $id;
}
}
function getRequest($name=''){
$result = '';
if($_POST[$name]){
$result = $_POST[$name];
}else if($_GET[$name]){
$result = $_GET[$name];
}
return $result;
}
function error_logger($content){
file_put_contents("error_log.html",date('Y-m-d H:i:s ').$content.'<br>',FILE_APPEND);
}
?>