function db_create_table_sql in Drupal 6
Same name in this branch
- 6 includes/database.mysql-common.inc \db_create_table_sql()
- 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.pgsql.inc, line 488 - Database interface code for PostgreSQL database servers.
Code
function db_create_table_sql($name, $table) {
$sql_fields = array();
foreach ($table['fields'] as $field_name => $field) {
$sql_fields[] = _db_create_field_sql($field_name, _db_process_field($field));
}
$sql_keys = array();
if (isset($table['primary key']) && is_array($table['primary key'])) {
$sql_keys[] = 'PRIMARY KEY (' . implode(', ', $table['primary key']) . ')';
}
if (isset($table['unique keys']) && is_array($table['unique keys'])) {
foreach ($table['unique keys'] as $key_name => $key) {
$sql_keys[] = 'CONSTRAINT {' . $name . '}_' . $key_name . '_key UNIQUE (' . implode(', ', $key) . ')';
}
}
$sql = "CREATE TABLE {" . $name . "} (\n\t";
$sql .= implode(",\n\t", $sql_fields);
if (count($sql_keys) > 0) {
$sql .= ",\n\t";
}
$sql .= implode(",\n\t", $sql_keys);
$sql .= "\n)";
$statements[] = $sql;
if (isset($table['indexes']) && is_array($table['indexes'])) {
foreach ($table['indexes'] as $key_name => $key) {
$statements[] = _db_create_index_sql($name, $key_name, $key);
}
}
return $statements;
}