Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
579ff523
Commit
579ff523
authored
Jan 07, 2018
by
pupi1985
Committed by
Scott
Jan 13, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Turn DBResult methods into an OrFail approach and apply exceptions
parent
0bcca19d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
21 deletions
+77
-21
DbResult.php
qa-src/Database/DbResult.php
+43
-21
ReadingFromEmptyResultException.php
...c/Database/Exceptions/ReadingFromEmptyResultException.php
+34
-0
No files found.
qa-src/Database/DbResult.php
View file @
579ff523
...
...
@@ -20,6 +20,7 @@ namespace Q2A\Database;
use
PDO
;
use
PDOStatement
;
use
Q2A\Database\Exceptions\ReadingFromEmptyResultException
;
class
DbResult
{
...
...
@@ -32,12 +33,24 @@ class DbResult
/**
* 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
*/
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
}
/**
* 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 bool $allowempty If false, throw a fatal error if there is no result.
* @return array|null
* @return string
*/
public
function
fetchOneValue
(
$col
=
0
)
{
$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
fetchOneValue
(
$col
=
0
,
$allowempty
=
false
)
public
function
fetchOneValue
OrFail
(
$col
=
0
)
{
return
$this
->
handleResult
(
$this
->
stmt
->
fetchColumn
(
$col
),
$allowempty
,
'Reading one value from empty results'
);
return
$this
->
getResultOrFail
(
$this
->
stmt
->
fetchColumn
(
$col
)
);
}
/**
...
...
@@ -83,23 +110,18 @@ class DbResult
}
/**
* Return data or optionally fail if no more rows.
* @param array $data
* @param bool $allowempty
* @param string $errorMsg
* Return data or optionally throw an exception, if data is false (empty).
* @param mixed $data
* @return mixed
* @throws ReadingFromEmptyResultException
*/
private
function
handleResult
(
$data
,
$allowempty
,
$errorMsg
)
private
function
getResultOrFail
(
$data
)
{
if
(
$data
!==
false
)
{
return
$data
;
}
if
(
!
$allowempty
)
{
qa_fatal_error
(
$errorMsg
);
if
(
$data
===
false
)
{
throw
new
ReadingFromEmptyResultException
(
'Reading one value from empty results'
);
}
return
null
;
return
$data
;
}
/**
...
...
@@ -123,7 +145,7 @@ class DbResult
/**
* Obtain the raw PDOStatement object.
* @return PDO
* @return PDO
Statement
*/
public
function
getPDOStatement
()
{
...
...
qa-src/Database/Exceptions/ReadingFromEmptyResultException.php
0 → 100644
View file @
579ff523
<?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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment