function Schema::createFieldSql

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

Create an SQL string for a field to be used in table creation or alteration.

Parameters

string $name: Name of the field.

array $spec: The field specification, as per the schema data structure format.

3 calls to Schema::createFieldSql()
Schema::addField in core/modules/mysql/src/Driver/Database/mysql/Schema.php
Add a new field to a table.
Schema::changeField in core/modules/mysql/src/Driver/Database/mysql/Schema.php
Change a field definition.
Schema::createTableSql in core/modules/mysql/src/Driver/Database/mysql/Schema.php
Generate SQL to create a new table from a Drupal schema definition.

File

core/modules/mysql/src/Driver/Database/mysql/Schema.php, line 143

Class

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

Namespace

Drupal\mysql\Driver\Database\mysql

Code

protected function createFieldSql($name, $spec) {
    $sql = "[" . $name . "] " . $spec['mysql_type'];
    if (in_array($spec['mysql_type'], $this->mysqlStringTypes)) {
        if (isset($spec['length'])) {
            $sql .= '(' . $spec['length'] . ')';
        }
        if (isset($spec['type']) && $spec['type'] == 'varchar_ascii') {
            $sql .= ' CHARACTER SET ascii';
        }
        if (!empty($spec['binary'])) {
            $sql .= ' BINARY';
        }
        elseif (isset($spec['type']) && $spec['type'] == 'varchar_ascii') {
            $sql .= ' COLLATE ascii_general_ci';
        }
    }
    elseif (isset($spec['precision']) && isset($spec['scale'])) {
        $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
    }
    if (!empty($spec['unsigned'])) {
        $sql .= ' unsigned';
    }
    if (isset($spec['not null'])) {
        if ($spec['not null']) {
            $sql .= ' NOT NULL';
        }
        else {
            $sql .= ' NULL';
        }
    }
    if (!empty($spec['auto_increment'])) {
        $sql .= ' auto_increment';
    }
    // $spec['default'] can be NULL, so we explicitly check for the key here.
    if (array_key_exists('default', $spec)) {
        $sql .= ' DEFAULT ' . $this->escapeDefaultValue($spec['default']);
    }
    if (empty($spec['not null']) && !isset($spec['default'])) {
        $sql .= ' DEFAULT NULL';
    }
    // Add column comment.
    if (!empty($spec['description'])) {
        $sql .= ' COMMENT ' . $this->prepareComment($spec['description'], self::COMMENT_MAX_COLUMN);
    }
    return $sql;
}

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