Driver.php 5.3 KB

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