PHP的PDO數據庫操作類

ouhp9343 8年前發布 | 1K 次閱讀 PHP Caffe,Theano

一個簡單的PDO類封裝。。僅供學習交流

PdoDb 數據庫類

<?php
/**
 * @throws Error
 * PDO數據庫
 */

class PdoDb extends DatabaseAbstract
{
    /**
     * PDO實例
     * @var PDO
     */
    protected $DB;
    /**
     * PDO準備語句
     * @var PDOStatement
     */
    protected $Stmt;
    /**
     * 最后的SQL語句
     * @var string
     */
    protected $Sql;
    /**
     * 配置信息 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
     * @var array
     */
    protected $Config;

    /**
     * 構造函數
     * @param array $config
     */
    public function __construct($config)
    {
        $this->Config = $config;
    }

    /**
     * 連接數據庫
     * @return void
     */
    public function connect()
    {
        $this->DB = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password'], $this->Config['option']);
        //默認把結果序列化成stdClass
        $this->DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
        //自己寫代碼捕獲Exception
        $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
    }

    /**
     * 斷開連接
     * @return void
     */
    public function disConnect()
    {
        $this->DB = null;
        $this->Stmt = null;
    }

    /**
     * 執行sql,返回新加入的id
     * @param string $statement
     * @return string
     */
    public function exec($statement)
    {
        if ($this->DB->exec($statement)) {
            $this->Sql = $statement;
            return $this->lastId();
        }
        $this->errorMessage();
    }

    /**
     * 查詢sql
     * @param string $statement
     * @return PdoDb
     */
    public function query($statement)
    {
        $res = $this->DB->query($statement);
        if ($res) {
            $this->Stmt = $res;
            $this->Sql = $statement;
            return $this;
        }
        $this->errorMessage();
    }

    /**
     * 序列化一次數據
     * @return mixed
     */
    public function fetchOne()
    {
        return $this->Stmt->fetch();
    }

    /**
     * 序列化所有數據
     * @return array
     */
    public function fetchAll()
    {
        return $this->Stmt->fetchAll();
    }

    /**
     * 最后添加的id
     * @return string
     */
    public function lastId()
    {
        return $this->DB->lastInsertId();
    }

    /**
     * 影響的行數
     * @return int
     */
    public function affectRows()
    {
        return $this->Stmt->rowCount();
    }

    /**
     * 預備語句
     * @param string $statement
     * @return PdoDb
     */
    public function prepare($statement)
    {
        $res = $this->DB->prepare($statement);
        if ($res) {
            $this->Stmt = $res;
            $this->Sql = $statement;
            return $this;
        }
        $this->errorMessage();
    }

    /**
     * 綁定數據
     * @param array $array
     * @return PdoDb
     */
    public function bindArray($array)
    {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                //array的有效結構 array('value'=>xxx,'type'=>PDO::PARAM_XXX)
                $this->Stmt->bindValue($k + 1, $v['value'], $v['type']);
            } else {
                $this->Stmt->bindValue($k + 1, $v, PDO::PARAM_STR);
            }
        }
        return $this;
    }

    /**
     * 執行預備語句
     * @return bool
     */
    public function execute()
    {
        if ($this->Stmt->execute()) {
            return true;
        }
        $this->errorMessage();
    }

    /**
     * 開啟事務
     * @return bool
     */
    public function beginTransaction()
    {
        return $this->DB->beginTransaction();
    }

    /**
     * 執行事務
     * @return bool
     */
    public function commitTransaction()
    {
        return $this->DB->commit();
    }

    /**
     * 回滾事務
     * @return bool
     */
    public function rollbackTransaction()
    {
        return $this->DB->rollBack();
    }

    /**
     * 拋出錯誤
     * @throws Error
     * @return void
     */
    public function errorMessage()
    {
        $msg = $this->DB->errorInfo();
        throw new Error('數據庫錯誤:' . $msg[2]);
    }

    //---------------------
    /**
     * 單例實例
     * @var PdoDb
     */
    protected static $_instance;

    /**
     * 默認數據庫
     * @static
     * @param array $config
     * @return PdoDb
     */
    public static function instance($config)
    {
        if (!self::$_instance instanceof PdoDb) {
            self::$_instance = new PdoDb($config);
            self::$_instance->connect();
        }
        return self::$_instance;
    }

    //----------------------

    /**
     * 獲取PDO支持的數據庫
     * @static
     * @return array
     */
    public static function getSupportDriver(){
        return PDO::getAvailableDrivers();
    }
    /**
     * 獲取數據庫的版本信息
     * @return array
     */
    public function getDriverVersion(){
        $name = $this->DB->getAttribute(PDO::ATTR_DRIVER_NAME);
        return array($name=>$this->DB->getAttribute(PDO::ATTR_CLIENT_VERSION));
    }

}

使用的時候

PdoDb::instance($config);
 本文由用戶 ouhp9343 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!