_db_create_field_sql

Versions
6
_db_create_field_sql($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.

Related topics

▾ 2 functions call _db_create_field_sql()

db_add_field in includes/database.pgsql.inc
Add a new field to a table.
db_create_table_sql in includes/database.pgsql.inc
Generate SQL to create a new table from a Drupal schema definition.

Code

includes/database.pgsql.inc, line 598

<?php
function _db_create_field_sql($name, $spec) {
  $sql = $name .' '. $spec['pgsql_type'];

  if ($spec['type'] == 'serial') {
    unset($spec['not null']);
  }
  if (!empty($spec['unsigned'])) {
    if ($spec['type'] == 'serial') {
      $sql .= " CHECK ($name >= 0)";
    }
    else {
      $sql .= '_unsigned';
    }
  }

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

  if (isset($spec['not null']) && $spec['not null']) {
    $sql .= ' NOT NULL';
  }
  if (isset($spec['default'])) {
    $default = is_string($spec['default']) ? "'". $spec['default'] ."'" : $spec['default'];
    $sql .= " default $default";
  }

  return $sql;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.