Professional Documents
Culture Documents
php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2011 Simple Machines
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.0.15
*/
if (!defined('SMF'))
die('Hacking attempt...');
*/
if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] !=
'smf_db_create_table')
{
$smcFunc += array(
'db_add_column' => 'smf_db_add_column',
'db_add_index' => 'smf_db_add_index',
'db_calculate_type' => 'smf_db_calculate_type',
'db_change_column' => 'smf_db_change_column',
'db_create_table' => 'smf_db_create_table',
'db_drop_table' => 'smf_db_drop_table',
'db_table_structure' => 'smf_db_table_structure',
'db_list_columns' => 'smf_db_list_columns',
'db_list_indexes' => 'smf_db_list_indexes',
'db_remove_column' => 'smf_db_remove_column',
'db_remove_index' => 'smf_db_remove_index',
);
$db_package_log = array();
}
// Create a table.
function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters
= array(), $if_exists = 'ignore', $error = 'fatal')
{
global $reservedTables, $smcFunc, $db_package_log, $db_prefix,
$db_character_set;
// Strip out the table name, we might not need it in some cases
$real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) ===
1 ? $match[3] : $db_prefix;
// With or without the database name, the fullname looks like this.
$full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
$table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
// Is it the primary?
if (isset($index['type']) && $index['type'] == 'primary')
$table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',',
$index['columns']) . '),';
else
{
if (empty($index['name']))
$index['name'] = implode('_', $index['columns']);
$table_query .= "\n\t" . (isset($index['type']) && $index['type']
== 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $columns . '),';
}
}
// No trailing commas!
if (substr($table_query, -1) == ',')
$table_query = substr($table_query, 0, -1);
// Drop a table.
function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal')
{
global $reservedTables, $smcFunc, $db_prefix;
// Does it exist?
if (in_array($full_table_name, $smcFunc['db_list_tables']()))
{
$query = 'DROP TABLE ' . $table_name;
$smcFunc['db_query']('',
$query,
array(
'security_override' => true,
)
);
return true;
}
// Otherwise do 'nout.
return false;
}
// Add a column.
function smf_db_add_column($table_name, $column_info, $parameters = array(),
$if_exists = 'update', $error = 'fatal')
{
global $smcFunc, $db_package_log, $txt, $db_prefix;
return true;
}
// Remove a column.
function smf_db_remove_column($table_name, $column_name, $parameters = array(),
$error = 'fatal')
{
global $smcFunc, $db_prefix;
// Does it exist?
$columns = $smcFunc['db_list_columns']($table_name, true);
foreach ($columns as $column)
if ($column['name'] == $column_name)
{
$smcFunc['db_query']('', '
ALTER TABLE ' . $table_name . '
DROP COLUMN ' . $column_name,
array(
'security_override' => true,
)
);
return true;
}
// Change a column.
function smf_db_change_column($table_name, $old_column, $column_info, $parameters =
array(), $error = 'fatal')
{
global $smcFunc, $db_prefix;
// Nothing?
if ($old_info == null)
return false;
$smcFunc['db_query']('', '
ALTER TABLE ' . $table_name . '
CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' .
$type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ?
'NOT NULL' : '') . ' ' .
(!isset($column_info['default']) ? '' : 'default \'' .
$smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
(empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
array(
'security_override' => true,
)
);
}
// Add an index.
function smf_db_add_index($table_name, $index_info, $parameters = array(),
$if_exists = 'update', $error = 'fatal')
{
global $smcFunc, $db_package_log, $db_prefix;
// No columns = no index.
if (empty($index_info['columns']))
return false;
$columns = implode(',', $index_info['columns']);
// If we're here we know we don't have the index - so just add it.
if (!empty($index_info['type']) && $index_info['type'] == 'primary')
{
$smcFunc['db_query']('', '
ALTER TABLE ' . $table_name . '
ADD PRIMARY KEY (' . $columns . ')',
array(
'security_override' => true,
)
);
}
else
{
$smcFunc['db_query']('', '
ALTER TABLE ' . $table_name . '
ADD ' . (isset($index_info['type']) && $index_info['type'] ==
'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
array(
'security_override' => true,
)
);
}
}
// Remove an index.
function smf_db_remove_index($table_name, $index_name, $parameters = array(),
$error = 'fatal')
{
global $smcFunc, $db_prefix;
// Better exist!
$indexes = $smcFunc['db_list_indexes']($table_name, true);
return true;
}
if ($index['name'] == $index_name)
{
// Drop the bugger...
$smcFunc['db_query']('', '
ALTER TABLE ' . $table_name . '
DROP INDEX ' . $index_name,
array(
'security_override' => true,
)
);
return true;
}
}
// Not to be found ;(
return false;
}
return array(
'name' => $table_name,
'columns' => $smcFunc['db_list_columns']($table_name, true),
'indexes' => $smcFunc['db_list_indexes']($table_name, true),
);
}
$columns[$row['Field']] = array(
'name' => $row['Field'],
'null' => $row['Null'] != 'YES' ? false : true,
'default' => isset($row['Default']) ? $row['Default'] :
null,
'type' => $type,
'size' => $size,
'auto' => $auto,
);
if (isset($unsigned))
{
$columns[$row['Field']]['unsigned'] = $unsigned;
unset($unsigned);
}
}
}
$smcFunc['db_free_result']($result);
return $columns;
}
// Is it a partial index?
if (!empty($row['Sub_part']))
$indexes[$row['Key_name']]['columns'][] =
$row['Column_name'] . '(' . $row['Sub_part'] . ')';
else
$indexes[$row['Key_name']]['columns'][] =
$row['Column_name'];
}
}
$smcFunc['db_free_result']($result);
return $indexes;
}
?>