Driver.php 6.4 KB

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