Driver.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Qii\Driver\Mysqli;
  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. * @var array $useDB 当前数据库信息
  61. */
  62. public $useDB;
  63. /**
  64. * @var string $__markKey 用于保存数据库执行相关信息
  65. */
  66. private $__markKey = '__model';
  67. /**
  68. * @var string $_response Response对象
  69. */
  70. protected $_response;
  71. public function __construct(\Qii\Driver\ConnIntf $connection)
  72. {
  73. parent::__construct();
  74. $this->connection = $connection;
  75. $this->sysConfigure = $this->connection->getDBInfo();
  76. $this->useDB = $this->sysConfigure['master']['db'];
  77. $this->_response = new \Qii\Driver\Response();
  78. }
  79. /**
  80. * 用户直接输出这个实例化的类后会输出当前类的名称
  81. *
  82. * @return String
  83. */
  84. public function __toString()
  85. {
  86. return get_class($this);
  87. }
  88. /**
  89. * 查询预处理
  90. */
  91. public function setQuery($sql)
  92. {
  93. return $this->query($sql);
  94. }
  95. /**
  96. * 执行查询
  97. */
  98. public function query($sql)
  99. {
  100. /**
  101. * 如果调试SQL的话就启用时间的记录
  102. */
  103. if ($this->_debugSQL) {
  104. $startTime = microtime(true);
  105. $this->_exeSQL[] = $sql;
  106. \Qii::setPrivate($this->__markKey, array('_exeSQL' => $this->_exeSQL));
  107. }
  108. $this->sql = $sql;
  109. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($this->sql);
  110. if (!empty($this->sysConfigure['charset'])) {
  111. \mysqli_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
  112. } else {
  113. \mysqli_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
  114. }
  115. $this->rs = $rs = \mysqli_query($this->db['CURRENT'], $sql);
  116. $this->setError();
  117. if (!$rs) {
  118. $error = $this->getError('error');
  119. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  120. }
  121. /**
  122. * 如果调试SQL的话就启用时间的记录
  123. */
  124. if ($this->_debugSQL) {
  125. $endTime = microtime(true);
  126. $costTime = sprintf('%.4f', ($endTime - $startTime));
  127. $this->_querySeconds[$this->_queryTimes]['sql'] = $sql;
  128. $this->_querySeconds[$this->_queryTimes]['costTime'] = $costTime;
  129. $this->_querySeconds[$this->_queryTimes]['startTime'] = $startTime;
  130. $this->_querySeconds[$this->_queryTimes]['endTime'] = $endTime;
  131. \Qii::setPrivate($this->__markKey, array('_querySeconds' => $this->_querySeconds));
  132. }
  133. $this->_queryTimes++;
  134. \Qii::setPrivate($this->__markKey, array('_queryTimes' => $this->_queryTimes));
  135. return $rs;
  136. }
  137. /**
  138. * 执行SQL
  139. *
  140. * @param String $sql
  141. * @return Int
  142. */
  143. public function exec($sql)
  144. {
  145. $this->setQuery($sql);
  146. return $this->affectedRows();
  147. }
  148. public function getRow($sql)//获取一行
  149. {
  150. if (!preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/u", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/u", $sql)) {
  151. $sql = $sql . " LIMIT 1";
  152. }
  153. $rs = $this->query($sql);
  154. return \mysqli_fetch_assoc($rs);
  155. }
  156. public function getOne($sql)//获取一列
  157. {
  158. $data = $this->getRow($sql);
  159. return array_shift($data);
  160. }
  161. public function getAll($sql)//获取所有的行
  162. {
  163. $data = array();
  164. $rs = $this->query($sql);
  165. while ($row = \mysqli_fetch_assoc($rs)) {
  166. $data[] = $row;
  167. }
  168. return $data;
  169. }
  170. /**
  171. * 获取一行
  172. *
  173. * @param Resource $rs
  174. * @return Array
  175. */
  176. public function fetch($rs = null)
  177. {
  178. if (!$rs) return \mysqli_fetch_assoc($this->rs);
  179. return \mysqli_fetch_assoc($rs);
  180. }
  181. /**
  182. * 事务处理
  183. */
  184. public function transaction()
  185. {
  186. \mysqli_query('begin');
  187. }
  188. /**
  189. * 事务提交
  190. */
  191. public function commit()
  192. {
  193. \mysqli_query('commit');
  194. }
  195. /**
  196. * 事务回滚
  197. */
  198. public function rollback()
  199. {
  200. \mysqli_query('rollback');
  201. }
  202. /**
  203. * 返回影响的行数
  204. */
  205. public function affectedRows()
  206. {
  207. return \mysqli_affected_rows($this->db['CURRENT']);
  208. }
  209. /**
  210. * 返回自增长ID
  211. */
  212. public function lastInsertId()
  213. {
  214. return \mysqli_insert_id($this->db['CURRENT']);
  215. }
  216. /**
  217. * 获取最后一次出错的信息
  218. *
  219. * @return Array
  220. */
  221. public function getError($key = '')
  222. {
  223. $errorInfo = array_pop($this->_errorInfo);
  224. if ($errorInfo) {
  225. //将错误加回来
  226. array_push($this->_errorInfo, $errorInfo);
  227. if (!empty($key)) {
  228. return $errorInfo[$key];
  229. }
  230. return $errorInfo;
  231. }
  232. return false;
  233. }
  234. /**
  235. * 是否有错,有错误的话存储错误
  236. *
  237. */
  238. public function setError()//设置错误
  239. {
  240. if (\mysqli_errno($this->db['CURRENT'])) {
  241. $this->_errorInfo[$this->_queryTimes]['sql'] = $this->sql;
  242. $this->_errorInfo[$this->_queryTimes]['error'][2] = \mysqli_error($this->db['CURRENT']);
  243. $this->_response = \Qii\Driver\Response::Fail('pdo.error', $this->_errorInfo);
  244. \Qii::setPrivate($this->__markKey, array('_errorInfo' => $this->_errorInfo));
  245. }
  246. }
  247. /**
  248. * 是否执行出错
  249. *
  250. * @return Bool
  251. */
  252. public function isError()
  253. {
  254. if ($this->getError()) {
  255. return true;
  256. }
  257. return false;
  258. }
  259. /**
  260. * 返回response对象
  261. *
  262. * @return Bool
  263. */
  264. public function getResponse()
  265. {
  266. return $this->_response;
  267. }
  268. }