Skip to content

Commit db59da5

Browse files
committed
优化数据库参数绑定方式
1 parent a1ed0c9 commit db59da5

File tree

3 files changed

+89
-55
lines changed

3 files changed

+89
-55
lines changed

plugins/db/Db.php

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,30 @@ private function _reInit(){
9292
public function init($params){
9393
if(is_object($params))$params=(array)$params;
9494

95-
9695
if(!isset($params['engine']) || !in_array($params['engine'],$this->_allow_engines)){
9796
$params['engine']=$this->_engine_name;
9897
}
9998
$this->params=$params;
10099
$this->__setEngine($params['engine']);
101100
$this->_key = implode("|",$params);
101+
102+
if($this->_engine_name=="mysqli" && extension_loaded('mysqli')){
103+
$this->engine = new \SlightPHP\DbMysqli($this->params);
104+
}elseif(extension_loaded('pdo')){
105+
$this->engine = new \SlightPHP\DbPDO($this->params);
106+
}else{
107+
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
108+
return false;
109+
}
110+
$this->engine->init($this->params);
111+
if($this->engine->connect()===false){
112+
$this->error['code']=$this->engine->errno();
113+
$this->error['msg']=$this->engine->error();
114+
if(defined("DEBUG")){
115+
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
116+
}
117+
return false;
118+
}
102119
return true;
103120
}
104121
/**
@@ -146,7 +163,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
146163
//TABLE
147164
$table = $this->__array2string($table,true);
148165
//condition
149-
$condiStr = $this->__quote($condition,"AND");
166+
$condiStr = $this->__quote($condition,"AND",$params);
150167

151168
if($condiStr!=""){
152169
$condiStr=" WHERE ".$condiStr;
@@ -204,7 +221,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
204221
$sql="SELECT $item FROM ($table) $join $condiStr $groupby $orderby_sql $limit_sql";
205222
$start = microtime(true);
206223

207-
$result = $this->__query($sql);
224+
$result = $this->__query($sql,false,$params);
208225
if($result!==false){
209226
$data = new DbData;
210227
$data->page = (int)$this->page;
@@ -215,7 +232,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
215232
if($this->count==true){
216233
if($this->limit>0){
217234
$countsql="SELECT count(1) totalSize FROM ($table)$join $condiStr $groupby";
218-
$result_count = $this->__query($countsql);
235+
$result_count = $this->__query($countsql,false,$params);
219236
if(!empty($result_count[0])){
220237
$data->totalSize = (int)$result_count[0]['totalSize'];
221238
$data->totalPage = (int)ceil($data->totalSize/$data->limit);
@@ -260,13 +277,13 @@ public function selectOne($table,$condition="",$item="",$groupby="",$orderby="",
260277
*/
261278
public function update($table,$condition,$item){
262279
$table = $this->__array2string($table);
263-
$value = $this->__quote($item,",");
264-
$condiStr = $this->__quote($condition,"AND");
280+
$value = $this->__quote($item,",",$params);
281+
$condiStr = $this->__quote($condition,"AND",$params2);
265282
if($condiStr!=""){
266283
$condiStr=" WHERE ".$condiStr;
267284
}
268285
$sql="UPDATE $table SET $value $condiStr";
269-
return $this->__query($sql);
286+
return $this->__query($sql,false,$this->merge_params($params,$params2));
270287
}
271288
/**
272289
* delete
@@ -277,12 +294,15 @@ public function update($table,$condition,$item){
277294
*/
278295
public function delete($table,$condition){
279296
$table = $this->__array2string($table);
280-
$condiStr = $this->__quote($condition,"AND");
297+
$condiStr = $this->__quote($condition,"AND",$params);
281298
if($condiStr!=""){
282299
$condiStr=" WHERE ".$condiStr;
283300
}
284301
$sql="DELETE FROM $table $condiStr";
285-
return $this->__query($sql);
302+
return $this->__query($sql,false,$params);
303+
}
304+
public function escape($str){
305+
return $this->engine->escape($str);
286306
}
287307
/**
288308
* insert
@@ -309,14 +329,24 @@ public function insert($table,$item="",$isreplace=false,$isdelayed=false,$update
309329
$command.=" DELAYED ";
310330
}
311331

312-
$f = $this->__quote($item,",");
332+
$f = $this->__quote($item,",",$params);
313333

314334
$sql="$command INTO $table SET $f ";
315-
$v = $this->__quote($update,",");
335+
$v = $this->__quote($update,",",$params2);
316336
if(!empty($v)){
317337
$sql.="ON DUPLICATE KEY UPDATE $v";
318338
}
319-
return $this->__query($sql);
339+
return $this->__query($sql,false,$this->merge_params($params,$params2));
340+
}
341+
342+
/**
343+
* merge array
344+
*/
345+
private function merge_params(...$arr){
346+
$arr = array_filter($arr,function($var){
347+
return ($var && is_array($var)) ? true : false;
348+
});
349+
return array_merge(...$arr);
320350
}
321351

322352
/**
@@ -326,7 +356,7 @@ public function insert($table,$item="",$isreplace=false,$isdelayed=false,$update
326356
* @return Array $result || Boolean false
327357
*/
328358

329-
private function __query($sql, $retry=false){
359+
private function __query($sql, $retry=false, $params=[]){
330360
//{{{
331361
//SQL MODE 默认为DELETE,INSERT,REPLACE 或 UPDATE,不需要返回值
332362
$sql_mode = 1;//1.更新模式 2.查询模式 3.插入模式
@@ -346,26 +376,8 @@ private function __query($sql, $retry=false){
346376
if(defined("DEBUG")){
347377
trigger_error("{$this->_engine_name} ( $sql )");
348378
}
349-
//Connect
350-
if(extension_loaded('pdo')){
351-
$this->engine = new \SlightPHP\DbPDO($this->params);
352-
}elseif(extension_loaded('mysqli')){
353-
$this->engine = new \SlightPHP\DbMysqli($this->params);
354-
}else{
355-
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
356-
return false;
357-
}
358-
$this->engine->init($this->params);
359-
if($this->engine->connect()===false){
360-
$this->error['code']=$this->engine->errno();
361-
$this->error['msg']=$this->engine->error();
362-
if(defined("DEBUG")){
363-
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
364-
}
365-
return false;
366-
}
367379

368-
$result = $this->engine->query($sql);
380+
$result = $this->engine->query($sql, $params);
369381

370382
if($result){
371383
if($sql_mode==2){//查询模式
@@ -383,7 +395,7 @@ private function __query($sql, $retry=false){
383395

384396
if($retry===false && $this->engine->connectionError){
385397
$this->_reInit();
386-
return $this->__query($sql,true);
398+
return $this->__query($sql,true,$params);
387399
}
388400
trigger_error("DB QUERY ERROR (".var_export($this->error['msg'],true)."), CODE({$this->error['code']}), SQL({$sql})",E_USER_WARNING);
389401
return false;
@@ -397,7 +409,7 @@ public function execute($sql){
397409
return $this->__query($sql);
398410
}
399411

400-
private function __quote($condition,$split="AND"){
412+
private function __quote($condition,$split="AND",&$params=[]){
401413
$condiStr = "";
402414
if(is_array($condition) || is_object($condition)){
403415
$v1=array();
@@ -408,8 +420,8 @@ private function __quote($condition,$split="AND"){
408420
$k = $this->__addsqlslashes($k);
409421
}
410422
if(!is_null($v)){
411-
$v = addslashes($v);
412-
$v1[]="$k = \"$v\"";
423+
$params[]=$v;
424+
$v1[]="$k = ?";
413425
}else{
414426
$v1[]="$k = NULL";
415427
}

plugins/db/DbMysqli.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbEngine.php");
2323
class DbMysqli implements DbEngine{
2424
private $_mysqli;
25+
private $_stmt;
2526
private $_result;
2627

2728
private $_engine;
@@ -53,6 +54,11 @@ public function init($params=array()){
5354
$this->{"_".$key} = $value;
5455
}
5556
}
57+
public function __destruct(){
58+
if($this->_stmt){
59+
$this->_stmt->close();
60+
}
61+
}
5662
public function connect(){
5763
$host = $this->_host;
5864
if($this->_persistent){
@@ -68,46 +74,60 @@ public function connect(){
6874
}
6975
return true;
7076
}
71-
public function query($sql){
77+
public function query($sql, $params=[]){
7278
if($this->_mysqli->connect_errno){
7379
return false;
7480
}
75-
$this->_result= $this->_mysqli->query($sql);
76-
if($this->_result){
77-
return true;
81+
$this->_stmt = $this->_mysqli->prepare($sql);
82+
if($this->_stmt===false){
83+
return false;
84+
}
85+
$r = $this->_stmt->execute($params);
86+
if($r===false){
87+
return false;
7888
}
79-
return false;
89+
$this->_result = $this->_stmt->get_result();
90+
return true;
8091
}
8192
public function getAll(){
8293
if(!$this->_result)return false;
83-
$data=array();
84-
while($row= $this->_result->fetch_assoc()){$data[]=$row;};
85-
return $data;
94+
return $this->_result->fetch_all(MYSQLI_ASSOC);
8695
}
8796
public function count(){
8897
if($this->_mysqli->connect_errno){
8998
return false;
9099
}
91-
return $this->_mysqli->affected_rows;
100+
if(!$this->_stmt)return false;
101+
return $this->_stmt->affected_rows;
102+
}
103+
public function escape($str){
104+
if($this->_mysqli->connect_errno){
105+
return false;
106+
}
107+
return $this->_mysqli->real_escape_string($str);
92108
}
93109
public function lastId(){
94110
if($this->_mysqli->connect_errno){
95111
return false;
96112
}
97-
return $this->_mysqli->insert_id;
113+
if(!$this->_stmt)return false;
114+
return $this->_stmt->insert_id;
98115
}
99116
public function error(){
100117
if($this->_mysqli->connect_error){
101118
return $this->_mysqli->connect_error;
102119
}
103-
return $this->_mysqli->error;
120+
if(!$this->_stmt)return false;
121+
return $this->_stmt->error;
104122
}
105123
public function errno(){
106124
$error=0;
107125
if($this->_mysqli->connect_errno){
108126
$error = $this->_mysqli->connect_errno;
109127
}else{
110-
$error = $this->_mysqli->errno;
128+
if($this->_stmt){
129+
$error = $this->_stmt->errno;
130+
}
111131
}
112132
if($error=='2006'){
113133
$this->connectionError=true;

plugins/db/DbPDO.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,26 @@ public function connect(){
8585
}
8686
return true;
8787
}
88-
public function query($sql){
88+
public function query($sql,$params=[]){
8989
if(!$this->_pdo)return false;
9090
$this->_stmt = $this->_pdo->prepare($sql);
91-
if($this->_stmt && $this->_stmt->execute ()!==false){
92-
return true;
91+
if($this->_stmt){
92+
return $this->_stmt->execute($params);
9393
}
9494
return false;
9595
}
9696
public function getAll(){
97-
if($this->_stmt){
98-
return $this->_stmt->fetchAll (\PDO::FETCH_ASSOC );
99-
}
100-
return false;
97+
if(!$this->_stmt)return false;
98+
return $this->_stmt->fetchAll(\PDO::FETCH_ASSOC);
10199
}
102100
public function count(){
103101
if(!$this->_stmt)return false;
104102
return $this->_stmt->rowCount();
105103
}
104+
public function escape($str){
105+
if(!$this->_pdo)return false;
106+
return trim($this->_pdo->quote($str),"'");
107+
}
106108
public function lastId(){
107109
if(!$this->_pdo)return false;
108110
return $this->_pdo->lastInsertId();

0 commit comments

Comments
 (0)