You are here

protected function Schema::createFieldSql in Drupal driver for SQL Server and SQL Azure 3.0.x

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. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::createFieldSql()

Create Field SQL.

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

string $table: The name of the table.

string $name: Name of the field.

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

bool $skip_checks: Skip checks.

Return value

string The SQL statement to create the field.

3 calls to Schema::createFieldSql()
Schema::addField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Add a new field to a table.
Schema::changeField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Change a field definition.
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 1395

Class

Schema

Namespace

Drupal\Driver\Database\sqlsrv

Code

protected function createFieldSql($table, $name, $spec, $skip_checks = FALSE) {
  $sql = $this->connection
    ->escapeField($name) . ' ';
  $sql .= $this
    ->createDataType($table, $name, $spec);
  $sqlsrv_type = $spec['sqlsrv_type'];
  $sqlsrv_type_native = $spec['sqlsrv_type_native'];
  $is_text = in_array($sqlsrv_type_native, [
    'char',
    'varchar',
    'text',
    'nchar',
    'nvarchar',
    'ntext',
  ]);
  if ($is_text === TRUE) {

    // If collation is set in the spec array, use it.
    // Otherwise use the database default.
    if (isset($spec['binary'])) {
      $default_collation = $this
        ->getCollation();
      if ($spec['binary'] === TRUE) {
        $sql .= ' COLLATE ' . preg_replace("/_C[IS]_/", "_CS_", $default_collation);
      }
      elseif ($spec['binary'] === FALSE) {
        $sql .= ' COLLATE ' . preg_replace("/_C[IS]_/", "_CI_", $default_collation);
      }
    }
  }
  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}_{$name}_df} DEFAULT {$default}";
    }
    if (!empty($spec['identity'])) {
      $sql .= ' IDENTITY';
    }
    if (!empty($spec['unsigned'])) {
      $sql .= ' CHECK (' . $this->connection
        ->escapeField($name) . ' >= 0)';
    }
  }
  return $sql;
}