zhphp framework (十九) pdo数据库抽象类

来源:互联网 发布:土地整理预算软件 编辑:程序博客网 时间:2024/04/25 11:41
<?php/** * Created by JetBrains PhpStorm. * User: 张华 * Date: 14-3-8 * Time: 下午12:21 * QQ: 746502560@qq.com * To change this template use File | Settings | File Templates. */class dbPdo extends model {    protected    $name;    private  $auto_increment;    protected  $prefix;    public  function __construct(){        parent::__construct();    }  /**     * @param $sql     * @param array $data     * @return mixed     * 直接执行sql,执行非查询sql insert/updata/delete     */    public function execute($sql, $data = array()){        // TODO: Implement execute() method.        if(empty ($data)){            $dbh=$this->prepare();#得到数据库操作对象            $count=$dbh->exec($sql);#快速执行sql            unset($sql);            return $count;        }else{           $sth=$this->prepare($sql);#预处理sql语句,返回预处理对象            $sth=$this->bindParam($sth,$data);            $sth->execute();#执行sql           $count=$sth->rowCount();//返回操作结果集           $sth->closeCursor();           unset ($sql,$data);           return $count;        }    }   /**    * 查询一行数据    * @param type $sql    * @param type $data    * @return type    */    public function query($sql, $data = array()){        // TODO: Implement query() method.               if(empty ($data)){               $dbh=$this->prepare();#得到数据库操作对象               $sth = $dbh->query($sql);#执行查询sql语句               if(is_object($sth)){                $result = $sth->setFetchMode(PDO::FETCH_ASSOC);#设置数据返回格式为关联数组                $uData=$sth->fetch();                $sth->closeCursor();                unset ($sql,$data,$result);                return $uData;               }else{                   return false;               }           }else{              $sth=$this->prepare($sql);#预处理sql语句,返回预处理对象              $sth=$this->bindParam($sth, $data);#绑定数据              $count=$sth->execute();#执行sql              $result = $sth->setFetchMode(PDO::FETCH_ASSOC);#设置数据返回格式为关联数组              $uData=$sth->fetch();              $sth->closeCursor();              unset ($sql,$data,$result);              return $uData;          }       }   /**    *查询所有    * @param type $sql    * @param type $data    * @return type    */    public function  queryAll($sql, $data = array())    {        // TODO: Implement queryAll() method.         if(empty ($data)){             $dbh=$this->prepare();#得到数据库操作对象             $sth = $dbh->query($sql);#执行查询sql语句              if(is_object($sth)){                  $result = $sth->setFetchMode(PDO::FETCH_ASSOC);#设置数据返回格式为关联数组                  $uData=$sth->fetchAll();                   $sth->closeCursor();#释放资源                   unset ($sql,$data,$result);                   return $uData;                }else{                    return false;                }           }else{              $sth=$this->prepare($sql);#预处理sql语句,返回预处理对象              $sth=$this->bindParam($sth, $data);#绑定数据              $count=$sth->execute();#执行sql              $result = $sth->setFetchMode(PDO::FETCH_ASSOC);#设置数据返回格式为关联数组              $uData=$sth->fetchAll();              $sth->closeCursor();              unset ($sql,$data,$result);              return $uData;          }   }    /**     *  查询统计     * @param $sql     * @param array $data     * @return mixed     */    public function total($sql, $data = array()){        if(empty ($data)){            $dbh=$this->prepare();#得到数据库操作对象            $sth = $dbh->query($sql);#执行查询sql语句            $count=$sth->fetchColumn();#得到查询的结果行数            $sth->closeCursor();#关闭游标,使语句能再次被执行            unset ($sql,$data);            return $count;        }else{            $sth=$this->prepare($sql);#预处理sql语句,返回预处理对象            $sth=$this->bindParam($sth, $data);#绑定数据            $sth->execute();#执行sql            $count=$sth->fetchColumn();#得到查询的结果行数            $sth->closeCursor();#关闭游标,使语句能再次被执行            unset ($sql,$data);            return $count;        }    }    /**     * @param $data     * @return mixed     * Av模型执行sql     */    public function add($tableName,$data){        // TODO: Implement add() method.        $tableName=strtoupper($this->getTab_().$tableName);#得到真实的表名        $fields =$this->getField($tableName);        if($fields['EXTRA'] == 'auto_increment'){            $this->auto_increment=$fields['COLUMN_NAME'];        }        $queryStr=is_array($data)?$this->setQuery(array_values($data)):$data;#得到预处理查询格式,允许是字符串        $sql='INSERT  INTO  '.$tableName.'  VALUES  ('.$queryStr.')';#组装sql语句        $rows= is_array($data)?$this->execute($sql,array_values($data)):$this->execute($sql);#执行sql        unset($queryStr,$tableName,$sql,$data,$this->auto_increment);        return $rows;    }  /**   * 修改   * @param type $tableName   * @param type $data   * @param type $where   * @return type   */    public function  save($tableName,$data,$where='')    {        // TODO: Implement save() method.          $queryStr=$this->setQuery($data);#得到预处理查询格式          $tableName=strtoupper($this->getTab_().$tableName);#得到表名          if(empty ($where)){ #如果不存在where 那么where 条件就是data里面的最后一个元素              $arr=explode(',', $queryStr);               $tt=end($arr);               $where='  WHERE   '.$tt;          }else{ #否则where 要么是数组要么是字符串              if(is_string($where)){                  $where='  WHERE   '.$this->where($where);              }else if(is_array($where)){                  $where='  WHERE   '.$this->setQuery($data);#得到预处理查询格式              }          }          $sql='UPDATE '.$tableName.' SET '.$queryStr.$where;          $rows=$this->execute($sql, $data);#执行sql          unset($queryStr,$tableName,$sql,$data,$where);          return $rows;    }  /**   * 删除   * @param type $tableName   * @param string $where   * @return type   */    public function  delete($tableName,$where='')    {        // TODO: Implement delete() method.           $tableName=strtoupper($this->getTab_().$tableName);#得到表名           if(is_string($where)){#如果是字符串                $where='  WHERE   '.$this->where($where);                 $sql='DELETE FROM '.$tableName .$where;                 $rows=$this->execute($sql);#执行sql                  unset($tableName,$sql,$where);                  return $rows;              }else if(is_array($where)){#如果是数组                  $queryWhere='  WHERE   '.$this->setQuery($where);#得到预处理查询格式                  $sql='DELETE FROM '.$tableName .$queryWhere;                   $rows=$this->execute($sql,$where);#执行sql                  unset($tableName,$sql,$where);                  return $rows;              }    }  /**   * 查找一行   * @param type $tableName   * @param type $field   * @param string $where   * @return type   */    public function  find($tableName,$field='*', $where='')    {        // TODO: Implement find() method.          $tableName=strtoupper($this->getTab_().$tableName);#得到表名           if(is_string($where)){#如果是字符串               $where=empty($where)?null: '  WHERE   '.$this->where($where);               $sql='SELECT  '.$field.'  FROM  '.$tableName.$where;                $udata=$this->query($sql);                unset($tableName,$where,$sql);                return $udata;           }else if(is_array($where)){                $queryWhere='  WHERE   '.$this->setQuery($where);#得到预处理查询格式                if(strpos($queryWhere,',')){                    $queryWhere=str_ireplace(',',' and ',$queryWhere);                }                $sql='SELECT  '.$field.'  FROM  '.$tableName.$queryWhere;                $udata=$this->query($sql,$where);                unset($tableName,$where,$sql);                return $udata;           }    }   /**    * 查找多行    * @param type $tableName    * @param type $data    * @param string $where    * @return type    */    public function findAll($tableName,$field='*', $where='')    {        // TODO: Implement findAll() method.        $tableName=strtoupper($this->getTab_().$tableName);#得到表名           if(is_string($where)){#如果是字符串               $where=empty($where)?null: '  WHERE   '.$this->where($where);               $sql='SELECT  '.$field.'  FROM  '.$tableName.$where;               $udata=$this->queryAll($sql);                unset($tableName,$where,$sql);                return $udata;           }else if(is_array($where)){                $queryWhere='  WHERE   '.$this->setQuery($where);#得到预处理查询格式               if(strpos($queryWhere,',')){                   $queryWhere=str_ireplace(',',' and ',$queryWhere);               }                $sql='SELECT  '.$field.'  FROM  '.$tableName.$queryWhere;                $udata=$this->queryAll($sql,$where);                unset($tableName,$where,$sql);                return $udata;           }    }    private function  where($where){        $dbh=$this->prepare();#得到数据库操作对象        $dbh->quote($where);#sql条件进行预防sql注入处理        return $where;    }    /**     *预处理绑定     * @param type $sth     * @param type $data     * @return type     */    private function bindParam($sth,$data){        $keys=array_keys($data);        //判断是有自动增长字段        if( ! empty($this->auto_increment)){            array_unshift($data,null);        }         $count=count($data);        if(is_int($keys[0])){           for($i=0;$i<$count;$i++){             $sth->bindParam($i+1,$data[$i]);          }        }else{            for($i=0;$i<$count;$i++){              $sth->bindParam(':'.$keys[$i],$data[$keys[$i]]);          }        }        unset($keys,$count,$data);        return $sth;    }  private   function bindArrayValue($req, $array, $typeArray = false)    {        if(is_object($req) && ($req instanceof PDOStatement))        {           //判断是有自动增长字段             if( ! empty($this->auto_increment)){                 array_unshift($data,null);             }            foreach($array as $key => $value)            {                if($typeArray)                    $req->bindValue(":$key",$value,$typeArray[$key]);                else                {                    if(is_int($value))                        $param = PDO::PARAM_INT;                    elseif(is_bool($value))                        $param = PDO::PARAM_BOOL;                    elseif(is_null($value))                        $param = PDO::PARAM_NULL;                    elseif(is_string($value))                        $param = PDO::PARAM_STR;                    else                        $param = FALSE;                    if($param){                       $req=is_int($key)? $req->bindValue(($key+1),$value,$param): $req->bindValue(":$key",$value,$param);                      }                   }            }        }        return $req;    }   /**     *预处理sql,返回预处理对象     * @param type $sql     * @return type     */    private function  prepare($sql=''){        $dbh=config::readConfig('DbConnection');#得到pdo操作对象        #$dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);#强制大写#        if($sql == ''){           return  $dbh;        }else{           $sth=$dbh->prepare($sql);#预处理sql           return $sth;        }      }    /**     *得到表前缀     * @return type     */    public function  getTab_(){        $table_pre=config::readConfig('db','table_pre');        $this->prefix=$table_pre;       return  ($this->prefix === false || empty( $this->prefix) || is_null( $this->prefix))?null:$table_pre;    }    /**     *设置查询;预处理sql格式     * @param type $data     * @return type     */    private function setQuery($data){        $queryStr='';        $key=key($data);        if( ! empty($this->auto_increment)){            array_unshift($data,'null');      }        $count=count($data);        if(is_int($key)){            for($i=0;$i<$count;$i++){               $queryStr.='?,';            }        }else{              $keys=array_keys($data);              for($i=0;$i<$count;$i++){                   $queryStr.=$keys[$i].'=:'.$keys[$i].',';              }              unset($keys);         }        $queryStr=substr($queryStr, 0, -1);        unset($key,$count,$data);        return $queryStr;    }    /**     * 额外功能     */    public function  getField($tableName)    {        // TODO: Implement getField() method.        $sql='select * from information_schema.columns where table_name="'.$tableName.'"';        return $this->query($sql);    }    /**     * 事物处理     * @param array $sql     * 需要传递的你的sql语句集合     * 注意你的先后顺序     * 执行简单的事物     */    public  function  transaction($query=array()){        try{            $dbh=$this->prepare();#得到数据库操作对象            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);            $dbh->beginTransaction();    //启动一个事务            foreach($query as $sql){                $this->execute($sql);            }            $dbh->commit();  //提交事务执行        }catch (PDOException $e){            $dbh->rollBack();    //执行失败,回滚            die('失败:'.$e->getMessage());        }    }    /**     * 手动开启一个事物     */    public  function  begin($dbh=null){        $dbh=is_null($dbh)?$this->prepare():$dbh;        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);        $dbh->beginTransaction();    //启动一个事务    }    /**     * 手动提交事物     * @param $dbh     * @return bool|void     */    public  function  commit($dbh=null){        $dbh=is_null($dbh)?$this->prepare():$dbh;        $dbh->commit();  //提交事务执行    }    /**     * 手动回滚事物     * @param $dbh     * @return bool|void     */    public  function rollBack($dbh=null){        $dbh=is_null($dbh)?$this->prepare():$dbh;        $dbh->rollBack();    //执行失败,回滚    }    public function  lastInsertId($dbh=null){        $dbh=is_null($dbh)?$this->prepare():$dbh;        return $dbh->lastInsertId();    }}

0 0