28 lines
665 B
PHP
28 lines
665 B
PHP
<?php
|
|
namespace Hanyuu;
|
|
|
|
use Index\Data\IDbConnection;
|
|
use Index\Data\IDbStatement;
|
|
|
|
class StatementCache {
|
|
private array $statements = [];
|
|
|
|
public function __construct(
|
|
private IDbConnection $conn
|
|
) {}
|
|
|
|
public function getStatement(string $name, callable $query): IDbStatement {
|
|
if(array_key_exists($name, $this->statements)) {
|
|
$stmt = $this->statements[$name];
|
|
$stmt->reset();
|
|
} else
|
|
$this->statements[$name] = $stmt = $this->conn->prepare($query());
|
|
|
|
return $stmt;
|
|
}
|
|
|
|
public function clearStatement(string $name): void {
|
|
unset($this->statements[$name]);
|
|
}
|
|
}
|