PdoDb 数据库类

<?php
002/**
003* @throws Error
004* PDO数据库
005*/
006 
007class PdoDb extends DatabaseAbstract
008{
009    /**
010     * PDO实例
011     * @var PDO
012     */
013    protected $DB;
014    /**
015     * PDO准备语句
016     * @var PDOStatement
017     */
018    protected $Stmt;
019    /**
020     * 最后的SQL语句
021     * @var string
022     */
023    protected $Sql;
024    /**
025     * 配置信息 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
026     * @var array
027     */
028    protected $Config;
029 
030    /**
031     * 构造函数
032     * @param array $config
033     */
034    public function __construct($config)
035    {
036        $this->Config = $config;
037    }
038 
039 
040    /**
041     * 连接数据库
042     * @return void
043     */
044    public function connect()
045    {
046        $this->DB = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password'], $this->Config['option']);
047        //默认把结果序列化成stdClass
048        $this->DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
049        //自己写代码捕获Exception
050        $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
051    }
052 
053    /**
054     * 断开连接
055     * @return void
056     */
057    public function disConnect()
058    {
059        $this->DB = null;
060        $this->Stmt = null;
061    }
062 
063    /**
064     * 执行sql,返回新加入的id
065     * @param string $statement
066     * @return string
067     */
068    public function exec($statement)
069    {
070        if ($this->DB->exec($statement)) {
071            $this->Sql = $statement;
072            return $this->lastId();
073        }
074        $this->errorMessage();
075    }
076 
077    /**
078     * 查询sql
079     * @param string $statement
080     * @return PdoDb
081     */
082    public function query($statement)
083    {
084        $res = $this->DB->query($statement);
085        if ($res) {
086            $this->Stmt = $res;
087            $this->Sql = $statement;
088            return $this;
089        }
090        $this->errorMessage();
091    }
092 
093    /**
094     * 序列化一次数据
095     * @return mixed
096     */
097    public function fetchOne()
098    {
099        return $this->Stmt->fetch();
100    }
101 
102    /**
103     * 序列化所有数据
104     * @return array
105     */
106    public function fetchAll()
107    {
108        return $this->Stmt->fetchAll();
109    }
110 
111    /**
112     * 最后添加的id
113     * @return string
114     */
115    public function lastId()
116    {
117        return $this->DB->lastInsertId();
118    }
119 
120    /**
121     * 影响的行数
122     * @return int
123     */
124    public function affectRows()
125    {
126        return $this->Stmt->rowCount();
127    }
128 
129    /**
130     * 预备语句
131     * @param string $statement
132     * @return PdoDb
133     */
134    public function prepare($statement)
135    {
136        $res = $this->DB->prepare($statement);
137        if ($res) {
138            $this->Stmt = $res;
139            $this->Sql = $statement;
140            return $this;
141        }
142        $this->errorMessage();
143    }
144 
145    /**
146     * 绑定数据
147     * @param array $array
148     * @return PdoDb
149     */
150    public function bindArray($array)
151    {
152        foreach ($array as $k => $v) {
153            if (is_array($v)) {
154                //array的有效结构 array('value'=>xxx,'type'=>PDO::PARAM_XXX)
155                $this->Stmt->bindValue($k + 1, $v['value'], $v['type']);
156            } else {
157                $this->Stmt->bindValue($k + 1, $v, PDO::PARAM_STR);
158            }
159        }
160        return $this;
161    }
162 
163    /**
164     * 执行预备语句
165     * @return bool
166     */
167    public function execute()
168    {
169        if ($this->Stmt->execute()) {
170            return true;
171        }
172        $this->errorMessage();
173    }
174 
175    /**
176     * 开启事务
177     * @return bool
178     */
179    public function beginTransaction()
180    {
181        return $this->DB->beginTransaction();
182    }
183 
184    /**
185     * 执行事务
186     * @return bool
187     */
188    public function commitTransaction()
189    {
190        return $this->DB->commit();
191    }
192 
193    /**
194     * 回滚事务
195     * @return bool
196     */
197    public function rollbackTransaction()
198    {
199        return $this->DB->rollBack();
200    }
201 
202    /**
203     * 抛出错误
204     * @throws Error
205     * @return void
206     */
207    public function errorMessage()
208    {
209        $msg = $this->DB->errorInfo();
210        throw new Error('数据库错误:' . $msg[2]);
211    }
212 
213    //---------------------
214    /**
215     * 单例实例
216     * @var PdoDb
217     */
218    protected static $_instance;
219 
220    /**
221     * 默认数据库
222     * @static
223     * @param array $config
224     * @return PdoDb
225     */
226    public static function instance($config)
227    {
228        if (!self::$_instance instanceof PdoDb) {
229            self::$_instance = new PdoDb($config);
230            self::$_instance->connect();
231        }
232        return self::$_instance;
233    }
234 
235    //----------------------
236 
237    /**
238     * 获取PDO支持的数据库
239     * @static
240     * @return array
241     */
242    public static function getSupportDriver(){
243        return PDO::getAvailableDrivers();
244    }
245    /**
246     * 获取数据库的版本信息
247     * @return array
248     */
249    public function getDriverVersion(){
250        $name = $this->DB->getAttribute(PDO::ATTR_DRIVER_NAME);
251        return array($name=>$this->DB->getAttribute(PDO::ATTR_CLIENT_VERSION));
252    }
253 
254}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值