1. [文件] link.class.php~3KB 下载 (2) ?php namespace YBL;class mysql_link {protected $_link='';protected $_host='';protected $config=array();protected $connectFailures=0;public function __construct($config=array()){$this-config=array('d
1. [文件] link.class.php ~ 3KB 下载(2)
<?php namespace YBL; class mysql_link { protected $_link=''; protected $_host=''; protected $config=array(); protected $connectFailures=0; public function __construct($config=array()){ $this->config=array('driver_options'=>array(), 'interval_time'=>0, 'db_name'=>'', //'host'=>'127.0.0.1', 'user'=>'root', 'pass'=>'', 'port'=>'3306', 'auto_shift'=>true, 'max_connect_retries'=>5 //'dsn'=>'host="localhost"', ); if(!isset($config['db_deploy'])||!is_callable($config['db_deploy'])){ $config['db_deploy']=array(__CLASS__,'default_db_deploy'); } if(!isset($config['error_hander'])||!is_callable($config['error_hander'])){ $config['error_hander']=array(__CLASS__,'default_error_hander'); } $this->config=array_merge($this->config,$config); } public function link($host_list=array()){ $_host=$host_no=$default_host=$this->_host=''; try { if(empty($host_list)){ $host_no=-1; $_host=$this->parse_host(array()); }else{ $host_no=call_user_func_array($this->config['db_deploy'],array($host_list)); $_host=$this->parse_host($host_list[$host_no]); } $default_host=array( 'user'=>$this->config['user'], 'pass'=>$this->config['pass'], 'interval_time'=>$this->config['interval_time'], 'driver_options'=>$this->config['driver_options'], ); $this->_host=$_host=array_merge($default_host,$_host); $this->_host['host_no']=$host_no; $this->_link=new \PDO($_host['dsn'],$_host['user'],$_host['pass'],$_host['driver_options']); }catch(\PDOException $e){ $this->_link=''; if($this->config['auto_shift']){ if(count($host_list)>1){ call_user_func_array($this->config['error_hander'],array($this->_host)); usleep($_host['interval_time']); unset($host_list[$host_no]); $this->link($host_list); }else{ call_user_func_array($this->config['error_hander'],array($this->_host)); }}else{ call_user_func_array($this->config['error_hander'],array($this->_host)); }}} static public function default_db_deploy($host){ $keys=array_keys($host); $key=array_rand($keys,1); return $keys[$key]; } static public function default_error_hander($host=array(),$host_no=-1){ } protected function parse_host($host=array()){ $dsn=$db_name=$port=''; if(isset($host['db_name'])){ $db_name=$host['db_name']; unset($host['db_name']); }else{ $db_name=$this->config['db_name']; } if(isset($host['port'])){ $port=$host['port']; unset($host['port']); }else{ $port=$this->config['port']; } switch(1){ case isset($host['host']): $dsn='mysql:host='.$host['host'].';port='.$port.';'.(empty($db_name)?'':'dbname='.$db_name); unset($host['host']); break; case isset($host['dsn']): $dsn='mysql:'.$host['dsn'].';'.(empty($db_name)?'':'dbname='.$db_name); break; case isset($this->config['host']): $dsn='mysql:host='.$this->config['host'].';port='.$port.';'.(empty($db_name)?'':'dbname='.$db_name); break; case isset($this->config['dsn']): $dsn='mysql:'.$this->config['dsn'].';'.(empty($db_name)?'':'dbname='.$db_name); break; default: $dsn='mysql:host=127.0.0.1;port='.$port.';'.(empty($db_name)?'':'dbname='.$db_name); } $host['dsn']=$dsn; return $host; } public function _server(){ return $this->_host; } public function pdo_config($attribute='',$value=NULL){ if($attribute==='') return $this->_link->getAvailableDrivers(); if($value===NULL) return $this->_link->getAttribute($attribute); $this->_link->setAttribute($attribute,$value); return $this; } }
2. [文件] op.class.php ~ 6KB 下载(2)
<?php namespace YBL; require_once('link.class.php'); class mysql_op extends mysql_link{ protected $statement=''; protected $bind_data=''; public $auto_exe=true; //============================================================================================= public function pre($sql){ $this->auto_exe=true; $this->bind_data=''; $this->statement=$this->_link->prepare($sql); return $this; } //============================================================================================= public function cclose(){ $this->statement->closeCursor(); return $this; } //============================================================================================= public function query($sql){ $this->auto_exe=false; $this->statement=$this->_link->query($sql); return $this; } //============================================================================================= public function bind($bind){ foreach($bind as $v){ $v=(array)$v; $bind_key=$v[0]; if(!is_numeric($v[0])){ $v[0]=':'.$v[0]; } $data_type=isset($v[1])?$v[1]:\PDO::PARAM_STR; $length=isset($v[2])?$v[2]:12; $driver_options=isset($v[3])?$v[3]:array(); $this->statement->bindParam($v[0],$this->bind_data[$bind_key],$data_type,$length,$driver_options); } return $this;} //============================================================================================= public function value($value){ $i=0; //$value=array_values((array)$value); foreach($this->bind_data as $k=>$v){ $this->bind_data[$k]=$value[$i]; $i++; } return $this; } //============================================================================================= public function result($type=0,$option1=NULL,$option2=NULL,$option3=NULL){ if($this->auto_exe){$this->statement->execute();} $return=''; switch($type){ case 1:case 'fetchAll': if($option1===NULL){return $this->statement->fetchAll();} if($option2===NULL){return $this->statement->fetchAll($option1);} if($option3===NULL){return $this->statement->fetchAll($option1,$option2);} return $this->statement->fetchAll($option1,$option2,$option3); case 2:case 'lastInsertId': return $this->_link->lastInsertId(); case 3:case 'fetch':$this->auto_exe=false; return $this->statement->fetch($option1,$option2,$option3); case 4:case 'columnCount': return $this->statement->columnCount(); case 5:case 'fetchColumn': //$option1=(int)$option1; return $this->statement->fetchColumn($option1); case 6:case 'fetchObject':$this->auto_exe=false; if($option1===NULL){return $this->statement->fetchObject();} if($option2===NULL){return $this->statement->fetchObject($option1);} return $this->statement->fetchObject($option1,$option2); default:return $this->statement->rowCount(); }} //============================================================================================= public function tran($type=0){ switch($type){ //事务提交 case 1:$this->_link->commit(); break; //事务回滚 case 2:$this->_link->rollBack(); break; //事务检查 case 3:$this->_link->inTransaction(); break; //事务开始 default: $this->_link->beginTransaction(); } } //============================================================================================= public function check($sql,$type=\PDO::PARAM_STR){ return $this->_link->quote($sql,$type); } public function insert($table,$arr,$pre=true,$replace=false){ $sql=''; $this->auto_exe=false; if($replace){ $insert='REPLACE '; }else{ $insert='INSERT '; } if(is_string($arr)){//查询插入 $table=(array)$table; $sql=$insert."INTO `".$table[0]."` "; if(isset($table[1])) $sql.='('.$table[1].') '; $sql.='('.$arr.') ;'; if($pre){ $this->auto_exe=true; $this->statement=$this->_link->prepare($sql); }else{ $this->statement=$this->_link->query($sql); }}else{$key='';$values=''; if($pre){//使用预处理 if(isset($arr[0])){//有绑定情况 foreach($arr as $v){ $key.='`'.$v.'`,'; $values.=':'.$v.','; } $sql=$insert."INTO `".$table."` (".rtrim($key,',').")VALUES(".rtrim($values,',').");"; $this->statement=$this->_link->prepare($sql); $this->bind($arr); $this->auto_exe=true; }else{ foreach($arr as $k=>$v){ $key.='`'.$k.'`,'; $values.=':'.$k.','; } $sql=$insert."INTO `".$table."` (".rtrim($key,',').")VALUES(".rtrim($values,',').");"; $this->statement=$this->_link->prepare($sql); $this->statement->execute($arr); }}else{ foreach($arr as $k=>$v){ $key.='`'.$k.'`,'; $values.=$this->check($v).','; } $sql=$insert."INTO `".$table."` (".rtrim($key,',').")VALUES(".rtrim($values,',').");"; $this->statement=$this->_link->query($sql); }}return $this;} //============================================================================================= function update($table,$arr,$where,$pre=true){ $values='';$sql='';$this->auto_exe=false; if($pre){//使用预处理 if(isset($arr[0])){//有绑定情况 foreach($arr as $v){ $values.='`'.$v.'`=:'.$v.','; } $sql='UPDATE `'.$table.'` SET '.rtrim($values,',').' WHERE '.$where.';'; $this->statement=$this->_link->prepare($sql); $this->bind($arr); $this->auto_exe=true; }else{ foreach($arr as $k=>$v){ $values.='`'.$k.'`=:'.$k.','; } $sql='UPDATE `'.$table.'` SET '.rtrim($values,',').' WHERE '.$where.';'; $this->statement=$this->_link->prepare($sql); $this->statement->execute($arr); }}else{ foreach($arr as $k=>$v){ $values.='`'.$k.'`='.$this->check($v).','; } $sql='UPDATE `'.$table.'` SET '.rtrim($values,',').' WHERE '.$where.';'; $this->statement=$this->_link->query($sql); }return $this; } //===================================================================================== function delete($table,$where='',$pre=true){ $sql='delete from `'.$table.'` where '.$where; if($pre){$this->auto_exe=true; $this->statement=$this->_link->prepare($sql); }else{$this->auto_exe=false; $this->statement=$this->_link->query($sql); }return $this; } }