From 0e864b25d79403b1b24fca8cfc31078c56e7f5f0 Mon Sep 17 00:00:00 2001 From: javakky Date: Fri, 12 Mar 2021 12:09:11 +0900 Subject: [PATCH 1/3] Set default fetch style --- src/FakePdoStatementTrait.php | 2 +- src/FakePdoTrait.php | 7 +++++++ src/Php7/FakePdo.php | 6 +++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/FakePdoStatementTrait.php b/src/FakePdoStatementTrait.php index 7c8954b5..a7cf2ade 100644 --- a/src/FakePdoStatementTrait.php +++ b/src/FakePdoStatementTrait.php @@ -26,7 +26,7 @@ trait FakePdoStatementTrait /** * @var int */ - private $fetchMode = \PDO::ATTR_DEFAULT_FETCH_MODE; + private $fetchMode = \PDO::FETCH_BOTH; private $fetchArgument; diff --git a/src/FakePdoTrait.php b/src/FakePdoTrait.php index 1bafae16..c41ca3af 100644 --- a/src/FakePdoTrait.php +++ b/src/FakePdoTrait.php @@ -28,6 +28,9 @@ trait FakePdoTrait */ public $lowercaseResultKeys = false; + /** @var ?int */ + private $default_fetch_mode = null; + /** * @var bool */ @@ -70,6 +73,10 @@ public function setAttribute($key, $value) $this->lowercaseResultKeys = true; } + if ($key === \PDO::ATTR_DEFAULT_FETCH_MODE && is_int($value)) { + $this->default_fetch_mode = $value; + } + if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) { return $this->real->setAttribute($key, $value); } diff --git a/src/Php7/FakePdo.php b/src/Php7/FakePdo.php index 2d7ba3da..11424a67 100644 --- a/src/Php7/FakePdo.php +++ b/src/Php7/FakePdo.php @@ -16,7 +16,11 @@ class FakePdo extends PDO implements FakePdoInterface */ public function prepare($statement, $options = []) { - return new FakePdoStatement($this, $statement, $this->real); + $stmt = new FakePdoStatement($this, $statement, $this->real); + if ($this->default_fetch_mode) { + $stmt->setFetchMode($this->default_fetch_mode); + } + return $stmt; } /** From d6e033afc9f34a72e2eb6b9e8448cb4b4104018c Mon Sep 17 00:00:00 2001 From: javakky Date: Fri, 12 Mar 2021 12:31:03 +0900 Subject: [PATCH 2/3] to camel case --- src/FakePdoTrait.php | 4 ++-- src/Php7/FakePdo.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FakePdoTrait.php b/src/FakePdoTrait.php index c41ca3af..53c76f4b 100644 --- a/src/FakePdoTrait.php +++ b/src/FakePdoTrait.php @@ -29,7 +29,7 @@ trait FakePdoTrait public $lowercaseResultKeys = false; /** @var ?int */ - private $default_fetch_mode = null; + private $defaultFetchMode = null; /** * @var bool @@ -74,7 +74,7 @@ public function setAttribute($key, $value) } if ($key === \PDO::ATTR_DEFAULT_FETCH_MODE && is_int($value)) { - $this->default_fetch_mode = $value; + $this->defaultFetchMode = $value; } if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) { diff --git a/src/Php7/FakePdo.php b/src/Php7/FakePdo.php index 11424a67..e579d667 100644 --- a/src/Php7/FakePdo.php +++ b/src/Php7/FakePdo.php @@ -17,8 +17,8 @@ class FakePdo extends PDO implements FakePdoInterface public function prepare($statement, $options = []) { $stmt = new FakePdoStatement($this, $statement, $this->real); - if ($this->default_fetch_mode) { - $stmt->setFetchMode($this->default_fetch_mode); + if ($this->defaultFetchMode) { + $stmt->setFetchMode($this->defaultFetchMode); } return $stmt; } From 09f533efbb8a62eebd7dc5507da8e6f61b0e3429 Mon Sep 17 00:00:00 2001 From: javakky Date: Fri, 12 Mar 2021 16:00:14 +0900 Subject: [PATCH 3/3] throw error if invalid type --- src/FakePdoTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FakePdoTrait.php b/src/FakePdoTrait.php index 53c76f4b..e62148f6 100644 --- a/src/FakePdoTrait.php +++ b/src/FakePdoTrait.php @@ -73,7 +73,10 @@ public function setAttribute($key, $value) $this->lowercaseResultKeys = true; } - if ($key === \PDO::ATTR_DEFAULT_FETCH_MODE && is_int($value)) { + if ($key === \PDO::ATTR_DEFAULT_FETCH_MODE) { + if (!is_int($value)) { + throw new \PDOException("SQLSTATE[HY000]: General error: invalid fetch mode type"); + } $this->defaultFetchMode = $value; }