-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core-Database] added 2 new functions to help avoid code duplication #3428
Merged
driusan
merged 5 commits into
aces:major
from
ridz1208:2018_01_19_add_database_select_with_indexing
Feb 9, 2018
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -744,6 +744,77 @@ class Database | |
return $result; | ||
} | ||
|
||
/** | ||
* Runs an SQL select statement as a prepared query and re-indexes | ||
* the results using the given primary key. | ||
* | ||
* @param string $query The SQL SELECT query to be run | ||
* @param array $params Values to use for binding to the prepared statement | ||
* @param string $uniqueKey Key to use when re-indexing, this key must be a | ||
* single column, must be unique and must not be | ||
* nullable | ||
* | ||
* @throws LorisException If the supplied key is empty or null | ||
* @throws DatabaseException If the key is not part of the query itself | ||
* @throws DatabaseException If the key is not unique within the resulting set | ||
* | ||
* @return array An array of arrays containing the data. The outermost array is | ||
* associative and uses the supplied $uniqueKey parameter as | ||
* a key for each of the sub-arrays with the format | ||
* rowPrimaryKey=>rowValuesArray. Each nested array represents | ||
* a row from the returned by the query. Each element in the | ||
* nested array is an associative array representing the row in | ||
* the format ColumnName => Value | ||
*/ | ||
function pselectIndexPrimary($query, $params, $uniqueKey) | ||
{ | ||
if (is_null($uniqueKey) || empty($uniqueKey)) { | ||
throw new LorisException( | ||
"The pselectIndexPrimary() function expects the uniqueKey parameter | ||
to not be null or empty. If re-indexing on the primary key is | ||
not necessary please use the pselect() function instead." | ||
); | ||
} | ||
|
||
$result = $this->pselect($query, $params); | ||
|
||
if (empty($result)) { | ||
return $result; | ||
} | ||
|
||
$filteredResult = array(); | ||
// re-order the return array | ||
foreach ($result as $row) { | ||
// Check first that the row contains the primary key supplied | ||
if (!array_key_exists($uniqueKey, $row)) { | ||
throw new DatabaseException( | ||
"The query supplied to pselectIndexPrimary() does not contain | ||
the unique key to re-index on. Make sure to supply the | ||
appropriate key in the SELECT statement to match the supplied | ||
parameter of this function", | ||
$query | ||
); | ||
} | ||
|
||
// Check that the primary key is indeed unique to avoid overriding data | ||
// in the result array | ||
if (isset($filteredResult[$row[$uniqueKey]])) { | ||
throw new DatabaseException( | ||
"The uniqueKey supplied to pselectIndexPrimary() does not appear | ||
to be unique or is nullable. This function expects the key to | ||
be both UNIQUE and NOT NULL.", | ||
$query | ||
); | ||
} | ||
|
||
// If you get here, we just need to build the new array | ||
$filteredResult[$row[$uniqueKey]] = $row; | ||
unset($filteredResult[$row[$uniqueKey]][$uniqueKey]); | ||
} | ||
|
||
return $filteredResult; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me and will be very useful - this will replace a lot of the work that the biobank DAO is currently doing when querying the 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 | ||
|
@@ -772,6 +843,8 @@ class Database | |
* @param string $query The SQL SELECT query to be run | ||
* @param array $params Values to use for binding to prepared statement | ||
* | ||
* @throws DatabaseException if the query selected more than one column | ||
* | ||
* @return array Associative array of form rowID=>value containing all values | ||
* for the first element of the select statement | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it needs to be a unique non-null column, not necessarily a primary key (which also affects the function name..)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah your correct I had primary key everywhere before then changed it all except here I guess