function Connection::open

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  3. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::open()
  2. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::open()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::open()
  4. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  5. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  6. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  7. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  8. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  9. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  10. 11.x core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  11. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  12. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()

Overrides Connection::open

File

core/modules/pgsql/src/Driver/Database/pgsql/Connection.php, line 94

Class

Connection
PostgreSQL implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\pgsql\Driver\Database\pgsql

Code

public static function open(array &$connection_options = []) {
    // Default to TCP connection on port 5432.
    if (empty($connection_options['port'])) {
        $connection_options['port'] = 5432;
    }
    // PostgreSQL in trust mode doesn't require a password to be supplied.
    if (empty($connection_options['password'])) {
        $connection_options['password'] = NULL;
    }
    else {
        $connection_options['password'] = str_replace('\\', '\\\\', $connection_options['password']);
    }
    $connection_options['database'] = !empty($connection_options['database']) ? $connection_options['database'] : 'template1';
    $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port'];
    // Allow PDO options to be overridden.
    $connection_options += [
        'pdo' => [],
    ];
    $connection_options['pdo'] += [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        // Prepared statements are most effective for performance when queries
        // are recycled (used several times). However, if they are not re-used,
        // prepared statements become inefficient. Since most of Drupal's
        // prepared queries are not re-used, it should be faster to emulate
        // the preparation than to actually ready statements for re-use. If in
        // doubt, reset to FALSE and measure performance.
\PDO::ATTR_EMULATE_PREPARES => TRUE,
        // Convert numeric values to strings when fetching.
\PDO::ATTR_STRINGIFY_FETCHES => TRUE,
    ];
    try {
        $pdo = new \PDO($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
    } catch (\PDOException $e) {
        if (static::getSQLState($e) == static::CONNECTION_FAILURE) {
            if (strpos($e->getMessage(), 'password authentication failed for user') !== FALSE) {
                throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e);
            }
            elseif (strpos($e->getMessage(), 'database') !== FALSE && strpos($e->getMessage(), 'does not exist') !== FALSE) {
                throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e);
            }
        }
        throw $e;
    }
    return $pdo;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.