protected function DatabaseSchema_pgsql::createFieldSql

7.x schema.inc protected DatabaseSchema_pgsql::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.

2 calls to DatabaseSchema_pgsql::createFieldSql()
DatabaseSchema_pgsql::addField in drupal-7.x/includes/database/pgsql/schema.inc
Add a new field to a table.
DatabaseSchema_pgsql::createTableSql in drupal-7.x/includes/database/pgsql/schema.inc
Generate SQL to create a new table from a Drupal schema definition.

File

drupal-7.x/includes/database/pgsql/schema.inc, line 177
Database schema code for PostgreSQL database servers.

Class

DatabaseSchema_pgsql

Code

protected function createFieldSql($name, $spec) {
  $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', 'text')) && 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 (isset($spec['default'])) {
    $default = is_string($spec['default']) ? "'" . $spec['default'] . "'" : $spec['default'];
    $sql .= " default $default";
  }

  return $sql;
}