You are here

function db_create_table_sql in Drupal 6

Same name in this branch
  1. 6 includes/database.mysql-common.inc \db_create_table_sql()
  2. 6 includes/database.pgsql.inc \db_create_table_sql()

Generate SQL to create a new table from a Drupal schema definition.

Parameters

$name: The name of the table to create.

$table: A Schema API table definition array.

Return value

An array of SQL statements to create the table.

Related topics

1 call to db_create_table_sql()
db_create_table in includes/database.inc
Create a new table from a Drupal table definition.

File

includes/database.mysql-common.inc, line 60
Functions shared between mysql and mysqli database engines.

Code

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,
  );
}