PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/dblayer/mysqli.php

https://github.com/Dratone/EveBB
PHP | 448 lines | 303 code | 123 blank | 22 comment | 51 complexity | 52f77ecaf0a0cd5d23fc1e415093d00d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2010 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('mysqli_connect'))
  9. exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) 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. // Was a custom port supplied with $db_host?
  26. if (strpos($db_host, ':') !== false)
  27. list($db_host, $db_port) = explode(':', $db_host);
  28. // Persistent connection in MySQLi are only available in PHP 5.3 and later releases
  29. $p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : '';
  30. if (isset($db_port))
  31. $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name, $db_port);
  32. else
  33. $this->link_id = @mysqli_connect($p_connect.$db_host, $db_username, $db_password, $db_name);
  34. if (!$this->link_id)
  35. error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_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. $this->query_result = @mysqli_query($this->link_id, $sql);
  54. if ($this->query_result)
  55. {
  56. if (defined('PUN_SHOW_QUERIES'))
  57. $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
  58. ++$this->num_queries;
  59. return $this->query_result;
  60. }
  61. else
  62. {
  63. if (defined('PUN_SHOW_QUERIES'))
  64. $this->saved_queries[] = array($sql, 0);
  65. $this->error_no = @mysqli_errno($this->link_id);
  66. $this->error_msg = @mysqli_error($this->link_id);
  67. return false;
  68. }
  69. }
  70. /**
  71. * This function mimics the functionality for the 'ON DUPLICATE KEY UPDATE' command in MySQL5.
  72. * Since this is mysqli, we will just reconstruct the normal query.
  73. * You are still expected to pass the correct $primaryKey to reference however.
  74. */
  75. function insert_or_update($fields, $primaryKey, $table, $fields_to_update = array()) {
  76. if (is_array($primaryKey)) {
  77. if (count($primaryKey) == 0) {
  78. $this->error_msg = 'No Primary keys passed.';
  79. return false;
  80. } //End if.
  81. foreach ($primaryKey as $key) {
  82. if (!isset($fields[$key])) {
  83. $this->error_msg = '[0] Primary key value missing.';
  84. return false;
  85. } //End if.
  86. } //End foreach().
  87. } else {
  88. if (!isset($fields[$primaryKey])) {
  89. $this->error_msg = '[1] Primary key value missing.';
  90. return false;
  91. } //End if.
  92. } //End if - else.
  93. $table_fields = $table."(";
  94. $insert_fields = "VALUES(";
  95. $update_fields = "";
  96. //Do a full update?
  97. if (empty($fields_to_update)) {
  98. foreach($fields as $key => $value) {
  99. $update_fields .= "`".$key."`=".(is_string($value) ? "'".$value."'": $value).",";
  100. $insert_fields .= (is_string($value) ? "'".$value."'": $value).",";
  101. $table_fields .= '`'.$key.'`,';
  102. } //End foreach().
  103. } else {
  104. foreach($fields_to_update as $key => $value) {
  105. $update_fields .= "`".$key."`=".(is_string($value) ? "'".$value."'": $value).",";
  106. } //End foreach().
  107. foreach($fields as $key => $value) {
  108. $update_fields .= "`".$key."`=".(is_string($value) ? "'".$value."'": $value).",";
  109. $insert_fields .= (is_string($value) ? "'".$value."'": $value).",";
  110. $table_fields .= '`'.$key.'`,';
  111. } //End foreach().
  112. } //End if - else.
  113. //Lob off the last character, which should be the comma.
  114. $update_fields = substr($update_fields, 0, -1);
  115. $insert_fields = substr($insert_fields, 0, -1);
  116. $table_fields = substr($table_fields, 0, -1);
  117. $table_fields .= ")";
  118. $insert_fields .= ")";
  119. $sql = "INSERT INTO ".$table_fields." ".$insert_fields." ON DUPLICATE KEY UPDATE ".$update_fields.";";
  120. return $this->query($sql);
  121. } //End insert_or_update().
  122. function result($query_id = 0, $row = 0, $col = 0)
  123. {
  124. if ($query_id)
  125. {
  126. if ($row !== 0 && @mysqli_data_seek($query_id, $row) === false)
  127. return false;
  128. $cur_row = @mysqli_fetch_row($query_id);
  129. if ($cur_row === false)
  130. return false;
  131. return $cur_row[$col];
  132. }
  133. else
  134. return false;
  135. }
  136. function fetch_assoc($query_id = 0)
  137. {
  138. $result = ($query_id) ? @mysqli_fetch_assoc($query_id) : false;
  139. if (is_array($result)) {
  140. return array_change_key_case($result, CASE_LOWER);
  141. } //End if.
  142. return $result;
  143. }
  144. function fetch_row($query_id = 0)
  145. {
  146. return ($query_id) ? @mysqli_fetch_row($query_id) : false;
  147. }
  148. function num_rows($query_id = 0)
  149. {
  150. return ($query_id) ? @mysqli_num_rows($query_id) : false;
  151. }
  152. function affected_rows()
  153. {
  154. return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false;
  155. }
  156. function insert_id()
  157. {
  158. return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false;
  159. }
  160. function get_num_queries()
  161. {
  162. return $this->num_queries;
  163. }
  164. function get_saved_queries()
  165. {
  166. return $this->saved_queries;
  167. }
  168. function free_result($query_id = false)
  169. {
  170. return ($query_id) ? @mysqli_free_result($query_id) : false;
  171. }
  172. function escape($str)
  173. {
  174. return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str);
  175. }
  176. function error()
  177. {
  178. $result['error_sql'] = @current(@end($this->saved_queries));
  179. $result['error_no'] = $this->error_no;
  180. $result['error_msg'] = $this->error_msg;
  181. return $result;
  182. }
  183. function close()
  184. {
  185. if ($this->link_id)
  186. {
  187. if ($this->query_result)
  188. @mysqli_free_result($this->query_result);
  189. return @mysqli_close($this->link_id);
  190. }
  191. else
  192. return false;
  193. }
  194. function get_names()
  195. {
  196. $result = $this->query('SHOW VARIABLES LIKE \'character_set_connection\'');
  197. return $this->result($result, 0, 1);
  198. }
  199. function set_names($names)
  200. {
  201. return $this->query('SET NAMES \''.$this->escape($names).'\'');
  202. }
  203. function get_version()
  204. {
  205. $result = $this->query('SELECT VERSION()');
  206. return array(
  207. 'name' => 'MySQL Improved',
  208. 'version' => preg_replace('/^([^-]+).*$/', '\\1', $this->result($result))
  209. );
  210. }
  211. function table_exists($table_name, $no_prefix = false)
  212. {
  213. $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\'');
  214. return $this->num_rows($result) > 0;
  215. }
  216. function field_exists($table_name, $field_name, $no_prefix = false)
  217. {
  218. $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\'');
  219. return $this->num_rows($result) > 0;
  220. }
  221. function index_exists($table_name, $index_name, $no_prefix = false)
  222. {
  223. $exists = false;
  224. $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name);
  225. while ($cur_index = $this->fetch_assoc($result))
  226. {
  227. if (strtolower($cur_index['key_name']) == strtolower(($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name))
  228. {
  229. $exists = true;
  230. break;
  231. }
  232. }
  233. return $exists;
  234. }
  235. function rename_table($old_table, $new_table, $no_prefix = false)
  236. {
  237. // If there new table exists and the old one doesn't, then we're happy
  238. if ($this->table_exists($new_table, $no_prefix) && !$this->table_exists($old_table, $no_prefix))
  239. return true;
  240. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$old_table.' RENAME TO '.($no_prefix ? '' : $this->prefix).$new_table) ? true : false;
  241. }
  242. function create_table($table_name, $schema, $no_prefix = false)
  243. {
  244. if ($this->table_exists($table_name, $no_prefix))
  245. return true;
  246. $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n";
  247. // Go through every schema element and add it to the query
  248. foreach ($schema['FIELDS'] as $field_name => $field_data)
  249. {
  250. $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']);
  251. $query .= $field_name.' '.$field_data['datatype'];
  252. if (isset($field_data['collation']))
  253. $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
  254. if (!$field_data['allow_null'])
  255. $query .= ' NOT NULL';
  256. if (isset($field_data['default']))
  257. $query .= ' DEFAULT '.$field_data['default'];
  258. $query .= ",\n";
  259. }
  260. // If we have a primary key, add it
  261. if (isset($schema['PRIMARY KEY']))
  262. $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n";
  263. // Add unique keys
  264. if (isset($schema['UNIQUE KEYS']))
  265. {
  266. foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields)
  267. $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n";
  268. }
  269. // Add indexes
  270. if (isset($schema['INDEXES']))
  271. {
  272. foreach ($schema['INDEXES'] as $index_name => $index_fields)
  273. $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n";
  274. }
  275. // We remove the last two characters (a newline and a comma) and add on the ending
  276. $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
  277. return $this->query($query) ? true : false;
  278. }
  279. function drop_table($table_name, $no_prefix = false)
  280. {
  281. if (!$this->table_exists($table_name, $no_prefix))
  282. return true;
  283. return $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
  284. }
  285. function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  286. {
  287. if ($this->field_exists($table_name, $field_name, $no_prefix))
  288. return true;
  289. $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
  290. if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
  291. $default_value = '\''.$this->escape($default_value).'\'';
  292. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false;
  293. }
  294. function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false)
  295. {
  296. if (!$this->field_exists($table_name, $field_name, $no_prefix))
  297. return true;
  298. $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type);
  299. if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
  300. $default_value = '\''.$this->escape($default_value).'\'';
  301. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' MODIFY '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) ? true : false;
  302. }
  303. function drop_field($table_name, $field_name, $no_prefix = false)
  304. {
  305. if (!$this->field_exists($table_name, $field_name, $no_prefix))
  306. return true;
  307. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) ? true : false;
  308. }
  309. function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false)
  310. {
  311. if ($this->index_exists($table_name, $index_name, $no_prefix))
  312. return true;
  313. 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;
  314. }
  315. function drop_index($table_name, $index_name, $no_prefix = false)
  316. {
  317. if (!$this->index_exists($table_name, $index_name, $no_prefix))
  318. return true;
  319. return $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) ? true : false;
  320. }
  321. function truncate_table($table_name, $no_prefix = false)
  322. {
  323. return $this->query('TRUNCATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name) ? true : false;
  324. }
  325. }