Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Core: Candidate] Fix getFirstVisit method #4075

Merged
merged 17 commits into from
Jan 28, 2019
4 changes: 2 additions & 2 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,9 @@ class Candidate
$query = "SELECT Visit_label
FROM session s JOIN candidate c ON (c.CandID = s.CandID)
WHERE c.CandID=:cid AND Date_Visit IS NOT NULL
ORDER BY Date_Visit";
ORDER BY Date_Visit LIMIT 1";
davidblader marked this conversation as resolved.
Show resolved Hide resolved
$where = array('cid' => $candID);
$vLabel = $db->pselectOne($query, $where);
$vLabel = $db->pselectOne($query, $where, false);

return $vLabel;
}
Expand Down
33 changes: 23 additions & 10 deletions php/libraries/Database.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -749,17 +749,27 @@ class Database
/**
* Runs a query as a prepared statement and returns the first row as an
* associative array. Automatically adds a limit clause to the query being
* run for efficiency.
* run for efficiency. Limit clause can be disabled by setting false as
* the third argument.
*
* @param string $query The SQL SELECT query to be run
* @param array $params Values to use for binding to prepared statement
* @param string $query The SQL SELECT query to be run
* @param array $params Values to use for binding to prepared
* statement
* @param bool $efficiencyLimit Determines whether to use LIMIT clause or not
*
* @return ?array Associative array of form ColumnName => Value for each column
* in the first row of the query
*/
function pselectRow(string $query, array $params) : ?array
{
$rows = $this->pselect($query . " LIMIT 2", $params);
function pselectRow(
string $query,
array $params,
bool $efficiencyLimit = true
) : ?array {

if ($efficiencyLimit) {
$query .= " LIMIT 2";
}
$rows = $this->pselect($query, $params);
davidblader marked this conversation as resolved.
Show resolved Hide resolved
if (count($rows) > 1) {
throw new \DomainException(
"Attempt to use pselectRow on a query that returns multiple rows"
Expand Down Expand Up @@ -882,14 +892,17 @@ class Database
* Runs a query as a prepared statement and returns the value of the first
* column of the first row
*
* @param string $query The SQL statement to run
* @param array $params Values to use for binding in the prepared statement
* @param string $query The SQL statement to run
* @param array $params Values to use for binding in the prepared
* statement
* @param bool $efficiencyLimit Determines whether to use LIMIT clause in
* pselectRow
*
* @return mixed The value returned by the query.
*/
function pselectOne($query, $params)
function pselectOne($query, $params, $efficiencyLimit = true)
{
$result = $this->pselectRow($query, $params);
$result = $this->pselectRow($query, $params, $efficiencyLimit);
davidblader marked this conversation as resolved.
Show resolved Hide resolved
if (is_array($result) && count($result)) {
$result = array_values($result)[0];
}
Expand Down