You are here

protected function Schema::createFieldSql in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createFieldSql()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\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 _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 Schema::createFieldSql()
Schema::addField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::addField().
Schema::changeField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::changeField().
Schema::createTableSql in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Generate SQL to create a new table from a Drupal schema definition.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 665
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function createFieldSql($table, $name, $spec, $skip_checks = FALSE) {

  // Use a prefixed table.
  $table_prefixed = $this->connection
    ->prefixTables('{' . $table . '}');
  $sqlsrv_type = $spec['sqlsrv_type'];
  $sqlsrv_type_native = $spec['sqlsrv_type_native'];
  $is_text = in_array($sqlsrv_type_native, array(
    'char',
    'varchar',
    'text',
    'nchar',
    'nvarchar',
    'ntext',
  ));
  $lengthable = in_array($sqlsrv_type_native, array(
    'char',
    'varchar',
    'nchar',
    'nvarchar',
  ));
  $sql = $this->connection
    ->quoteIdentifier($name) . ' ';
  if (!empty($spec['length']) && $lengthable) {
    $sql .= $sqlsrv_type_native . '(' . $spec['length'] . ')';
  }
  elseif (in_array($sqlsrv_type_native, array(
    'numeric',
    'decimal',
  )) && isset($spec['precision']) && isset($spec['scale'])) {

    // Maximum precision for SQL Server 2008 orn greater is 38.
    // For previous versions it's 28.
    if ($spec['precision'] > 38) {

      // Logs an error
      \Drupal::logger('sqlsrv')
        ->warning("Field '@field' in table '@table' has had it's precision dropped from @precision to 38", array(
        '@field' => $name,
        '@table' => $table,
        '@precision' => $spec['precision'],
      ));
      $spec['precision'] = 38;
    }
    $sql .= $sqlsrv_type_native . '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
  }
  else {
    $sql .= $sqlsrv_type;
  }

  // When binary is true, case sensitivity is requested.
  if ($is_text === TRUE && isset($spec['binary']) && $spec['binary'] === TRUE) {
    $sql .= ' COLLATE ' . self::DEFAULT_COLLATION_CS;
  }
  if (isset($spec['not null']) && $spec['not null']) {
    $sql .= ' NOT NULL';
  }
  if (!$skip_checks) {
    if (isset($spec['default'])) {
      $default = $this
        ->defaultValueExpression($sqlsrv_type, $spec['default']);
      $sql .= " CONSTRAINT {$table_prefixed}_{$name}_df DEFAULT  {$default}";
    }
    if (!empty($spec['identity'])) {
      $sql .= ' IDENTITY';
    }
    if (!empty($spec['unsigned'])) {
      $sql .= ' CHECK (' . $this->connection
        ->quoteIdentifier($name) . ' >= 0)';
    }
  }
  return $sql;
}