mirror of
https://github.com/danog/postgres.git
synced 2024-12-14 02:17:27 +01:00
69 lines
1.5 KiB
PHP
69 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
use Amp\Promise;
|
|
|
|
class PgSqlStatement implements Statement {
|
|
/** @var string */
|
|
private $name;
|
|
|
|
/** @var string */
|
|
private $sql;
|
|
|
|
/** @var callable */
|
|
private $execute;
|
|
|
|
/** @var callable */
|
|
private $deallocate;
|
|
|
|
/** @var \Amp\Postgres\Internal\ReferenceQueue */
|
|
private $queue;
|
|
|
|
/**
|
|
* @internal
|
|
*
|
|
* @param string $name
|
|
* @param string $sql
|
|
* @param callable $execute
|
|
* @param callable $deallocate
|
|
*/
|
|
public function __construct(string $name, string $sql, callable $execute, callable $deallocate) {
|
|
$this->name = $name;
|
|
$this->sql = $sql;
|
|
$this->execute = $execute;
|
|
$this->deallocate = $deallocate;
|
|
$this->queue = new Internal\ReferenceQueue;
|
|
}
|
|
|
|
public function __destruct() {
|
|
($this->deallocate)($this->name);
|
|
$this->queue->unreference();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getQuery(): string {
|
|
return $this->sql;
|
|
}
|
|
|
|
/**
|
|
* @param mixed ...$params
|
|
*
|
|
* @return \Amp\Promise<\Amp\Postgres\Result>
|
|
*
|
|
* @throws \Amp\Postgres\FailureException If executing the statement fails.
|
|
*/
|
|
public function execute(array $params = []): Promise {
|
|
return ($this->execute)($this->name, $params);
|
|
}
|
|
|
|
/**
|
|
* @param callable $onDestruct
|
|
*/
|
|
public function onDestruct(callable $onDestruct) {
|
|
$this->queue->onDestruct($onDestruct);
|
|
}
|
|
}
|