Don't reprepare already prepared statements.
This commit is contained in:
parent
1b0aae2d8c
commit
86aa0f5cf9
1 changed files with 12 additions and 0 deletions
12
src/db.php
12
src/db.php
|
@ -31,6 +31,18 @@ function db_connection(?string $name = null): ?PDO
|
||||||
}
|
}
|
||||||
|
|
||||||
function db_prepare(string $statement, ?string $connection = null, $options = []): PDOStatement
|
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);
|
return db_connection($connection)->prepare($statement, $options);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue