-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Update doc dql-custom-walkers.rst with an output walker to interpolate parameters into SQL
#12060
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
Open
n0099
wants to merge
7
commits into
doctrine:3.5.x
Choose a base branch
from
n0099:patch-1
base: 3.5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3932974
Update dql-custom-walkers.rst
n0099 705d23d
Add summary about `InterpolateParametersSQLOutputWalker`
n0099 644e27d
Update docs/en/cookbook/dql-custom-walkers.rst
n0099 a9f7859
Apply suggestions from code review
n0099 38dd0cc
s/params/parameters/g
n0099 1cc805f
Create InterpolateParametersSQLOutputWalker.php
n0099 407487c
Update dql-custom-walkers.rst
n0099 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 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
47 changes: 47 additions & 0 deletions
47
docs/en/cookbook/dql-custom-walkers/InterpolateParametersSQLOutputWalker.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
| use Doctrine\DBAL\ArrayParameterType; | ||
| use Doctrine\DBAL\ParameterType; | ||
| use Doctrine\DBAL\Types\BooleanType; | ||
| use Doctrine\DBAL\Types\Exception\ValueNotConvertible; | ||
| use Doctrine\DBAL\Types\Type; | ||
| use Doctrine\ORM\Query\AST; | ||
| use Doctrine\ORM\Query\SqlOutputWalker; | ||
|
|
||
| class InterpolateParametersSQLOutputWalker extends SqlOutputWalker | ||
| { | ||
| /** {@inheritdoc} */ | ||
| public function walkInputParameter(AST\InputParameter $inputParam): string | ||
| { | ||
| $parameter = $this->getQuery()->getParameter($inputParam->name); | ||
| if ($parameter === null) { | ||
| return '?'; | ||
| } | ||
|
|
||
| $value = $parameter->getValue(); | ||
| /** @var ParameterType|ArrayParameterType|int|string $typeName */ | ||
| /** @see \Doctrine\ORM\Query\ParameterTypeInferer::inferType() */ | ||
| $typeName = $parameter->getType(); | ||
| $platform = $this->getConnection()->getDatabasePlatform(); | ||
| $processParameterType = static fn(ParameterType $type) => static fn($value): string => | ||
| (match ($type) { /** @see Type::getBindingType() */ | ||
| ParameterType::NULL => 'NULL', | ||
| ParameterType::INTEGER => $value, | ||
| ParameterType::BOOLEAN => (new BooleanType())->convertToDatabaseValue($value, $platform), | ||
| ParameterType::STRING, ParameterType::ASCII => $platform->quoteStringLiteral($value), | ||
| default => throw new ValueNotConvertible($value, $type->name) | ||
| }); | ||
|
|
||
| if (is_string($typeName) && Type::hasType($typeName)) { | ||
| return Type::getType($typeName)->convertToDatabaseValue($value, $platform); | ||
| } | ||
| if ($typeName instanceof ParameterType) { | ||
| return $processParameterType($typeName)($value); | ||
| } | ||
| if ($typeName instanceof ArrayParameterType && is_array($value)) { | ||
| $type = ArrayParameterType::toElementParameterType($typeName); | ||
| return implode(', ', array_map($processParameterType($type), $value)); | ||
| } | ||
|
|
||
| throw new ValueNotConvertible($value, $typeName); | ||
| } | ||
| } |
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.
You should elaborate here. I think your need is to get a SQL statement that you can replay yourself, with an SQL client. If so, please say so explicitly and clearly.
Uh oh!
There was an error while loading. Please reload this page.
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.
For me there's two main usage of raw SQL: log the SQL being executed to debug possible slow queries in future, and UNION results of multiple DQL with WHERE as only DBAL query supports UNION as in https://stackoverflow.com/questions/4155288/how-to-write-union-in-doctrine-2-0/79647475#79647475 and https://github.com/n0099/open-tbm/blob/c960f09242097937403880a6ebd02ef4946ce3a8/be/src/PostsQuery/QueryResult.php#L219-L237.
Uh oh!
There was an error while loading. Please reload this page.
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.