You are here

public function Scheme::getCollation in Drupal driver for SQL Server and SQL Azure 8.2

Get the collation of current connection wether it has or not a database defined in it.

Parameters

string $database: Name of the database.

string $schema: Name of the schema.

string $table: Name of the table.

string $column: Name of the column.

Return value

string

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Scheme.php, line 833

Class

Scheme

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function getCollation($database, $schema, $table = null, $column = null) {

  // No table or column provided, then get info about
  // database (if exists) or server defaul collation.
  if (empty($table) && empty($column)) {

    // Database is defaulted from active connection.
    if (!empty($database)) {

      // Default collation for specific table.
      $sql = "SELECT CONVERT (varchar, DATABASEPROPERTYEX('{$database}', 'collation'))";
      return $this->cnn
        ->query_execute($sql)
        ->fetchField();
    }
    else {

      // Server default collation.
      $sql = "SELECT SERVERPROPERTY ('collation') as collation";
      return $this->cnn
        ->query_execute($sql)
        ->fetchField();
    }
  }
  $sql = <<<EOF
      SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLLATION_NAME, DATA_TYPE
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = ':schema'
        AND TABLE_NAME = ':table'
        AND COLUMN_NAME = ':column'
EOF;
  $params = array();
  $params[':schema'] = $schema;
  $params[':table'] = $table;
  $params[':column'] = $column;
  $result = $this->cnn
    ->query_execute($sql, $params)
    ->fetchObject();
  return $result->COLLATION_NAME;
}