diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Persisters/SqlExpressionVisitor.php')
-rw-r--r-- | vendor/doctrine/orm/src/Persisters/SqlExpressionVisitor.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Persisters/SqlExpressionVisitor.php b/vendor/doctrine/orm/src/Persisters/SqlExpressionVisitor.php new file mode 100644 index 0000000..df60f6a --- /dev/null +++ b/vendor/doctrine/orm/src/Persisters/SqlExpressionVisitor.php | |||
@@ -0,0 +1,79 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Persisters; | ||
6 | |||
7 | use Doctrine\Common\Collections\Expr\Comparison; | ||
8 | use Doctrine\Common\Collections\Expr\CompositeExpression; | ||
9 | use Doctrine\Common\Collections\Expr\ExpressionVisitor; | ||
10 | use Doctrine\Common\Collections\Expr\Value; | ||
11 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
12 | use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; | ||
13 | use RuntimeException; | ||
14 | |||
15 | use function implode; | ||
16 | use function in_array; | ||
17 | use function is_object; | ||
18 | |||
19 | /** | ||
20 | * Visit Expressions and generate SQL WHERE conditions from them. | ||
21 | */ | ||
22 | class SqlExpressionVisitor extends ExpressionVisitor | ||
23 | { | ||
24 | public function __construct( | ||
25 | private readonly BasicEntityPersister $persister, | ||
26 | private readonly ClassMetadata $classMetadata, | ||
27 | ) { | ||
28 | } | ||
29 | |||
30 | /** Converts a comparison expression into the target query language output. */ | ||
31 | public function walkComparison(Comparison $comparison): string | ||
32 | { | ||
33 | $field = $comparison->getField(); | ||
34 | $value = $comparison->getValue()->getValue(); // shortcut for walkValue() | ||
35 | |||
36 | if ( | ||
37 | isset($this->classMetadata->associationMappings[$field]) && | ||
38 | $value !== null && | ||
39 | ! is_object($value) && | ||
40 | ! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true) | ||
41 | ) { | ||
42 | throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation( | ||
43 | $this->classMetadata->name, | ||
44 | $field, | ||
45 | ); | ||
46 | } | ||
47 | |||
48 | return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator()); | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Converts a composite expression into the target query language output. | ||
53 | * | ||
54 | * @throws RuntimeException | ||
55 | */ | ||
56 | public function walkCompositeExpression(CompositeExpression $expr): string | ||
57 | { | ||
58 | $expressionList = []; | ||
59 | |||
60 | foreach ($expr->getExpressionList() as $child) { | ||
61 | $expressionList[] = $this->dispatch($child); | ||
62 | } | ||
63 | |||
64 | return match ($expr->getType()) { | ||
65 | CompositeExpression::TYPE_AND => '(' . implode(' AND ', $expressionList) . ')', | ||
66 | CompositeExpression::TYPE_OR => '(' . implode(' OR ', $expressionList) . ')', | ||
67 | CompositeExpression::TYPE_NOT => 'NOT (' . $expressionList[0] . ')', | ||
68 | default => throw new RuntimeException('Unknown composite ' . $expr->getType()), | ||
69 | }; | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Converts a value expression into the target query language part. | ||
74 | */ | ||
75 | public function walkValue(Value $value): string | ||
76 | { | ||
77 | return '?'; | ||
78 | } | ||
79 | } | ||