Db exceptions, query with binding

This commit is contained in:
leroy0 2017-01-18 22:56:40 +03:00
commit c3eb61d449
4 changed files with 143 additions and 25 deletions

View file

@ -28,6 +28,8 @@ namespace TorrentPier;
use \PDO; use \PDO;
use \PDOException; use \PDOException;
use \PDOStatement; use \PDOStatement;
use TorrentPier\Db\Exception;
use TorrentPier\Db\IntegrityViolationException;
class Db extends PDO class Db extends PDO
{ {
@ -120,39 +122,78 @@ class Db extends PDO
$this->setAttribute(static::ATTR_STATEMENT_CLASS, ['\\TorrentPier\\Db\\Statement', [$this]]); $this->setAttribute(static::ATTR_STATEMENT_CLASS, ['\\TorrentPier\\Db\\Statement', [$this]]);
} }
public function prepare($statement, $options = []) public function prepare($statement, /** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
$options = [])
{ {
if (!$this->stat) { try {
return parent::prepare($statement); if ($this->stat) {
}
$t = microtime(true); $t = microtime(true);
$ret = parent::prepare($statement); }
return parent::prepare($statement, $options);
} catch (PDOException $e) {
throw new Exception($e);
} finally {
if (isset($t)) {
$this->sqlTimeTotal += microtime(true) - $t; $this->sqlTimeTotal += microtime(true) - $t;
return $ret; }
}
} }
/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
/**
* @param string $statement
* @param array ...$args
* @return PDOStatement
*/
public function query($statement, ...$args) public function query($statement, ...$args)
{ {
if (!$this->stat) { try {
return parent::query($statement, ...$args); if ($this->stat) {
}
$t = microtime(true); $t = microtime(true);
$ret = parent::query($statement, ...$args); }
if (func_num_args() > 1) {
$input = func_get_arg(1);
if (is_array($input)) {
$stmt = parent::prepare($statement);
if (func_num_args() > 2) {
$stmt->setFetchMode(...array_slice(func_get_args(), 2));
}
$stmt->execute($input);
return $stmt;
}
}
return parent::query($statement, ...$args);
} catch (PDOException $e) {
if ($e->getCode() == '23000') {
throw new IntegrityViolationException($e);
}
throw new Exception($e);
} finally {
if (isset($t)) {
$this->sqlTimeTotal += microtime(true) - $t; $this->sqlTimeTotal += microtime(true) - $t;
$this->numQueries++; $this->numQueries++;
return $ret; }
}
} }
public function exec($statement) public function exec($statement)
{ {
if (!$this->stat) { try {
return parent::exec($statement); if ($this->stat) {
}
$t = microtime(true); $t = microtime(true);
$ret = parent::exec($statement); }
return parent::exec($statement);
} catch (PDOException $e) {
if ($e->getCode() == '23000') {
throw new IntegrityViolationException($e);
}
throw new Exception($e);
} finally {
if (isset($t)) {
$this->sqlTimeTotal += microtime(true) - $t; $this->sqlTimeTotal += microtime(true) - $t;
$this->numQueries++; $this->numQueries++;
return $ret; }
}
} }
/** @deprecated /** @deprecated

38
src/Db/Exception.php Normal file
View file

@ -0,0 +1,38 @@
<?php
/**
* MIT License
*
* Copyright (c) 2005-2017 TorrentPier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace TorrentPier\Db;
use \PDOException;
class Exception extends PDOException
{
public function __construct(PDOException $exception)
{
parent::__construct($exception->getMessage(), 0, $exception);
$this->code = $exception->getCode();
$this->errorInfo = $exception->errorInfo;
}
}

View file

@ -0,0 +1,30 @@
<?php
/**
* MIT License
*
* Copyright (c) 2005-2017 TorrentPier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace TorrentPier\Db;
class IntegrityViolationException extends Exception
{
}

View file

@ -26,6 +26,7 @@
namespace TorrentPier\Db; namespace TorrentPier\Db;
use \PDOStatement; use \PDOStatement;
use \PDOException;
use TorrentPier\Db; use TorrentPier\Db;
class Statement extends PDOStatement class Statement extends PDOStatement
@ -39,13 +40,21 @@ class Statement extends PDOStatement
public function execute($input_parameters = null) public function execute($input_parameters = null)
{ {
if (!$this->db->stat) { try {
return parent::execute($input_parameters); if ($this->db->stat) {
}
$t = microtime(true); $t = microtime(true);
$ret = parent::execute($input_parameters); }
return parent::execute($input_parameters);
} catch (PDOException $e) {
if ($e->getCode() == '23000') {
throw new IntegrityViolationException($e);
}
throw new Exception($e);
} finally {
if (isset($t)) {
$this->db->sqlTimeTotal += microtime(true) - $t; $this->db->sqlTimeTotal += microtime(true) - $t;
$this->db->numQueries++; $this->db->numQueries++;
return $ret; }
}
} }
} }