public function testExpressionWalkerWorks() { $expression = O\Expression::variable(O\Expression::value('foo')); $expressionWalker = new O\DynamicExpressionWalker([O\ValueExpression::getType() => function (O\ValueExpression $expression) { return O\Expression::value('bar'); }]); $newExpression = $expressionWalker->walk($expression); $this->assertNotEquals($expression, $newExpression); $this->assertSame($newExpression->getName()->getValue(), 'bar'); }
public function getSource() { if ($this->isSource()) { return $this; } else { $expression = $this->expression; while ($expression instanceof O\TraversalExpression) { $expression = $expression->getValue(); } if ($expression instanceof O\ValueExpression) { return $expression->getValue(); } else { throw new PinqException('Invalid origin expression: must be instance of %s', O\ValueExpression::getType()); } } }
public function interpretSource(O\Expression $expression) { $isQueryScope = false; $queryableQueryResolver = new O\DynamicExpressionWalker([O\TraversalExpression::getType() => function (O\TraversalExpression $expression, O\ExpressionWalker $self) use(&$isQueryScope) { $expression = $expression->updateValue($self->walk($expression->getValue())); if ($isQueryScope) { return $expression; } else { return $self->walk(O\Expression::value($expression->evaluate($this->evaluationContext))); } }, O\ValueExpression::getType() => function (O\ValueExpression $expression) use(&$isQueryScope) { if ($expression->getValue() instanceof IQueryable) { $isQueryScope = true; } return $expression; }]); $expression = $queryableQueryResolver->walk($expression); if ($isQueryScope) { $this->scopeInterpreter->interpretScope($expression); $this->interpretation->interpretQueryScope($this->getId('source-scope'), $this->scopeInterpreter->getInterpretation()); } else { $this->interpretation->interpretArrayOrIterator($this->getId('source-iterator'), $expression->evaluate($this->evaluationContext)); } }
public function visitValue(O\ValueExpression $expression) { $this->analysis[$expression] = $this->typeSystem->getTypeFromValue($expression->getValue()); }
protected function visitValue(O\ValueExpression $expression) { $value = $expression->getValue(); if (is_bool($value)) { $this->sql .= $value ? 'TRUE' : 'FALSE'; } elseif (is_int($value) || is_float($value)) { $this->sql .= $value; } else { $this->sql .= $this->query->connection->quote($value); } }
public function tryComputeResults(O\Expression $queryExpression, &$results) { if (isset($this->storage[$queryExpression])) { $results = $this->storage[$queryExpression]; return true; } $foundApplicableResults = false; //Searches the query expression tree and checks if any parent expressions have saved results //If so, the expression tree is updated with a Traversable implementation with the saved results $applicableScopeFinder = function (O\Expression $expression, O\ExpressionWalker $self) use(&$foundApplicableResults) { if (isset($this->storage[$expression])) { $foundApplicableResults = true; return O\Expression::value($this->newTraversable($this->storage[$expression])); } if ($expression instanceof O\ValueExpression) { return $expression; } /** @var $expression O\TraversalExpression */ return $expression->updateValue($self->walk($expression->getValue())); }; $traversalWalker = new O\DynamicExpressionWalker([O\TraversalExpression::getType() => $applicableScopeFinder, O\ValueExpression::getType() => $applicableScopeFinder]); $remainingQueryExpression = $traversalWalker->walk($queryExpression); //If found applicable results, execute the updated expression tree against the Traversable //implementation to compute the result of the query. $results = $foundApplicableResults ? $remainingQueryExpression->evaluate() : null; return $foundApplicableResults; }
/** * Compiles the expression tree into debug code. * * @return string */ public final function compileDebug() { return (new DynamicExpressionWalker([ValueExpression::getType() => function (ValueExpression $expression) { $value = $expression->getValue(); return !is_scalar($value) && $value !== null ? Expression::constant('{' . Utilities::getTypeOrClass($expression->getValue()) . '}') : $expression; }]))->walk($this)->compile(); }