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

/forum/includes/db/mysql.php

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