Commit 579ff523 by pupi1985 Committed by Scott

Turn DBResult methods into an OrFail approach and apply exceptions

parent 0bcca19d
...@@ -20,6 +20,7 @@ namespace Q2A\Database; ...@@ -20,6 +20,7 @@ namespace Q2A\Database;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use Q2A\Database\Exceptions\ReadingFromEmptyResultException;
class DbResult class DbResult
{ {
...@@ -32,12 +33,24 @@ class DbResult ...@@ -32,12 +33,24 @@ class DbResult
/** /**
* Return the first row from the results as an array of [column name] => [column value]. * Return the first row from the results as an array of [column name] => [column value].
* @param bool $allowempty If false, throw a fatal error if there is no result.
* @return array|null * @return array|null
*/ */
public function fetchNextAssoc($allowempty = false) public function fetchNextAssoc()
{ {
return $this->handleResult($this->stmt->fetch(PDO::FETCH_ASSOC), $allowempty, 'Reading one assoc from invalid result'); $data = $this->stmt->fetch();
return $data === false ? null : $data;
}
/**
* Return the first row from the results as an array of [column name] => [column value]. If no value is fetched
* from the database then an exception is thrown.
* @return array|null
* @throws ReadingFromEmptyResultException
*/
public function fetchNextAssocOrFail()
{
return $this->getResultOrFail($this->stmt->fetch(PDO::FETCH_ASSOC));
} }
/** /**
...@@ -62,14 +75,28 @@ class DbResult ...@@ -62,14 +75,28 @@ class DbResult
} }
/** /**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries. * Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries. If
* no value is fetched form the database return null.
* @param $col 0-indexed column to select from (defaults to first column). * @param $col 0-indexed column to select from (defaults to first column).
* @param bool $allowempty If false, throw a fatal error if there is no result. * @return string
* @return array|null
*/ */
public function fetchOneValue($col = 0, $allowempty = false) public function fetchOneValue($col = 0)
{ {
return $this->handleResult($this->stmt->fetchColumn($col), $allowempty, 'Reading one value from empty results'); $data = $this->stmt->fetchColumn($col);
return $data === false ? null : $data;
}
/**
* Return a specific cell from the results. Typically used with (single-row, single-column) aggregate queries. If
* no value is fetched from the database then an exception is thrown.
* @param $col 0-indexed column to select from (defaults to first column).
* @return string
* @throws ReadingFromEmptyResultException
*/
public function fetchOneValueOrFail($col = 0)
{
return $this->getResultOrFail($this->stmt->fetchColumn($col));
} }
/** /**
...@@ -83,23 +110,18 @@ class DbResult ...@@ -83,23 +110,18 @@ class DbResult
} }
/** /**
* Return data or optionally fail if no more rows. * Return data or optionally throw an exception, if data is false (empty).
* @param array $data * @param mixed $data
* @param bool $allowempty
* @param string $errorMsg
* @return mixed * @return mixed
* @throws ReadingFromEmptyResultException
*/ */
private function handleResult($data, $allowempty, $errorMsg) private function getResultOrFail($data)
{ {
if ($data !== false) { if ($data === false) {
return $data; throw new ReadingFromEmptyResultException('Reading one value from empty results');
}
if (!$allowempty) {
qa_fatal_error($errorMsg);
} }
return null; return $data;
} }
/** /**
...@@ -123,7 +145,7 @@ class DbResult ...@@ -123,7 +145,7 @@ class DbResult
/** /**
* Obtain the raw PDOStatement object. * Obtain the raw PDOStatement object.
* @return PDO * @return PDOStatement
*/ */
public function getPDOStatement() public function getPDOStatement()
{ {
......
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Database\Exceptions;
use Q2A\Exceptions\FatalErrorException;
class ReadingFromEmptyResultException extends FatalErrorException
{
/**
* ReadingFromEmptyResultException constructor.
*
* @param string $message
*/
public function __construct($message = 'There has been an attempt to read from an empty database result')
{
parent::__construct($message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment