1: <?php
2:
3: namespace PHPixie\ORM\Planners\Planner;
4:
5: class In extends \PHPixie\ORM\Planners\Planner
6: {
7: protected $conditions;
8: protected $mappers;
9: protected $steps;
10:
11: public function __construct($conditions, $mappers, $steps)
12: {
13: $this->conditions = $conditions;
14: $this->mappers = $mappers;
15: $this->steps = $steps;
16: }
17:
18: public function items($query, $modelName, $items, $plan, $logic = 'and', $negate = false)
19: {
20: $condition = $this->conditions->in($modelName, $items);
21: $condition->setLogic($logic);
22: $condition->setIsNegated($negate);
23:
24: $this->mappers->conditions()->map(
25: $query,
26: $modelName,
27: array($condition),
28: $plan
29: );
30: }
31:
32: public function itemIds($query, $queryField, $repository, $items, $plan, $logic = 'and', $negate = false)
33: {
34: $idField = $repository->config()->idField;
35: $modelQuery = $repository->query()->in($items);
36:
37: $this->databaseModelQuery($query, $queryField, $modelQuery, $idField, $plan, $logic, $negate);
38: }
39:
40: public function databaseModelQuery($query, $queryField, $modelQuery, $modelQueryField, $plan, $logic = 'and', $negate = false)
41: {
42: $queryPlan = $modelQuery->planFind();
43: $plan->appendPlan($queryPlan->requiredPlan());
44: $subquery = $queryPlan->queryStep()->query();
45:
46: $this->subquery($query, $queryField, $subquery, $modelQueryField, $plan, $logic, $negate);
47:
48: }
49:
50: public function result($query, $queryField, $reusableResult, $resultField, $plan, $logic = 'and', $negate = false)
51: {
52: $placeholder = $query->addPlaceholder($logic, $negate);
53: $inStep = $this->steps->in($placeholder, $queryField, $reusableResult, $resultField);
54: $plan->add($inStep);
55: }
56:
57: public function subquery($query, $queryField, $subquery, $subqueryField, $plan, $logic = 'and', $negate = false)
58: {
59: $strategy = $this->selectStrategy($query->connection(), $subquery->connection());
60: $strategy->in($query, $queryField, $subquery, $subqueryField, $plan, $logic, $negate);
61: }
62:
63: protected function selectStrategy($queryConnection, $subqueryConnection)
64: {
65: if (!($queryConnection instanceof \PHPixie\Database\Type\SQL\Connection)) {
66: return $this->strategy('multiquery');
67: }
68:
69: if ($queryConnection !== $subqueryConnection) {
70: return $this->strategy('multiquery');
71: }
72:
73: return $this->strategy('subquery');
74: }
75:
76: protected function buildSubqueryStrategy()
77: {
78: return new \PHPixie\ORM\Planners\Planner\In\Strategy\Subquery();
79: }
80:
81: protected function buildMultiqueryStrategy()
82: {
83: return new \PHPixie\ORM\Planners\Planner\In\Strategy\Multiquery(
84: $this->steps
85: );
86: }
87: }
88: