Chris@76: Size of column (If applicable) - for example 255 for a large varchar, 10 for an int etc. If not Chris@76: set SMF will pick a size. Chris@76: + 'default' = Default value - do not set if no default required. Chris@76: + 'null' => Can it be null (true or false) - if not set default will be false. Chris@76: + 'auto' => Set to true to make it an auto incrementing column. Set to a numerical value to set Chris@76: from what it should begin counting. Chris@76: - Adds indexes as specified within indexes parameter. Each index should be a member of $indexes. Values are: Chris@76: + 'name' => Index name (If left empty SMF will generate). Chris@76: + 'type' => Type of index. Choose from 'primary', 'unique' or 'index'. If not set will default to 'index'. Chris@76: + 'columns' => Array containing columns that form part of key - in the order the index is to be created. Chris@76: - parameters: (None yet) Chris@76: - if_exists values: Chris@76: + 'ignore' will do nothing if the table exists. (And will return true) Chris@76: + 'overwrite' will drop any existing table of the same name. Chris@76: + 'error' will return false if the table already exists. Chris@76: Chris@76: */ Chris@76: Chris@76: // Add the file functions to the $smcFunc array. Chris@76: function db_packages_init() Chris@76: { Chris@76: global $smcFunc, $reservedTables, $db_package_log, $db_prefix; Chris@76: Chris@76: if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] != 'smf_db_create_table') Chris@76: { Chris@76: $smcFunc += array( Chris@76: 'db_add_column' => 'smf_db_add_column', Chris@76: 'db_add_index' => 'smf_db_add_index', Chris@76: 'db_alter_table' => 'smf_db_alter_table', Chris@76: 'db_calculate_type' => 'smf_db_calculate_type', Chris@76: 'db_change_column' => 'smf_db_change_column', Chris@76: 'db_create_table' => 'smf_db_create_table', Chris@76: 'db_drop_table' => 'smf_db_drop_table', Chris@76: 'db_table_structure' => 'smf_db_table_structure', Chris@76: 'db_list_columns' => 'smf_db_list_columns', Chris@76: 'db_list_indexes' => 'smf_db_list_indexes', Chris@76: 'db_remove_column' => 'smf_db_remove_column', Chris@76: 'db_remove_index' => 'smf_db_remove_index', Chris@76: ); Chris@76: $db_package_log = array(); Chris@76: } Chris@76: Chris@76: // We setup an array of SMF tables we can't do auto-remove on - in case a mod writer cocks it up! Chris@76: $reservedTables = array('admin_info_files', 'approval_queue', 'attachments', 'ban_groups', 'ban_items', Chris@76: 'board_permissions', 'boards', 'calendar', 'calendar_holidays', 'categories', 'collapsed_categories', Chris@76: 'custom_fields', 'group_moderators', 'log_actions', 'log_activity', 'log_banned', 'log_boards', Chris@76: 'log_digest', 'log_errors', 'log_floodcontrol', 'log_group_requests', 'log_karma', 'log_mark_read', Chris@76: 'log_notify', 'log_online', 'log_packages', 'log_polls', 'log_reported', 'log_reported_comments', Chris@76: 'log_scheduled_tasks', 'log_search_messages', 'log_search_results', 'log_search_subjects', Chris@76: 'log_search_topics', 'log_topics', 'mail_queue', 'membergroups', 'members', 'message_icons', Chris@76: 'messages', 'moderators', 'package_servers', 'permission_profiles', 'permissions', 'personal_messages', Chris@76: 'pm_recipients', 'poll_choices', 'polls', 'scheduled_tasks', 'sessions', 'settings', 'smileys', Chris@76: 'themes', 'topics'); Chris@76: foreach ($reservedTables as $k => $table_name) Chris@76: $reservedTables[$k] = strtolower($db_prefix . $table_name); Chris@76: Chris@76: // We in turn may need the extra stuff. Chris@76: db_extend('extra'); Chris@76: } Chris@76: Chris@76: // Create a table. Chris@76: function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal') Chris@76: { Chris@76: global $reservedTables, $smcFunc, $db_package_log, $db_prefix; Chris@76: Chris@76: // With or without the database name, the full name looks like this. Chris@76: $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; Chris@76: $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // First - no way do we touch SMF tables. Chris@76: // Commented out for now. We need to alter SMF tables in order to use this in the upgrade. Chris@76: /* Chris@76: if (in_array(strtolower($table_name), $reservedTables)) Chris@76: return false; Chris@76: */ Chris@76: Chris@76: // Log that we'll want to remove this on uninstall. Chris@76: $db_package_log[] = array('remove_table', $table_name); Chris@76: Chris@76: // Does this table exist or not? Chris@76: $tables = $smcFunc['db_list_tables'](); Chris@76: if (in_array($full_table_name, $tables)) Chris@76: { Chris@76: // This is a sad day... drop the table? If not, return false (error) by default. Chris@76: if ($if_exists == 'overwrite') Chris@76: $smcFunc['db_drop_table']($table_name); Chris@76: else Chris@76: return $if_exists == 'ignore'; Chris@76: } Chris@76: Chris@76: // Righty - let's do the damn thing! Chris@76: $table_query = 'CREATE TABLE ' . $table_name . "\n" . '('; Chris@76: $done_primary = false; Chris@76: foreach ($columns as $column) Chris@76: { Chris@76: // Auto increment is special Chris@76: if (!empty($column['auto'])) Chris@76: { Chris@76: $table_query .= "\n" . $column['name'] . ' integer PRIMARY KEY,'; Chris@76: $done_primary = true; Chris@76: continue; Chris@76: } Chris@76: elseif (isset($column['default']) && $column['default'] !== null) Chris@76: $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\''; Chris@76: else Chris@76: $default = ''; Chris@76: Chris@76: // Sort out the size... and stuff... Chris@76: $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null; Chris@76: list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']); Chris@76: if ($size !== null) Chris@76: $type = $type . '(' . $size . ')'; Chris@76: Chris@76: // Now just put it together! Chris@76: $table_query .= "\n\t" . $column['name'] . ' ' . $type . ' ' . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default . ','; Chris@76: } Chris@76: Chris@76: // Loop through the indexes next... Chris@76: $index_queries = array(); Chris@76: foreach ($indexes as $index) Chris@76: { Chris@76: $columns = implode(',', $index['columns']); Chris@76: Chris@76: // Is it the primary? Chris@76: if (isset($index['type']) && $index['type'] == 'primary') Chris@76: { Chris@76: // If we've done the primary via auto_inc, don't do it again! Chris@76: if (!$done_primary) Chris@76: $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),'; Chris@76: } Chris@76: else Chris@76: { Chris@76: if (empty($index['name'])) Chris@76: $index['name'] = implode('_', $index['columns']); Chris@76: $index_queries[] = 'CREATE ' . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $table_name . '_' . $index['name'] . ' ON ' . $table_name . ' (' . $columns . ')'; Chris@76: } Chris@76: } Chris@76: Chris@76: // No trailing commas! Chris@76: if (substr($table_query, -1) == ',') Chris@76: $table_query = substr($table_query, 0, -1); Chris@76: Chris@76: $table_query .= ')'; Chris@76: Chris@76: if (empty($parameters['skip_transaction'])) Chris@76: $smcFunc['db_transaction']('begin'); Chris@76: Chris@76: // Do the table and indexes... Chris@76: $smcFunc['db_query']('', $table_query, Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: foreach ($index_queries as $query) Chris@76: $smcFunc['db_query']('', $query, Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: Chris@76: if (empty($parameters['skip_transaction'])) Chris@76: $smcFunc['db_transaction']('commit'); Chris@76: } Chris@76: Chris@76: // Drop a table. Chris@76: function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal') Chris@76: { Chris@76: global $reservedTables, $smcFunc, $db_prefix; Chris@76: Chris@76: // Strip out the table name, we might not need it in some cases Chris@76: $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; Chris@76: $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name); Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // God no - dropping one of these = bad. Chris@76: if (in_array(strtolower($table_name), $reservedTables)) Chris@76: return false; Chris@76: Chris@76: // Does it exist? Chris@76: if (in_array($full_table_name, $smcFunc['db_list_tables']())) Chris@76: { Chris@76: $query = 'DROP TABLE ' . $table_name; Chris@76: $smcFunc['db_query']('', $query, Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Otherwise do 'nout. Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Add a column. Chris@76: function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') Chris@76: { Chris@76: global $smcFunc, $db_package_log, $txt, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // Log that we will want to uninstall this! Chris@76: $db_package_log[] = array('remove_column', $table_name, $column_info['name']); Chris@76: Chris@76: // Does it exist - if so don't add it again! Chris@76: $columns = $smcFunc['db_list_columns']($table_name, false); Chris@76: foreach ($columns as $column) Chris@76: if ($column == $column_info['name']) Chris@76: { Chris@76: // If we're going to overwrite then use change column. Chris@76: if ($if_exists == 'update') Chris@76: return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info); Chris@76: else Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Alter the table to add the column. Chris@76: if ($smcFunc['db_alter_table']($table_name, array('add' => array($column_info))) === false) Chris@76: return false; Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: // We can't reliably do this on SQLite - damn! Chris@76: function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal') Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: if ($smcFunc['db_alter_table']($table_name, array('remove' => array(array('name' => $column_name))))) Chris@76: return true; Chris@76: else Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Change a column. Chris@76: function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal') Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: if ($smcFunc['db_alter_table']($table_name, array('change' => array(array('name' => $old_column) + $column_info)))) Chris@76: return true; Chris@76: else Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Add an index. Chris@76: function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal') Chris@76: { Chris@76: global $smcFunc, $db_package_log, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // No columns = no index. Chris@76: if (empty($index_info['columns'])) Chris@76: return false; Chris@76: $columns = implode(',', $index_info['columns']); Chris@76: Chris@76: // No name - make it up! Chris@76: if (empty($index_info['name'])) Chris@76: { Chris@76: // No need for primary. Chris@76: if (isset($index_info['type']) && $index_info['type'] == 'primary') Chris@76: $index_info['name'] = ''; Chris@76: else Chris@76: $index_info['name'] = implode('_', $index_info['columns']); Chris@76: } Chris@76: else Chris@76: $index_info['name'] = $index_info['name']; Chris@76: Chris@76: // Log that we are going to want to remove this! Chris@76: $db_package_log[] = array('remove_index', $table_name, $index_info['name']); Chris@76: Chris@76: // Let's get all our indexes. Chris@76: $indexes = $smcFunc['db_list_indexes']($table_name, true); Chris@76: // Do we already have it? Chris@76: foreach ($indexes as $index) Chris@76: { Chris@76: if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary')) Chris@76: { Chris@76: // If we want to overwrite simply remove the current one then continue. Chris@76: if ($if_exists != 'update' || $index['type'] == 'primary') Chris@76: return false; Chris@76: else Chris@76: $smcFunc['db_remove_index']($table_name, $index_info['name']); Chris@76: } Chris@76: } Chris@76: Chris@76: // If we're here we know we don't have the index - so just add it. Chris@76: if (!empty($index_info['type']) && $index_info['type'] == 'primary') Chris@76: { Chris@76: //!!! Doesn't work with PRIMARY KEY yet. Chris@76: } Chris@76: else Chris@76: { Chris@76: $smcFunc['db_query']('', ' Chris@76: CREATE ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : '') . ' INDEX ' . $index_info['name'] . ' ON ' . $table_name . ' (' . $columns . ')', Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Remove an index. Chris@76: function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal') Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // Better exist! Chris@76: $indexes = $smcFunc['db_list_indexes']($table_name, true); Chris@76: Chris@76: foreach ($indexes as $index) Chris@76: { Chris@76: //!!! Doesn't do primary key at the moment! Chris@76: if ($index['type'] != 'primary' && $index['name'] == $index_name) Chris@76: { Chris@76: // Drop the bugger... Chris@76: $smcFunc['db_query']('', ' Chris@76: DROP INDEX ' . $index_name, Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: Chris@76: return true; Chris@76: } Chris@76: } Chris@76: Chris@76: // Not to be found ;( Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Get the schema formatted name for a type. Chris@76: function smf_db_calculate_type($type_name, $type_size = null, $reverse = false) Chris@76: { Chris@76: // Generic => Specific. Chris@76: if (!$reverse) Chris@76: { Chris@76: $types = array( Chris@76: 'mediumint' => 'int', Chris@76: 'tinyint' => 'smallint', Chris@76: 'mediumtext' => 'text', Chris@76: 'largetext' => 'text', Chris@76: ); Chris@76: } Chris@76: else Chris@76: { Chris@76: $types = array( Chris@76: 'integer' => 'int', Chris@76: ); Chris@76: } Chris@76: Chris@76: // Got it? Change it! Chris@76: if (isset($types[$type_name])) Chris@76: { Chris@76: if ($type_name == 'tinytext') Chris@76: $type_size = 255; Chris@76: $type_name = $types[$type_name]; Chris@76: } Chris@76: // Numbers don't have a size. Chris@76: if (strpos($type_name, 'int') !== false) Chris@76: $type_size = null; Chris@76: Chris@76: return array($type_name, $type_size); Chris@76: } Chris@76: Chris@76: // Get table structure. Chris@76: function smf_db_table_structure($table_name, $parameters = array()) Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: return array( Chris@76: 'name' => $table_name, Chris@76: 'columns' => $smcFunc['db_list_columns']($table_name, true), Chris@76: 'indexes' => $smcFunc['db_list_indexes']($table_name, true), Chris@76: ); Chris@76: } Chris@76: Chris@76: // Harder than it should be on sqlite! Chris@76: function smf_db_list_columns($table_name, $detail = false, $parameters = array()) Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: PRAGMA table_info(' . $table_name . ')', Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: $columns = array(); Chris@76: Chris@76: $primaries = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: if (!$detail) Chris@76: { Chris@76: $columns[] = $row['name']; Chris@76: } Chris@76: else Chris@76: { Chris@76: // Auto increment is hard to tell really... if there's only one primary it probably is. Chris@76: if ($row['pk']) Chris@76: $primaries[] = $row['name']; Chris@76: Chris@76: // Can we split out the size? Chris@76: if (preg_match('~(.+?)\s*\((\d+)\)~i', $row['type'], $matches)) Chris@76: { Chris@76: $type = $matches[1]; Chris@76: $size = $matches[2]; Chris@76: } Chris@76: else Chris@76: { Chris@76: $type = $row['type']; Chris@76: $size = null; Chris@76: } Chris@76: Chris@76: $columns[$row['name']] = array( Chris@76: 'name' => $row['name'], Chris@76: 'null' => $row['notnull'] ? false : true, Chris@76: 'default' => $row['dflt_value'], Chris@76: 'type' => $type, Chris@76: 'size' => $size, Chris@76: 'auto' => false, Chris@76: ); Chris@76: } Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // Put in our guess at auto_inc. Chris@76: if (count($primaries) == 1) Chris@76: $columns[$primaries[0]]['auto'] = true; Chris@76: Chris@76: return $columns; Chris@76: } Chris@76: Chris@76: // What about some index information? Chris@76: function smf_db_list_indexes($table_name, $detail = false, $parameters = array()) Chris@76: { Chris@76: global $smcFunc, $db_prefix; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: PRAGMA index_list(' . $table_name . ')', Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: $indexes = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: if (!$detail) Chris@76: $indexes[] = $row['name']; Chris@76: else Chris@76: { Chris@76: $result2 = $smcFunc['db_query']('', ' Chris@76: PRAGMA index_info(' . $row['name'] . ')', Chris@76: array( Chris@76: 'security_override' => true, Chris@76: ) Chris@76: ); Chris@76: while ($row2 = $smcFunc['db_fetch_assoc']($result2)) Chris@76: { Chris@76: // What is the type? Chris@76: if ($row['unique']) Chris@76: $type = 'unique'; Chris@76: else Chris@76: $type = 'index'; Chris@76: Chris@76: // This is the first column we've seen? Chris@76: if (empty($indexes[$row['name']])) Chris@76: { Chris@76: $indexes[$row['name']] = array( Chris@76: 'name' => $row['name'], Chris@76: 'type' => $type, Chris@76: 'columns' => array(), Chris@76: ); Chris@76: } Chris@76: Chris@76: // Add the column... Chris@76: $indexes[$row['name']]['columns'][] = $row2['name']; Chris@76: } Chris@76: $smcFunc['db_free_result']($result2); Chris@76: } Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: return $indexes; Chris@76: } Chris@76: Chris@76: function smf_db_alter_table($table_name, $columns) Chris@76: { Chris@76: global $smcFunc, $db_prefix, $db_name, $boarddir; Chris@76: Chris@76: $db_file = substr($db_name, -3) === '.db' ? $db_name : $db_name . '.db'; Chris@76: Chris@76: $table_name = str_replace('{db_prefix}', $db_prefix, $table_name); Chris@76: Chris@76: // Let's get the current columns for the table. Chris@76: $current_columns = $smcFunc['db_list_columns']($table_name, true); Chris@76: Chris@76: // Let's get a list of columns for the temp table. Chris@76: $temp_table_columns = array(); Chris@76: Chris@76: // Let's see if we have columns to remove or columns that are being added that already exist. Chris@76: foreach ($current_columns as $key => $column) Chris@76: { Chris@76: $exists = false; Chris@76: if (isset($columns['remove'])) Chris@76: foreach ($columns['remove'] as $drop) Chris@76: if ($drop['name'] == $column['name']) Chris@76: { Chris@76: $exists = true; Chris@76: break; Chris@76: } Chris@76: Chris@76: if (isset($columns['add'])) Chris@76: foreach ($columns['add'] as $key2 => $add) Chris@76: if ($add['name'] == $column['name']) Chris@76: { Chris@76: unset($columns['add'][$key2]); Chris@76: break; Chris@76: } Chris@76: Chris@76: // Doesn't exist then we 'remove'. Chris@76: if (!$exists) Chris@76: $temp_table_columns[] = $column['name']; Chris@76: } Chris@76: Chris@76: // If they are equal then that means that the column that we are adding exists or it doesn't exist and we are not looking to change any one of them. Chris@76: if (count($temp_table_columns) == count($current_columns) && empty($columns['change']) && empty($columns['add'])) Chris@76: return true; Chris@76: Chris@76: // Drop the temp table. Chris@76: $smcFunc['db_query']('', ' Chris@76: DROP TABLE {raw:temp_table_name}', Chris@76: array( Chris@76: 'temp_table_name' => $table_name . '_tmp', Chris@76: 'db_error_skip' => true, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Let's make a backup of the current database. Chris@76: // We only want the first backup of a table modification. So if there is a backup file and older than an hour just delete and back up again Chris@76: $db_backup_file = $boarddir . '/Packages/backups/backup_' . $table_name . '_' . basename($db_file) . md5($table_name . $db_file); Chris@76: if (file_exists($db_backup_file) && time() - filemtime($db_backup_file) > 3600) Chris@76: { Chris@76: @unlink($db_backup_file); Chris@76: @copy($db_file, $db_backup_file); Chris@76: } Chris@76: elseif (!file_exists($db_backup_file)) Chris@76: @copy($db_file, $db_backup_file); Chris@76: Chris@76: // If we don't have temp tables then everything crapped out. Just exit. Chris@76: if (empty($temp_table_columns)) Chris@76: return false; Chris@76: Chris@76: // Start Chris@76: $smcFunc['db_transaction']('begin'); Chris@76: Chris@76: // Let's create the temporary table. Chris@76: $createTempTable = $smcFunc['db_query']('', ' Chris@76: CREATE TEMPORARY TABLE {raw:temp_table_name} Chris@76: ( Chris@76: {raw:columns} Chris@76: );', Chris@76: array( Chris@76: 'temp_table_name' => $table_name . '_tmp', Chris@76: 'columns' => implode(', ', $temp_table_columns), Chris@76: 'db_error_skip' => true, Chris@76: ) Chris@76: ) !== false; Chris@76: Chris@76: if (!$createTempTable) Chris@76: return false; Chris@76: Chris@76: // Insert into temp table. Chris@76: $smcFunc['db_query']('', ' Chris@76: INSERT INTO {raw:temp_table_name} Chris@76: ({raw:columns}) Chris@76: SELECT {raw:columns} Chris@76: FROM {raw:table_name}', Chris@76: array( Chris@76: 'table_name' => $table_name, Chris@76: 'columns' => implode(', ', $temp_table_columns), Chris@76: 'temp_table_name' => $table_name . '_tmp', Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Drop the current table. Chris@76: $dropTable = $smcFunc['db_query']('', ' Chris@76: DROP TABLE {raw:table_name}', Chris@76: array( Chris@76: 'table_name' => $table_name, Chris@76: 'db_error_skip' => true, Chris@76: ) Chris@76: ) !== false; Chris@76: Chris@76: // If you can't drop the main table then there is no where to go from here. Just return. Chris@76: if (!$dropTable) Chris@76: return false; Chris@76: Chris@76: // We need to keep track of the structure for the current columns and the new columns. Chris@76: $new_columns = array(); Chris@76: $column_names = array(); Chris@76: Chris@76: // Let's get the ones that we already have first. Chris@76: foreach ($current_columns as $name => $column) Chris@76: { Chris@76: if (in_array($name, $temp_table_columns)) Chris@76: { Chris@76: $new_columns[$name] = array( Chris@76: 'name' => $name, Chris@76: 'type' => $column['type'], Chris@76: 'size' => isset($column['size']) ? (int) $column['size'] : null, Chris@76: 'null' => !empty($column['null']), Chris@76: 'auto' => isset($column['auto']) ? $column['auto'] : false, Chris@76: 'default' => isset($column['default']) ? $column['default'] : '', Chris@76: ); Chris@76: Chris@76: // Lets keep track of the name for the column. Chris@76: $column_names[$name] = $name; Chris@76: } Chris@76: } Chris@76: Chris@76: // Now the new. Chris@76: if (!empty($columns['add'])) Chris@76: foreach ($columns['add'] as $add) Chris@76: { Chris@76: $new_columns[$add['name']] = array( Chris@76: 'name' => $add['name'], Chris@76: 'type' => $add['type'], Chris@76: 'size' => isset($add['size']) ? (int) $add['size'] : null, Chris@76: 'null' => !empty($add['null']), Chris@76: 'auto' => isset($add['auto']) ? $add['auto'] : false, Chris@76: 'default' => isset($add['default']) ? $add['default'] : '', Chris@76: ); Chris@76: Chris@76: // Let's keep track of the name for the column. Chris@76: $column_names[$add['name']] = strstr('int', $add['type']) ? ' 0 AS ' . $add['name'] : ' {string:empty_string} AS ' . $add['name']; Chris@76: } Chris@76: Chris@76: // Now to change a column. Not drop but change it. Chris@76: if (isset($columns['change'])) Chris@76: foreach ($columns['change'] as $change) Chris@76: if (isset($new_columns[$change['name']])) Chris@76: $new_columns[$change['name']] = array( Chris@76: 'name' => $change['name'], Chris@76: 'type' => $change['type'], Chris@76: 'size' => isset($change['size']) ? (int) $change['size'] : null, Chris@76: 'null' => !empty($change['null']), Chris@76: 'auto' => isset($change['auto']) ? $change['auto'] : false, Chris@76: 'default' => isset($change['default']) ? $change['default'] : '', Chris@76: ); Chris@76: Chris@76: // Now let's create the table. Chris@76: $createTable = $smcFunc['db_create_table']($table_name, $new_columns, array(), array('skip_transaction' => true)); Chris@76: Chris@76: // Did it create correctly? Chris@76: if ($createTable === false) Chris@76: return false; Chris@76: Chris@76: // Back to it's original table. Chris@76: $insertData = $smcFunc['db_query']('', ' Chris@76: INSERT INTO {raw:table_name} Chris@76: ({raw:columns}) Chris@76: SELECT ' . implode(', ', $column_names) . ' Chris@76: FROM {raw:temp_table_name}', Chris@76: array( Chris@76: 'table_name' => $table_name, Chris@76: 'columns' => implode(', ', array_keys($new_columns)), Chris@76: 'columns_select' => implode(', ', $column_names), Chris@76: 'temp_table_name' => $table_name . '_tmp', Chris@76: 'empty_string' => '', Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Did everything insert correctly? Chris@76: if (!$insertData) Chris@76: return false; Chris@76: Chris@76: // Drop the temp table. Chris@76: $smcFunc['db_query']('', ' Chris@76: DROP TABLE {raw:temp_table_name}', Chris@76: array( Chris@76: 'temp_table_name' => $table_name . '_tmp', Chris@76: 'db_error_skip' => true, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Commit or else there is no point in doing the previous steps. Chris@76: $smcFunc['db_transaction']('commit'); Chris@76: Chris@76: // We got here so we're good. The temp table should be deleted, if not it will be gone later on >:D. Chris@76: return true; Chris@76: } Chris@76: Chris@76: ?>