function Schema::createFieldSql

Same name in this branch
  1. 11.x core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::createFieldSql()
  2. 11.x 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. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::createFieldSql()
  8. 10 core/modules/mysql/src/Driver/Database/mysql/Schema.php \Drupal\mysql\Driver\Database\mysql\Schema::createFieldSql()
  9. 10 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.

Before passing a field out of a schema definition into this function it has to be processed by self::processField().

Parameters

$name: Name of the field.

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

2 calls to Schema::createFieldSql()
Schema::addField in core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
Schema::createColumnsSql in core/modules/sqlite/src/Driver/Database/sqlite/Schema.php
Build the SQL expression for creating columns.

File

core/modules/sqlite/src/Driver/Database/sqlite/Schema.php, line 162

Class

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

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

protected function createFieldSql($name, $spec) {
    $name = $this->connection
        ->escapeField($name);
    if (!empty($spec['auto_increment'])) {
        $sql = $name . " INTEGER PRIMARY KEY AUTOINCREMENT";
        if (!empty($spec['unsigned'])) {
            $sql .= ' CHECK (' . $name . '>= 0)';
        }
    }
    else {
        $sql = $name . ' ' . $spec['sqlite_type'];
        if (in_array($spec['sqlite_type'], [
            'VARCHAR',
            'TEXT',
        ])) {
            if (isset($spec['length'])) {
                $sql .= '(' . $spec['length'] . ')';
            }
            if (isset($spec['binary']) && $spec['binary'] === FALSE) {
                $sql .= ' COLLATE NOCASE_UTF8';
            }
        }
        if (isset($spec['not null'])) {
            if ($spec['not null']) {
                $sql .= ' NOT NULL';
            }
            else {
                $sql .= ' NULL';
            }
        }
        if (!empty($spec['unsigned'])) {
            $sql .= ' CHECK (' . $name . '>= 0)';
        }
        if (isset($spec['default'])) {
            if (is_string($spec['default'])) {
                $spec['default'] = $this->connection
                    ->quote($spec['default']);
            }
            $sql .= ' DEFAULT ' . $spec['default'];
        }
        if (empty($spec['not null']) && !isset($spec['default'])) {
            $sql .= ' DEFAULT NULL';
        }
    }
    return $sql;
}

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