@param Request    $request
@param Connection $connection

@return Psr7\Request
967311
protected function _createPsr7Request(Request $request, Connection $connection)
    {
        $req = new Psr7\Request(
            $request->getMethod(),
            $this->_getActionPath($request),
            $connection->hasConfig('headers') && \is_array($connection->getConfig('headers'))
                ? $connection->getConfig('headers')
                : []
        );

        $data = $request->getData();
        if (!empty($data) || '0' === $data) {
            if (Request::GET == $req->getMethod()) {
                $req = $req->withMethod(Request::POST);
            }

            if ($this->hasParam('postWithRequestBody') && true == $this->getParam('postWithRequestBody')) {
                $request->setMethod(Request::POST);
                $req = $req->withMethod(Request::POST);
            }

            $req = $req->withBody(
                Psr7\stream_for(\is_array($data)
                    ? JSON::stringify($data, JSON_UNESCAPED_UNICODE)
                    : $data
                )
            );
        }

        return $req;
    }
330452
protected function createConnectionErrorMessage($connection)
    {
        $message =
            'Not connected to database. dsn: ' .
            $connection->getDriver()->getName() .
            '://' .
            '****:****@' .
            $connection->getHost() . ':' . $connection->getPort() .
            '/' . $connection->getDatabase();

        return $message;
    }
974943
public function forward(ConnectionInterface $connection, $forwardAddress, $forwardPort, $protocolVersion = 2)
    {
        if ($connection instanceof PortConnection) {
            $sourceAddress = $connection->getSourceAddress();
            $sourcePort = $connection->getSourcePort();
            $targetAddress = $connection->getTargetAddress();
            $targetPort = $connection->getTargetPort();
        } elseif ($connection instanceof Connection) {
            list($sourceAddress, $sourcePort) = explode(':', stream_socket_get_name($connection->stream, true));
            list($targetAddress, $targetPort) = explode(':', stream_socket_get_name($connection->stream, false));
        } else {
            throw new \InvalidArgumentException("This connection type is not supported.");
        }

        $connection->pause();

        $header = Header::createForward4($sourceAddress, $sourcePort, $targetAddress, $targetPort, $protocolVersion);
        /** @var Promise $promise */
        $promise = $this->connector->create($forwardAddress, $forwardPort);
        return $promise
            ->then(function(Stream $forwardedConnection) use (
                $connection, $header,
                $sourceAddress, $sourcePort,
                $targetAddress, $targetPort
            ) {
                $forwardedConnection->pause();
                $forwardedConnection->getBuffer()->once('full-drain', function() use ($connection, $forwardedConnection) {
                    $this->emit('forward', [$connection, $forwardedConnection]);
                    $connection->pipe($forwardedConnection);
                    $forwardedConnection->pipe($connection);
                    $connection->resume();
                    $forwardedConnection->resume();
                });
                $forwardedConnection->write($header);

        });
    }
538091
public function findByConnection($connection)
    {
        return $this->first(function ($client, $key) use ($connection) {
            return $client->connection === $connection;
        });
    }
556769
public function addConnection(string $name = null, Connection $connection = null): self
    {
        if (is_null($name)) {
            $this->defaultConnection = $connection;
        } else {
            $this->registry[$name] = $connection;
        }

        return $this;
    }
1302866
protected function configure(Connection $connection, $type)
    {
        $connection = $this->setPdoForType($connection, $type);

        // First we'll set the fetch mode and a few other dependencies of the database
        // connection. This method basically just configures and prepares it to get
        // used by the application. Once we're finished we'll return it back out.
        if ($this->container->has('events')) {
            $connection->setEventDispatcher($this->container['events']);
        }

        // Here we'll set a reconnector callback. This reconnector can be any callable
        // so we will set a Closure to reconnect from this manager with the name of
        // the connection, which will allow us to reconnect from the connections.
        $connection->setReconnector(function ($connection) {
            $this->reconnect($connection->getName());
        });

        return $connection;
    }
847365
public function setData($connectName, $driver,Connection $connection)
    {
        $this->connectName = $connectName;
        $this->driver = $driver;
        $this->connection = $connection;
        return $this;
    }
607180
public static function create($connectionURI) {
    $connection = new MongoDataConnection();
    $database = parse_url($connectionURI, PHP_URL_PATH);
    $database = ltrim($database, '/');
    $mongo = new \MongoClient($connectionURI);
    $connection->db = $mongo->selectDB($database);

    return $connection;
  }
527808
public function get( $connection, $cache = true )
    {

        if( !is_string( $connection ) ) {
            throw new BadTypeException( $connection, 'string' );
        }

        // use the multiton
        if( !isset( $this->instances[$connection] ) or !$cache ) {

            if( !isset( $this->connectionSettings[$connection]) ) {
                throw new UnknownNamedConnectionException( $connection );
            }

            $resource = new Resource( $this->connectionSettings[$connection], $this );

            if( $cache ) {
                $this->instances[$connection] = $resource;
            }

        // get the resource from the multiton
        } else {
            $resource = $this->instances[$connection];
        }

        return new Pg( $resource, $connection );
    }
