Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
polishing in database
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Mar 29, 2019
1 parent 17bdcfa commit 4f1d0ea
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Config/DatabaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
29 changes: 6 additions & 23 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand All @@ -85,13 +82,6 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface
*/
private $transactionLevel = 0;

/**
* Reusable query cache.
*
* @var Statement[]
*/
private $queryCache = [];

/**
* @param array $options
*/
Expand Down Expand Up @@ -191,7 +181,6 @@ public function disconnect()
{
$this->pdo = null;
$this->transactionLevel = 0;
$this->queryCache = [];
}

/**
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Driver/Postgres/Query/PostgresInsertQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down
22 changes: 19 additions & 3 deletions src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}
2 changes: 2 additions & 0 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
14 changes: 0 additions & 14 deletions tests/Database/QueryCacheTest.php

This file was deleted.

0 comments on commit 4f1d0ea

Please sign in to comment.