Driver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. public $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. }
  106. $this->sql = $sql;
  107. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($this->sql);
  108. if (!empty($this->sysConfigure['charset'])) {
  109. \mysqli_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
  110. } else {
  111. \mysqli_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
  112. }
  113. $this->rs = $rs = \mysqli_query($this->db['CURRENT'], $sql);
  114. $this->setError();
  115. if (!$rs) {
  116. $error = $this->getError('error');
  117. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  118. }
  119. /**
  120. * 如果调试SQL的话就启用时间的记录
  121. */
  122. if ($this->_debugSQL) {
  123. $endTime = microtime(true);
  124. $costTime = sprintf('%.4f', ($endTime - $startTime));
  125. $this->querySeconds[$this->queryTimes]['sql'] = $sql;
  126. $this->querySeconds[$this->queryTimes]['costTime'] = $costTime;
  127. $this->querySeconds[$this->queryTimes]['startTime'] = $startTime;
  128. $this->querySeconds[$this->queryTimes]['endTime'] = $endTime;
  129. }
  130. $this->queryTimes++;
  131. return $rs;
  132. }
  133. /**
  134. * 执行SQL
  135. *
  136. * @param String $sql
  137. * @return Int
  138. */
  139. public function exec($sql)
  140. {
  141. $this->setQuery($sql);
  142. return $this->affectedRows();
  143. }
  144. public function getRow($sql)//获取一行
  145. {
  146. if (!preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/u", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/u", $sql)) {
  147. $sql = $sql . " LIMIT 1";
  148. }
  149. $rs = $this->query($sql);
  150. return \mysqli_fetch_assoc($rs);
  151. }
  152. public function getOne($sql)//获取一列
  153. {
  154. $data = $this->getRow($sql);
  155. return array_shift($data);
  156. }
  157. public function getAll($sql)//获取所有的行
  158. {
  159. $data = array();
  160. $rs = $this->query($sql);
  161. while ($row = \mysqli_fetch_assoc($rs)) {
  162. $data[] = $row;
  163. }
  164. return $data;
  165. }
  166. /**
  167. * 获取一行
  168. *
  169. * @param Resource $rs
  170. * @return Array
  171. */
  172. public function fetch($rs = null)
  173. {
  174. if (!$rs) return \mysqli_fetch_assoc($this->rs);
  175. return \mysqli_fetch_assoc($rs);
  176. }
  177. /**
  178. * 事务处理
  179. */
  180. public function transaction()
  181. {
  182. \mysqli_query('begin');
  183. }
  184. /**
  185. * 事务提交
  186. */
  187. public function commit()
  188. {
  189. \mysqli_query('commit');
  190. }
  191. /**
  192. * 事务回滚
  193. */
  194. public function rollback()
  195. {
  196. \mysqli_query('rollback');
  197. }
  198. /**
  199. * 返回影响的行数
  200. */
  201. public function affectedRows()
  202. {
  203. return \mysqli_affected_rows($this->db['CURRENT']);
  204. }
  205. /**
  206. * 返回自增长ID
  207. */
  208. public function lastInsertId()
  209. {
  210. return \mysqli_insert_id($this->db['CURRENT']);
  211. }
  212. /**
  213. * 获取最后一次出错的信息
  214. *
  215. * @return Array
  216. */
  217. public function getError($key = '')
  218. {
  219. $errorInfo = array_pop($this->_errorInfo);
  220. if ($errorInfo) {
  221. //将错误加回来
  222. array_push($this->_errorInfo, $errorInfo);
  223. if (!empty($key)) {
  224. return $errorInfo[$key];
  225. }
  226. return $errorInfo;
  227. }
  228. return false;
  229. }
  230. /**
  231. * 是否有错,有错误的话存储错误
  232. *
  233. */
  234. public function setError()//设置错误
  235. {
  236. if (\mysqli_errno($this->db['CURRENT'])) {
  237. $this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
  238. $this->_errorInfo[$this->queryTimes]['error'][2] = iconv('GB2312', 'UTF-8', \mysqli_error($this->db['CURRENT']));
  239. $this->response = \Qii\Driver\Response::Fail('mysqli.error', $this->_errorInfo);
  240. }
  241. }
  242. /**
  243. * 是否执行出错
  244. *
  245. * @return Bool
  246. */
  247. public function isError()
  248. {
  249. if ($this->getError()) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. }