简易封装PDO的操作类

Posted rickyctbu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易封装PDO的操作类相关的知识,希望对你有一定的参考价值。

<?php
ini_set(‘display_errors‘, true);

final class DB 
    private static $db;
    public static function getInstance(): PDO 
        if(!self::$db instanceof PDO) 
            new self();
        
        return self::$db;
    
    private function __clone() 
    private function __construct() 
        $config = parse_ini_file(‘./db.ini‘);
        $dsn = sprintf(‘mysql:localhost=%s;dbname=%s;port=%d;charset=%s‘, $config[‘host‘], $config[‘dbname‘], $config[‘port‘], $config[‘charset‘]);
        try
            self::$db = new PDO($dsn, $config[‘user‘], $config[‘password‘]);
        catch(PDOException $e) 
            exit($e->getMessage());
        
    


class Statement 
    private static $statement;
    private $state;
    private function __construct(PDO $db, string $sql) 
        $this->state = $db->prepare($sql);
    
    public static function getInstance(PDO $db, string $sql) 
        if(!self::$statement instanceof Statement) 
            self::$statement = new self($db, $sql);
        
        return self::$statement;
    
    public function bindValue($key, $val) 
        return $this->state->bindValue($key, $val);
    
    public function bindParam($key, $val) 
        return $this->state->bindParam($key, $val);
    
    public function execute() 
        return $this->state->execute();
    
    public function fetchOne() 
        return $this->state->fetch(PDO::FETCH_ASSOC);
    
    public function fetchAll() 
        return $this->state->fetchAll(PDO::FETCH_ASSOC);
    
    public function fetchColumn(int $index = null) 
        return $this->state->fetchColumn($index);
    
    public function resetQuery() 
        return $this->state->closeCursor();
    
    public function effectRow() 
        return $this->state->rowCount();
    
    public function errorReason() 
        return $this->state->errorInfo()[2];
    



class Query 
    private $db;
    private $statement;
    public function __construct(string $sql) 
        $this->db = DB::getInstance();
        $this->statement = Statement::getInstance($this->db, $sql);
    
    private function exec() 
        return $this->statement->execute() ? true: $this->statement->errorReason() ?? ‘SQL语句有误‘;
    
    private function queryRes(bool $is_all = false) 
        $res = $this->exec();
        if($res) 
            $data = $is_all? $this->statement->fetchAll(): $this->statement->fetchOne();
            return $data === false? []: $data;
        
        return $res;
    
    public function bindValue($key = null, $val = null) 
        if(func_num_args() === 0 || (!is_array($key) && !$val)) 
            return $this;
         else if(is_array($key)) 
            foreach($key as $k => $v) 
                $this->statement->bindValue($k, $v);
            
         else 
            $this->statement->bindValue($key, $val);
        
        return $this;
    
    public function bindParam($key = null, $val = null) 
        if(func_num_args() === 0 || (!is_array($key) && !$val)) 
            return $this;
         else if(is_array($key)) 
            foreach($key as $k => $v) 
                $this->statement->bindParam($k, $v);
            
         else 
            $this->statement->bindParam($key, $val);
        
        return $this;
    
    public function one() 
        return $this->queryRes(false);
    
    public function all() 
        return $this->queryRes(true);
    
    public function fetchColumn($index = 0) 
        $res = $this->exec();
        if($res) 
            $data = $this->statement->fetchColumn($index);
            return $data === false? ‘‘: $data;
        
        return $res;
    
    public function __destruct() 
        $this->statement->resetQuery();
    

$res = (new Query(‘SELECT * FROM `user` WHERE `id` > :id ‘))->bindValue(‘:id‘,2)->all();
var_dump($res);
?>

 

以上是关于简易封装PDO的操作类的主要内容,如果未能解决你的问题,请参考以下文章

简易高重用的jdbcutils工具封装实现类以及简易连接池实现

Android 简易音乐播放器

二手写MyBatis简易版框架

Android项目中okhttp3的简易封装和使用

python日志打印和写入并发简易版本实现

Python3简易接口自动化测试框架设计与实现 实例2