Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Jul 3, 2021
1 parent 690332e commit edc9b61
Show file tree
Hide file tree
Showing 437 changed files with 1,193 additions and 608 deletions.
4 changes: 4 additions & 0 deletions src/Drivers/SQL/MySQLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
trait MySQLFilter
{
use SQLLambda;

/**
* MySQL-specific SQL filter generation
* @param Event $event Filter event
Expand Down Expand Up @@ -161,6 +163,8 @@ public function mysqlFilter(Event $event): ?bool
break;
}

$this->sqlLambdaFilter($event);

return false;
}
}
4 changes: 4 additions & 0 deletions src/Drivers/SQL/PostgreSQLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
trait PostgreSQLFilter
{
use SQLLambda;

/**
* PostgreSQL-specific SQL filter generation
* @param Event $event Filter event
Expand Down Expand Up @@ -144,6 +146,8 @@ public function pgsqlFilter(Event $event): ?bool
break;
}

$this->sqlLambdaFilter($event);

return false;
}
}
4 changes: 2 additions & 2 deletions src/Drivers/SQL/SQLFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Flat3\Lodata\Expression\Event\ArgumentSeparator;
use Flat3\Lodata\Expression\Event\EndFunction;
use Flat3\Lodata\Expression\Event\EndGroup;
use Flat3\Lodata\Expression\Event\Field;
use Flat3\Lodata\Expression\Event\Literal;
use Flat3\Lodata\Expression\Event\Operator;
use Flat3\Lodata\Expression\Event\Property;
use Flat3\Lodata\Expression\Event\StartFunction;
use Flat3\Lodata\Expression\Event\StartGroup;
use Flat3\Lodata\Expression\Node\Func\StringCollection\Contains;
Expand Down Expand Up @@ -70,7 +70,7 @@ public function filter(Event $event): ?bool

return true;

case $event instanceof Field:
case $event instanceof Property:
/** @var EntityType $type */
$type = $this->getType();

Expand Down
75 changes: 75 additions & 0 deletions src/Drivers/SQL/SQLLambda.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Flat3\Lodata\Drivers\SQL;

use Flat3\Lodata\Drivers\SQLEntitySet;
use Flat3\Lodata\Exception\Internal\NodeHandledException;
use Flat3\Lodata\Expression\Event;
use Flat3\Lodata\Expression\Event\Operator;
use Flat3\Lodata\Expression\Node\Operator\Lambda;
use Flat3\Lodata\NavigationBinding;
use Flat3\Lodata\NavigationProperty;
use Flat3\Lodata\ReferentialConstraint;

trait SQLLambda
{
public function sqlLambdaFilter(Event $event): ?bool
{
switch (true) {
case $event instanceof Operator:
$operator = $event->getNode();

switch (true) {
case $operator instanceof Lambda:
list ($lambdaExpression) = $operator->getArguments();

/** @var NavigationProperty $navigationProperty */
$navigationProperty = $operator->getNavigationProperty()->getValue();

/** @var NavigationBinding $navigationBinding */
$navigationBinding = $this->getBindingByNavigationProperty($navigationProperty);

/** @var SQLEntitySet $targetSet */
$targetSet = $navigationBinding->getTarget();

/** @var ReferentialConstraint[] $constraints */
$constraints = $navigationBinding->getPath()->getConstraints()->all();

$parser = $lambdaExpression->getParser();

while ($constraints) {
$constraint = array_shift($constraints);

$this->addWhere(
sprintf('( %s = %s ( SELECT %s from %s WHERE',
$this->propertyToField($constraint->getProperty()),
$operator instanceof Lambda\Any ? 'ANY' : 'ALL',
$targetSet->propertyToField($constraint->getReferencedProperty()),
$targetSet->getTable(),
)
);

$operatingTargetSet = clone $targetSet;

$parser->pushEntitySet($operatingTargetSet);
$lambdaExpression->compute();
$parser->popEntitySet();

$this->addWhere($operatingTargetSet->where);
$this->parameters = array_merge($this->parameters, $operatingTargetSet->getParameters());

$this->addWhere(')');

if ($constraints) {
$this->addWhere('OR ');
}
}

throw new NodeHandledException();
}
break;
}

return false;
}
}
4 changes: 4 additions & 0 deletions src/Drivers/SQL/SQLServerFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
trait SQLServerFilter
{
use SQLLambda;

/**
* Microsoft SQL Server-specific SQL filter generation
* @param Event $event Filter event
Expand Down Expand Up @@ -139,6 +141,8 @@ public function sqlsrvFilter(Event $event): ?bool
break;
}

$this->sqlLambdaFilter($event);

return false;
}
}
11 changes: 1 addition & 10 deletions src/Drivers/SQL/SQLWhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Flat3\Lodata\Drivers\SQL;

use Flat3\Lodata\DeclaredProperty;
use Flat3\Lodata\Exception\Protocol\InternalServerErrorException;
use Flat3\Lodata\Interfaces\EntitySet\FilterInterface;
use Flat3\Lodata\Interfaces\EntitySet\SearchInterface;
Expand Down Expand Up @@ -42,16 +41,8 @@ protected function generateWhere(): void
$filter = $this->getFilter();
if ($filter->hasValue()) {
$this->whereMaybeAnd();
$validLiterals = [];

/** @var DeclaredProperty $property */
foreach ($this->getType()->getDeclaredProperties() as $property) {
if ($property->isFilterable()) {
$validLiterals[] = (string) $property->getName();
}
}

$this->applyFilterQueryOption($validLiterals);
$this->applyFilterQueryOption();
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/EntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,21 +724,17 @@ public function getSelectedProperties(): ObjectArray

/**
* Apply the filter system query option
* @param array $validLiterals List of valid literals for this entity set
*/
public function applyFilterQueryOption(array $validLiterals = []): void
public function applyFilterQueryOption(): void
{
$filter = $this->getFilter();

if (!$filter->hasValue()) {
return;
}

$parser = new FilterParser($this, $this->getTransaction());

foreach ($validLiterals as $validLiteral) {
$parser->addValidLiteral($validLiteral);
}
$parser = new FilterParser($this->getTransaction());
$parser->pushEntitySet($this);

$tree = $parser->generateTree($filter->getValue());
$tree->compute();
Expand All @@ -755,7 +751,8 @@ public function applySearchQueryOption(): void
return;
}

$parser = new SearchParser($this);
$parser = new SearchParser();
$parser->pushEntitySet($this);

$tree = $parser->generateTree($search->getValue());
$tree->compute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Flat3\Lodata\Expression\Event;

/**
* Field
* Property
* @package Flat3\Lodata\Expression\Event
*/
class Field extends Event
class Property extends Event
{
}
10 changes: 10 additions & 0 deletions src/Expression/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Lexer
public const TIME_OF_DAY = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,12})?';
public const DIGIT = '\d';
public const PATH_SEPARATOR = '/';
public const LAMBDA_VARIABLE = self::IDENTIFIER.'\:';

