Driver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Qii\Driver\Mysql;
  3. use Qii\Driver\Base;
  4. use Qii\Driver\ConnIntf;
  5. use Qii\Driver\Intf;
  6. use Qii\Driver\Response;
  7. class Driver extends Base implements Intf
  8. {
  9. const VERSION = '1.2';
  10. private static $_instance;
  11. protected $connection;
  12. private $sysConfigure;
  13. private $rs;
  14. public $db;
  15. /**
  16. * 是否开启调试
  17. *
  18. * @var BOOL
  19. */
  20. public $_debugSQL = false;
  21. /**
  22. * 执行SQL的列表
  23. *
  24. * @var Array
  25. */
  26. public $_exeSQL = array();
  27. /**
  28. * 查询次数
  29. *
  30. * @var int $queryTimes
  31. */
  32. public $queryTimes = 0;
  33. /**
  34. * 查询耗时
  35. *
  36. * @var array $querySeconds
  37. */
  38. public $querySeconds = array();
  39. /**
  40. * 最后一次执行的SQL
  41. *
  42. * @var unknown_type
  43. */
  44. private $sql;
  45. /**
  46. * 是否开启执行SQL的时间
  47. *
  48. * @var BOOL
  49. */
  50. public $_debugTime = false;
  51. public $_errorInfo = array();
  52. /**
  53. * 保存未定义变量
  54. *
  55. * @var Array
  56. */
  57. private $_undefined;
  58. /**
  59. * @var string $charset 数据库默认编码
  60. */
  61. public $charset = 'UTF8';
  62. /**
  63. * @var array $currentDB 当前数据库信息
  64. */
  65. public $currentDB;
  66. /**
  67. * @var string $markKey 用于保存数据库执行相关信息
  68. */
  69. public $markKey = '__model';
  70. /**
  71. * @var string $response Response对象
  72. */
  73. public $response;
  74. public function __construct(ConnIntf $connection)
  75. {
  76. parent::__construct();
  77. $this->connection = $connection;
  78. $this->sysConfigure = $this->connection->getDBInfo();
  79. $this->currentDB = $this->sysConfigure['master']['db'];
  80. $this->response = new Response();
  81. }
  82. /**
  83. * 用户直接输出这个实例化的类后会输出当前类的名称
  84. *
  85. * @return String
  86. */
  87. public function __toString()
  88. {
  89. return get_class($this);
  90. }
  91. public function setQuery($sql)//查询预处理
  92. {
  93. return $this->query($sql);
  94. }
  95. public function query($sql)//查询
  96. {
  97. /**
  98. * 如果调试SQL的话就启用时间的记录
  99. */
  100. if ($this->_debugSQL) {
  101. $startTime = microtime(true);
  102. }
  103. $this->sql = $sql;
  104. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($this->sql);
  105. if (!empty($this->sysConfigure['charset'])) {
  106. \mysql_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
  107. } else {
  108. \mysql_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
  109. }
  110. $this->rs = $rs = \mysql_query($this->db['CURRENT'], $sql);
  111. $this->setError();
  112. if (!$rs) {
  113. $error = $this->getError('error');
  114. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  115. }
  116. $this->queryTimes++;
  117. /**
  118. * 如果调试SQL的话就启用时间的记录
  119. */
  120. if ($this->_debugSQL) {
  121. $endTime = microtime(true);
  122. $costTime = sprintf('%.4f', ($endTime - $startTime));
  123. $this->querySeconds[$this->queryTimes]['sql'] = $sql;
  124. $this->querySeconds[$this->queryTimes]['costTime'] = $costTime;
  125. $this->querySeconds[$this->queryTimes]['startTime'] = $startTime;
  126. $this->querySeconds[$this->queryTimes]['endTime'] = $endTime;
  127. }
  128. return $rs;
  129. }
  130. /**
  131. * 执行SQL
  132. *
  133. * @param String $sql
  134. * @return Int
  135. */
  136. public function exec($sql)
  137. {
  138. $this->setQuery($sql);
  139. return $this->affectedRows();
  140. }
  141. public function getRow($sql)//获取一行
  142. {
  143. if (!preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/u", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/u", $sql)) {
  144. $sql = $sql . " LIMIT 1";
  145. }
  146. $rs = $this->query($sql);
  147. return \mysql_fetch_assoc($rs);
  148. }
  149. public function getOne($sql)//获取一列
  150. {
  151. $data = $this->getRow($sql);
  152. return array_shift($data);
  153. }
  154. public function getAll($sql)//获取所有的行
  155. {
  156. $data = array();
  157. $rs = $this->query($sql);
  158. while ($row = \mysql_fetch_assoc($rs)) {
  159. $data[] = $row;
  160. }
  161. return $data;
  162. }
  163. /**
  164. * 获取一行
  165. *
  166. * @param Resource $rs
  167. * @return Array
  168. */
  169. public function fetch($rs = null)
  170. {
  171. if (!$rs) return \mysql_fetch_assoc($this->rs);
  172. return \mysql_fetch_assoc($rs);
  173. }
  174. public function transaction()//事务处理
  175. {
  176. \mysql_query('begin');
  177. }
  178. public function commit()//事务提交
  179. {
  180. \mysql_query('commit');
  181. \mysql_query('end');
  182. }
  183. public function rollback()//事务回滚
  184. {
  185. \mysql_query('rollback');
  186. }
  187. public function affectedRows()//返回影响的行数
  188. {
  189. return \mysql_affected_rows($this->db['CURRENT']);
  190. }
  191. public function lastInsertId()//返回自增长ID
  192. {
  193. return \mysql_insert_id($this->db['CURRENT']);
  194. }
  195. /**
  196. * 获取最后一次出错的信息
  197. *
  198. * @return Array
  199. */
  200. public function getError($key = '')
  201. {
  202. $errorInfo = array_pop($this->_errorInfo);
  203. if ($errorInfo) {
  204. //将错误加回来
  205. array_push($this->_errorInfo, $errorInfo);
  206. if (!empty($key)) {
  207. return $errorInfo[$key];
  208. }
  209. return $errorInfo;
  210. }
  211. return false;
  212. }
  213. /**
  214. * 是否有错,有错误的话存储错误
  215. *
  216. */
  217. public function setError()//设置错误
  218. {
  219. if (\mysql_errno($this->db['CURRENT'])) {
  220. $this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
  221. $this->_errorInfo[$this->queryTimes]['error'][2] = $this->iconv(\mysql_error($this->db['CURRENT']));
  222. $this->response = Response::Fail('mysql.error', $this->_errorInfo);
  223. }
  224. }
  225. /**
  226. * 是否执行出错
  227. *
  228. * @return Bool
  229. */
  230. public function isError()
  231. {
  232. if ($this->getError()) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * 返回response对象
  239. *
  240. * @return Bool
  241. */
  242. public function getResponse()
  243. {
  244. return $this->response;
  245. }
  246. }