Create a new PDO connection.

@param  string  $dsn
@param  array   $config
@param  array   $options
@return \PDO

@throws \Exception
499285
public function createConnection($dsn, array $config, array $options)
    {
        [$username, $password] = [
            $config['username'] ?? null, $config['password'] ?? null,
        ];

        try {
            return $this->createPdoConnection(
                $dsn, $username, $password, $options
            );
        } catch (Exception $e) {
            return $this->tryAgainIfCausedByLostConnection(
                $e, $dsn, $username, $password, $options
            );
        }
    }
27545
protected function setupMongo($dsn, $options)
    {
        try {
            $this->client = new \MongoClient($dsn, $options);
            $this->dbh    = $this->client->selectDB($this->dbName);
        } catch (\MongoConnectionException $e) {
            throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
        }
    }
933377
public function createConnection($dsn, array $config, array $options)
    {
        list($username, $password) = [
            Arr::get($config, 'username'), Arr::get($config, 'password'),
        ];

        try {
            return $this->createPdoConnection(
                $dsn,
                $username,
                $password,
                $options
            );
        } catch (Exception $e) {
            return $this->tryAgainIfCausedByLostConnection(
                $e,
                $dsn,
                $username,
                $password,
                $options
            );
        }
    }
24248
protected function createPDO($dsn, $username, $password, array $options)
    {
        return new PDO($dsn, $username, $password, $options);
    }
887896
protected function getDsn(array $config)
	{
		// First we will create the basic DSN setup as well as the port if it is in
		// in the configuration options. This will give us the basic DSN we will
		// need to establish the PDO connections and return them back for use.
		if (in_array('dblib', $this->getAvailableDrivers()))
		{
			return $this->getDblibDsn($config);
		}
		else
		{
			return $this->getSqlSrvDsn($config);
		}
	}
1269627
protected function conn_prepare($dsn)
    {
        // Create database file, required by PDO to exist on connection
        if (!empty($dsn['database']) && !file_exists($dsn['database'])) {
            $created = touch($dsn['database']);

            // File mode setting, for compat. with MDB2
            if (!empty($dsn['mode']) && $created) {
                chmod($dsn['database'], octdec($dsn['mode']));
            }
        }
    }
1058512
protected function createPdoConnection($dsn, $username, $password, $options)
    {
        if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
            return new PDOConnection($dsn, $username, $password, $options);
        }

        return new PDO($dsn, $username, $password, $options);
    }
1314375
public static function connect($dsn, $name = false)
    {
        Eresus_Kernel::log(__METHOD__, LOG_DEBUG, '("%s", %s)', $dsn, $name);

        try
        {
            $db = ezcDbFactory::create($dsn);
        }
        catch (Exception $e)
        {
            throw new Eresus_DB_Exception("Can not connect to '$dsn'", 0, $e);
        }

        $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);

        if (substr($dsn, 0, 5) == 'mysql' && preg_match('/charset=(.*?)(&|$)/', $dsn, $m))
        {
            $db->query("SET NAMES {$m[1]}");
        }

        ezcDbInstance::set($db, $name);
        return $db;
    }
442700
private function connect($dsn, $username, $password, array $options, $charset)
    {
        $sessionMode = array_key_exists('session_mode', $options) ? $options['session_mode'] : null;

        if (array_key_exists(PDO::ATTR_PERSISTENT, $options) && $options[PDO::ATTR_PERSISTENT]) {
            $this->dbh = @oci_pconnect($username, $password, $dsn, $charset, $sessionMode);
        } else {
            $this->dbh = @oci_connect($username, $password, $dsn, $charset, $sessionMode);
        }

        if (! $this->dbh) {
            $e = oci_error();
            throw new Oci8Exception($e['message']);
        }
    }
