From 4bd8d192b0bda9228415a34fbd8a3ff581da04ee Mon Sep 17 00:00:00 2001 From: David Blader Date: Wed, 7 Nov 2018 13:55:42 -0500 Subject: [PATCH 01/16] done --- php/libraries/Candidate.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 81e7edeadf5..77ddd1df5b6 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -627,8 +627,8 @@ class Candidate WHERE c.CandID=:cid AND Date_Visit IS NOT NULL ORDER BY Date_Visit"; $where = array('cid' => $candID); - $vLabel = $db->pselectOne($query, $where); - + $vLabels = $db->pselectCol($query, $where); + $vLabel = $vLabels[0] ?? null; return $vLabel; } From c6c52fc91250aec0f87c8fde84b1f250c4e74603 Mon Sep 17 00:00:00 2001 From: David Blader Date: Wed, 7 Nov 2018 13:56:47 -0500 Subject: [PATCH 02/16] trabus --- php/libraries/Candidate.class.inc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 77ddd1df5b6..dcb43897aec 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -622,13 +622,13 @@ class Candidate $candID = $this->getCandID(); - $query = "SELECT Visit_label + $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"; - $where = array('cid' => $candID); + $where = array('cid' => $candID); $vLabels = $db->pselectCol($query, $where); - $vLabel = $vLabels[0] ?? null; + $vLabel = $vLabels[0] ?? null; return $vLabel; } From d4abdf46d47a38e86ea3565b2754714789dcf8bf Mon Sep 17 00:00:00 2001 From: David Blader Date: Wed, 7 Nov 2018 14:22:34 -0500 Subject: [PATCH 03/16] different approach --- php/libraries/Candidate.class.inc | 8 ++++---- php/libraries/Database.class.inc | 33 +++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index dcb43897aec..a19a98a7296 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -622,13 +622,13 @@ class Candidate $candID = $this->getCandID(); - $query = "SELECT Visit_label + $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"; - $where = array('cid' => $candID); - $vLabels = $db->pselectCol($query, $where); - $vLabel = $vLabels[0] ?? null; + $where = array('cid' => $candID); + $vLabel = $db->pselectOne($query, $where, false); + return $vLabel; } diff --git a/php/libraries/Database.class.inc b/php/libraries/Database.class.inc index 76488c250bb..a0960d04d80 100644 --- a/php/libraries/Database.class.inc +++ b/php/libraries/Database.class.inc @@ -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); if (count($rows) > 1) { throw new \DomainException( "Attempt to use pselectRow on a query that returns multiple rows" @@ -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); if (is_array($result) && count($result)) { $result = array_values($result)[0]; } From 224bbb1a8443232618759d0952399ef5e3ea1e4b Mon Sep 17 00:00:00 2001 From: David Blader Date: Wed, 7 Nov 2018 14:26:27 -0500 Subject: [PATCH 04/16] change query --- php/libraries/Candidate.class.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index a19a98a7296..f7b5a9836d3 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -625,7 +625,7 @@ 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"; $where = array('cid' => $candID); $vLabel = $db->pselectOne($query, $where, false); From 3daac8160ca79b6cdaa1292dd4a6e35e3c56331f Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 12 Nov 2018 12:24:40 -0500 Subject: [PATCH 05/16] ugly query in progress --- php/libraries/Candidate.class.inc | 33 ++++++++++++++++++++++++++----- php/libraries/Database.class.inc | 33 ++++++++++--------------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index f7b5a9836d3..7a60b001283 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -621,13 +621,36 @@ class Candidate $db = $factory->database(); $candID = $this->getCandID(); - + // Visits may occur on the same date, so ordering by Date_Visit + // or selecting by MIN(Date_Visit) will not produce a unique result + // as required by pselectOne + // This query creates a row_num variable and simply selects whichever + // one comes first + /*$query = "SELECT Visit_label FROM ( + SELECT Visit_label, + @r:=@r + 1 AS row_num + FROM session s + JOIN candidate c ON (c.CandID = s.CandID) + CROSS JOIN (SELECT @r:=0)t + WHERE c.CandID=:cid AND Date_Visit IS NOT NULL + ORDER BY Date_Visit + ) t1 + where row_num <= 1";*/ + print_r($candID . "\n"); $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 LIMIT 1"; + FROM session + WHERE ID = + (SELECT MIN(ID) FROM + (SELECT ID FROM session + WHERE Date_Visit = + (SELECT MIN(Date_Visit) FROM session s + JOIN candidate c ON s.CandID = c.CandID + WHERE c.CandID=:cid AND Date_Visit IS NOT NULL + ) AS T1 + ) AS T2 + )"; $where = array('cid' => $candID); - $vLabel = $db->pselectOne($query, $where, false); + $vLabel = $db->pselectOne($query, $where); return $vLabel; } diff --git a/php/libraries/Database.class.inc b/php/libraries/Database.class.inc index a0960d04d80..76488c250bb 100644 --- a/php/libraries/Database.class.inc +++ b/php/libraries/Database.class.inc @@ -749,27 +749,17 @@ 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. Limit clause can be disabled by setting false as - * the third argument. + * run for efficiency. * - * @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 + * @param string $query The SQL SELECT query to be run + * @param array $params Values to use for binding to prepared statement * * @return ?array Associative array of form ColumnName => Value for each column * in the first row of the query */ - function pselectRow( - string $query, - array $params, - bool $efficiencyLimit = true - ) : ?array { - - if ($efficiencyLimit) { - $query .= " LIMIT 2"; - } - $rows = $this->pselect($query, $params); + function pselectRow(string $query, array $params) : ?array + { + $rows = $this->pselect($query . " LIMIT 2", $params); if (count($rows) > 1) { throw new \DomainException( "Attempt to use pselectRow on a query that returns multiple rows" @@ -892,17 +882,14 @@ 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 bool $efficiencyLimit Determines whether to use LIMIT clause in - * pselectRow + * @param string $query The SQL statement to run + * @param array $params Values to use for binding in the prepared statement * * @return mixed The value returned by the query. */ - function pselectOne($query, $params, $efficiencyLimit = true) + function pselectOne($query, $params) { - $result = $this->pselectRow($query, $params, $efficiencyLimit); + $result = $this->pselectRow($query, $params); if (is_array($result) && count($result)) { $result = array_values($result)[0]; } From d31db98a452db08266389264c011c2f48457ad21 Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 19 Nov 2018 12:21:22 -0500 Subject: [PATCH 06/16] less ugly query --- php/libraries/Candidate.class.inc | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 7a60b001283..0cff6fc78c4 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -624,30 +624,18 @@ class Candidate // Visits may occur on the same date, so ordering by Date_Visit // or selecting by MIN(Date_Visit) will not produce a unique result // as required by pselectOne - // This query creates a row_num variable and simply selects whichever - // one comes first - /*$query = "SELECT Visit_label FROM ( - SELECT Visit_label, - @r:=@r + 1 AS row_num - FROM session s - JOIN candidate c ON (c.CandID = s.CandID) - CROSS JOIN (SELECT @r:=0)t - WHERE c.CandID=:cid AND Date_Visit IS NOT NULL - ORDER BY Date_Visit - ) t1 - where row_num <= 1";*/ - print_r($candID . "\n"); + // This selects the smallest session ID, in the event that there + // are multiple visits starting on the same day $query = "SELECT Visit_label FROM session - WHERE ID = - (SELECT MIN(ID) FROM - (SELECT ID FROM session + WHERE ID = + (SELECT MIN(ID) FROM session WHERE Date_Visit = (SELECT MIN(Date_Visit) FROM session s JOIN candidate c ON s.CandID = c.CandID WHERE c.CandID=:cid AND Date_Visit IS NOT NULL - ) AS T1 - ) AS T2 + ) + AND CandID=:cid )"; $where = array('cid' => $candID); $vLabel = $db->pselectOne($query, $where); From 2f99c3e87821c27d321cba921870d63af13540b3 Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 19 Nov 2018 12:26:58 -0500 Subject: [PATCH 07/16] remove redundnant join --- php/libraries/Candidate.class.inc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 0cff6fc78c4..efc44e26a5b 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -631,9 +631,9 @@ class Candidate WHERE ID = (SELECT MIN(ID) FROM session WHERE Date_Visit = - (SELECT MIN(Date_Visit) FROM session s - JOIN candidate c ON s.CandID = c.CandID - WHERE c.CandID=:cid AND Date_Visit IS NOT NULL + (SELECT MIN(Date_Visit) FROM session + WHERE CandID=:cid + AND Date_Visit IS NOT NULL ) AND CandID=:cid )"; From 109a97ecc4baf60ea0383259765f87b577ee14a7 Mon Sep 17 00:00:00 2001 From: David Blader Date: Fri, 14 Dec 2018 11:26:22 -0500 Subject: [PATCH 08/16] feedback --- php/libraries/Candidate.class.inc | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index efc44e26a5b..fa47c455800 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -621,24 +621,11 @@ class Candidate $db = $factory->database(); $candID = $this->getCandID(); - // Visits may occur on the same date, so ordering by Date_Visit - // or selecting by MIN(Date_Visit) will not produce a unique result - // as required by pselectOne - // This selects the smallest session ID, in the event that there - // are multiple visits starting on the same day - $query = "SELECT Visit_label - FROM session - WHERE ID = - (SELECT MIN(ID) FROM session - WHERE Date_Visit = - (SELECT MIN(Date_Visit) FROM session - WHERE CandID=:cid - AND Date_Visit IS NOT NULL - ) - AND CandID=:cid - )"; + $query = "SELECT Visit_label FROM session + WHERE CandID = :cid AND Date_visit IS NOT NULL + ORDER BY Date_visit, ID ASC LIMIT 1"; $where = array('cid' => $candID); - $vLabel = $db->pselectOne($query, $where); + $vLabel = $db->pselectCol($query, $where)[0] ?? null; return $vLabel; } From 6640db55a68f033702be2cbfef1ba066ade9ebd2 Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 17 Dec 2018 12:54:15 -0500 Subject: [PATCH 09/16] update return type tag --- php/libraries/Candidate.class.inc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index fa47c455800..ff4b7eaef29 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -613,7 +613,7 @@ class Candidate /** * Returns first visit for a candidate based on Date_visit * - * @return string The visit label for the candidate's first visit + * @return string|null The visit label for the candidate's first visit */ public function getFirstVisit() { @@ -626,7 +626,6 @@ class Candidate ORDER BY Date_visit, ID ASC LIMIT 1"; $where = array('cid' => $candID); $vLabel = $db->pselectCol($query, $where)[0] ?? null; - return $vLabel; } From f60ba7ce702f74d7b05c645fed01729ca96a962d Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 7 Jan 2019 13:37:27 -0500 Subject: [PATCH 10/16] use array shift --- php/libraries/Candidate.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index ff4b7eaef29..fae9850a9c3 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -625,8 +625,8 @@ class Candidate WHERE CandID = :cid AND Date_visit IS NOT NULL ORDER BY Date_visit, ID ASC LIMIT 1"; $where = array('cid' => $candID); - $vLabel = $db->pselectCol($query, $where)[0] ?? null; - return $vLabel; + $result = $db->pselectCol($query, $where); + return array_shift($result); } /** From 65ea200090d30e8f21b996a7955ecf0cf96a890a Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 14 Jan 2019 12:11:47 -0500 Subject: [PATCH 11/16] travis --- php/libraries/Candidate.class.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index fae9850a9c3..340b7fcc10b 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -626,7 +626,8 @@ class Candidate ORDER BY Date_visit, ID ASC LIMIT 1"; $where = array('cid' => $candID); $result = $db->pselectCol($query, $where); - return array_shift($result); + + return is_array($result) ? array_shift($result) : null; } /** From c6451a84a2bd92f7f6ae09fc4724686b0efa09af Mon Sep 17 00:00:00 2001 From: David Blader Date: Thu, 17 Jan 2019 14:30:25 -0500 Subject: [PATCH 12/16] return string only --- php/libraries/Candidate.class.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 340b7fcc10b..d1a653492f4 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -613,7 +613,7 @@ class Candidate /** * Returns first visit for a candidate based on Date_visit * - * @return string|null The visit label for the candidate's first visit + * @return string The visit label for the candidate's first visit */ public function getFirstVisit() { @@ -627,7 +627,7 @@ class Candidate $where = array('cid' => $candID); $result = $db->pselectCol($query, $where); - return is_array($result) ? array_shift($result) : null; + return is_array($result) ? array_shift($result) : ''; } /** From bf286ebcc425e5f99f6c4a57a0c63f100089af52 Mon Sep 17 00:00:00 2001 From: John Saigle <4022790+johnsaigle@users.noreply.github.com> Date: Wed, 23 Jan 2019 16:55:32 -0500 Subject: [PATCH 13/16] Update php/libraries/Candidate.class.inc Co-Authored-By: davidblader --- php/libraries/Candidate.class.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index d1a653492f4..a09209b6a6c 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -627,7 +627,7 @@ class Candidate $where = array('cid' => $candID); $result = $db->pselectCol($query, $where); - return is_array($result) ? array_shift($result) : ''; + return (is_array($result) && count($result) > 0) ? array_shift($result) : ''; } /** From 01e207b876f346e69e1cadbec4884a140515d1a2 Mon Sep 17 00:00:00 2001 From: David Blader Date: Fri, 25 Jan 2019 10:49:12 -0500 Subject: [PATCH 14/16] fix project return type --- php/libraries/Candidate.class.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index d1a653492f4..88e057280f9 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -409,7 +409,7 @@ class Candidate function getProjectTitle() { $ProjectList = Utility::getProjectList(); - return $ProjectList[$this->getProjectID()]; + return $ProjectList[$this->getProjectID()] ?? ''; } /** From bf0bdf10fffef90cfc002c2c38f5f10a0976d23a Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 28 Jan 2019 11:22:05 -0500 Subject: [PATCH 15/16] use visitno column --- php/libraries/Candidate.class.inc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index 26d6e5171fc..dbe1888f04e 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -622,12 +622,13 @@ class Candidate $candID = $this->getCandID(); $query = "SELECT Visit_label FROM session - WHERE CandID = :cid AND Date_visit IS NOT NULL - ORDER BY Date_visit, ID ASC LIMIT 1"; + LEFT JOIN candidate USING (CandID) + WHERE CandID = :cid AND VisitNo = 1 + AND Entity_type <> 'Scanner'"; $where = array('cid' => $candID); - $result = $db->pselectCol($query, $where); - - return (is_array($result) && count($result) > 0) ? array_shift($result) : ''; + $result = $db->pselectOne($query, $where); + echo $result . "\n"; + return $result ?? ''; } /** From c40d6f26463a6191dfbe696ed855604fe201234c Mon Sep 17 00:00:00 2001 From: David Blader Date: Mon, 28 Jan 2019 11:26:02 -0500 Subject: [PATCH 16/16] remove echo --- php/libraries/Candidate.class.inc | 1 - 1 file changed, 1 deletion(-) diff --git a/php/libraries/Candidate.class.inc b/php/libraries/Candidate.class.inc index dbe1888f04e..c2acc33df62 100644 --- a/php/libraries/Candidate.class.inc +++ b/php/libraries/Candidate.class.inc @@ -627,7 +627,6 @@ class Candidate AND Entity_type <> 'Scanner'"; $where = array('cid' => $candID); $result = $db->pselectOne($query, $where); - echo $result . "\n"; return $result ?? ''; }