php读写分离数据库类

* *数据库操作基础类 */ class DbBase{ private static $instance = null; //唯一实例. private $db; //数据库连接 private $readdb = null; //读数据库连接 private $writedb = null ; //写数据库连接 private $readconfig; //读库的配置 private $writeconfig; //写库的配置 //下面的函数主要是主从分离和获取数据库的链接操作 public function _construct(){ $config = YafRegistry::get('config')->mysql; //获取配置文件 $this->readconfig = $config->read; //读配置文件 $this->writeconfig = $config->write; //写配置文件 $this->readConntection(); //默认的为读库链接 } //获取数据库的读写配置 public static function factory(){ if(self::$instance === null){ self::$instance = new self(); } return self::$instance; } //读库时的数据库链接 public function readConntection(){ if($this->readdb != null){ $this->db = $this->readdb; return; } $this->readdb = $this->getConnection($this->readconfig); $this->db = $this->readdb; $this->readdb->query("SET NAMES '{$this->readconfig['charset']}'"); } //写库时的数据库链接 public function witeConntection(){ if($this->writedb != null){ $this->db = $this->writedb; return; } $this->writedb = $this->getConnection($this->writeconfig); $this->db = $this->writedb; $this->writedb->query("SET NAMES '{$this->readconfig['charset']}'"); } //创建数据库链接 public function getConnection($DbconfigArr = array()){ if(isarray($DbconfigArr)) { throw new Exception("The DbcofigArr is not a array"); } $db = new mysqli($DbconfigArr['host'],$DbconfigArr['username'],$DbconfigArr['password'],$DbconfigArr['dbname'],$DbconfigArr['port']); if(mysqliconnecterrno()){ throw new Exception('Connect Error('.mysqliconnecterrno().')'.mysqliconnecterror()); } return $db; } public function query($sqlUrl){ //读写分离 if(pregmatch('/^\s*select/i',$sqlUrl)){ $this->readConntection(); }else{ $this->witeConntection(); } $result = $this->db->query($sqlUrl); //最后一次调用产生的错误代码 返回0代表没有错误发生 if($this->db->errno){ throw new Exception('Query Error('.mysqliconnecterrno().')'.mysqliconnecterror()); } return $result; } //////下面的函数主要是根据需求获取不同查询需求: ////// 1.获取信息(单条、多条) 2.insert操作 3.update操作 4.delete操作 (批量操作) 5.获取相关信息的总数 ///// 函数参数说明:1.数据库字段以array('field1','field2'...)获取,2:sql语句完善where部分 //获取结果中所有的行 public function fetchAssoc($sql){ $returnArr = array(); $result = $this->query($sql); while($row = $result->fetchassoc()){ $returnArr[] = $row; } $result ->free(); return $returnArr; } //获取结果中的第一行的值 public function fetchRow($sql){ $row = array(); $returnArr = array(); $result = $this->query($sql); $row = $result->fetcharray(MYSQLASSOC);//获取第一条数据 if(empty($row)){ return array(); } return $row; } //插入操作 public function insert($sql){ $this->witeConntection(); $result = $this->db->query($sql); return $this->db->insertid; } //更新删除操作 public function modify($sql){ $this->witeConntection(); $this->db->query($sql); return $this->db->affectedrows; } ///////对提供的参数进行主装成不同的sql语句 //把查询的字段数组主装成字符串 public function getFieldStr($arrayField = array()){ $newArr = array(); foreach($arrayField as $v){ $newArr[] ="$v"; } return join(',',$newArr); } //获取对应数组的key对应的value并对其进行处理 private function _getValue($value){ if(empty($value)){ return "''"; }else if (isstring($value)){ return "'".self::$instance->db->realescapestring($value)."'"; }else{ return "''"; } } //获取更新的字符串 public function getModifyStr($arrayFieldKeyValue = array()){ $returnArr = array(); foreach($arrayFieldKeyValue as $key=>$value){ $returnArr[] = "".$key."" . '=' . $this->getValue($value); } return join(',',$returnArr); } public function getInsertStr($fieldArr = array()){ $field = array(); $value = array(); foreach($fieldArr as $f=>$v){ $field[] = "".$f.""; $value[] = "'".self::$instance->db->realescape_string($v)."'"; } return array(join(',',$field),join(',',$value)); } }

Hibbs

0