2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-03-17 16:17:24 +01:00
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
class PgSqlStatement implements Statement {
|
2017-07-26 20:49:40 +02:00
|
|
|
/** @var string */
|
|
|
|
private $name;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var string */
|
|
|
|
private $sql;
|
|
|
|
|
|
|
|
/** @var callable */
|
|
|
|
private $execute;
|
|
|
|
|
2017-07-27 07:32:34 +02:00
|
|
|
/** @var callable */
|
|
|
|
private $deallocate;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
2017-07-27 07:32:34 +02:00
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @param string $name
|
2016-09-14 16:27:39 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @param callable $execute
|
2017-07-27 07:32:34 +02:00
|
|
|
* @param callable $deallocate
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-07-27 07:32:34 +02:00
|
|
|
public function __construct(string $name, string $sql, callable $execute, callable $deallocate) {
|
2017-07-26 20:49:40 +02:00
|
|
|
$this->name = $name;
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->sql = $sql;
|
|
|
|
$this->execute = $execute;
|
2017-07-27 07:32:34 +02:00
|
|
|
$this->deallocate = $deallocate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
($this->deallocate)($this->name);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getQuery(): string {
|
|
|
|
return $this->sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function execute(...$params): Promise {
|
2017-07-26 20:49:40 +02:00
|
|
|
return ($this->execute)($this->name, $params);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
}
|