From c3eb61d4497fe53a45d0a8c09a8c39a786c501d4 Mon Sep 17 00:00:00 2001 From: leroy0 Date: Wed, 18 Jan 2017 22:56:40 +0300 Subject: [PATCH] Db exceptions, query with binding --- src/Db.php | 79 +++++++++++++++++++------- src/Db/Exception.php | 38 +++++++++++++ src/Db/IntegrityViolationException.php | 30 ++++++++++ src/Db/Statement.php | 21 +++++-- 4 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 src/Db/Exception.php create mode 100644 src/Db/IntegrityViolationException.php diff --git a/src/Db.php b/src/Db.php index 2f3f1810f..8f35d3cb7 100644 --- a/src/Db.php +++ b/src/Db.php @@ -28,6 +28,8 @@ namespace TorrentPier; use \PDO; use \PDOException; use \PDOStatement; +use TorrentPier\Db\Exception; +use TorrentPier\Db\IntegrityViolationException; class Db extends PDO { @@ -120,39 +122,78 @@ class Db extends PDO $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) { - return parent::prepare($statement); + try { + if ($this->stat) { + $t = microtime(true); + } + return parent::prepare($statement, $options); + } catch (PDOException $e) { + throw new Exception($e); + } finally { + if (isset($t)) { + $this->sqlTimeTotal += microtime(true) - $t; + } } - $t = microtime(true); - $ret = parent::prepare($statement); - $this->sqlTimeTotal += microtime(true) - $t; - return $ret; } + /** @noinspection PhpSignatureMismatchDuringInheritanceInspection */ + /** + * @param string $statement + * @param array ...$args + * @return PDOStatement + */ public function query($statement, ...$args) { - if (!$this->stat) { + try { + if ($this->stat) { + $t = microtime(true); + } + 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->numQueries++; + } } - $t = microtime(true); - $ret = parent::query($statement, ...$args); - $this->sqlTimeTotal += microtime(true) - $t; - $this->numQueries++; - return $ret; } public function exec($statement) { - if (!$this->stat) { + try { + if ($this->stat) { + $t = microtime(true); + } 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->numQueries++; + } } - $t = microtime(true); - $ret = parent::exec($statement); - $this->sqlTimeTotal += microtime(true) - $t; - $this->numQueries++; - return $ret; } /** @deprecated diff --git a/src/Db/Exception.php b/src/Db/Exception.php new file mode 100644 index 000000000..d8e64e2b7 --- /dev/null +++ b/src/Db/Exception.php @@ -0,0 +1,38 @@ +getMessage(), 0, $exception); + $this->code = $exception->getCode(); + $this->errorInfo = $exception->errorInfo; + } +} diff --git a/src/Db/IntegrityViolationException.php b/src/Db/IntegrityViolationException.php new file mode 100644 index 000000000..66e92fd2e --- /dev/null +++ b/src/Db/IntegrityViolationException.php @@ -0,0 +1,30 @@ +db->stat) { + try { + if ($this->db->stat) { + $t = microtime(true); + } 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->numQueries++; + } } - $t = microtime(true); - $ret = parent::execute($input_parameters); - $this->db->sqlTimeTotal += microtime(true) - $t; - $this->db->numQueries++; - return $ret; } }