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));
     }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值