/**
* The text passed to the Lexer
Expand Down Expand Up @@ -717,6 +718,15 @@ public function qualifiedIdentifier(): string
return $this->expression(self::QUALIFIED_IDENTIFIER);
}

/**
* Maybe match a lambda variable
* @return string|null
*/
public function maybeLambdaVariable(): ?string
{
return $this->maybeExpression(self::LAMBDA_VARIABLE);
}

/**
* Maybe match a string enclosed in matching parentheses
* @return string|null
Expand Down
20 changes: 15 additions & 5 deletions src/Expression/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ abstract class Node
/**
* Captured symbol
* @var string
* @internal
*/
public const symbol = '';

Expand Down Expand Up @@ -96,9 +95,9 @@ public function getValue()

/**
* Set the value of this node
* @param string $value
* @param mixed $value
*/
public function setValue(string $value): void
public function setValue($value): void
{
$this->value = $value;
}
Expand Down Expand Up @@ -145,6 +144,15 @@ public function getArguments(): array
return $this->args;
}

/**
* Get the parser that generated this node
* @return Parser
*/
public function getParser(): Parser
{
return $this->parser;
}

/**
* Handle an expression event
* @param Event $event Event
Expand Down Expand Up @@ -178,8 +186,10 @@ protected function expressionEvent(Event $event): void
);

default:
throw new NotImplementedException('unsupported_expression',
'This entity set does not support the provided expression');
throw new NotImplementedException(
'unsupported_expression',
'This entity set does not support the provided expression'
);
}
}
}
8 changes: 0 additions & 8 deletions src/Expression/Node/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@

use Flat3\Lodata\Expression\Event\Literal as LiteralEvent;
use Flat3\Lodata\Expression\Node;
use Flat3\Lodata\Property;

/**
* Literal
* @package Flat3\Lodata\Expression\Node
*/
abstract class Literal extends Node
{
/**
* Literal property
* @var Property property
* @internal
*/
public const property = null;

public function compute(): void
{
$this->expressionEvent(new LiteralEvent($this));
Expand Down
13 changes: 13 additions & 0 deletions src/Expression/Node/Literal/LambdaVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Flat3\Lodata\Expression\Node\Literal;

use Flat3\Lodata\Expression\Node\Literal;

/**
* Lambda Argument
* @package Flat3\Lodata\Expression\Node\Literal
*/
class LambdaVariable extends Literal
{
}
Loading

0 comments on commit edc9b61

Please sign in to comment.