function Schema::createTableSql

Same name in this branch
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::createTableSql()
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::createTableSql()
  3. 11.x core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::createTableSql()
Same name in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::createTableSql()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::createTableSql()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::createTableSql()
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::createTableSql()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Schema.php \Drupal\Core\Database\Driver\mysql\Schema::createTableSql()
  6. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::createTableSql()
  7. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::createTableSql()
  8. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::createTableSql()
  9. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::createTableSql()
  10. 10 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::createTableSql()

Overrides Schema::createTableSql

File

core/modules/pgsql/src/Driver/Database/pgsql/Schema.php, line 287

Class

Schema
PostgreSQL implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

protected function createTableSql($name, $table) {
    $sql_fields = [];
    foreach ($table['fields'] as $field_name => $field) {
        $sql_fields[] = $this->createFieldSql($field_name, $this->processField($field));
    }
    $sql_keys = [];
    if (!empty($table['primary key']) && is_array($table['primary key'])) {
        $this->ensureNotNullPrimaryKey($table['primary key'], $table['fields']);
        $sql_keys[] = 'CONSTRAINT ' . $this->ensureIdentifiersLength($name, '', 'pkey') . ' PRIMARY KEY (' . $this->createPrimaryKeySql($table['primary key']) . ')';
    }
    if (isset($table['unique keys']) && is_array($table['unique keys'])) {
        foreach ($table['unique keys'] as $key_name => $key) {
            // Use the createPrimaryKeySql(), which already discards any prefix
            // lengths passed as part of the key column specifiers. (Postgres
            // doesn't support setting a prefix length for PRIMARY or UNIQUE
            // indices.)
            $sql_keys[] = 'CONSTRAINT ' . $this->ensureIdentifiersLength($name, $key_name, 'key') . ' UNIQUE (' . $this->createPrimaryKeySql($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[] = $this->_createIndexSql($name, $key_name, $key);
        }
    }
    // Add table comment.
    if (!empty($table['description'])) {
        $statements[] = 'COMMENT ON TABLE {' . $name . '} IS ' . $this->prepareComment($table['description']);
    }
    // Add column comments.
    foreach ($table['fields'] as $field_name => $field) {
        if (!empty($field['description'])) {
            $statements[] = 'COMMENT ON COLUMN {' . $name . '}.' . $field_name . ' IS ' . $this->prepareComment($field['description']);
        }
    }
    return $statements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.