Driver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Qii\Driver\Pdo;
  3. \Qii\Autoloader\Import::requires(dirname(dirname(__FILE__)) . DS . 'Response.php');
  4. class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
  5. {
  6. const VERSION = '1.2';
  7. private static $_instance;
  8. protected $connection;
  9. private $sysConfigure;
  10. private $rs;
  11. public $db;
  12. /**
  13. * 是否开启调试
  14. *
  15. * @var BOOL
  16. */
  17. public $_debugSQL = true;
  18. /**
  19. * 执行SQL的列表
  20. *
  21. * @var Array
  22. */
  23. public $_exeSQL = array();
  24. /**
  25. * 查询次数
  26. *
  27. * @var unknown_type
  28. */
  29. public $_queryTimes = 0;
  30. /**
  31. * 查询耗时
  32. *
  33. * @var INT
  34. */
  35. public $_querySeconds = array();
  36. /**
  37. * 最后一次执行的SQL
  38. *
  39. * @var unknown_type
  40. */
  41. private $sql;
  42. /**
  43. * 是否开启执行SQL的时间
  44. *
  45. * @var BOOL
  46. */
  47. public $_debugTime = false;
  48. public $_errorInfo = array();
  49. /**
  50. * 保存未定义变量
  51. *
  52. * @var Array
  53. */
  54. private $_undefined;
  55. /**
  56. * @var string $charset 数据库默认编码
  57. */
  58. public $charset = 'UTF8';
  59. /**
  60. * 当前使用的db信息
  61. */
  62. public $useDB;
  63. /**
  64. * @var string $__markKey debug信息保存用的key
  65. */
  66. private $__markKey = '__model';
  67. /**
  68. * @var string $_response Response对象
  69. */
  70. protected $_response;
  71. /**
  72. * 初始化
  73. * @param \Qii_Driver_ConnIntf $connection 数据库连接
  74. */
  75. public function __construct(\Qii\Driver\ConnIntf $connection)
  76. {
  77. parent::__construct();
  78. $this->connection = $connection;
  79. $this->sysConfigure = $this->connection->getDBInfo();
  80. $this->useDB = $this->sysConfigure['master']['db'];
  81. $this->_response = new \Qii\Driver\Response();
  82. }
  83. /**
  84. * 用户直接输出这个实例化的类后会输出当前类的名称
  85. *
  86. * @return String
  87. */
  88. public function __toString()
  89. {
  90. return get_class($this);
  91. }
  92. /**
  93. * 查询预处理
  94. * @var string $sql 执行的sql语句
  95. */
  96. public function setQuery($sql)
  97. {
  98. $this->rs = $rs = $this->query($sql);
  99. return $rs;
  100. }
  101. /**
  102. * 查询
  103. * @var string $sql 执行的sql语句
  104. */
  105. public function query($sql)
  106. {
  107. /**
  108. * 如果调试SQL的话就启用时间的记录
  109. */
  110. if ($this->_debugSQL) {
  111. $startTime = microtime(true);
  112. $this->_exeSQL[] = $sql;
  113. \Qii::setPrivate($this->__markKey, array('_exeSQL' => $this->_exeSQL));
  114. }
  115. $this->sql = $sql;
  116. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($sql);
  117. $this->db['CURRENT']->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  118. $this->db['CURRENT']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  119. $this->db['CURRENT']->query('set names utf8');
  120. $rs = $this->db['CURRENT']->query($sql);
  121. $this->setError();
  122. if (!$rs) {
  123. $error = $this->getError('error');
  124. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  125. }
  126. $rs->setFetchMode(\PDO::FETCH_ASSOC);
  127. /**
  128. * 如果调试SQL的话就启用时间的记录
  129. */
  130. if ($this->_debugSQL) {
  131. $endTime = microtime(true);
  132. $costTime = sprintf('%.4f', ($endTime - $startTime));
  133. $this->_querySeconds[$this->_queryTimes]['sql'] = $sql;
  134. $this->_querySeconds[$this->_queryTimes]['costTime'] = $costTime;
  135. $this->_querySeconds[$this->_queryTimes]['startTime'] = $startTime;
  136. $this->_querySeconds[$this->_queryTimes]['endTime'] = $endTime;
  137. \Qii::setPrivate($this->__markKey, array('_querySeconds' => $this->_querySeconds));
  138. }
  139. $this->_queryTimes++;
  140. \Qii::setPrivate($this->__markKey, array('_queryTimes' => $this->_queryTimes));
  141. return $rs;
  142. }
  143. /**
  144. * 获取一行
  145. *
  146. * @param Resource $rs
  147. * @return Array
  148. */
  149. public function fetch($rs = null)
  150. {
  151. if (!$rs) return $this->rs->rech();
  152. return $rs->fetch();
  153. }
  154. /**
  155. * 执行SQL
  156. *
  157. * @param String $sql
  158. * @return Int
  159. */
  160. public function exec($sql)
  161. {
  162. $this->rs = $this->query($sql);
  163. return $this->affectedRows();
  164. }
  165. /**
  166. * 设置获取数据的类型
  167. *
  168. */
  169. public function setFetchMode()
  170. {
  171. $this->rs->setFetchMode(PDO::FETCH_ASSOC);
  172. }
  173. /**
  174. * 获取一行
  175. * @var string $sql 获取一行
  176. * @return array 返回数据
  177. */
  178. public function getRow($sql)
  179. {
  180. if (!$this->sysConfigure['driver'] == 'mssql' && !preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/ui", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/ui", $sql)) {
  181. $sql = $sql . " LIMIT 1";
  182. } else if ($this->sysConfigure['driver'] == 'mssql' && !preg_match("/^SELECT(\s)TOP(\s)(\d){1,}/i", $sql)) {
  183. $sql = preg_replace("/^SELECT(\s)/i", "SELECT TOP 1 ", $sql);
  184. }
  185. $this->rs = $rs = $this->setQuery($sql);
  186. return $rs->fetch();
  187. }
  188. /**
  189. * 获取一列
  190. * @var string $sql 需要获取一列的sql语句
  191. * @return strin | bool 返回其中一列或者是false
  192. */
  193. public function getOne($sql)
  194. {
  195. $rs = $this->setQuery($sql);
  196. if ($rs) {
  197. return $rs->fetchColumn();
  198. }
  199. return false;
  200. }
  201. /**
  202. * 获取所有的行
  203. * @var string $sql 需要获取所有行的sql语句
  204. * @return array
  205. */
  206. public function getAll($sql)
  207. {
  208. $this->rs = $rs = $this->setQuery($sql);
  209. return $rs->fetchAll();
  210. }
  211. /**
  212. * 事务处理
  213. */
  214. public function transaction()
  215. {
  216. $this->db['CURRENT']->beginTransaction();
  217. }
  218. /**
  219. * 事务提交
  220. */
  221. public function commit()
  222. {
  223. $this->db['CURRENT']->commit();
  224. }
  225. /**
  226. * 事务回滚
  227. */
  228. public function rollback()
  229. {
  230. $this->db['CURRENT']->rollBack();
  231. }
  232. /**
  233. * 影响的行数
  234. *
  235. * @return Int
  236. */
  237. public function affectedRows()
  238. {
  239. if (!$this->rs) return false;
  240. return $this->rs->rowCount();
  241. }
  242. /**
  243. * 最后插入到数据库的自增长ID
  244. *
  245. * @return Int
  246. */
  247. public function lastInsertId()
  248. {
  249. return $this->db['CURRENT']->lastInsertId();
  250. }
  251. /**
  252. * 获取最后一次出错的信息
  253. *
  254. * @return Array
  255. */
  256. public function getError($key = '')
  257. {
  258. $errorInfo = array_pop($this->_errorInfo);
  259. if ($errorInfo) {
  260. //将错误加回来
  261. array_push($this->_errorInfo, $errorInfo);
  262. if (!empty($key)) {
  263. return $errorInfo[$key];
  264. }
  265. return $errorInfo;
  266. }
  267. return null;
  268. }
  269. /**
  270. * 是否有错,有错误的话存储错误
  271. *
  272. */
  273. public function setError()
  274. {
  275. if ($this->connection->getConnectionBySQL($this->sql)->errorCode() != '00000') {
  276. $this->_errorInfo[$this->_queryTimes]['sql'] = $this->sql;
  277. $this->_errorInfo[$this->_queryTimes]['error'] = $this->connection->getConnectionBySQL($this->sql)->errorInfo();
  278. $this->_response = \Qii\Driver\Response::Fail('pdo.error', $this->_errorInfo);
  279. \Qii::setPrivate($this->__markKey, array('_errorInfo' => $this->_errorInfo));
  280. }
  281. }
  282. /**
  283. * 是否执行出错
  284. *
  285. * @return Bool
  286. */
  287. public function isError()
  288. {
  289. $errorInfo = $this->rs->errorInfo();
  290. if ($this->connection->getConnectionBySQL($this->sql)->errorCode() != '00000') {
  291. return true;
  292. }
  293. return false;
  294. }
  295. }