Skip to content

Commit

Permalink
connection|driver: refactored logging queries from driver [closes #53]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 22, 2017
1 parent 4587ab2 commit 3ed9bde
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/Connection.php
Expand Up @@ -69,13 +69,8 @@ public function connect()
if ($this->connected) {
return;
}
$this->driver->connect($this->config, function (string $sql, bool $onlyLog = false) {
if ($onlyLog) {
$this->fireEvent('onQuery', [$this, $sql]);
return null;
} else {
return $this->nativeQuery($sql);
}
$this->driver->connect($this->config, function (string $sql, float $time, Result $result = null, DriverException $exception = null) {
$this->fireEvent('onQuery', [$this, $sql, $time, $result, $exception]);
});
$this->connected = true;
$this->nestedTransactionsWithSavepoint = (bool) ($this->config['nestedTransactionsWithSavepoint'] ?? true);
Expand Down
15 changes: 11 additions & 4 deletions src/Drivers/Mysqli/MysqliDriver.php
Expand Up @@ -35,7 +35,7 @@ class MysqliDriver implements IDriver
private $connectionTz;

/** @var callable */
private $loggedQueryCallback;
private $onQueryCallback;

/** @var float */
private $timeTaken = 0.0;
Expand All @@ -47,9 +47,9 @@ public function __destruct()
}


public function connect(array $params, callable $loggedQueryCallback)
public function connect(array $params, callable $onQueryCallback)
{
$this->loggedQueryCallback = $loggedQueryCallback;
$this->onQueryCallback = $onQueryCallback;

$host = $params['host'] ?? ini_get('mysqli.default_host');
$port = $params['port'] ?? (int) (ini_get('mysqli.default_port') ?: 3306);
Expand Down Expand Up @@ -375,6 +375,13 @@ protected function createException($error, $errorNo, $sqlState, $query = null)

protected function loggedQuery(string $sql)
{
return ($this->loggedQueryCallback)($sql);
try {
$result = $this->query($sql);
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), $result, null);
return $result;
} catch (DriverException $exception) {
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), null, $exception);
throw $exception;
}
}
}
15 changes: 11 additions & 4 deletions src/Drivers/Pgsql/PgsqlDriver.php
Expand Up @@ -34,7 +34,7 @@ class PgsqlDriver implements IDriver
private $connectionTz;

/** @var callable */
private $loggedQueryCallback;
private $onQueryCallback;

/** @var int */
private $affectedRows = 0;
Expand All @@ -49,14 +49,14 @@ public function __destruct()
}


public function connect(array $params, callable $loggedQueryCallback)
public function connect(array $params, callable $onQueryCallback)
{
static $knownKeys = [
'host', 'hostaddr', 'port', 'dbname', 'user', 'password',
'connect_timeout', 'options', 'sslmode', 'service',
];

$this->loggedQueryCallback = $loggedQueryCallback;
$this->onQueryCallback = $onQueryCallback;

$params = $this->processConfig($params);
$connectionString = '';
Expand Down Expand Up @@ -372,7 +372,14 @@ protected function createException($error, $errorNo, $sqlState, $query = null)

protected function loggedQuery(string $sql)
{
return ($this->loggedQueryCallback)($sql);
try {
$result = $this->query($sql);
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), $result, null);
return $result;
} catch (DriverException $exception) {
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), null, $exception);
throw $exception;
}
}


Expand Down
38 changes: 27 additions & 11 deletions src/Drivers/Sqlsrv/SqlsrvDriver.php
Expand Up @@ -32,7 +32,7 @@ class SqlsrvDriver implements IDriver
private $connection;

/** @var callable */
private $loggedQueryCallback;
private $onQueryCallback;

/** @var int */
private $affectedRows = 0;
Expand All @@ -47,7 +47,7 @@ public function __destruct()
}


public function connect(array $params, callable $loggedQueryCallback)
public function connect(array $params, callable $onQueryCallback)
{
// see https://msdn.microsoft.com/en-us/library/ff628167.aspx
static $knownConnectionOptions = [
Expand All @@ -58,7 +58,7 @@ public function connect(array $params, callable $loggedQueryCallback)
'TransactionIsolation', 'TrustServerCertificate', 'WSID'
];

$this->loggedQueryCallback = $loggedQueryCallback;
$this->onQueryCallback = $onQueryCallback;
$connectionString = isset($params['port']) ? $params['host'] . ',' . $params['port'] : $params['host'];

$connectionOptions = [];
Expand Down Expand Up @@ -187,26 +187,35 @@ public function setTransactionIsolationLevel(int $level)

public function beginTransaction()
{
$this->loggedQuery('BEGIN TRANSACTION', true);
if (!sqlsrv_begin_transaction($this->connection)) {
$time = microtime(true);
$result = sqlsrv_begin_transaction($this->connection);
$timeTaken = microtime(true) - $time;
($this->onQueryCallback)('BEGIN TRANSACTION', $timeTaken, null, null);
if (!$result) {
$this->throwErrors();
}
}


public function commitTransaction()
{
$this->loggedQuery('COMMIT TRANSACTION', true);
if (!sqlsrv_commit($this->connection)) {
$time = microtime(true);
$result = sqlsrv_commit($this->connection);
$timeTaken = microtime(true) - $time;
($this->onQueryCallback)('COMMIT TRANSACTION', $timeTaken, null, null);
if (!$result) {
$this->throwErrors();
}
}


public function rollbackTransaction()
{
$this->loggedQuery('ROLLBACK TRANSACTION', true);
if (!sqlsrv_rollback($this->connection)) {
$time = microtime(true);
$result = sqlsrv_rollback($this->connection);
$timeTaken = microtime(true) - $time;
($this->onQueryCallback)('ROLLBACK TRANSACTION', $timeTaken, null, null);
if (!$result) {
$this->throwErrors();
}
}
Expand Down Expand Up @@ -365,8 +374,15 @@ protected function createException($error, $errorNo, $sqlState, $query = null)
}


protected function loggedQuery(string $sql, bool $onlyLog = false): ?Result
protected function loggedQuery(string $sql)
{
return ($this->loggedQueryCallback)($sql, $onlyLog);
try {
$result = $this->query($sql);
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), $result, null);
return $result;
} catch (DriverException $exception) {
($this->onQueryCallback)($sql, $this->getQueryElapsedTime(), null, $exception);
throw $exception;
}
}
}

0 comments on commit 3ed9bde

Please sign in to comment.