-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
operator expressions & embedded filters (#13)
* Added ternary operator expression (inline if/else) Added arithmetic operator expression Added embedded filters * code cleanup
- Loading branch information
1 parent
b16de75
commit cc76101
Showing
17 changed files
with
348 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,42 @@ | ||
# Operator expressions | ||
AQL has two special operator expressions that require their own functions: | ||
- Ternary expressions (inline if/else) | ||
- Arithmetic expressions (calculations) | ||
|
||
## if() | ||
``` | ||
if($conditions, $then, $else) | ||
``` | ||
|
||
**Example 1: basic if expression** | ||
``` | ||
$qb = new QueryBuilder(); | ||
$qb->let('x', 5)->return($qb->if(['x', '==', '5'], true, false)); | ||
``` | ||
Resulting AQL: `LET x = 5 RETURN (x == 5) ? true : false` | ||
|
||
[ArangoDB Ternary Operator documentation](https://www.arangodb.com/docs/stable/aql/operators.html#ternary-operator) | ||
|
||
## calc() | ||
``` | ||
calc($leftOperand, $operator, $rightOperand) | ||
``` | ||
The left and right operands can be numbers or should result in a number. | ||
Number strings are considered to be numbers. So the string '5' equals the number 5. | ||
|
||
**Example 1: basic calculation** | ||
``` | ||
$qb = new QueryBuilder(); | ||
$qb->return($qb->calc(3, '*', 3)); | ||
``` | ||
Resulting AQL: `RETURN 3 * 3` | ||
|
||
|
||
**Example 2: calculation with another calculation embedded** | ||
``` | ||
$qb = new QueryBuilder(); | ||
$qb->return($qb->calc(3, '+', $qb->calc(3, '*', 3))); | ||
``` | ||
Resulting AQL: `RETURN 3 + (3 * 3)` | ||
|
||
[ArangoDB Arithmetic Operator documentation](https://www.arangodb.com/docs/stable/aql/operators.html#arithmetic-operators) |
This file contains 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
This file contains 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,44 @@ | ||
<?php | ||
|
||
namespace LaravelFreelancerNL\FluentAQL\AQL; | ||
|
||
use LaravelFreelancerNL\FluentAQL\Expressions\ArithmeticExpression; | ||
use LaravelFreelancerNL\FluentAQL\Expressions\TernaryExpression; | ||
|
||
/** | ||
* Trait hasFunctions. | ||
* | ||
* AQL Function API calls. | ||
*/ | ||
trait HasOperatorExpressions | ||
{ | ||
/** | ||
* Evaluate a condition | ||
* | ||
* @link https://www.arangodb.com/docs/stable/aql/operators.html#ternary-operator | ||
* | ||
* @param $conditions | ||
* @param $then | ||
* @param $else | ||
* @return TernaryExpression | ||
*/ | ||
public function if($conditions, $then, $else) | ||
{ | ||
return new TernaryExpression($conditions, $then, $else); | ||
} | ||
|
||
/** | ||
* Perform an arithmetic operation on two numbers | ||
* | ||
* @link https://www.arangodb.com/docs/stable/aql/operators.html#arithmetic-operators | ||
* | ||
* @param $leftOperand | ||
* @param $operator | ||
* @param $rightOperand | ||
* @return ArithmeticExpression | ||
*/ | ||
public function calc($leftOperand, $operator, $rightOperand) | ||
{ | ||
return new ArithmeticExpression($leftOperand, $operator, $rightOperand); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,75 @@ | ||
<?php | ||
|
||
namespace LaravelFreelancerNL\FluentAQL\Expressions; | ||
|
||
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException; | ||
use LaravelFreelancerNL\FluentAQL\QueryBuilder; | ||
|
||
class ArithmeticExpression extends PredicateExpression implements ExpressionInterface | ||
{ | ||
|
||
protected $calculation = []; | ||
|
||
/** | ||
* Create predicate expression. | ||
* | ||
* @param string $leftOperand | ||
* @param string $rightOperand | ||
* @param string $operator | ||
*/ | ||
public function __construct($leftOperand, $operator, $rightOperand) | ||
{ | ||
$this->calculation = [$leftOperand, $operator, $rightOperand]; | ||
} | ||
|
||
/** | ||
* Compile calculation. | ||
* | ||
* @param QueryBuilder|null $queryBuilder | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public function compile(QueryBuilder $queryBuilder = null): string | ||
{ | ||
$normalizedCalculation = $this->normalizeCalculation($queryBuilder, $this->calculation); | ||
|
||
$leftOperand = $normalizedCalculation['leftOperand']->compile($queryBuilder); | ||
if ($normalizedCalculation['leftOperand'] instanceof ArithmeticExpression) { | ||
$leftOperand = '(' . $leftOperand . ')'; | ||
} | ||
|
||
$rightOperand = $normalizedCalculation['rightOperand']->compile($queryBuilder); | ||
if ($normalizedCalculation['rightOperand'] instanceof ArithmeticExpression) { | ||
$rightOperand = '(' . $rightOperand . ')'; | ||
} | ||
|
||
return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand; | ||
return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand; | ||
} | ||
|
||
/** | ||
* @param QueryBuilder $queryBuilder | ||
* @param array $calculation | ||
* @return mixed | ||
* @throws ExpressionTypeException | ||
*/ | ||
public function normalizeCalculation(QueryBuilder $queryBuilder, array $calculation) | ||
{ | ||
$normalizedCalculation = []; | ||
|
||
$leftOperand = $queryBuilder->normalizeArgument($calculation[0]); | ||
|
||
$arithmeticOperator = '+'; | ||
if ($queryBuilder->grammar->isArithmeticOperator($calculation[1])) { | ||
$arithmeticOperator = $calculation[1]; | ||
} | ||
|
||
$rightOperand = $queryBuilder->normalizeArgument($calculation[2]); | ||
|
||
$normalizedCalculation['leftOperand'] = $leftOperand; | ||
$normalizedCalculation['arithmeticOperator'] = $arithmeticOperator; | ||
$normalizedCalculation['rightOperand'] = $rightOperand; | ||
|
||
return $normalizedCalculation; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.