Skip to content

Commit

Permalink
Drop do*() private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 4, 2017
1 parent 2d56d53 commit 0226588
Showing 1 changed file with 101 additions and 114 deletions.
215 changes: 101 additions & 114 deletions lib/AbstractPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Amp\Coroutine;
use Amp\Deferred;
use Amp\Promise;
use function Amp\call;

abstract class AbstractPool implements Pool {
use CallableMaker;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function __construct() {
* {@inheritdoc}
*/
public function extractConnection(): Promise {
return \Amp\call(function () {
return call(function () {
$connection = yield from $this->pop();
$this->connections->detach($connection);
return $connection;
Expand Down Expand Up @@ -86,8 +87,6 @@ protected function addConnection(Connection $connection) {
}

/**
* @coroutine
*
* @return \Generator
*
* @resolve \Amp\Postgres\Connection
Expand Down Expand Up @@ -145,170 +144,158 @@ private function push(Connection $connection) {
* {@inheritdoc}
*/
public function query(string $sql): Promise {
return new Coroutine($this->doQuery($sql));
}

private function doQuery(string $sql): \Generator {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();
return call(function () use ($sql) {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();

try {
$result = yield $connection->query($sql);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}
try {
$result = yield $connection->query($sql);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}

if ($result instanceof Operation) {
$result->onDestruct(function () use ($connection) {
if ($result instanceof Operation) {
$result->onDestruct(function () use ($connection) {
$this->push($connection);
});
} else {
$this->push($connection);
});
} else {
$this->push($connection);
}
}

return $result;
return $result;
});
}

/**
* {@inheritdoc}
*/
public function execute(string $sql, array $params = []): Promise {
return new Coroutine($this->doExecute($sql, $params));
}

private function doExecute(string $sql, array $params): \Generator {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();
return call(function () use ($sql, $params) {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();

try {
$result = yield $connection->execute($sql, $params);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}
try {
$result = yield $connection->execute($sql, $params);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}

if ($result instanceof Operation) {
$result->onDestruct(function () use ($connection) {
if ($result instanceof Operation) {
$result->onDestruct(function () use ($connection) {
$this->push($connection);
});
} else {
$this->push($connection);
});
} else {
$this->push($connection);
}
}

return $result;
return $result;
});
}

/**
* {@inheritdoc}
*/
public function prepare(string $sql): Promise {
return new Coroutine($this->doPrepare($sql));
}
return call(function () use ($sql) {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();

private function doPrepare(string $sql): \Generator {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();
try {
/** @var \Amp\Postgres\Statement $statement */
$statement = yield $connection->prepare($sql);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}

try {
/** @var \Amp\Postgres\Statement $statement */
$statement = yield $connection->prepare($sql);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}
$statement->onDestruct(function () use ($connection) {
$this->push($connection);
});

$statement->onDestruct(function () use ($connection) {
$this->push($connection);
return $statement;
});

return $statement;
}

/**
* {@inheritdoc}
*/
public function notify(string $channel, string $payload = ""): Promise {
return new Coroutine($this->doNotify($channel, $payload));
}

private function doNotify(string $channel, string $payload): \Generator {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();
return call(function () use ($channel, $payload) {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();

try {
$result = yield $connection->notify($channel, $payload);
} finally {
$this->push($connection);
}
try {
$result = yield $connection->notify($channel, $payload);
} finally {
$this->push($connection);
}

return $result;
return $result;
});
}

/**
* {@inheritdoc}
*/
public function listen(string $channel): Promise {
return new Coroutine($this->doListen($channel));
}

public function doListen(string $channel): \Generator {
++$this->listenerCount;
return call(function () use ($channel) {
++$this->listenerCount;

if ($this->listeningConnection === null) {
$this->listeningConnection = new Coroutine($this->pop());
}

if ($this->listeningConnection instanceof Promise) {
$this->listeningConnection = yield $this->listeningConnection;
}
if ($this->listeningConnection === null) {
$this->listeningConnection = new Coroutine($this->pop());
}

try {
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->listeningConnection->listen($channel);
} catch (\Throwable $exception) {
if (--$this->listenerCount === 0) {
$connection = $this->listeningConnection;
$this->listeningConnection = null;
$this->push($connection);
if ($this->listeningConnection instanceof Promise) {
$this->listeningConnection = yield $this->listeningConnection;
}
throw $exception;
}

$listener->onDestruct(function () {
if (--$this->listenerCount === 0) {
$connection = $this->listeningConnection;
$this->listeningConnection = null;
$this->push($connection);
try {
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->listeningConnection->listen($channel);
} catch (\Throwable $exception) {
if (--$this->listenerCount === 0) {
$connection = $this->listeningConnection;
$this->listeningConnection = null;
$this->push($connection);
}
throw $exception;
}
});

return $listener;
$listener->onDestruct(function () {
if (--$this->listenerCount === 0) {
$connection = $this->listeningConnection;
$this->listeningConnection = null;
$this->push($connection);
}
});

return $listener;
});
}

/**
* {@inheritdoc}
*/
public function transaction(int $isolation = Transaction::COMMITTED): Promise {
return new Coroutine($this->doTransaction($isolation));
}
return call(function () use ($isolation) {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();

private function doTransaction(int $isolation = Transaction::COMMITTED): \Generator {
/** @var \Amp\Postgres\Connection $connection */
$connection = yield from $this->pop();
try {
/** @var \Amp\Postgres\Transaction $transaction */
$transaction = yield $connection->transaction($isolation);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}

try {
/** @var \Amp\Postgres\Transaction $transaction */
$transaction = yield $connection->transaction($isolation);
} catch (\Throwable $exception) {
$this->push($connection);
throw $exception;
}
$transaction->onDestruct(function () use ($connection) {
$this->push($connection);
});

$transaction->onDestruct(function () use ($connection) {
$this->push($connection);
return $transaction;
});

return $transaction;
}
}

0 comments on commit 0226588

Please sign in to comment.