2017-05-26 21:41:02 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
use Amp\Postgres;
|
|
|
|
|
|
|
|
Amp\Loop::run(function () {
|
|
|
|
$pool = Postgres\pool('host=localhost user=postgres');
|
|
|
|
|
|
|
|
yield $pool->query('DROP TABLE IF EXISTS test');
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Transaction $transaction */
|
|
|
|
$transaction = yield $pool->transaction();
|
|
|
|
|
|
|
|
yield $transaction->query('CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))');
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Statement $statement */
|
|
|
|
$statement = yield $transaction->prepare('INSERT INTO test VALUES ($1, $2)');
|
|
|
|
|
|
|
|
yield $statement->execute('amphp', 'org');
|
|
|
|
yield $statement->execute('google', 'com');
|
|
|
|
yield $statement->execute('github', 'com');
|
|
|
|
|
2017-12-03 02:35:49 +01:00
|
|
|
/** @var \Amp\Postgres\ResultSet $result */
|
2017-05-26 21:41:02 +02:00
|
|
|
$result = yield $transaction->execute('SELECT * FROM test WHERE tld = $1', 'com');
|
|
|
|
|
|
|
|
$format = "%-20s | %-10s\n";
|
|
|
|
printf($format, 'TLD', 'Domain');
|
|
|
|
while (yield $result->advance()) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
printf($format, $row['domain'], $row['tld']);
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $transaction->rollback();
|
|
|
|
});
|