PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/db/mysql.php

http://seo-phpbb.googlecode.com/
PHP | 479 lines | 313 code | 72 blank | 94 comment | 59 complexity | d1246720c85f411d471f92a4b928f153 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package dbal
  5. * @version $Id: mysql.php 8479 2008-03-29 00:22:48Z naderman $
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
  18. /**
  19. * MySQL4 Database Abstraction Layer
  20. * Compatible with:
  21. * MySQL 3.23+
  22. * MySQL 4.0+
  23. * MySQL 4.1+
  24. * MySQL 5.0+
  25. * @package dbal
  26. */
  27. class dbal_mysql extends dbal
  28. {
  29. var $mysql_version;
  30. var $multi_insert = true;
  31. /**
  32. * Connect to server
  33. * @access public
  34. */
  35. function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
  36. {
  37. $this->persistency = $persistency;
  38. $this->user = $sqluser;
  39. $this->server = $sqlserver . (($port) ? ':' . $port : '');
  40. $this->dbname = $database;
  41. $this->sql_layer = 'mysql4';
  42. $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword, $new_link) : @mysql_connect($this->server, $this->user, $sqlpassword, $new_link);
  43. if ($this->db_connect_id && $this->dbname != '')
  44. {
  45. if (@mysql_select_db($this->dbname, $this->db_connect_id))
  46. {
  47. // Determine what version we are using and if it natively supports UNICODE
  48. $this->mysql_version = mysql_get_server_info($this->db_connect_id);
  49. if (version_compare($this->mysql_version, '4.1.3', '>='))
  50. {
  51. @mysql_query("SET NAMES 'utf8'", $this->db_connect_id);
  52. // enforce strict mode on databases that support it
  53. if (version_compare($this->mysql_version, '5.0.2', '>='))
  54. {
  55. $result = @mysql_query('SELECT @@session.sql_mode AS sql_mode', $this->db_connect_id);
  56. $row = @mysql_fetch_assoc($result);
  57. @mysql_free_result($result);
  58. $modes = array_map('trim', explode(',', $row['sql_mode']));
  59. // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
  60. if (!in_array('TRADITIONAL', $modes))
  61. {
  62. if (!in_array('STRICT_ALL_TABLES', $modes))
  63. {
  64. $modes[] = 'STRICT_ALL_TABLES';
  65. }
  66. if (!in_array('STRICT_TRANS_TABLES', $modes))
  67. {
  68. $modes[] = 'STRICT_TRANS_TABLES';
  69. }
  70. }
  71. $mode = implode(',', $modes);
  72. @mysql_query("SET SESSION sql_mode='{$mode}'", $this->db_connect_id);
  73. }
  74. }
  75. else if (version_compare($this->mysql_version, '4.0.0', '<'))
  76. {
  77. $this->sql_layer = 'mysql';
  78. }
  79. return $this->db_connect_id;
  80. }
  81. }
  82. return $this->sql_error('');
  83. }
  84. /**
  85. * Version information about used database
  86. */
  87. function sql_server_info()
  88. {
  89. return 'MySQL ' . $this->mysql_version;
  90. }
  91. /**
  92. * SQL Transaction
  93. * @access private
  94. */
  95. function _sql_transaction($status = 'begin')
  96. {
  97. switch ($status)
  98. {
  99. case 'begin':
  100. return @mysql_query('BEGIN', $this->db_connect_id);
  101. break;
  102. case 'commit':
  103. return @mysql_query('COMMIT', $this->db_connect_id);
  104. break;
  105. case 'rollback':
  106. return @mysql_query('ROLLBACK', $this->db_connect_id);
  107. break;
  108. }
  109. return true;
  110. }
  111. /**
  112. * Base query method
  113. *
  114. * @param string $query Contains the SQL query which shall be executed
  115. * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
  116. * @return mixed When casted to bool the returned value returns true on success and false on failure
  117. *
  118. * @access public
  119. */
  120. function sql_query($query = '', $cache_ttl = 0)
  121. {
  122. if ($query != '')
  123. {
  124. global $cache;
  125. // EXPLAIN only in extra debug mode
  126. if (defined('DEBUG_EXTRA'))
  127. {
  128. $this->sql_report('start', $query);
  129. }
  130. $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
  131. $this->sql_add_num_queries($this->query_result);
  132. if ($this->query_result === false)
  133. {
  134. if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false)
  135. {
  136. $this->sql_error($query);
  137. }
  138. if (defined('DEBUG_EXTRA'))
  139. {
  140. $this->sql_report('stop', $query);
  141. }
  142. if ($cache_ttl && method_exists($cache, 'sql_save'))
  143. {
  144. $this->open_queries[(int) $this->query_result] = $this->query_result;
  145. $cache->sql_save($query, $this->query_result, $cache_ttl);
  146. }
  147. else if (strpos($query, 'SELECT') === 0 && $this->query_result)
  148. {
  149. $this->open_queries[(int) $this->query_result] = $this->query_result;
  150. }
  151. }
  152. else if (defined('DEBUG_EXTRA'))
  153. {
  154. $this->sql_report('fromcache', $query);
  155. }
  156. }
  157. else
  158. {
  159. return false;
  160. }
  161. return ($this->query_result) ? $this->query_result : false;
  162. }
  163. /**
  164. * Build LIMIT query
  165. */
  166. function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
  167. {
  168. $this->query_result = false;
  169. // if $total is set to 0 we do not want to limit the number of rows
  170. if ($total == 0)
  171. {
  172. // Having a value of -1 was always a bug
  173. $total = '18446744073709551615';
  174. }
  175. $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
  176. return $this->sql_query($query, $cache_ttl);
  177. }
  178. /**
  179. * Return number of affected rows
  180. */
  181. function sql_affectedrows()
  182. {
  183. return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false;
  184. }
  185. /**
  186. * Fetch current row
  187. */
  188. function sql_fetchrow($query_id = false)
  189. {
  190. global $cache;
  191. if ($query_id === false)
  192. {
  193. $query_id = $this->query_result;
  194. }
  195. if (isset($cache->sql_rowset[$query_id]))
  196. {
  197. return $cache->sql_fetchrow($query_id);
  198. }
  199. return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false;
  200. }
  201. /**
  202. * Seek to given row number
  203. * rownum is zero-based
  204. */
  205. function sql_rowseek($rownum, &$query_id)
  206. {
  207. global $cache;
  208. if ($query_id === false)
  209. {
  210. $query_id = $this->query_result;
  211. }
  212. if (isset($cache->sql_rowset[$query_id]))
  213. {
  214. return $cache->sql_rowseek($rownum, $query_id);
  215. }
  216. return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false;
  217. }
  218. /**
  219. * Get last inserted id after insert statement
  220. */
  221. function sql_nextid()
  222. {
  223. return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false;
  224. }
  225. /**
  226. * Free sql result
  227. */
  228. function sql_freeresult($query_id = false)
  229. {
  230. global $cache;
  231. if ($query_id === false)
  232. {
  233. $query_id = $this->query_result;
  234. }
  235. if (isset($cache->sql_rowset[$query_id]))
  236. {
  237. return $cache->sql_freeresult($query_id);
  238. }
  239. if (isset($this->open_queries[(int) $query_id]))
  240. {
  241. unset($this->open_queries[(int) $query_id]);
  242. return @mysql_free_result($query_id);
  243. }
  244. return false;
  245. }
  246. /**
  247. * Escape string used in sql query
  248. */
  249. function sql_escape($msg)
  250. {
  251. if (!$this->db_connect_id)
  252. {
  253. return @mysql_real_escape_string($msg);
  254. }
  255. return @mysql_real_escape_string($msg, $this->db_connect_id);
  256. }
  257. /**
  258. * Build LIKE expression
  259. * @access private
  260. */
  261. function _sql_like_expression($expression)
  262. {
  263. return $expression;
  264. }
  265. /**
  266. * Build db-specific query data
  267. * @access private
  268. */
  269. function _sql_custom_build($stage, $data)
  270. {
  271. switch ($stage)
  272. {
  273. case 'FROM':
  274. $data = '(' . $data . ')';
  275. break;
  276. }
  277. return $data;
  278. }
  279. /**
  280. * return sql error array
  281. * @access private
  282. */
  283. function _sql_error()
  284. {
  285. if (!$this->db_connect_id)
  286. {
  287. return array(
  288. 'message' => @mysql_error(),
  289. 'code' => @mysql_errno()
  290. );
  291. }
  292. return array(
  293. 'message' => @mysql_error($this->db_connect_id),
  294. 'code' => @mysql_errno($this->db_connect_id)
  295. );
  296. }
  297. /**
  298. * Close sql connection
  299. * @access private
  300. */
  301. function _sql_close()
  302. {
  303. return @mysql_close($this->db_connect_id);
  304. }
  305. /**
  306. * Build db-specific report
  307. * @access private
  308. */
  309. function _sql_report($mode, $query = '')
  310. {
  311. static $test_prof;
  312. // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
  313. if ($test_prof === null)
  314. {
  315. $test_prof = false;
  316. if (strpos($this->mysql_version, 'community') !== false)
  317. {
  318. $ver = substr($this->mysql_version, 0, strpos($this->mysql_version, '-'));
  319. if (version_compare($ver, '5.0.37', '>=') && version_compare($ver, '5.1', '<'))
  320. {
  321. $test_prof = true;
  322. }
  323. }
  324. }
  325. switch ($mode)
  326. {
  327. case 'start':
  328. $explain_query = $query;
  329. if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
  330. {
  331. $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
  332. }
  333. else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
  334. {
  335. $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
  336. }
  337. if (preg_match('/^SELECT/', $explain_query))
  338. {
  339. $html_table = false;
  340. // begin profiling
  341. if ($test_prof)
  342. {
  343. @mysql_query('SET profiling = 1;', $this->db_connect_id);
  344. }
  345. if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id))
  346. {
  347. while ($row = @mysql_fetch_assoc($result))
  348. {
  349. $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
  350. }
  351. }
  352. @mysql_free_result($result);
  353. if ($html_table)
  354. {
  355. $this->html_hold .= '</table>';
  356. }
  357. if ($test_prof)
  358. {
  359. $html_table = false;
  360. // get the last profile
  361. if ($result = @mysql_query('SHOW PROFILE ALL;', $this->db_connect_id))
  362. {
  363. $this->html_hold .= '<br />';
  364. while ($row = @mysql_fetch_assoc($result))
  365. {
  366. // make <unknown> HTML safe
  367. if (!empty($row['Source_function']))
  368. {
  369. $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
  370. }
  371. // remove unsupported features
  372. foreach ($row as $key => $val)
  373. {
  374. if ($val === null)
  375. {
  376. unset($row[$key]);
  377. }
  378. }
  379. $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
  380. }
  381. }
  382. @mysql_free_result($result);
  383. if ($html_table)
  384. {
  385. $this->html_hold .= '</table>';
  386. }
  387. @mysql_query('SET profiling = 0;', $this->db_connect_id);
  388. }
  389. }
  390. break;
  391. case 'fromcache':
  392. $endtime = explode(' ', microtime());
  393. $endtime = $endtime[0] + $endtime[1];
  394. $result = @mysql_query($query, $this->db_connect_id);
  395. while ($void = @mysql_fetch_assoc($result))
  396. {
  397. // Take the time spent on parsing rows into account
  398. }
  399. @mysql_free_result($result);
  400. $splittime = explode(' ', microtime());
  401. $splittime = $splittime[0] + $splittime[1];
  402. $this->sql_report('record_fromcache', $query, $endtime, $splittime);
  403. break;
  404. }
  405. }
  406. }
  407. ?>