Driver.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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. 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. $this->_exeSQL[] = $sql;
  100. \Qii::setPrivate('model', array('_exeSQL' => $this->_exeSQL));
  101. }
  102. $this->sql = $sql;
  103. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($this->sql);
  104. if (!empty($this->sysConfigure['charset'])) {
  105. \mysql_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
  106. } else {
  107. \mysql_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
  108. }
  109. $this->rs = $rs = \mysql_query($this->db['CURRENT'], $sql);
  110. $this->setError();
  111. if (!$rs) {
  112. $error = $this->getError('error');
  113. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  114. }
  115. /**
  116. * 如果调试SQL的话就启用时间的记录
  117. */
  118. if ($this->_debugSQL) {
  119. $endTime = microtime(true);
  120. $costTime = sprintf('%.4f', ($endTime - $startTime));
  121. $this->_querySeconds[$this->_queryTimes]['sql'] = $sql;
  122. $this->_querySeconds[$this->_queryTimes]['costTime'] = $costTime;
  123. $this->_querySeconds[$this->_queryTimes]['startTime'] = $startTime;
  124. $this->_querySeconds[$this->_queryTimes]['endTime'] = $endTime;
  125. \Qii::setPrivate('model', array('_querySeconds' => $this->_querySeconds));
  126. }
  127. $this->_queryTimes++;
  128. \Qii::setPrivate('model', array('_queryTimes' => $this->_queryTimes));
  129. return $rs;
  130. }
  131. /**
  132. * 执行SQL
  133. *
  134. * @param String $sql
  135. * @return Int
  136. */
  137. public function exec($sql)
  138. {
  139. $this->setQuery($sql);
  140. return $this->affectedRows();
  141. }
  142. public function getRow($sql)//获取一行
  143. {
  144. if (!preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/u", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/u", $sql)) {
  145. $sql = $sql . " LIMIT 1";
  146. }
  147. $rs = $this->query($sql);
  148. return \mysql_fetch_assoc($rs);
  149. }
  150. public function getOne($sql)//获取一列
  151. {
  152. $data = $this->getRow($sql);
  153. return array_shift($data);
  154. }
  155. public function getAll($sql)//获取所有的行
  156. {
  157. $data = array();
  158. $rs = $this->query($sql);
  159. while ($row = \mysql_fetch_assoc($rs)) {
  160. $data[] = $row;
  161. }
  162. return $data;
  163. }
  164. /**
  165. * 获取一行
  166. *
  167. * @param Resource $rs
  168. * @return Array
  169. */
  170. public function fetch($rs = null)
  171. {
  172. if (!$rs) return \mysql_fetch_assoc($this->rs);
  173. return \mysql_fetch_assoc($rs);
  174. }
  175. public function transaction()//事务处理
  176. {
  177. \mysql_query('begin');
  178. }
  179. public function commit()//事务提交
  180. {
  181. \mysql_query('commit');
  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] = \mysql_error($this->db['CURRENT']);
  222. $this->_response = \Qii\Driver\Response::Fail('pdo.error', $this->_errorInfo);
  223. \Qii::setPrivate('model', array('_errorInfo' => $this->_errorInfo));
  224. }
  225. }
  226. /**
  227. * 是否执行出错
  228. *
  229. * @return Bool
  230. */
  231. public function isError()
  232. {
  233. if ($this->getError()) {
  234. return true;
  235. }
  236. return false;
  237. }
  238. /**
  239. * 返回response对象
  240. *
  241. * @return Bool
  242. */
  243. public function getResponse()
  244. {
  245. return $this->_response;
  246. }
  247. }