65641
private function createClientsView(Connection $connection, DatabaseSchemaInterface $schema)
    {
        $view             = $schema->getClientsView();
        $scopes           = $schema->getClientsViewScopesColumn();
        $redirectUris     = $schema->getClientsViewRedirectUrisColumn();
        $clientsScopes    = $schema->getClientsScopesTable();
        $clientsUris      = $schema->getRedirectUrisTable();
        $clients          = $schema->getClientsTable();
        $clientsClientId  = $schema->getClientsIdentityColumn();
        $clScopesClientId = $schema->getClientsScopesClientIdentityColumn();
        $clUrisClientId   = $schema->getRedirectUrisClientIdentityColumn();
        $urisValue        = $schema->getRedirectUrisValueColumn();
        $scopesScopeId    = $schema->getScopesIdentityColumn();
        $sql              = <<< EOT
CREATE VIEW {$view} AS
    SELECT
      c.*,
      array_remove(array_agg(s.{$scopesScopeId}), NULL) AS {$scopes},
      array_remove(array_agg(u.{$urisValue}), NULL)     AS {$redirectUris}
    FROM {$clients} AS c
      LEFT JOIN {$clientsScopes} AS s ON c.{$clientsClientId} = s.{$clScopesClientId}
      LEFT JOIN {$clientsUris}   AS u ON c.{$clientsClientId} = u.{$clUrisClientId}
    GROUP BY c.{$clientsClientId};
EOT;
        $connection->exec($sql);
    }
875811
public static function createObject(
        Connection $connection = null,
        Model $model = null,
        Query $query = null,
        $kwargs = []
    ) {
        return new static($connection, $model, $query, $kwargs);
    }
940236
public function on($connection)
    {
        $this->setDatabase($connection);
        $this->_connection = null;

        $this->setDatabaseConnection($this->getDatabase());

        return $this;
    }
236889
protected function updateConnection($connection)
    {
        if ($this->connection !== $connection) {
            $this->connection  = $connection;
            $this->schemaSetUp = false;
        }

        return $this;
    }
306536
public DatabaseConnectionRequest<T, U> setConnection(String connection) {
        request.addParameter(ParameterBuilder.CONNECTION_KEY, connection);
        return this;
    }
312041
protected function createClientsScopesTable(Connection $connection, DatabaseSchemaInterface $schema): void
    {
        $manager = $connection->getSchemaManager();

        $table = new Table($schema->getClientsScopesTable());
        $table->addColumn($schema->getClientsScopesIdentityColumn(), Type::INTEGER)
            ->setNotnull(true)->setAutoincrement(true)->setUnsigned(true);
        $table->addColumn($schema->getClientsScopesClientIdentityColumn(), Type::STRING)->setNotnull(true);
        $table->addColumn($schema->getClientsScopesScopeIdentityColumn(), Type::STRING)->setNotnull(true);
        $table->setPrimaryKey([$schema->getClientsScopesIdentityColumn()]);
        $table->addUniqueIndex([
            $schema->getClientsScopesClientIdentityColumn(),
            $schema->getClientsScopesScopeIdentityColumn()
        ]);

        $table->addForeignKeyConstraint(
            $schema->getClientsTable(),
            [$schema->getClientsScopesClientIdentityColumn()],
            [$schema->getClientsIdentityColumn()],
            $this->getOnDeleteCascadeConstraint()
        );

        $table->addForeignKeyConstraint(
            $schema->getScopesTable(),
            [$schema->getClientsScopesScopeIdentityColumn()],
            [$schema->getScopesIdentityColumn()],
            $this->getOnDeleteCascadeConstraint()
        );

        $manager->dropAndCreateTable($table);
    }
615157
public function connect($host, $port, Connection $connection, $timeout = 5)
    {
        $deferred = new Deferred();
        $this->disconnect()
            ->then(function () use ($deferred, $host, $port, $connection, $timeout) {
                $this->establishConnection($host, $port, $connection, $timeout)
                    ->then(function () use ($deferred) {
                        $deferred->resolve($this);
                    })
                    ->otherwise(function (\Exception $error) use ($deferred) {
                        $deferred->reject($error);
                    });
            })
            ->otherwise(function () use ($deferred) {
                $deferred->reject($this);
            });

        return $deferred->promise();
    }
1024321
protected function processException(RequestException $exception): void
    {
        $requestContents = $this->getContentsFromMessage($exception->getRequest());
        $responseContents = $this->getContentsFromMessage($exception->getResponse());

        if ($exception instanceof ConnectException) {
            throw new ConnectionException($exception->getMessage(), $requestContents, $exception);
        } else {
            $message = $this->extractMessageFromErrorResponse($responseContents, $exception->getMessage());
            $statusCode = 0;
            if ($exception->getResponse() instanceof PsrResponseInterface) {
                $statusCode = $exception->getResponse()->getStatusCode();
            }

            throw $this->createApiClientException($statusCode, $message, $requestContents, $responseContents);
        }
    }