316462
final protected function connect($con, array $options = array())
	{
		if (is_null($con)) {
			$con = self::DEFAULT_CON;
		}
		if (is_string($con)) {
			if (! pathinfo($con, PATHINFO_EXTENSION)) {
				$con .= '.' . self::DEFAULT_EXTENSION;
			}
			$con = $this->parse($con);
		} elseif (is_array($con)) {
			$con = (object)$con;
		}
		if (!is_object($con) or !(isset($con->user) and isset($con->password))) {
			throw new \DomainException('PDO requires at least a user and password');
		}
		$this->user = $con->user;
		$this->password = $con->password;
		$this->type = (isset($con->type)) ? $con->type : self::DEFAULT_TYPE;
		$this->database = (isset($con->database)) ? $con->database : $this->user;
		$this->port = (isset($con->port) and $con->port !== self::DEFAULT_PORT) ? $con->port : null;
		$this->host = (isset($con->host) and $con->host !== self::DEFAULT_HOST) ? $con->host : null;
		$this->dsn = "{$this->type}:dbname={$this->database}";

		if (
			is_string($this->host) and (
				filter_var($this->host, FILTER_VALIDATE_IP)
				or filter_var("http://{$this->host}", FILTER_VALIDATE_URL)
			)
		) {
			$this->dsn .= ";host={$this->host}";
		}

		if (is_int($this->port)) {
			$this->dsn .= ";port={$this->port}";
		}

		try {
			parent::__construct(
				$this->dsn,
				$this->user,
				$this->password,
				$options
			);

			$this->connected = true;
		} catch(\PDOException $e) {
			$this->connected = false;
			//echo $e . PHP_EOL;
		}
	}
712727
public static function createFromDSN($dsn)
    {
        if (is_string($dsn)) {
            $parsed = self::parseDSN($dsn);
        } else {
            $parsed = $dsn;
        }

        $connection = DriverManager::getConnection($parsed);

        if ($parsed['driver'] === 'pdo_sqlite') {
            return new ConnectionHandler\SqliteConnectionHandler($connection);
        }

        if ($parsed['driver'] === 'pdo_pgsql') {
            return new ConnectionHandler\PostgresConnectionHandler($connection);
        }

        return new self($connection);
    }
714320
protected function createPdoConnection($dsn, $username, $password, $options)
    {
        return new SwoolePDO($dsn, $username, $password, $options);
    }
927201
protected function init($dsn, $username, $password, array $options = [])
    {
        try {
            //With parameters of method scope, because if there is a exception, this object is destroyed
            parent::__construct($dsn, $username, $password, $options);
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['\cordillera\middlewares\db\Statement', [$this]]);
        } catch (\PDOException $e) {
            logger()->critical('DB connection failed', [
                    'dsn' => $dsn,
                    'username' => $username,
                    'password' => $password,
                    'options' => $options,
                ]);
            throw new Exception($e->getMessage(), 500, Exception::DBCONNECTION);
        }
    }
144063
public static function setDSN($dsn)
        {
            $dsn_details = parse_url($dsn);
            if (!is_array($dsn_details) || !array_key_exists('scheme', $dsn_details)) {
                throw new Exception('This does not look like a valid DSN - cannot read the database type');
            }
            try {
                self::setDriver($dsn_details['scheme']);
                $details = explode(';', $dsn_details['path']);
                foreach ($details as $detail) {
                    $detail_info = explode('=', $detail);
                    if (count($detail_info) != 2) {
                        throw new Exception('This does not look like a valid DSN - cannot read the connection details');
                    }
                    switch ($detail_info[0]) {
                        case 'host':
                            self::setHostname($detail_info[1]);
                            break;
                        case 'port':
                            self::setPort($detail_info[1]);
                            break;
                        case 'dbname':
                            self::setDatabaseName($detail_info[1]);
                            break;
                    }
                }
            } catch (\Exception $e) {
                throw $e;
            }
            self::$dsn = $dsn;
        }
241905
public static function Connect ($dsn) {
		try {
			$dsn = static::AdjustDsn($dsn);

			static::ValidateDsn($dsn);

			return new static (
				static::MakeDsn($dsn),
				$dsn['username'] ?? null,
				$dsn['password'] ?? null,
				static::MakeOptionList($dsn['options'] ?? [])
			);
		} catch (\PDOException $pdo_e) {
 			throw new \Exception(sprintf(
 				'%s dsn=%s user=%s password=%s options=%s',
 				$pdo_e->getMessage(),
 				static::MakeDsn($dsn),
 				$dsn['username'] ?? '',
 				$dsn['password'] ?? '',
 				implode(', ', static::MakeOptionList($dsn['options'] ?? []))
 			), 0, $pdo_e);
		}
	}
1291547
public function connect(array $config)
    {
        $options = $this->getOptions($config);

        return $this->createConnection($this->getDsn($config), $config, $options);
    }
724161
protected function _connect($dsn, array $config)
    {
        $connection = new PDO(
            $dsn,
            $config['username'],
            $config['password'],
            $config['flags']
        );
        $this->setConnection($connection);

        return true;
    }