Don't reprepare already prepared statements.

This commit is contained in:
flash 2019-02-27 13:30:07 +01:00
parent 1b0aae2d8c
commit 86aa0f5cf9

View file

@ -31,6 +31,18 @@ function db_connection(?string $name = null): ?PDO
}
function db_prepare(string $statement, ?string $connection = null, $options = []): PDOStatement
{
static $stmts = [];
$encodedOptions = serialize($options);
if (!empty($stmts[$connection][$statement][$encodedOptions])) {
return $stmts[$connection][$statement][$encodedOptions];
}
return $stmts[$connection][$statement][$encodedOptions] = db_prepare_direct($statement, $connection, $options);
}
function db_prepare_direct(string $statement, ?string $connection = null, $options = []): PDOStatement
{
return db_connection($connection)->prepare($statement, $options);
}