Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 19, 2023
1 parent ab7af1c commit ae3d72b
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 104 deletions.
6 changes: 6 additions & 0 deletions src/Internal/AbstractHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Amp\Postgres\ByteA;
use Amp\Postgres\PostgresConfig;
use Amp\Sql\ConnectionException;
use Amp\Sql\Transaction;
use Revolt\EventLoop;

/**
Expand Down Expand Up @@ -37,6 +38,11 @@ public function __destruct()
}
}

public function beginTransaction(): Transaction
{
// TODO: Implement beginTransaction() method.
}

public function getConfig(): PostgresConfig
{
return $this->config;
Expand Down
10 changes: 4 additions & 6 deletions src/PostgresConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Amp\Sql\SqlConnector;
use Amp\Sql\Statement;
use Amp\Sql\Transaction;
use Amp\Sql\TransactionIsolation;
use Amp\Sql\TransactionIsolationLevel;
use function Amp\async;

/**
Expand Down Expand Up @@ -69,10 +67,10 @@ protected function createTransaction(Transaction $transaction, \Closure $release
/**
* Changes return type to this library's Transaction type.
*/
public function beginTransaction(
TransactionIsolation $isolation = TransactionIsolationLevel::Committed
): PostgresTransaction {
return parent::beginTransaction($isolation);

public function beginTransaction(): PostgresTransaction
{
return parent::beginTransaction();
}

protected function pop(): PostgresHandleConnection
Expand Down
8 changes: 4 additions & 4 deletions src/PostgresExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ interface PostgresExecutor extends Executor, PostgresQuoter
/**
* @return PostgresResult Result object specific to this library.
*/
public function query(string $sql): PostgresResult;
// public function query(string $sql): PostgresResult;

/**
* @return PostgresStatement Statement object specific to this library.
*/
public function prepare(string $sql): PostgresStatement;
// public function prepare(string $sql): PostgresStatement;

/**
* @return PostgresResult Result object specific to this library.
*/
public function execute(string $sql, array $params = []): PostgresResult;
// public function execute(string $sql, array $params = []): PostgresResult;

/**
* @return PostgresTransaction Transaction object specific to this library.
*/
public function beginTransaction(): PostgresTransaction;
// public function beginTransaction(): PostgresTransaction;

/**
* @param non-empty-string $channel Channel name.
Expand Down
24 changes: 12 additions & 12 deletions test/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ abstract class AbstractConnectionTest extends AbstractLinkTest
{
public function testIsClosed()
{
$this->assertFalse($this->link->isClosed());
$this->assertFalse($this->executor->isClosed());
}

public function testConnectionCloseDuringQuery(): void
{
$query = async($this->link->execute(...), 'SELECT pg_sleep(10)');
$close = async($this->link->close(...));
$query = async($this->executor->execute(...), 'SELECT pg_sleep(10)');
$close = async($this->executor->close(...));

$start = \microtime(true);

Expand All @@ -41,14 +41,14 @@ public function testConnectionCloseDuringQuery(): void
public function testListen()
{
$channel = "test";
$listener = $this->link->listen($channel);
$listener = $this->executor->listen($channel);

$this->assertInstanceOf(PostgresListener::class, $listener);
$this->assertSame($channel, $listener->getChannel());

EventLoop::delay(0.1, function () use ($channel): void {
$this->link->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
$this->link->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
$this->executor->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
$this->executor->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
});

$count = 0;
Expand All @@ -68,11 +68,11 @@ public function testListen()
public function testNotify()
{
$channel = "test";
$listener = $this->link->listen($channel);
$listener = $this->executor->listen($channel);

EventLoop::delay(0.1, function () use ($channel) {
$this->link->notify($channel, '0');
$this->link->notify($channel, '1');
$this->executor->notify($channel, '0');
$this->executor->notify($channel, '1');
});

$count = 0;
Expand All @@ -95,18 +95,18 @@ public function testListenOnSameChannel()
$this->expectExceptionMessage('Already listening on channel');

$channel = "test";
Future\await([$this->link->listen($channel), $this->link->listen($channel)]);
Future\await([$this->executor->listen($channel), $this->executor->listen($channel)]);
}

public function testQueryAfterErroredQuery()
{
try {
$result = $this->link->query("INSERT INTO test VALUES ('github', 'com', '{1, 2, 3}', true, 4.2)");
$result = $this->executor->query("INSERT INTO test VALUES ('github', 'com', '{1, 2, 3}', true, 4.2)");
} catch (QueryExecutionError $exception) {
// Expected exception due to duplicate key.
}

$result = $this->link->query("INSERT INTO test VALUES ('gitlab', 'com', '{1, 2, 3}', true, 4.2)");
$result = $this->executor->query("INSERT INTO test VALUES ('gitlab', 'com', '{1, 2, 3}', true, 4.2)");

$this->assertSame(1, $result->getRowCount());
}
Expand Down
Loading

0 comments on commit ae3d72b

Please sign in to comment.