From 4f1d0eaae0e89d594ee879c3041ce96ad93f04b3 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Fri, 29 Mar 2019 15:53:22 +0300 Subject: [PATCH] polishing in database --- src/Config/DatabaseConfig.php | 2 +- src/Driver/Driver.php | 29 ++++--------------- .../Postgres/Query/PostgresInsertQuery.php | 12 +++++--- src/Query/SelectQuery.php | 22 ++++++++++++-- src/Statement.php | 2 ++ tests/Database/QueryCacheTest.php | 14 --------- 6 files changed, 36 insertions(+), 45 deletions(-) delete mode 100644 tests/Database/QueryCacheTest.php diff --git a/src/Config/DatabaseConfig.php b/src/Config/DatabaseConfig.php index 82c03d17..c9934227 100644 --- a/src/Config/DatabaseConfig.php +++ b/src/Config/DatabaseConfig.php @@ -118,7 +118,7 @@ public function hasDriver(string $driver): bool public function getDriver(string $driver): Autowire { if (!$this->hasDriver($driver)) { - throw new ConfigException("Undefined driver `{$driver}`."); + throw new ConfigException("Undefined driver `{$driver}`"); } $config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver]; diff --git a/src/Driver/Driver.php b/src/Driver/Driver.php index 77b045bf..7ca56dce 100644 --- a/src/Driver/Driver.php +++ b/src/Driver/Driver.php @@ -59,9 +59,6 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface // allow reconnects 'reconnect' => false, - // when set to true Driver will cache prepared statements - 'queryCache' => false, - //All datetime objects will be converted relative to this timezone (must match with DB timezone!) 'timezone' => 'UTC', @@ -85,13 +82,6 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface */ private $transactionLevel = 0; - /** - * Reusable query cache. - * - * @var Statement[] - */ - private $queryCache = []; - /** * @param array $options */ @@ -191,7 +181,6 @@ public function disconnect() { $this->pdo = null; $this->transactionLevel = 0; - $this->queryCache = []; } /** @@ -280,8 +269,10 @@ protected function statement(string $query, array $parameters = [], bool $retry } try { + $flatten = $this->flattenParameters($parameters); + //Mounting all input parameters - $statement = $this->bindParameters($this->prepare($query), $this->flattenParameters($parameters)); + $statement = $this->bindParameters($this->prepare($query), $flatten); $statement->execute(); if ($this->isProfiling()) { @@ -315,19 +306,11 @@ protected function statement(string $query, array $parameters = [], bool $retry /** * @param string $query - * @return Statement + * @return \PDOStatement */ - protected function prepare(string $query): Statement + protected function prepare(string $query): \PDOStatement { - if (!$this->options['queryCache']) { - return $this->getPDO()->prepare($query); - } - - if (isset($this->queryCache[$query])) { - return $this->queryCache[$query]; - } - - return $this->queryCache[$query] = $this->getPDO()->prepare($query); + return $this->getPDO()->prepare($query); } /** diff --git a/src/Driver/Postgres/Query/PostgresInsertQuery.php b/src/Driver/Postgres/Query/PostgresInsertQuery.php index 4b621684..a5c67239 100644 --- a/src/Driver/Postgres/Query/PostgresInsertQuery.php +++ b/src/Driver/Postgres/Query/PostgresInsertQuery.php @@ -53,11 +53,15 @@ public function run() { $result = $this->driver->query($this->sqlStatement(), $this->getParameters()); - if ($this->getPrimaryKey($this->compiler->getPrefix(), $this->table) !== null) { - return (int)$result->fetchColumn(); - } + try { + if ($this->getPrimaryKey($this->compiler->getPrefix(), $this->table) !== null) { + return (int)$result->fetchColumn(); + } - return null; + return null; + } finally { + $result->close(); + } } /** diff --git a/src/Query/SelectQuery.php b/src/Query/SelectQuery.php index c81e6867..e8a3282d 100644 --- a/src/Query/SelectQuery.php +++ b/src/Query/SelectQuery.php @@ -328,7 +328,12 @@ public function count(string $column = '*'): int $select->ordering = []; $select->grouping = []; - return (int)$select->run()->fetchColumn(); + $st = $select->run(); + try { + return (int)$st->fetchColumn(); + } finally { + $st->close(); + } } /** @@ -362,7 +367,13 @@ public function __call($method, array $arguments) //To be escaped in compiler $select->columns = ["{$method}({$arguments[0]})"]; - $result = $select->run()->fetchColumn(); + $st = $select->run(); + + try { + $result = $st->fetchColumn(); + } finally { + $st->close(); + } //Selecting type between int and float if ((float)$result == $result && (int)$result != $result) { @@ -415,6 +426,11 @@ public function sqlStatement(CompilerInterface $compiler = null): string */ public function fetchAll(): array { - return $this->getIterator()->fetchAll(\PDO::FETCH_ASSOC); + $st = $this->run(); + try { + return $st->fetchAll(\PDO::FETCH_ASSOC); + } finally { + $st->close(); + } } } diff --git a/src/Statement.php b/src/Statement.php index 5df146f7..bc3fe6b3 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -13,6 +13,8 @@ /** * Adds few quick methods to PDOStatement and fully compatible with it. By default uses * PDO::FETCH_ASSOC mode. + * + * @internal */ final class Statement extends PDOStatement { diff --git a/tests/Database/QueryCacheTest.php b/tests/Database/QueryCacheTest.php deleted file mode 100644 index 0d7cde46..00000000 --- a/tests/Database/QueryCacheTest.php +++ /dev/null @@ -1,14 +0,0 @@ -