2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
use pq;
|
|
|
|
|
|
|
|
class PqStatement implements Statement {
|
|
|
|
/** @var \pq\Statement */
|
|
|
|
private $statement;
|
|
|
|
|
|
|
|
/** @var callable */
|
|
|
|
private $execute;
|
|
|
|
|
2017-07-28 06:20:16 +02:00
|
|
|
/** @var callable */
|
|
|
|
private $deallocate;
|
|
|
|
|
2017-11-18 06:55:16 +01:00
|
|
|
/** @var \Amp\Postgres\Internal\ReferenceQueue */
|
|
|
|
private $queue;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
2017-07-27 07:32:34 +02:00
|
|
|
* @internal
|
|
|
|
*
|
2016-09-14 16:27:39 +02:00
|
|
|
* @param \pq\Statement $statement
|
|
|
|
* @param callable $execute
|
2017-07-28 06:20:16 +02:00
|
|
|
* @param callable $deallocate
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-07-29 00:34:43 +02:00
|
|
|
public function __construct(pq\Statement $statement, callable $execute, callable $deallocate) {
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->statement = $statement;
|
|
|
|
$this->execute = $execute;
|
2017-07-28 06:20:16 +02:00
|
|
|
$this->deallocate = $deallocate;
|
2017-11-18 06:55:16 +01:00
|
|
|
$this->queue = new Internal\ReferenceQueue;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function __destruct() {
|
2017-07-29 00:34:43 +02:00
|
|
|
($this->deallocate)($this->statement->name);
|
2017-11-18 06:55:16 +01:00
|
|
|
$this->queue->unreference();
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getQuery(): string {
|
|
|
|
return $this->statement->query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed ...$params
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\Result>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\FailureException If executing the statement fails.
|
|
|
|
*/
|
2017-11-18 04:33:49 +01:00
|
|
|
public function execute(array $params = []): Promise {
|
2017-07-27 07:32:34 +02:00
|
|
|
return ($this->execute)([$this->statement, "execAsync"], $params);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-11-18 06:55:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $onDestruct
|
|
|
|
*/
|
|
|
|
public function onDestruct(callable $onDestruct) {
|
|
|
|
$this->queue->onDestruct($onDestruct);
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
}
|