You are here

protected function DatabaseSchema_sqlsrv::createFieldSql in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::createFieldSql()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::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 _db_process_field().

Parameters

$table: The name of the table.

$name: Name of the field.

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

3 calls to DatabaseSchema_sqlsrv::createFieldSql()
DatabaseSchema_sqlsrv::addField in sqlsrv/schema.inc
Override DatabaseSchema::addField().
DatabaseSchema_sqlsrv::changeField in sqlsrv/schema.inc
Override DatabaseSchema::changeField().
DatabaseSchema_sqlsrv::createTableSql in sqlsrv/schema.inc
Generate SQL to create a new table from a Drupal schema definition.

File

sqlsrv/schema.inc, line 262
Database schema code for Microsoft SQL Server database servers.

Class

DatabaseSchema_sqlsrv

Code

protected function createFieldSql($table, $name, $spec, $skip_checks = FALSE) {
  $sql = $this->connection
    ->quoteIdentifier($name) . ' ' . $spec['sqlsrv_type'];
  if (in_array($spec['sqlsrv_type'], array(
    'char',
    'varchar',
    'text',
    'nchar',
    'nvarchar',
    'ntext',
  )) && !empty($spec['length'])) {
    $sql .= '(' . $spec['length'] . ')';
  }
  elseif (in_array($spec['sqlsrv_type'], array(
    'numeric',
    'decimal',
  )) && isset($spec['precision']) && isset($spec['scale'])) {
    $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
  }
  if (isset($spec['not null']) && $spec['not null']) {
    $sql .= ' NOT NULL';
  }
  if (!$skip_checks) {
    if (isset($spec['default'])) {
      $default = is_string($spec['default']) ? "'" . addslashes($spec['default']) . "'" : $spec['default'];
      $sql .= ' CONSTRAINT {' . $table . '}_' . $name . '_df DEFAULT ' . $default;
    }
    if (!empty($spec['identity'])) {
      $sql .= ' IDENTITY';
    }
    if (!empty($spec['unsigned'])) {
      $sql .= ' CHECK (' . $this->connection
        ->quoteIdentifier($name) . ' >= 0)';
    }
  }
  return $sql;
}