简易封装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);
?>

猜你喜欢

转载自www.cnblogs.com/rickyctbu/p/11074821.html