Fetching rows as native arrays for better static analysis - #336
Open
wonka007 wants to merge 1 commit into
Open
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Apply the possibility to use fetchAssoc to get native array type of next row to fetchAll without the need to convert from \Nette\Database\Row back to array.
\Nette\Database\Row cannot be typed properly (at least I wasn't able to use something like
/** @var \Nette\Database\Row{id: int} */But this syntax is possible to use with fetchAssoc because that returns array:
/** @var array{id: int, name: string} $this->db->fetchAssoc("SELECT id, name FROM user WHERE id = ?", $user_id);This new proposed function fetchAllArray copies what fetchAll does but without the need to convert array that are retrieved from the driver into \Nette\Database\Row (Arrays::toObject($data, new Row)) while not breaking the iterator over rows with original type.
The result is list which can be typed similarly as example above.
/** @var list<array{id: int, name: string}> $this->db->fetchAssoc("SELECT id, name FROM user");This appoach unclocks new possibilities for static analysis. We have to keep in mind that these hints rely on developer knowledge of the query string used and will likely not be useful for higly dynamic queries where unions should have to be used and checked.
It also saves some performance because convertion to \Nette\Database\Row is not useful most of the time.