You are here

public static function Utils::convertTypes in Drupal driver for SQL Server and SQL Azure 8.2

Gets the statement needed to convert one type to another

Parameters

string $reference:

string $source_type:

string $destination_type:

string $destination_collation:

1 call to Utils::convertTypes()
Schema::changeField in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::changeField().

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Utils.php, line 120

Class

Utils

Namespace

Drupal\Driver\Database\sqlsrv

Code

public static function convertTypes($reference, $source_type, $destination_type, $destination_collation = null) {
  $field_old_expression = $reference;

  // If the destination column is text, but source column is not text
  // we need to do an explicit convert to text before collating.
  if (static::IsTextType($destination_type) && !static::IsTextType($source_type)) {
    $field_old_expression = "CONVERT({$destination_type}, {$field_old_expression})";
  }

  // Add collation data if necessary.
  if (!empty($destination_collation)) {
    $field_old_expression .= " COLLATE {$destination_collation}";
  }
  $result = '';
  if (static::GetMSSQLType($destination_type) == 'varbinary') {
    switch (static::GetMSSQLType($source_type)) {
      case 'varchar':
      case 'char':
        $result = "CAST({$field_old_expression} AS {$destination_type})";
        break;
      case 'nvarchar':
        $result = "CONVERT({$destination_type}, {$field_old_expression})";
        break;
      default:
        $result = "CONVERT({$destination_type}, {$field_old_expression}, 1)";
    }
  }
  else {
    $result = "CONVERT({$destination_type}, {$field_old_expression})";
  }
  return $result;
}