Skip to content

Commit

Permalink
Merge pull request #15 from Javakky-pxv/feature/default-fetch-mode
Browse files Browse the repository at this point in the history
Set default fetch style
  • Loading branch information
muglug committed Mar 13, 2021
2 parents 5241021 + 09f533e commit 9afb99f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/FakePdoStatementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait FakePdoStatementTrait
/**
* @var int
*/
private $fetchMode = \PDO::ATTR_DEFAULT_FETCH_MODE;
private $fetchMode = \PDO::FETCH_BOTH;

private $fetchArgument;

Expand Down
10 changes: 10 additions & 0 deletions src/FakePdoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ trait FakePdoTrait
*/
public $lowercaseResultKeys = false;

/** @var ?int */
private $defaultFetchMode = null;

/**
* @var bool
*/
Expand Down Expand Up @@ -70,6 +73,13 @@ public function setAttribute($key, $value)
$this->lowercaseResultKeys = true;
}

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;
}

if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) {
return $this->real->setAttribute($key, $value);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Php7/FakePdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->defaultFetchMode) {
$stmt->setFetchMode($this->defaultFetchMode);
}
return $stmt;
}

/**
Expand Down

0 comments on commit 9afb99f

Please sign in to comment.