/
db.php
753 lines (723 loc) · 22.6 KB
/
db.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
<?php
class DB{
/**db链接*/
public $conn = null;
public $host = null;
public $port = null;
public $username = null;
public $password = null;
public $dbname = null;
/**事务*/
public $autocommit = true;
public $hascommit = false;
/**
* 单例DB引用
*
* @var DB
*/
public static $db = null;
/**
* 数据库操作描述
*
* @var DBDes
*/
public static $dbdes = null;
/**db多库*/
public static $is_main_sub = false;
public static $current_db = null;
/**此次查询是否是CUD操作*/
public static $is_cud = false;
/**此次查询是否是CUD操作*/
public static $last_sql = '';
/**
* 单列模式获得数据库连接类<br/>
* 如果人为指定连接名称则DB类会按照多库规则处理,不会自动切换数据库连接<br/>
* @param string $conn_name
* 多库并存的情况下指明要获得连接的名称<br/>
* 传入false则锁定到主配数据库<br/>传入null切换主配数据库恢复到主从模式
* <br/>传入true时随机切换到从属数据库
* @return DB
*/
public static function get_db($conn_name=true,$reconnect = false){
if($reconnect){
self::$db = null;
}
if(empty(Conf::$db_boys)){
$conn_name = false;
}
if(self::$db === null){
self::$db = new DB();
self::$db->to_connect($conn_name);
}else{
//特定指定的数据库
if(is_string($conn_name)){
if($conn_name===self::$current_db){
self::$is_main_sub = false;
}
}elseif($conn_name===true){
//要切换到从属数据库
if(is_string(self::$current_db)){
//已经选择了从属数据库就不再选了
self::$is_main_sub = true;
return self::$db;
}else{
self::$db->to_connect($conn_name);
}
}elseif($conn_name===false){
//要锁定到主数据库
if(!is_string(self::$current_db)){
//已经选择了从属数据库就不再选了
self::$is_main_sub = false;
return self::$db;
}else{
self::$db->to_connect($conn_name);
}
}else{
//恢复到默认
if(!is_string(self::$current_db)){
//已经选择了从属数据库就不再选了
self::$is_main_sub = true;
return self::$db;
}else{
self::$db->to_connect($conn_name);
}
}
}
return self::$db;
}
/**
* 数据库连接测试<br/>
* @param string $host 连接地址
* @param string $port 连接端口
* @param string $username 连接用户名
* @param string $password 连接密码
* @param string $dbname 默认数据库名称
* @return mixed <br/>成功返回true<br/>失败返回失败原因
*/
public static function test_conn($host,$port,$username,$password,$dbname = null){
$link = @mysql_connect($host.':'.$port, $username, $password);
if (!$link){
return '连接测试失败';
}
if($dbname == null){
return true;
}
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list)) {
if($row->Database === $dbname){
return true;
}
}
return '连接成功,数据库"'.$dbname.'"不存在';
}
/**
* 数据库操作类初始化<br/>
* @param string $host 连接地址
* @param string $port 连接端口
* @param string $username 连接用户名
* @param string $password 连接密码
* @param string $dbname 默认数据库名称
*/
public function init($host,$port,$username,$password,$dbname){
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
if (!$this->conn = @mysql_connect($host.':'.$port, $username, $password)){
die('<center>DB Connect Error</center>');
}
mysql_select_db($dbname, $this->conn);
mysql_query("SET NAMES 'utf8'");
mysql_query("set charset utf8");
}
/**
* 根据配置文件初始化数据库连接类<br/>
* 开发中请用get_db方法获取
* @param string $conn_name 多库并存的情况下指明要获得连接的名称<br/>传入false则锁定到主配数据库<br/>传入null恢复到主从模式<br/>传入true时随机切换到从属数据库
*/
public function to_connect($conn_name=null){
if(self::$db!==null){
if(!self::$db->autocommit && self::$db->hascommit===false){
die('当前数据库('.(is_string(self::$current_db) ? self::$current_db : '主数据库').')事务还未提交或回滚,无法进行库切换,请检查程序源码');
}
}
if($conn_name===null||$conn_name===false){
self::$is_main_sub = (!empty(Conf::$db_boys)&&$conn_name===null)? true: false;
self::$current_db = $conn_name;
$this->init(Conf::$db_host,Conf::$db_port,Conf::$db_username,Conf::$db_password,Conf::$db_dbname);
Log::debug('切换到主数据库');
}else{
$pz_arr = null;
if($conn_name===true){
self::$current_db = array_rand(Conf::$db_boys);
$pz_arr = Conf::$db_boys[self::$current_db];
self::$is_main_sub = true;
}else{
self::$current_db = $conn_name;
$pz_arr = Conf::$db_boys[$conn_name];
self::$is_main_sub = false;
}
$this->init($pz_arr['host'],$pz_arr['port'],$pz_arr['username'],$pz_arr['password'],$pz_arr['dbname']);
Log::debug('切换到数据库:'.self::$current_db);
}
}
/**
* 开启事务
*/
public function begin_transaction(){
$this->base_query('START TRANSACTION');
$this->autocommit = false;
}
/**
* 事务回滚
*/
public function rollback(){
$this->base_query('ROLLBACK');
$this->hascommit = null;
}
/**
* 事务提交
*/
public function commit(){
$this->base_query('COMMIT');
$this->hascommit = true;
}
/**
* 获得原生的SQL字符串到参数中 不进行任何的SQL字符转移
*
* @param string sql
* @return string
*/
public static function raw($sql){
return '@yYUc_RaW@'.$sql;
}
/**
*基于MySql的底层查询封装<br/>
* @param string $sql 要执行查询的SQL语句参数请用"?"代替
* @param array $pam 数字下标的数组,数组项依次替换SQL中的"?"参数
*/
public function base_query($sql,$pam=null) {
if(is_array($sql)){
return $this->base_query($sql[0],$sql[1]);
}else if(!isset($pam)){
//DB自动切换
if(self::$is_main_sub){
//如果是主从模式
if(self::$is_cud && self::$current_db!==null){
//如过是CUD操作
self::get_db(null);
}elseif (!self::$is_cud && self::$current_db===null){
self::get_db(true);
}
}
if(Conf::$need_db_log){
//开发模式下记录db日志
file_put_contents(YYUC_FRAME_PATH.'/log/db/'.date('Y_m_d_H',time()).'.log', date('i:s',time()).','.self::$current_db.': '.$sql."\n",FILE_APPEND);
}
//调试日志
if(Conf::$need_debug_log){
Debug::log_sql_begin(self::$current_db.':'.Request::ip().'::'.$sql);
}
//数据验证的执行
if(self::$dbdes !== null){
self::$dbdes->db = $this;
DBDes::init_by_sql($sql);
if(self::$dbdes->operate !='R'){
Cache::set('YYUC_TABLE_TIME'.self::$dbdes->real_table,time());
}
if(is_callable('db_validations')){
db_validations(clone self::$dbdes);
}
self::$dbdes = null;
}
$this->statement = @mysql_query($sql, $this->conn);
if(Conf::$need_debug_log){
Debug::log_sql_end();
}
if (!$this->statement) {
$mysqlerr = mysql_error();
if(is_array($mysqlerr)){
$mysqlerr = implode(',', $mysqlerr);
}
Log::error($mysqlerr.":".$sql);
}else{
self::$last_sql = $sql;
}
return $this->statement;
}else{
if(is_string($pam)){//参数为字符串类型
$sql = str_replace('@yYUc_SqL@',"'".mysql_escape_string($pam)."'",$sql);
}else if(is_array($pam)){//单纯数组
$sqlss = explode('@yYUc_SqL@', $sql);
$sqllen = count($pam);
$sql = '';
for($i=0;$i<$sqllen;$i++){
if($pam[$i]===null){
$sql.=($sqlss[$i]."null");
}elseif(strpos($pam[$i], '@yYUc_RaW@') === 0){
$sql.=($sqlss[$i].substr($pam[$i], 10));
}else{
$sql.=($sqlss[$i]."'".mysql_escape_string($pam[$i])."'");
}
}
$sql.= $sqlss[$sqllen];
}
return $this->base_query($sql);
}
}
/**
* 执行无结果查询(增,删,改)<br/>
* @param string $sql 要执行查询的SQL语句参数请用"?"代替
* @param array $pam 数字下标的数组,数组项依次替换SQL中的"?"参数
*/
public function execute($sql,$pam=null){
self::$is_cud = true;
if(self::$dbdes ==null){
self::$dbdes = new DBDes();
}
if(is_string($sql)){
$sql = str_replace('?', '@yYUc_SqL@', $sql);
}
$res = $this->base_query($sql,$pam);
self::$is_cud = false;
return $res;
}
/**
* 普通SQL查询
* @param string $sql 要执行查询的SQL语句参数请用"?"代替
* @param array $pam 数字下标的数组,数组项依次替换SQL中的"?"参数
* @return array 字符下标的数组集合
*/
public function query($sql,$pam=null,$callback = null){
$res = array();
if(is_string($sql)){
$sql = str_replace('?', '@yYUc_SqL@', $sql);
}
$query = $this->base_query($sql,$pam);
if(self::$dbdes ==null){
self::$dbdes = new DBDes();
self::$dbdes->operate = 'R';
}
if(is_callable($callback)){
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
call_user_func_array($callback,array($tn));
}
}else{
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
$res[] = $tn;
}
return $res;
}
}
/**
* 普通SQL查询
* @param string $sql 要执行查询的SQL语句参数请用"?"代替
* @param array $pam 数字下标的数组,数组项依次替换SQL中的"?"参数
* @return array<model> 模型集合
*/
public function query_model($sql,$pam=null,$callback = null){
$res = array();
if(is_string($sql)){
$sql = str_replace('?', '@yYUc_SqL@', $sql);
}
$query = $this->base_query($sql,$pam);
if(self::$dbdes ==null){
self::$dbdes = new DBDes();
self::$dbdes->operate = 'R';
}
if(is_callable($callback)){
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
call_user_func_array($callback,array((object)$tn));
}
}else{
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
$res[] = (object)$tn;
}
return $res;
}
}
/**
* 查询并返回Model单列数据集合查询
* @param Model $model 数据模型
* @param mixed $condition 查询条件
* @param string $order 排序方式
* @param string $limit 查询范围
* @param string $field 查询字段
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return array 单列数据集合
*/
public function fill_model_one_column($model,$condition=null,$order=null,$limit=null,$field='*',$pam=null){
$sql = $this->com_sql($model->YYUCSYS_real_tablename,$condition,$order,null,$limit,$field,$pam);
return $this->one_column_array($sql);
}
/**
* 单列数据集合查询
* @param string $sql 要执行查询的SQL语句参数请用"?"代替
* @param array $pam 数字下标的数组,数组项依次替换SQL中的"?"参数
* @return array 某一数据字段结果集合
*/
public function one_column_array($sql,$pam=null){
$query = $this->base_query($sql,$pam);
$res = array();
while($tn=mysql_fetch_array($query,MYSQL_NUM)){
$res[] = $tn[0];
}
if(count($res)>0){
return $res;
}
return null;
}
/**
* 查询并返回Model实体类的集合
* @param Model $model 数据模型
* @param mixed $condition 查询条件
* @param string $order 排序方式
* @param string $limit 查询范围
* @param string $field 查询字段
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return array Model实体类的集合
*/
public function fill_models($model,$condition=null,$order=null,$group=null,$limit=null,$field='*',$pam=null){
$sql = $this->com_sql($model->YYUCSYS_real_tablename,$condition,$order,$group,$limit,$field,$pam);
$res = array();
$query = $this->base_query($sql);
$modelname = $model->YYUCSYS_tablename;
if(!$model->YYUCSYS_isorgin){//如果是用户的扩展类
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
$newmodel = new $modelname();
foreach ($tn as $k=>$v){
$newmodel->$k = $v;
}
$newmodel->fill_virtual_field();
$res[] = $newmodel;
}
}else{
while($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
$newmodel = new Model($modelname);
foreach ($tn as $k=>$v){
$newmodel->$k = $v;
}
$res[] = $newmodel;
}
}
return $res;
}
/**
* 查询并返回Model实体类的字符下标的数组集合
* @param Model $model 数据模型
* @param mixed $condition 查询条件
* @param string $order 排序方式
* @param string $limit 查询范围
* @param string $field 查询字段
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return array 字符下标的数组集合
*/
public function fill_models_array($model,$condition=null,$order=null,$group=null,$limit=null,$field='*',$pam=null){
$sql = $this->com_sql($model->YYUCSYS_real_tablename,$condition,$order,$group,$limit,$field,$pam);
return $query = $this->query($sql);
}
/**
* 填充这个Model实体
* @param Model $model 要填充数据模型
* @param mixed $condition 查询条件
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return boolean 是否填充成功
*/
public function fill_one_model(& $model,$condition,$pam=null,$ord=null,$group=null,$limit=null){
$sql = $this->com_sql($model->YYUCSYS_real_tablename,$condition,$ord,$group,$limit,$model->YYUCSYS_select,$pam);
$query = $this->base_query($sql);
if($tn=mysql_fetch_array($query,MYSQL_ASSOC)){
foreach ($tn as $k=>$v){
$model->$k = $v;
}
return true;
}
return false;
}
/**
* 向数据库表中添加一条数据
* @param string $table 表名
* @param array $data 要添加的数据,字符下标的数组形式
* @return integer 成功插入返回插入ID,插入失败返回null
*/
public function insert_one_entity($table,$data=array(),$forceinsert = false){
$ssql = "insert into `$table` (";
$vars = array();
$vals = array();
$pres = array();
$columns = $this->list_fields($table);
if(!$forceinsert){
unset($data['id']);
}
foreach ($data as $k=>$val){
if(in_array($k,$columns)){
$vars[] = "`$k`";
$vals[] = $val === null ? self::raw('null'):$val;
$pres[] = '@yYUc_SqL@';
}
}
$ssql.=(implode(",", $vars).") values (".implode(",", $pres).")");
if($this->execute($ssql,$vals)){
if($forceinsert){
return $data['id'];
}else{
return mysql_insert_id($this->conn);
}
}
return null;
}
/**
* 更新数据
* @param string $table 表名
* @param mixed $condition 要更新的where条件或者字符下标的条件数组
* @param array $data 要更新的数据,字符下标的数组形式
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return integer 成功返回受影响的行数,更新失败返回null
*/
public function update_entity($table,$condition=null,$data=array(),$pam=null){
$ssql = "update `$table` set ";
$vars = array();
$vals = array();
$columns = $this->list_fields($table);
foreach ($data as $k=>$val){
if(in_array($k,$columns)){
$vars[] = "`$k`=@yYUc_SqL@ ";
$vals[] = $val === null ? self::raw('null'):$val;;
}
}
$ssql.=implode(",", $vars);
if(!empty($condition)){
if(is_string($condition)){
$condition = str_replace('?', '@yYUc_SqL@', $condition);
}
$whereres = $this->con_sql($condition,$pam);
$ssql .= trim($whereres[0]);
$pam = $whereres[1];
if($pam!==null){
$vals = array_merge($vals, $pam);
}
}
if($this->execute($ssql,$vals)){
return @mysql_affected_rows($this->conn);
}
return null;
}
/**
* 根据条件删除
* @param string $table 表名
* @param mixed $condition 要更新的where条件或者字符下标的条件数组
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return integer 成功返回受影响的行数,更新失败返回null
*/
public function del_entity($table,$condition=null,$pam=null){
$ssql = "delete from `$table` ";
if(!empty($condition)){
if(is_string($condition)){
$condition = str_replace('?', '@yYUc_SqL@', $condition);
}
$whereres = $this->con_sql($condition,$pam);
$ssql .= trim($whereres[0]);
$pam = $whereres[1];
}
if($this->execute($ssql,$pam)){
return @mysql_affected_rows($this->conn);
}
return null;
}
/**
* 获得数据库表名称集合
* @return array 数据库表名称集合
*/
public function list_tables(){
return $this->one_column_array('SHOW TABLES FROM '.Conf::$db_dbname);
}
/**
* 判断表中是否含有符合条件的数据
* @param string $table 表名
* @param mixed $condition 要更新的where条件或者字符下标的条件数组
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
* @return boolean 含有返回true不含有返回false
*/
public function has($table,$condition=null,$pam=null){
$sql = $this->com_sql($table,$condition,null,null,null,'id',$pam);
return !(count($this->query($sql))===0);
}
static $tablefieldsarray = array();
/**
* 获得数据库表字段综合信息数组<br/>
* 成功获得后将存入 静态变量中 作为数据缓冲
* @param string $table 表名
* @return array 数据表的详细字段信息集合
*/
public function get_fields_array($table){
if(empty(self::$tablefieldsarray[$table])){
self::$tablefieldsarray[$table] = $this->query("show full fields from `$table`");
}
return self::$tablefieldsarray[$table];
}
static $tablefields = array();
/**
* 获得数据库表字段名称一维数组<br/>
* 成功获得后将存入 静态变量中 作为数据缓冲
* @param $table 表名
* @return array 数据表的字段名称集合
*/
public function list_fields($table){
if(empty(self::$tablefields[$table])){
$all_field = $this->get_fields_array($table);
$res = array();
foreach ($all_field as $af){
$res[] = $af['Field'];
}
self::$tablefields[$table] = $res;
}
return self::$tablefields[$table];
}
///////////////////////以下为通用方法
/**
* 根据键值组合条件
*/
private function _con_or_sql($ord,$val,&$pam){
$vars = array();
foreach ($val as $k=>$v){
$k = trim($k);
if($v===true){
$vars[] = "`$k` is not null";
}elseif($v===null){
$vars[] = "`$k` is null";
}elseif(is_array($v)){
//如果是数组 结合OR的形式
if(isset($v[0])){
//数字索引数组
$temporval = array();
foreach ($v as $vvv){
$temporval[] = "`$k`=@yYUc_SqL@";
$pam[] = $vvv;
}
$vars[] = '('.implode(" or ", $temporval).')';
}else{
$vars[] = $this->_con_or_sql($k,$v,$pam);
}
}elseif(strpos($k, '@')!==false){
$ks = explode('@', $k);
if($ks[1]=='~'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$pam[] = '%'.$v.'%';
}elseif($ks[1]=='|~'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$pam[] = $v.'%';
}elseif($ks[1]=='~|'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$pam[] = '%'.$v;
}else{
$vars[] = "`".$ks[0]."` ".$ks[1]." @yYUc_SqL@";
$pam[] = $v;
}
}else{
$vars[] = "`$k` = @yYUc_SqL@";
$pam[] = $v;
}
}
return '('.implode(" ".$ord." ", $vars).')';
}
/**
* 并且关系的条件语句组合
* @param mix $where 如过是数组形式如下<br/>
* array('un'=>'mqq','up@~'=>'mqq','uc@|~'=>'mqq','uc@~|'=>'mqq','age@>'=>'13','bz'=>array(....),'OR'=>array('a'=>'1','b'=>'2'))
* @param array $pam 数字下标的数组,数组项依次替换$where中的"?"参数
* @return array 拼接好的SQL条件[0]为语句 [1]为$pam参数
*/
public function con_sql($where=null,$pam=null){
if($where===null){
return array(null,null);
}
$sql = '';
if(is_string($where)){
$sql .= ' where '.$where;
}else if (is_array($where)||!empty($where)){
$vars = array();
$vals = array();
foreach ($where as $k=>$val){
$k = trim($k);
if($val===true){
$vars[] = "`$k` is not null";
}elseif($val===null){
$vars[] = "`$k` is null";
}elseif(is_array($val)){
//如果是数组 结合OR的形式
if(isset($val[0])){
//数字索引数组
$temporval = array();
foreach ($val as $vvv){
$temporval[] = "`$k`=@yYUc_SqL@";
$vals[] = $vvv;
}
$vars[] = '('.implode(" or ", $temporval).')';
}else{
$vars[] = $this->_con_or_sql($k,$val,$vals);
}
}elseif(strpos($k, '@')!==false){
$ks = explode('@', $k);
if($ks[1]=='~'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$vals[] = '%'.$val.'%';
}elseif($ks[1]=='|~'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$vals[] = $val.'%';
}elseif($ks[1]=='~|'){
$vars[] = "`".$ks[0]."` like @yYUc_SqL@";
$vals[] = '%'.$val;
}else{
$vars[] = "`".$ks[0]."` ".$ks[1]." @yYUc_SqL@";
$vals[] = $val;
}
}else{
$vars[] = "`$k`=@yYUc_SqL@";
$vals[] = $val;
}
}
$sql.=(" where ".implode(" and ", $vars));
$pam = $vals;
}
return array($sql,$pam);
}
/**
* 组合单表的SQL查询语句
* @param string $table 表名
* @param mixed $where 查询条件
* @param string $order 排序方式
* @param string $limit 查询范围
* @param string $field 查询字段
* @param array $pam 数字下标的数组,数组项依次替换$condition中的"?"参数
*/
public function com_sql($table,$where=null,$order=null,$group=null,$limit=null,$field='*',$pam=null){
$sql = 'select '.$field.' from `'.$table.'` ';
if(!empty($where)){
if(is_string($where)){
$where = str_replace('?', '@yYUc_SqL@', $where);
}
$whereres = $this->con_sql($where,$pam);
$sql .= trim($whereres[0]);
$pam = $whereres[1];
}
if(!empty($order)){
$sql .= ' order by '.$order;
}
if(!empty($group)){
$sql .= ' group by '.$group;
}
if(!empty($limit)){
$sql .= ' limit '.$limit;
}
return array($sql,$pam);
}
/**
* 获得数据库版本
* @return string 版本号
*/
public function version(){
return mysql_get_server_info();
}
}
?>