You are here

public function DatabaseSchema_sqlsrv::getCollation 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::getCollation()
  2. 7.2 sqlsrv/schema.inc \DatabaseSchema_sqlsrv::getCollation()

Gets collation for current connection, whether it has or not a database defined in it.

@status needs review

File

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

Class

DatabaseSchema_sqlsrv

Code

public function getCollation($table = NULL, $column = NULL) {

  // Database is defaulted from active connection.
  $options = $this->connection
    ->getConnectionOptions();
  $database = $options['database'];
  if (!empty($table) && !empty($column)) {
    $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'] = $this->defaultSchema;
    $params[':table'] = $table;
    $params[':column'] = $column;
    $result = $this->connection
      ->query($sql, $params)
      ->fetchObject();
    return $result->COLLATION_NAME;
  }
  elseif (!empty($database)) {

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

    // Server default collation.
    $sql = "SELECT SERVERPROPERTY ('collation') as collation";
    return $this->connection
      ->query($sql)
      ->fetchField();
  }
}