protected function Schema::createFieldSql

protected Schema::createFieldSql($name, $spec)

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 _db_process_field().

Parameters

$name: Name of the field.

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

File

core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 279

Class

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

Namespace

Drupal\Core\Database\Driver\pgsql

Code

protected function createFieldSql($name, $spec) {
  // The PostgreSQL server converts names into lowercase, unless quoted.
  $sql = '"' . $name . '" ' . $spec['pgsql_type'];

  if (isset($spec['type']) && $spec['type'] == 'serial') {
    unset($spec['not null']);
  }

  if (in_array($spec['pgsql_type'], array('varchar', 'character')) && isset($spec['length'])) {
    $sql .= '(' . $spec['length'] . ')';
  }
  elseif (isset($spec['precision']) && isset($spec['scale'])) {
    $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
  }

  if (!empty($spec['unsigned'])) {
    $sql .= " CHECK ($name >= 0)";
  }

  if (isset($spec['not null'])) {
    if ($spec['not null']) {
      $sql .= ' NOT NULL';
    }
    else {
      $sql .= ' NULL';
    }
  }
  if (array_key_exists('default', $spec)) {
    $default = $this->escapeDefaultValue($spec['default']);
    $sql .= " default $default";
  }

  return $sql;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Schema.php/function/Schema::createFieldSql/8.1.x