工厂模式:单例模式封装数据库的具体操作(PDO)

php  封装mysql的类

interface等 需自己定义  单例模式不适用于工厂模式,若是需要 引入到工厂模式


  具体的类代码

dsn=$dsn;
        $pdo=new PDO($this->dsn['dsn'],$this->dsn['username'],$this->dsn['password']);
        $pdo->exec("set names ".$this->dsn['charset']."");
        $this->pdo=$pdo;
        return $this;
    }
    public function getInstance()
    {
        if(!self::$obj instanceof self){//instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例:
            self::$obj=new self;
        }
        return self::$obj;
    }
    public function connect(){

    }
    public function table($tableName){
        $tableName=trim($tableName);
        $this->table=$tableName;
        return $this;
    }
    public function insert($data){//进行添加  返回 上一条的添加值
        $keyStr="";
        $valStr="";
        foreach($data as $k=>$v){
            $keyStr.=','.$k;
            $valStr.=','."'$v'";
        }
        $keyStr=trim($keyStr,',');
        $valStr=trim($valStr,',');

        $sql="INSERT INTO ".$this->table." ($keyStr) VALUES($valStr)";
        $res=$this->pdo->exec($sql);
        if($res){
            return $this->pdo->lastInsertId();
        }else{
            return $this->pdo->errorInfo();
        }

    }
    public function select($columns, $join = null, $where = null, $group = null, $having = null, $order = null, $limit = null){
        $columns = implode(',', array_values($columns));
        // 需要加条件判断是否为空,是否需要拼接
        $sql = "select ".$columns." from `".$this->table."` ";
        if($join!=null){
            $sql .= $join;
        }
        if($where!=null){
            $sql .= " where ".$where;
        }
        if($group!=null){
            $sql .= " group by ".$group;
        }
        if($having!=null){
            $sql .= " having ".$having;
        }
        if($order!=null){
            $sql .= " order by ".$order;
        }
        if($limit!=null){
            $sql .= " limit ".$limit;
        }

        //echo $sql;exit;
        $res = $this->pdo->query($sql);
        return $res;
    }
    public  function getRow($field='*',$where)
    {//获取 单行数据    参数为  字段名和where条件  (数据类型为数组)
        $whereStr=1;
        if(!empty($where)){
            foreach ($where as $k => $val) {
                $whereStr.=" AND ".$k."="."'$val'";
            }
        }
        $whereStr=substr($whereStr,5);
        //$fieldStr='*';
        if(!empty($field)){
            $field=trim($field,',');
        }

        $sql= "select $field from ".$this->table." where $whereStr";
        $data=$this->pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
        return $data;

    }
    public function getOne($field="*",$where)
    {//获取 单个字段名的数据  参数为字段名(string) 和where条件(array)
        $whereStr=1;
        if(!empty($where)){
            foreach ($where as $k => $val) {
                $whereStr.=" AND ".$k."="."'$val'";
            }
        }
        $whereStr=substr($whereStr,5);

        if(!empty($field)){
            $field=trim($field,',');
        }

        $sql= "select $field from ".$this->table." where $whereStr";
        $data=$this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        return $data;

    }
    public function getAll()//所有数据
    {
        $sql="select * from ".$this->table ;
        $res=$this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        return $res;
    }
    public function update($data,$where)//改
    {
        $setStr="";
        foreach($data as $k=>$v){
            $setStr.=$k.'='."'$v'".',';
        }
        $whereStr="";
        foreach ($where as $k => $val) {
            $whereStr.=" AND ".$k."="."'$val'";
        }
        $whereStr=substr($whereStr,5);
        $setStr=trim($setStr,',');
        $sql="update ".$this->table." set  $setStr where $whereStr";
        $res=$this->pdo->exec($sql);
        return  $res;
    }
    public function delete($where)//删除
    {   $str="";
        foreach ($where as $k => $val) {
            $str.=" AND ".$k."="."'$val'";
        }
        $str=substr($str,5);
        $sql="delete from $this->table where $str";
        $res=$this->pdo->exec($sql);
        return $res;
    }
}
$mysql=mysqlDB::getInstance();

$data=array(
    'title'=>'测试数据',
    'contents'=>'测试数据内容'
);

$res=$mysql->table('news')->insert($data);
var_dump($res);

给一个接口 具体方法 可以加入接口中 并使用
implements  引用
interface DB{
    //增加 arr为传入的数组
    public function insert($arr);
    //查询
    public function select();
    //修改
    public function update($arr,$where);
    //删除
    public function delete($where);
    //执行sql语句
    public function query($sql);
    public function table($tableName);

}




你可能感兴趣的:(工厂模式)