| Versionen | |
|---|---|
| drupal6 | db_create_table_sql($name, $table) |
Generate SQL to create a new table from a Drupal schema definition.
$name The name of the table to create.
$table A Schema API table definition array.
An array of SQL statements to create the table.
includes/
<?php
function db_create_table_sql($name, $table) {
if (empty($table['mysql_suffix'])) {
$table['mysql_suffix'] = '/*!40100 DEFAULT CHARACTER SET utf8';
// By default, MySQL uses the default collation for new tables, which is
// 'utf8_general_ci' for utf8. If an alternate collation has been set, it
// needs to be explicitly specified.
// @see db_connect()
$collation = (!empty($table['collation']) ? $table['collation'] : (!empty($GLOBALS['db_collation']) ? $GLOBALS['db_collation'] : ''));
if ($collation) {
$table['mysql_suffix'] .= ' COLLATE ' . $collation;
}
$table['mysql_suffix'] .= ' */';
}
$sql = "CREATE TABLE {" . $name . "} (\n";
// Add the SQL statement for each field.
foreach ($table['fields'] as $field_name => $field) {
$sql .= _db_create_field_sql($field_name, _db_process_field($field)) . ", \n";
}
// Process keys & indexes.
$keys = _db_create_keys_sql($table);
if (count($keys)) {
$sql .= implode(", \n", $keys) . ", \n";
}
// Remove the last comma and space.
$sql = substr($sql, 0, -3) . "\n) ";
$sql .= $table['mysql_suffix'];
return array($sql);
}
?>
Kommentare
Kommentar hinzufügen