PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/nacridan/forum/include/dblayer/mysql.php

https://gitlab.com/nacridan/Nacridan
PHP | 372 lines | 254 code | 105 blank | 13 comment | 39 complexity | 4f7b5d1a8449afca67d07d84c6041f08 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. // Make sure we have built in support for MySQL
  8. if (!function_exists('mysql_connect'))
  9. exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
  10. class DBLayer
  11. {
  12. var $prefix;
  13. var $link_id;
  14. var $query_result;
  15. var $saved_queries = array();
  16. var $num_queries = 0;
  17. var $error_no = false;
  18. var $error_msg = 'Unknown';
  19. var $datatype_transformations = array(
  20. '%^SERIAL$%' => 'INT(10) UNSIGNED AUTO_INCREMENT'
  21. );
  22. function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
  23. {
  24. $this->prefix = $db_prefix;
  25. if ($p_connect)
  26. $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
  27. else
  28. $this->link_id = @mysql_connect($db_host, $db_username, $db_password);
  29. if ($this->link_id)
  30. {
  31. if (!@mysql_select_db($db_name, $this->link_id))
  32. error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
  33. }
  34. else
  35. error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
  36. // Setup the client-server character set (UTF-8)
  37. if (!defined('FORUM_NO_SET_NAMES'))
  38. $this->set_names('utf8');
  39. return $this->link_id;
  40. }
  41. function start_transaction()
  42. {
  43. return;
  44. }
  45. function end_transaction()
  46. {
  47. return;
  48. }
  49. function query($sql, $unbuffered = false)
  50. {
  51. if (defined('PUN_SHOW_QUERIES'))
  52. $q_start = get_microtime();
  53. if ($unbuffered)
  54. $this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
  55. else
  56. $this->query_result = @mysql_query($sql, $this->link_id);
  57. if ($this->query_result)
  58. {
  59. if (defined('PUN_SHOW_QUERIES'))
  60. $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
  61. ++$this->num_queries;
  62. return $this->query_result;
  63. }
  64. else
  65. {
  66. if (defined('PUN_SHOW_QUERIES'))
  67. $this->saved_queries[] = array($sql, 0);
  68. $this->error_no = @mysql_errno($this->link_id);
  69. $this->error_msg = @mysql_error($this->link_id);
  70. return false;
  71. }
  72. }
  73. function result($query_id = 0, $row = 0, $col = 0)
  74. {
  75. return ($query_id) ? @mysql_result($query_id, $row, $col) : false;
  76. }
  77. function fetch_assoc($query_id = 0)
  78. {
  79. return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
  80. }
  81. function fetch_row($query_id = 0)
  82. {
  83. return ($query_id) ? @mysql_fetch_row($query_id) : false;
  84. }
  85. function num_rows($query_id = 0)
  86. {
  87. return ($query_id) ? @mysql_num_rows($query_id) : false;
  88. }
  89. function affected_rows()
  90. {
  91. return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
  92. }
  93. function insert_id()
  94. {
  95. return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
  96. }
  97. function get_num_queries()
  98. {
  99. return $this->num_queries;
  100. }
  101. function get_saved_queries()
  102. {
  103. return $this->saved_queries;
  104. }
  105. function free_result($query_id = false)
  106. {
  107. return ($query_id) ? @mysql_free_result($query_id) : false;
  108. }
  109. function escape($str)
  110. {
  111. if (is_array($str))
  112. return '';
  113. else if (function_exists('mysql_real_escape_string'))
  114. return mysql_real_escape_string($str, $this->link_id);
  115. else
  116. return mysql_escape_string($str);
  117. }
  118. function error()
  119. {
  120. $result['error_sql'] = @current(@end($this->saved_queries));
  121. $result['error_no'] = $this->error_no;
  122. $result['error_msg'] = $this->error_msg;
  123. return $result;
  124. }
  125. function close()
  126. {
  127. if ($this->link_id)
  128. {
  129. if ($this->query_result)
  130. @mysql_free_result($this->query_result);
  131. return @mysql_close($this->link_id);
  132. }
  133. else
  134. return false;
  135. }
  136. function get_names()
  137. {
  138. $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
  139. return $this->result($result, 0, 1);
  140. }
  141. function set_names($names)
  142. {
  143. return $this->query('SET NAMES \''.$this->escape($names).'\'');
  144. }
  145. function get_version()
  146. {
  147. $result = $this->query('SELECT VERSION()');
  148. return array(
  149. 'name' => 'MySQL Standard',
  150. 'version' => preg_replace('%^([^-]+).*$%', '\\1', $this->result($result))
  151. );
  152. }
  153. function table_exists($table_name, $no_prefix = false)
  154. {
  155. $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
  156. return $this->num_rows($result) > 0;
  157. }
  158. function field_exists($table_name, $field_name, $no_prefix = false)
  159. {
  160. $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
  161. return $this->num_rows($result) > 0;
  162. }
  163. function index_exists($table_name, $index_name, $no_prefix = false)
  164. {
  165. $exists = false;
  166. $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
  167. while ($cur_index = $this->fetch_assoc($result))
  168. {
  169. if (strtolower($cur_index['Key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
  170. {
  171. $exists = true;
  172. break;
  173. }
  174. }
  175. return $exists;
  176. }
  177. function create_table($table_name, $schema, $no_prefix = false)
  178. {
  179. if ($this->table_exists($table_name, $no_prefix))
  180. return true;
  181. $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
  182. // Go through every schema element and add it to the query
  183. foreach ($schema['FIELDS'] as $field_name => $field_data)
  184. {
  185. $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
  186. $query .= $field_name.' '.$field_data['datatype'];
  187. if (isset($field_data['collation']))
  188. $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
  189. if (!$field_data['allow_null'])
  190. $query .= ' NOT NULL';
  191. if (isset($field_data['default']))
  192. $query .= ' DEFAULT '.$field_data['default'];
  193. $query .= ",\n";
  194. }
  195. // If we have a primary key, add it
  196. if (isset($schema['PRIMARY KEY']))
  197. $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
  198. // Add unique keys
  199. if (isset($schema['UNIQUE KEYS']))
  200. {
  201. foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
  202. $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
  203. }
  204. // Add indexes
  205. if (isset($schema['INDEXES']))
  206. {
  207. foreach ($schema['INDEXES'] as $index_name => $index_fields)
  208. $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
  209. }
  210. // We remove the last two characters (a newline and a comma) and add on the ending
  211. $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
  212. return $this->query($query) ? true : false;
  213. }
  214. function drop_table($table_name, $no_prefix = false)
  215. {
  216. if (!$this->table_exists($table_name, $no_prefix))
  217. return true;
  218. return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
  219. }
  220. function rename_table($old_table, $new_table, $no_prefix = false)
  221. {
  222. // If the new table exists and the old one doesn't, then we're happy
  223. if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
  224. return true;
  225. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
  226. }
  227. function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  228. {
  229. if ($this->field_exists($table_name, $field_name, $no_prefix))
  230. return true;
  231. $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
  232. if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
  233. $default_value = '\''.$this->escape($default_value).'\'';
  234. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
  235. }
  236. function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  237. {
  238. if (!$this->field_exists($table_name, $field_name, $no_prefix))
  239. return true;
  240. $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
  241. if (!is_null($default_value) && !is_int($default_value) && !is_float($default_value))
  242. $default_value = '\''.$this->escape($default_value).'\'';
  243. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? '' : ' NOT NULL').(!is_null($default_value) ? ' DEFAULT '.$default_value : '').(!is_null($after_field) ? ' AFTER '.$after_field : '')) ? true : false;
  244. }
  245. function drop_field($table_name, $field_name, $no_prefix = false)
  246. {
  247. if (!$this->field_exists($table_name, $field_name, $no_prefix))
  248. return true;
  249. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
  250. }
  251. function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
  252. {
  253. if ($this->index_exists($table_name, $index_name, $no_prefix))
  254. return true;
  255. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.($unique ? 'UNIQUE ' : '').'INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.' ('.implode(',', $index_fields).')') ? true : false;
  256. }
  257. function drop_index($table_name, $index_name, $no_prefix = false)
  258. {
  259. if (!$this->index_exists($table_name, $index_name, $no_prefix))
  260. return true;
  261. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
  262. }
  263. function truncate_table($table_name, $no_prefix = false)
  264. {
  265. return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
  266. }
  267. }