| punbb-1.3.5/include/dblayer/mysqli_innodb.php |
punbb-1.3.6/include/dblayer/mysqli_innodb.php |
| | 1: <?php |
| | 2: /** |
| | 3: * A database layer class supporting transactions that relies on the MySQLi PHP extension. |
| | 4: * |
| | 5: * @copyright (C) 2008-2009 PunBB, partially based on code (C) 2008-2009 FluxBB.org |
| | 6: * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher |
| | 7: * @package PunBB |
| | 8: */ |
| | 9: |
| | 10: |
| | 11: // Make sure we have built in support for MySQL |
| | 12: if (!function_exists('mysqli_connect')) |
| | 13: 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.'); |
| | 14: |
| | 15: |
| | 16: class DBLayer |
| | 17: { |
| | 18: var $prefix; |
| | 19: var $link_id; |
| | 20: var $query_result; |
| | 21: |
| | 22: var $saved_queries = array(); |
| | 23: var $num_queries = 0; |
| | 24: var $in_transaction = 0; |
| | 25: |
| | 26: var $datatype_transformations = array( |
| | 27: '/^SERIAL$/' => 'INT(10) UNSIGNED AUTO_INCREMENT' |
| | 28: ); |
| | 29: |
| | 30: |
| | 31: function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $foo) |
| | 32: { |
| | 33: $this->prefix = $db_prefix; |
| | 34: |
| | 35: // Was a custom port supplied with $db_host? |
| | 36: if (strpos($db_host, ':') !== false) |
| | 37: list($db_host, $db_port) = explode(':', $db_host); |
| | 38: |
| | 39: if (isset($db_port)) |
| | 40: $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name, $db_port); |
| | 41: else |
| | 42: $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name); |
| | 43: |
| | 44: if (!$this->link_id) |
| | 45: error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__); |
| | 46: |
| | 47: // Setup the client-server character set (UTF-8) |
| | 48: if (!defined('FORUM_NO_SET_NAMES')) |
| | 49: $this->set_names('utf8'); |
| | 50: |
| | 51: return $this->link_id; |
| | 52: } |
| | 53: |
| | 54: |
| | 55: function start_transaction() |
| | 56: { |
| | 57: ++$this->in_transaction; |
| | 58: |
| | 59: mysqli_query($this->link_id, 'START TRANSACTION'); |
| | 60: return; |
| | 61: } |
| | 62: |
| | 63: |
| | 64: function end_transaction() |
| | 65: { |
| | 66: --$this->in_transaction; |
| | 67: |
| | 68: mysqli_query($this->link_id, 'COMMIT'); |
| | 69: return; |
| | 70: } |
| | 71: |
| | 72: |
| | 73: function query($sql, $unbuffered = false) |
| | 74: { |
| | 75: if (strlen($sql) > 140000) |
| | 76: exit('Insane query. Aborting.'); |
| | 77: |
| | 78: if (defined('FORUM_SHOW_QUERIES')) |
| | 79: $q_start = get_microtime(); |
| | 80: |
| | 81: $this->query_result = @mysqli_query($this->link_id, $sql); |
| | 82: |
| | 83: if ($this->query_result) |
| | 84: { |
| | 85: if (defined('FORUM_SHOW_QUERIES')) |
| | 86: $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
| | 87: |
| | 88: ++$this->num_queries; |
| | 89: |
| | 90: return $this->query_result; |
| | 91: } |
| | 92: else |
| | 93: { |
| | 94: if (defined('FORUM_SHOW_QUERIES')) |
| | 95: $this->saved_queries[] = array($sql, 0); |
| | 96: |
| | 97: // Rollback transaction |
| | 98: if ($this->in_transaction) |
| | 99: mysqli_query($this->link_id, 'ROLLBACK'); |
| | 100: |
| | 101: --$this->in_transaction; |
| | 102: |
| | 103: return false; |
| | 104: } |
| | 105: } |
| | 106: |
| | 107: |
| | 108: function result($query_id = 0, $row = 0, $col = 0) |
| | 109: { |
| | 110: if ($query_id) |
| | 111: { |
| | 112: if ($row) |
| | 113: @mysqli_data_seek($query_id, $row); |
| | 114: |
| | 115: $cur_row = @mysqli_fetch_row($query_id); |
| | 116: return $cur_row[$col]; |
| | 117: } |
| | 118: else |
| | 119: return false; |
| | 120: } |
| | 121: |
| | 122: |
| | 123: function fetch_assoc($query_id = 0) |
| | 124: { |
| | 125: return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; |
| | 126: } |
| | 127: |
| | 128: |
| | 129: function fetch_row($query_id = 0) |
| | 130: { |
| | 131: return ($query_id) ? @mysqli_fetch_row($query_id) : false; |
| | 132: } |
| | 133: |
| | 134: |
| | 135: function num_rows($query_id = 0) |
| | 136: { |
| | 137: return ($query_id) ? @mysqli_num_rows($query_id) : false; |
| | 138: } |
| | 139: |
| | 140: |
| | 141: function affected_rows() |
| | 142: { |
| | 143: return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false; |
| | 144: } |
| | 145: |
| | 146: |
| | 147: function insert_id() |
| | 148: { |
| | 149: return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false; |
| | 150: } |
| | 151: |
| | 152: |
| | 153: function get_num_queries() |
| | 154: { |
| | 155: return $this->num_queries; |
| | 156: } |
| | 157: |
| | 158: |
| | 159: function get_saved_queries() |
| | 160: { |
| | 161: return $this->saved_queries; |
| | 162: } |
| | 163: |
| | 164: |
| | 165: function free_result($query_id = false) |
| | 166: { |
| | 167: return ($query_id) ? @mysqli_free_result($query_id) : false; |
| | 168: } |
| | 169: |
| | 170: |
| | 171: function escape($str) |
| | 172: { |
| | 173: return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str); |
| | 174: } |
| | 175: |
| | 176: |
| | 177: function error() |
| | 178: { |
| | 179: $result['error_sql'] = @current(@end($this->saved_queries)); |
| | 180: $result['error_no'] = @mysqli_errno($this->link_id); |
| | 181: $result['error_msg'] = @mysqli_error($this->link_id); |
| | 182: |
| | 183: return $result; |
| | 184: } |
| | 185: |
| | 186: |
| | 187: function close() |
| | 188: { |
| | 189: if ($this->link_id) |
| | 190: { |
| | 191: if ($this->query_result) |
| | 192: @mysqli_free_result($this->query_result); |
| | 193: |
| | 194: return @mysqli_close($this->link_id); |
| | 195: } |
| | 196: else |
| | 197: return false; |
| | 198: } |
| | 199: |
| | 200: |
| | 201: function set_names($names) |
| | 202: { |
| | 203: return $this->query('SET NAMES \''.$this->escape($names).'\''); |
| | 204: } |
| | 205: |
| | 206: |
| | 207: function get_version() |
| | 208: { |
| | 209: $result = $this->query('SELECT VERSION()'); |
| | 210: |
| | 211: return array( |
| | 212: 'name' => 'MySQL Improved (InnoDB)', |
| | 213: 'version' => preg_replace('/^([^-]+).*$/', '\\1', $this->result($result)) |
| | 214: ); |
| | 215: } |
| | 216: |
| | 217: |
| | 218: function table_exists($table_name, $no_prefix = false) |
| | 219: { |
| | 220: $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); |
| | 221: return $this->num_rows($result) > 0; |
| | 222: } |
| | 223: |
| | 224: |
| | 225: function field_exists($table_name, $field_name, $no_prefix = false) |
| | 226: { |
| | 227: $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\''); |
| | 228: return $this->num_rows($result) > 0; |
| | 229: } |
| | 230: |
| | 231: |
| | 232: function index_exists($table_name, $index_name, $no_prefix = false) |
| | 233: { |
| | 234: $exists = false; |
| | 235: |
| | 236: $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name); |
| | 237: while ($cur_index = $this->fetch_assoc($result)) |
| | 238: { |
| | 239: if ($cur_index['Key_name'] == ($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) |
| | 240: { |
| | 241: $exists = true; |
| | 242: break; |
| | 243: } |
| | 244: } |
| | 245: |
| | 246: return $exists; |
| | 247: } |
| | 248: |
| | 249: |
| | 250: function create_table($table_name, $schema, $no_prefix = false) |
| | 251: { |
| | 252: if ($this->table_exists($table_name, $no_prefix)) |
| | 253: return; |
| | 254: |
| | 255: $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; |
| | 256: |
| | 257: // Go through every schema element and add it to the query |
| | 258: foreach ($schema['FIELDS'] as $field_name => $field_data) |
| | 259: { |
| | 260: $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); |
| | 261: |
| | 262: $query .= $field_name.' '.$field_data['datatype']; |
| | 263: |
| | 264: if (isset($field_data['collation'])) |
| | 265: $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation']; |
| | 266: |
| | 267: if (!$field_data['allow_null']) |
| | 268: $query .= ' NOT NULL'; |
| | 269: |
| | 270: if (isset($field_data['default'])) |
| | 271: $query .= ' DEFAULT '.$field_data['default']; |
| | 272: |
| | 273: $query .= ",\n"; |
| | 274: } |
| | 275: |
| | 276: // If we have a primary key, add it |
| | 277: if (isset($schema['PRIMARY KEY'])) |
| | 278: $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; |
| | 279: |
| | 280: // Add unique keys |
| | 281: if (isset($schema['UNIQUE KEYS'])) |
| | 282: { |
| | 283: foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) |
| | 284: $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n"; |
| | 285: } |
| | 286: |
| | 287: // Add indexes |
| | 288: if (isset($schema['INDEXES'])) |
| | 289: { |
| | 290: foreach ($schema['INDEXES'] as $index_name => $index_fields) |
| | 291: $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n"; |
| | 292: } |
| | 293: |
| | 294: // We remove the last two characters (a newline and a comma) and add on the ending |
| | 295: $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8'; |
| | 296: |
| | 297: $this->query($query) or error(__FILE__, __LINE__); |
| | 298: } |
| | 299: |
| | 300: |
| | 301: function drop_table($table_name, $no_prefix = false) |
| | 302: { |
| | 303: if (!$this->table_exists($table_name, $no_prefix)) |
| | 304: return; |
| | 305: |
| | 306: $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) or error(__FILE__, __LINE__); |
| | 307: } |
| | 308: |
| | 309: |
| | 310: function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
| | 311: { |
| | 312: if ($this->field_exists($table_name, $field_name, $no_prefix)) |
| | 313: return; |
| | 314: |
| | 315: $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
| | 316: |
| | 317: if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
| | 318: $default_value = '\''.$this->escape($default_value).'\''; |
| | 319: |
| | 320: $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 : '')) or error(__FILE__, __LINE__); |
| | 321: } |
| | 322: |
| | 323: |
| | 324: function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
| | 325: { |
| | 326: if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
| | 327: return; |
| | 328: |
| | 329: $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
| | 330: |
| | 331: if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
| | 332: $default_value = '\''.$this->escape($default_value).'\''; |
| | 333: |
| | 334: $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 : '')) or error(__FILE__, __LINE__); |
| | 335: } |
| | 336: |
| | 337: |
| | 338: function drop_field($table_name, $field_name, $no_prefix = false) |
| | 339: { |
| | 340: if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
| | 341: return; |
| | 342: |
| | 343: $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__); |
| | 344: } |
| | 345: |
| | 346: |
| | 347: function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) |
| | 348: { |
| | 349: if ($this->index_exists($table_name, $index_name, $no_prefix)) |
| | 350: return; |
| | 351: |
| | 352: $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).')') or error(__FILE__, __LINE__); |
| | 353: } |
| | 354: |
| | 355: |
| | 356: function drop_index($table_name, $index_name, $no_prefix = false) |
| | 357: { |
| | 358: if (!$this->index_exists($table_name, $index_name, $no_prefix)) |
| | 359: return; |
| | 360: |
| | 361: $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) or error(__FILE__, __LINE__); |
| | 362: } |
| | 363: } |
| punbb-1.3.5/include/dblayer/mysql_innodb.php |
punbb-1.3.6/include/dblayer/mysql_innodb.php |
| | 1: <?php |
| | 2: /** |
| | 3: * A database layer class supporting transactions that relies on the MySQL PHP extension. |
| | 4: * |
| | 5: * @copyright (C) 2008-2009 PunBB, partially based on code (C) 2008-2009 FluxBB.org |
| | 6: * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher |
| | 7: * @package PunBB |
| | 8: */ |
| | 9: |
| | 10: |
| | 11: // Make sure we have built in support for MySQL |
| | 12: if (!function_exists('mysql_connect')) |
| | 13: 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.'); |
| | 14: |
| | 15: |
| | 16: class DBLayer |
| | 17: { |
| | 18: var $prefix; |
| | 19: var $link_id; |
| | 20: var $query_result; |
| | 21: var $in_transaction = 0; |
| | 22: |
| | 23: var $saved_queries = array(); |
| | 24: var $num_queries = 0; |
| | 25: |
| | 26: var $datatype_transformations = array( |
| | 27: '/^SERIAL$/' => 'INT(10) UNSIGNED AUTO_INCREMENT' |
| | 28: ); |
| | 29: |
| | 30: |
| | 31: function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
| | 32: { |
| | 33: $this->prefix = $db_prefix; |
| | 34: |
| | 35: if ($p_connect) |
| | 36: $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); |
| | 37: else |
| | 38: $this->link_id = @mysql_connect($db_host, $db_username, $db_password); |
| | 39: |
| | 40: if ($this->link_id) |
| | 41: { |
| | 42: if (!@mysql_select_db($db_name, $this->link_id)) |
| | 43: error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
| | 44: } |
| | 45: else |
| | 46: error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
| | 47: |
| | 48: // Setup the client-server character set (UTF-8) |
| | 49: if (!defined('FORUM_NO_SET_NAMES')) |
| | 50: $this->set_names('utf8'); |
| | 51: |
| | 52: return $this->link_id; |
| | 53: } |
| | 54: |
| | 55: |
| | 56: function start_transaction() |
| | 57: { |
| | 58: ++$this->in_transaction; |
| | 59: |
| | 60: mysql_query('START TRANSACTION', $this->link_id); |
| | 61: return; |
| | 62: } |
| | 63: |
| | 64: |
| | 65: function end_transaction() |
| | 66: { |
| | 67: --$this->in_transaction; |
| | 68: |
| | 69: mysql_query('COMMIT', $this->link_id); |
| | 70: return; |
| | 71: } |
| | 72: |
| | 73: |
| | 74: function query($sql, $unbuffered = false) |
| | 75: { |
| | 76: if (strlen($sql) > 140000) |
| | 77: exit('Insane query. Aborting.'); |
| | 78: |
| | 79: if (defined('FORUM_SHOW_QUERIES')) |
| | 80: $q_start = get_microtime(); |
| | 81: |
| | 82: if ($unbuffered) |
| | 83: $this->query_result = @mysql_unbuffered_query($sql, $this->link_id); |
| | 84: else |
| | 85: $this->query_result = @mysql_query($sql, $this->link_id); |
| | 86: |
| | 87: if ($this->query_result) |
| | 88: { |
| | 89: if (defined('FORUM_SHOW_QUERIES')) |
| | 90: $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
| | 91: |
| | 92: ++$this->num_queries; |
| | 93: |
| | 94: return $this->query_result; |
| | 95: } |
| | 96: else |
| | 97: { |
| | 98: if (defined('FORUM_SHOW_QUERIES')) |
| | 99: $this->saved_queries[] = array($sql, 0); |
| | 100: |
| | 101: // Rollback transaction |
| | 102: if ($this->in_transaction) |
| | 103: mysql_query('ROLLBACK', $this->link_id); |
| | 104: |
| | 105: --$this->in_transaction; |
| | 106: |
| | 107: return false; |
| | 108: } |
| | 109: } |
| | 110: |
| | 111: |
| | 112: function result($query_id = 0, $row = 0, $col = 0) |
| | 113: { |
| | 114: return ($query_id) ? @mysql_result($query_id, $row, $col) : false; |
| | 115: } |
| | 116: |
| | 117: |
| | 118: function fetch_assoc($query_id = 0) |
| | 119: { |
| | 120: return ($query_id) ? @mysql_fetch_assoc($query_id) : false; |
| | 121: } |
| | 122: |
| | 123: |
| | 124: function fetch_row($query_id = 0) |
| | 125: { |
| | 126: return ($query_id) ? @mysql_fetch_row($query_id) : false; |
| | 127: } |
| | 128: |
| | 129: |
| | 130: function num_rows($query_id = 0) |
| | 131: { |
| | 132: return ($query_id) ? @mysql_num_rows($query_id) : false; |
| | 133: } |
| | 134: |
| | 135: |
| | 136: function affected_rows() |
| | 137: { |
| | 138: return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false; |
| | 139: } |
| | 140: |
| | 141: |
| | 142: function insert_id() |
| | 143: { |
| | 144: return ($this->link_id) ? @mysql_insert_id($this->link_id) : false; |
| | 145: } |
| | 146: |
| | 147: |
| | 148: function get_num_queries() |
| | 149: { |
| | 150: return $this->num_queries; |
| | 151: } |
| | 152: |
| | 153: |
| | 154: function get_saved_queries() |
| | 155: { |
| | 156: return $this->saved_queries; |
| | 157: } |
| | 158: |
| | 159: |
| | 160: function free_result($query_id = false) |
| | 161: { |
| | 162: return ($query_id) ? @mysql_free_result($query_id) : false; |
| | 163: } |
| | 164: |
| | 165: |
| | 166: function escape($str) |
| | 167: { |
| | 168: if (is_array($str)) |
| | 169: return ''; |
| | 170: else if (function_exists('mysql_real_escape_string')) |
| | 171: return mysql_real_escape_string($str, $this->link_id); |
| | 172: else |
| | 173: return mysql_escape_string($str); |
| | 174: } |
| | 175: |
| | 176: |
| | 177: function error() |
| | 178: { |
| | 179: $result['error_sql'] = @current(@end($this->saved_queries)); |
| | 180: $result['error_no'] = @mysql_errno($this->link_id); |
| | 181: $result['error_msg'] = @mysql_error($this->link_id); |
| | 182: |
| | 183: return $result; |
| | 184: } |
| | 185: |
| | 186: |
| | 187: function close() |
| | 188: { |
| | 189: if ($this->link_id) |
| | 190: { |
| | 191: if ($this->query_result) |
| | 192: @mysql_free_result($this->query_result); |
| | 193: |
| | 194: return @mysql_close($this->link_id); |
| | 195: } |
| | 196: else |
| | 197: return false; |
| | 198: } |
| | 199: |
| | 200: |
| | 201: function set_names($names) |
| | 202: { |
| | 203: return $this->query('SET NAMES \''.$this->escape($names).'\''); |
| | 204: } |
| | 205: |
| | 206: |
| | 207: function get_version() |
| | 208: { |
| | 209: $result = $this->query('SELECT VERSION()'); |
| | 210: |
| | 211: return array( |
| | 212: 'name' => 'MySQL Standard (InnoDB)', |
| | 213: 'version' => preg_replace('/^([^-]+).*$/', '\\1', $this->result($result)) |
| | 214: ); |
| | 215: } |
| | 216: |
| | 217: |
| | 218: function table_exists($table_name, $no_prefix = false) |
| | 219: { |
| | 220: $result = $this->query('SHOW TABLES LIKE \''.($no_prefix ? '' : $this->prefix).$this->escape($table_name).'\''); |
| | 221: return $this->num_rows($result) > 0; |
| | 222: } |
| | 223: |
| | 224: |
| | 225: function field_exists($table_name, $field_name, $no_prefix = false) |
| | 226: { |
| | 227: $result = $this->query('SHOW COLUMNS FROM '.($no_prefix ? '' : $this->prefix).$table_name.' LIKE \''.$this->escape($field_name).'\''); |
| | 228: return $this->num_rows($result) > 0; |
| | 229: } |
| | 230: |
| | 231: |
| | 232: function index_exists($table_name, $index_name, $no_prefix = false) |
| | 233: { |
| | 234: $exists = false; |
| | 235: |
| | 236: $result = $this->query('SHOW INDEX FROM '.($no_prefix ? '' : $this->prefix).$table_name); |
| | 237: while ($cur_index = $this->fetch_assoc($result)) |
| | 238: { |
| | 239: if ($cur_index['Key_name'] == ($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) |
| | 240: { |
| | 241: $exists = true; |
| | 242: break; |
| | 243: } |
| | 244: } |
| | 245: |
| | 246: return $exists; |
| | 247: } |
| | 248: |
| | 249: |
| | 250: function create_table($table_name, $schema, $no_prefix = false) |
| | 251: { |
| | 252: if ($this->table_exists($table_name, $no_prefix)) |
| | 253: return; |
| | 254: |
| | 255: $query = 'CREATE TABLE '.($no_prefix ? '' : $this->prefix).$table_name." (\n"; |
| | 256: |
| | 257: // Go through every schema element and add it to the query |
| | 258: foreach ($schema['FIELDS'] as $field_name => $field_data) |
| | 259: { |
| | 260: $field_data['datatype'] = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_data['datatype']); |
| | 261: |
| | 262: $query .= $field_name.' '.$field_data['datatype']; |
| | 263: |
| | 264: if (isset($field_data['collation'])) |
| | 265: $query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation']; |
| | 266: |
| | 267: if (!$field_data['allow_null']) |
| | 268: $query .= ' NOT NULL'; |
| | 269: |
| | 270: if (isset($field_data['default'])) |
| | 271: $query .= ' DEFAULT '.$field_data['default']; |
| | 272: |
| | 273: $query .= ",\n"; |
| | 274: } |
| | 275: |
| | 276: // If we have a primary key, add it |
| | 277: if (isset($schema['PRIMARY KEY'])) |
| | 278: $query .= 'PRIMARY KEY ('.implode(',', $schema['PRIMARY KEY']).'),'."\n"; |
| | 279: |
| | 280: // Add unique keys |
| | 281: if (isset($schema['UNIQUE KEYS'])) |
| | 282: { |
| | 283: foreach ($schema['UNIQUE KEYS'] as $key_name => $key_fields) |
| | 284: $query .= 'UNIQUE KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$key_name.'('.implode(',', $key_fields).'),'."\n"; |
| | 285: } |
| | 286: |
| | 287: // Add indexes |
| | 288: if (isset($schema['INDEXES'])) |
| | 289: { |
| | 290: foreach ($schema['INDEXES'] as $index_name => $index_fields) |
| | 291: $query .= 'KEY '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name.'('.implode(',', $index_fields).'),'."\n"; |
| | 292: } |
| | 293: |
| | 294: // We remove the last two characters (a newline and a comma) and add on the ending |
| | 295: $query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8'; |
| | 296: |
| | 297: $this->query($query) or error(__FILE__, __LINE__); |
| | 298: } |
| | 299: |
| | 300: |
| | 301: function drop_table($table_name, $no_prefix = false) |
| | 302: { |
| | 303: if (!$this->table_exists($table_name, $no_prefix)) |
| | 304: return; |
| | 305: |
| | 306: $this->query('DROP TABLE '.($no_prefix ? '' : $this->prefix).$table_name) or error(__FILE__, __LINE__); |
| | 307: } |
| | 308: |
| | 309: |
| | 310: function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
| | 311: { |
| | 312: if ($this->field_exists($table_name, $field_name, $no_prefix)) |
| | 313: return; |
| | 314: |
| | 315: $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
| | 316: |
| | 317: if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
| | 318: $default_value = '\''.$this->escape($default_value).'\''; |
| | 319: |
| | 320: $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 : '')) or error(__FILE__, __LINE__); |
| | 321: } |
| | 322: |
| | 323: |
| | 324: function alter_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null, $no_prefix = false) |
| | 325: { |
| | 326: if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
| | 327: return; |
| | 328: |
| | 329: $field_type = preg_replace(array_keys($this->datatype_transformations), array_values($this->datatype_transformations), $field_type); |
| | 330: |
| | 331: if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
| | 332: $default_value = '\''.$this->escape($default_value).'\''; |
| | 333: |
| | 334: $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 : '')) or error(__FILE__, __LINE__); |
| | 335: } |
| | 336: |
| | 337: |
| | 338: function drop_field($table_name, $field_name, $no_prefix = false) |
| | 339: { |
| | 340: if (!$this->field_exists($table_name, $field_name, $no_prefix)) |
| | 341: return; |
| | 342: |
| | 343: $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__); |
| | 344: } |
| | 345: |
| | 346: |
| | 347: function add_index($table_name, $index_name, $index_fields, $unique = false, $no_prefix = false) |
| | 348: { |
| | 349: if ($this->index_exists($table_name, $index_name, $no_prefix)) |
| | 350: return; |
| | 351: |
| | 352: $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).')') or error(__FILE__, __LINE__); |
| | 353: } |
| | 354: |
| | 355: |
| | 356: function drop_index($table_name, $index_name, $no_prefix = false) |
| | 357: { |
| | 358: if (!$this->index_exists($table_name, $index_name, $no_prefix)) |
| | 359: return; |
| | 360: |
| | 361: $this->query('ALTER TABLE '.($no_prefix ? '' : $this->prefix).$table_name.' DROP INDEX '.($no_prefix ? '' : $this->prefix).$table_name.'_'.$index_name) or error(__FILE__, __LINE__); |
| | 362: } |
| | 363: } |