PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/Db/Mysql.php

http://lxsphp.googlecode.com/
PHP | 311 lines | 145 code | 46 blank | 120 comment | 19 complexity | ac2fa33f7f86f936a8bf8051d93bd1d8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * [ActiveRecord] Mysql???
  4. *
  5. * @version $Id: Mysql.php 338 2012-04-16 09:12:33Z linsir123 $
  6. * @package Db
  7. */
  8. class DbMysql
  9. {
  10. /**
  11. * @var ??????
  12. */
  13. public $queries = array();
  14. /**
  15. * @var ????
  16. */
  17. public $numQuery = 0;
  18. /**
  19. * @var ?????????????ID
  20. */
  21. public $insertId = 0;
  22. /**
  23. * @var ??????????????
  24. */
  25. public $error = '';
  26. /**
  27. * @var ????????????
  28. */
  29. private $_result;
  30. /**
  31. * @var ??????
  32. */
  33. private $_debug;
  34. /**
  35. * @var ????
  36. */
  37. private $_dbh;
  38. /**
  39. * @var ?????
  40. */
  41. private $_dbname;
  42. /**
  43. * ????
  44. *
  45. * @param int $dsnNumber ???????,???"main.dsn"???
  46. */
  47. public function __construct($dsnNumber = 0)
  48. {
  49. $this->_debug = App::O('main.debug');
  50. ///
  51. list($host, $user, $pwd, $this->_dbname, $charset) = App::O('main.dsn.'.$dsnNumber);
  52. if ( ! $this->_dbh = @mysql_connect($host, $user, $pwd, true))
  53. throw new Exception("?????[Mysql]???({$host})?????({$user})?");
  54. if ( ! @mysql_select_db($this->_dbname, $this->_dbh))
  55. throw new Exception("???({$this->_dbname})????");
  56. ///
  57. if ($this->_supportCollation()) {
  58. if ( ! isset($charset))
  59. $charset = App::O('main.charset.db');
  60. ///
  61. if ( ! empty($charset))
  62. @mysql_query("SET NAMES '{$charset}'", $this->_dbh);
  63. }
  64. }
  65. /**
  66. * ????
  67. */
  68. public function __destruct()
  69. {
  70. @mysql_close($this->_dbh);
  71. }
  72. /**
  73. * ????,??
  74. *
  75. * @param string $query ?????SQL
  76. * @return mixed
  77. */
  78. public function query($query)
  79. {
  80. $returnVal = 0;
  81. $this->insertId = 0;
  82. $this->_result = array();
  83. if ($this->_debug)
  84. $this->_timerStart();
  85. $res = @mysql_query($query, $this->_dbh);
  86. ++$this->numQuery;
  87. if ($this->_debug)
  88. $this->queries[] = array($query, $this->_timerStop());
  89. if ($this->error = mysql_error($this->_dbh)) {
  90. $this->_bail($query);
  91. return false;
  92. }
  93. if (preg_match("/^\\s*(insert|delete|update|replace) /i", $query)) {
  94. $returnVal = mysql_affected_rows($this->_dbh);
  95. if (preg_match("/^\\s*(insert|replace) /i", $query))
  96. $this->insertId = mysql_insert_id($this->_dbh);
  97. } else {
  98. $numRows = 0;
  99. while ($row = @mysql_fetch_object($res)) {
  100. $this->_result[$numRows] = $row;
  101. $numRows++;
  102. }
  103. @mysql_free_result($res);
  104. $returnVal = $numRows;
  105. }
  106. return $returnVal;
  107. }
  108. /**
  109. * ??????
  110. *
  111. * @param string $query ??????
  112. * @return resource
  113. */
  114. public function operate($query)
  115. {
  116. return mysql_query($query, $this->_dbh);
  117. }
  118. /**
  119. * ?????
  120. *
  121. * @param string $query ??SQL
  122. * @param int $x ??????
  123. * @param int $y ??????
  124. * @return string
  125. */
  126. public function getVar($query, $x = 0, $y = 0)
  127. {
  128. $this->query($query);
  129. if ( ! isset($this->_result[$y]))
  130. return null;
  131. ///
  132. $row = array_values(get_object_vars($this->_result[$y]));
  133. return isset($row[$x]) ? $row[$x] : null;
  134. }
  135. /**
  136. * ??????
  137. *
  138. * @param string $query ??SQL
  139. * @param int $y ??????
  140. * @return array
  141. */
  142. public function getRow($query, $y = 0)
  143. {
  144. $this->query($query);
  145. if ( ! isset($this->_result[$y]))
  146. return null;
  147. ///
  148. return get_object_vars($this->_result[$y]);
  149. }
  150. /**
  151. * ???????
  152. *
  153. * @param string $query ??SQL
  154. * @param int $x ??????
  155. * @return array
  156. */
  157. public function getCol($query, $x = 0)
  158. {
  159. $array = array();
  160. $this->query($query);
  161. ///
  162. if ($result = $this->_result) {
  163. $i = 0;
  164. foreach($result as $row) {
  165. $row = array_values(get_object_vars($row));
  166. $array[$i++] = $row[$x];
  167. }
  168. }
  169. return $array;
  170. }
  171. /**
  172. * ?????????
  173. *
  174. * @param string $query ??SQL
  175. * @return array
  176. */
  177. public function getResults($query)
  178. {
  179. $array = array();
  180. $this->query($query);
  181. ///
  182. if ($result = $this->_result) {
  183. $i = 0;
  184. foreach($result as $row)
  185. $array[$i++] = get_object_vars($row);
  186. }
  187. return $array;
  188. }
  189. /**
  190. * ???????????
  191. *
  192. * @return array
  193. */
  194. public function getTables()
  195. {
  196. $array = array();
  197. if ($result = mysql_list_tables($this->_dbname, $this->_dbh)) {
  198. while ($row = mysql_fetch_row($result))
  199. array_push($array, $row[0]);
  200. mysql_free_result($result);
  201. }
  202. return $array;
  203. }
  204. /**
  205. * ?????????(?????????)
  206. *
  207. * @param string $table ????????
  208. * @return bool
  209. */
  210. public function existsTable($table)
  211. {
  212. $query = "SELECT 1 FROM `{$table}` LIMIT 0,1";
  213. $result = mysql_query($query, $this->_dbh);
  214. if (empty($result))
  215. return false;
  216. return true;
  217. }
  218. /**
  219. * ??Mysql????
  220. *
  221. * @return string
  222. */
  223. public function version()
  224. {
  225. return mysql_get_server_info($this->_dbh);
  226. }
  227. /// ??????? ///
  228. /**
  229. * ?????
  230. */
  231. private function _timerStart()
  232. {
  233. $mtime = explode(' ', microtime());
  234. $this->timeStart = $mtime[1] + $mtime[0];
  235. }
  236. /**
  237. * ?????,???????
  238. *
  239. * @return int
  240. */
  241. private function _timerStop()
  242. {
  243. $mtime = explode(' ', microtime());
  244. return $mtime[1] + $mtime[0] - $this->timeStart;
  245. }
  246. /**
  247. * ??Mysql???????,???4.1.0????
  248. *
  249. * @return bool
  250. */
  251. private function _supportCollation()
  252. {
  253. return (version_compare(mysql_get_server_info($this->_dbh), '4.1.0', '>='));
  254. }
  255. /**
  256. * ????????
  257. *
  258. * @param string $query ???SQL
  259. */
  260. private function _bail($query)
  261. {
  262. $content = $query.'('.mysql_error().' ['.mysql_errno().'])';
  263. App::log($content);
  264. }
  265. }