You are here

public static function Connection::createUrlFromConnectionOptions in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::createUrlFromConnectionOptions()

Adding schema to the connection URL.

Overrides Connection::createUrlFromConnectionOptions

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 701
Definition of Drupal\Driver\Database\sqlsrv\Connection

Class

Connection
Temporary tables: temporary table support is done by means of global temporary tables (#) to avoid the use of DIRECT QUERIES. You can enable and disable the use of direct queries with $this->driver_settings->defaultDirectQuery =…

Namespace

Drupal\Driver\Database\sqlsrv

Code

public static function createUrlFromConnectionOptions(array $connection_options) {
  if (!isset($connection_options['driver'], $connection_options['database'])) {
    throw new \InvalidArgumentException("As a minimum, the connection options array must contain at least the 'driver' and 'database' keys");
  }
  $user = '';
  if (isset($connection_options['username'])) {
    $user = $connection_options['username'];
    if (isset($connection_options['password'])) {
      $user .= ':' . $connection_options['password'];
    }
    $user .= '@';
  }
  $host = empty($connection_options['host']) ? 'localhost' : $connection_options['host'];
  $db_url = $connection_options['driver'] . '://' . $user . $host;
  if (isset($connection_options['port'])) {
    $db_url .= ':' . $connection_options['port'];
  }
  $db_url .= '/' . $connection_options['database'];
  $query = [];
  if (isset($connection_options['module'])) {
    $query['module'] = $connection_options['module'];
  }
  if (isset($connection_options['schema'])) {
    $query['schema'] = $connection_options['schema'];
  }
  if (isset($connection_options['cache_schema'])) {
    $query['cache_schema'] = $connection_options['cache_schema'];
  }
  if (count($query) > 0) {
    $parameters = [];
    foreach ($query as $key => $values) {
      $parameters[] = $key . '=' . $values;
    }
    $query_string = implode("&", $parameters);
    $db_url .= '?' . $query_string;
  }
  if (isset($connection_options['prefix']['default']) && $connection_options['prefix']['default'] !== '') {
    $db_url .= '#' . $connection_options['prefix']['default'];
  }
  return $db_url;
}