Driver.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Qii\Driver\Pdo;
  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. * @var string $charset 数据库默认编码
  51. */
  52. public $charset = 'UTF8';
  53. /**
  54. * 当前使用的db信息
  55. */
  56. public $useDB;
  57. /**
  58. * @var string $markKey debug信息保存用的key
  59. */
  60. public $markKey = '__model';
  61. /**
  62. * @var string $response Response对象
  63. */
  64. public $response;
  65. /**
  66. * 初始化
  67. * @param \Qii_Driver_ConnIntf $connection 数据库连接
  68. */
  69. public function __construct(\Qii\Driver\ConnIntf $connection)
  70. {
  71. parent::__construct();
  72. $this->connection = $connection;
  73. $this->sysConfigure = $this->connection->getDBInfo();
  74. $this->useDB = $this->sysConfigure['master']['db'];
  75. $this->response = new \Qii\Driver\Response();
  76. }
  77. /**
  78. * 用户直接输出这个实例化的类后会输出当前类的名称
  79. *
  80. * @return String
  81. */
  82. public function __toString()
  83. {
  84. return get_class($this);
  85. }
  86. /**
  87. * 查询预处理
  88. * @var string $sql 执行的sql语句
  89. */
  90. public function setQuery($sql)
  91. {
  92. $this->rs = $rs = $this->query($sql);
  93. return $rs;
  94. }
  95. /**
  96. * 查询
  97. * @var string $sql 执行的sql语句
  98. */
  99. public function query($sql)
  100. {
  101. /**
  102. * 如果调试SQL的话就启用时间的记录
  103. */
  104. if ($this->_debugSQL) {
  105. $startTime = microtime(true);
  106. }
  107. $this->sql = $sql;
  108. $this->db['CURRENT'] = $this->connection->getConnectionBySQL($sql);
  109. $this->db['CURRENT']->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  110. $this->db['CURRENT']->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  111. $this->db['CURRENT']->query('set names utf8');
  112. $rs = $this->db['CURRENT']->query($sql);
  113. $this->setError();
  114. if (!$rs) {
  115. $error = $this->getError('error');
  116. return \Qii::setError(false, __LINE__, 1509, $sql, $error[2] == '' ? 'NULL' : $error[2]);
  117. }
  118. $rs->setFetchMode(\PDO::FETCH_ASSOC);
  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. * 获取一行
  135. *
  136. * @param Resource $rs
  137. * @return Array
  138. */
  139. public function fetch($rs = null)
  140. {
  141. if (!$rs) return $this->rs->rech();
  142. return $rs->fetch();
  143. }
  144. /**
  145. * 执行SQL
  146. *
  147. * @param String $sql
  148. * @return Int
  149. */
  150. public function exec($sql)
  151. {
  152. $this->rs = $this->query($sql);
  153. return $this->affectedRows();
  154. }
  155. /**
  156. * 设置获取数据的类型
  157. *
  158. */
  159. public function setFetchMode()
  160. {
  161. $this->rs->setFetchMode(PDO::FETCH_ASSOC);
  162. }
  163. /**
  164. * 获取一行
  165. * @var string $sql 获取一行
  166. * @return array 返回数据
  167. */
  168. public function getRow($sql)
  169. {
  170. if (!$this->sysConfigure['driver'] == 'mssql' && !preg_match("/LIMIT(\s){1,}(\d){1,},(\s){0,}(\d){1,}/ui", $sql) && !preg_match("/LIMIT(\s){1,}(\d){1,}/ui", $sql)) {
  171. $sql = $sql . " LIMIT 1";
  172. } else if ($this->sysConfigure['driver'] == 'mssql' && !preg_match("/^SELECT(\s)TOP(\s)(\d){1,}/i", $sql)) {
  173. $sql = preg_replace("/^SELECT(\s)/i", "SELECT TOP 1 ", $sql);
  174. }
  175. $this->rs = $rs = $this->setQuery($sql);
  176. return $rs->fetch();
  177. }
  178. /**
  179. * 获取一列
  180. * @var string $sql 需要获取一列的sql语句
  181. * @return strin | bool 返回其中一列或者是false
  182. */
  183. public function getOne($sql)
  184. {
  185. $rs = $this->setQuery($sql);
  186. if ($rs) {
  187. return $rs->fetchColumn();
  188. }
  189. return false;
  190. }
  191. /**
  192. * 获取所有的行
  193. * @var string $sql 需要获取所有行的sql语句
  194. * @return array
  195. */
  196. public function getAll($sql)
  197. {
  198. $this->rs = $rs = $this->setQuery($sql);
  199. return $rs->fetchAll();
  200. }
  201. /**
  202. * 事务处理
  203. */
  204. public function transaction()
  205. {
  206. $this->db['CURRENT']->beginTransaction();
  207. }
  208. /**
  209. * 事务提交
  210. */
  211. public function commit()
  212. {
  213. $this->db['CURRENT']->commit();
  214. }
  215. /**
  216. * 事务回滚
  217. */
  218. public function rollback()
  219. {
  220. $this->db['CURRENT']->rollBack();
  221. }
  222. /**
  223. * 影响的行数
  224. *
  225. * @return Int
  226. */
  227. public function affectedRows()
  228. {
  229. if (!$this->rs) return false;
  230. return $this->rs->rowCount();
  231. }
  232. /**
  233. * 最后插入到数据库的自增长ID
  234. *
  235. * @return Int
  236. */
  237. public function lastInsertId()
  238. {
  239. return $this->db['CURRENT']->lastInsertId();
  240. }
  241. /**
  242. * 获取最后一次出错的信息
  243. *
  244. * @return Array
  245. */
  246. public function getError($key = '')
  247. {
  248. $errorInfo = array_pop($this->_errorInfo);
  249. if ($errorInfo) {
  250. //将错误加回来
  251. array_push($this->_errorInfo, $errorInfo);
  252. if (!empty($key)) {
  253. return $errorInfo[$key];
  254. }
  255. return $errorInfo;
  256. }
  257. return null;
  258. }
  259. /**
  260. * 是否有错,有错误的话存储错误
  261. *
  262. */
  263. public function setError()
  264. {
  265. if ($this->connection->getConnectionBySQL($this->sql)->errorCode() != '00000') {
  266. $this->_errorInfo[$this->queryTimes]['sql'] = $this->sql;
  267. $this->_errorInfo[$this->queryTimes]['error'] = $this->connection->getConnectionBySQL($this->sql)->errorInfo();
  268. $this->response = \Qii\Driver\Response::Fail('pdo.error', $this->_errorInfo);
  269. }
  270. }
  271. /**
  272. * 是否执行出错
  273. *
  274. * @return Bool
  275. */
  276. public function isError()
  277. {
  278. if (!$this->rs) {
  279. return true;
  280. }
  281. if ($this->connection->getConnectionBySQL($this->sql)->errorCode() != '00000') {
  282. return true;
  283. }
  284. return false;
  285. }
  286. /**
  287. * 返回response对象
  288. *
  289. * @return Bool
  290. */
  291. public function getResponse()
  292. {
  293. return $this->response;
  294. }
  295. }