2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2019-09-26 22:26:30 +02:00
|
|
|
use Amp\Loop;
|
2017-11-06 01:12:12 +01:00
|
|
|
use Amp\Postgres\Link;
|
2019-09-26 22:26:30 +02:00
|
|
|
use Amp\Postgres\PqBufferedResultSet;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Postgres\PqConnection;
|
2019-09-26 22:26:30 +02:00
|
|
|
use Amp\Postgres\PqUnbufferedResultSet;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2016-09-20 07:47:16 +02:00
|
|
|
/**
|
|
|
|
* @requires extension pq
|
|
|
|
*/
|
2018-07-01 19:33:12 +02:00
|
|
|
class PqConnectionTest extends AbstractConnectionTest
|
|
|
|
{
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var resource PostgreSQL connection resource. */
|
|
|
|
protected $handle;
|
|
|
|
|
2018-07-01 19:33:12 +02:00
|
|
|
public function createLink(string $connectionString): Link
|
|
|
|
{
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->handle = new \pq\Connection($connectionString);
|
2017-11-06 01:37:15 +01:00
|
|
|
$this->handle->nonblocking = true;
|
|
|
|
$this->handle->unbuffered = true;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-05-26 23:26:25 +02:00
|
|
|
$this->handle->exec("DROP TABLE IF EXISTS test");
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$result = $this->handle->exec("CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))");
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
if (!$result) {
|
|
|
|
$this->fail('Could not create test table.');
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
foreach ($this->getData() as $row) {
|
|
|
|
$result = $this->handle->execParams("INSERT INTO test VALUES (\$1, \$2)", $row);
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
if (!$result) {
|
|
|
|
$this->fail('Could not insert test data.');
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
return new PqConnection($this->handle);
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function tearDown(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->handle->exec("ROLLBACK");
|
|
|
|
$this->handle->exec("DROP TABLE test");
|
|
|
|
}
|
2019-09-26 22:26:30 +02:00
|
|
|
|
|
|
|
public function testBufferedResults()
|
|
|
|
{
|
|
|
|
Loop::run(function () {
|
|
|
|
\assert($this->connection instanceof PqConnection);
|
|
|
|
$this->connection->shouldBufferResults();
|
|
|
|
|
|
|
|
$this->assertTrue($this->connection->isBufferingResults());
|
|
|
|
|
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
\assert($result instanceof PqBufferedResultSet);
|
|
|
|
|
2020-05-23 00:53:14 +02:00
|
|
|
//$this->assertSame(2, $result->getFieldCount());
|
2019-09-26 22:26:30 +02:00
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
2020-05-21 22:44:56 +02:00
|
|
|
for ($i = 0; $row = yield $result->continue(); ++$i) {
|
2019-09-26 22:26:30 +02:00
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testBufferedResults
|
|
|
|
*/
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testUnbufferedResults(): \Generator
|
2019-09-26 22:26:30 +02:00
|
|
|
{
|
2020-02-06 23:34:49 +01:00
|
|
|
\assert($this->connection instanceof PqConnection);
|
|
|
|
$this->connection->shouldNotBufferResults();
|
2019-09-26 22:26:30 +02:00
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
$this->assertFalse($this->connection->isBufferingResults());
|
2019-09-26 22:26:30 +02:00
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
\assert($result instanceof PqUnbufferedResultSet);
|
2019-09-26 22:26:30 +02:00
|
|
|
|
2020-05-23 00:53:14 +02:00
|
|
|
//$this->assertSame(2, $result->getFieldCount());
|
2019-09-26 22:26:30 +02:00
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
$data = $this->getData();
|
2019-09-26 22:26:30 +02:00
|
|
|
|
2020-05-21 22:44:56 +02:00
|
|
|
for ($i = 0; $row = yield $result->continue(); ++$i) {
|
2020-02-06 23:34:49 +01:00
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
2019-09-26 22:26:30 +02:00
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|