Skip to content

Commit

Permalink
Bind expressions contain their data; even when added to a new query a…
Browse files Browse the repository at this point in the history
…s an expression
  • Loading branch information
LaravelFreelancerNL committed Nov 22, 2021
1 parent 142d58c commit 5325de6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/Expressions/BindExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,42 @@

namespace LaravelFreelancerNL\FluentAQL\Expressions;

use LaravelFreelancerNL\FluentAQL\QueryBuilder;

/**
* AQL literal expression.
*/
class BindExpression extends LiteralExpression implements ExpressionInterface
{
protected string $bindVariable;

protected mixed $data = null;

public function __construct(string $bindVariable, mixed $data = null)
{
$this->bindVariable = $bindVariable;

$this->data = $data;
}

/**
* Compile expression output.
*
* @param QueryBuilder $queryBuilder
* @return string
*/
public function compile(QueryBuilder $queryBuilder): string
{
return $this->bindVariable;
}

public function getBindVariable(): string
{
return $this->bindVariable;
}

public function getData(): mixed
{
return $this->data;
}
}
2 changes: 1 addition & 1 deletion src/Expressions/LiteralExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LiteralExpression extends Expression implements ExpressionInterface
* @param QueryBuilder $queryBuilder
* @return string
*/
public function compile(QueryBuilder $queryBuilder = null): string
public function compile(QueryBuilder $queryBuilder): string
{
return (string) $this->expression;
}
Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function bind(

$to = $this->grammar->formatBind($to, false);

return new BindExpression($to);
return new BindExpression($to, $data);
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/Traits/NormalizesExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

trait NormalizesExpressions
{

/**
* @param object|array<mixed>|string|int|float|bool|null $data
*/
Expand All @@ -36,6 +35,8 @@ public function normalizeArgument(
array|string $allowedExpressionTypes = null
): Expression {
if ($argument instanceof Expression) {
$argument = $this->processBindExpression($argument);

return $argument;
}

Expand Down Expand Up @@ -266,4 +267,16 @@ protected function normalizeObject(

return new ObjectExpression($this->normalizeIterable((array) $argument, $allowedExpressionTypes));
}

public function processBindExpression(Expression $argument): Expression
{
if ($argument instanceof BindExpression) {
$bindKey = ltrim($argument->getBindVariable(), '@');

if (!isset($this->binds[$bindKey])) {
$this->binds[$bindKey] = $argument->getData();
}
}
return $argument;
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Expressions/BindExpressionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Unit\Expressions;

use LaravelFreelancerNL\FluentAQL\Expressions\BindExpression;
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
use LaravelFreelancerNL\FluentAQL\Tests\TestCase;

/**
* Class QueryExpressionTest
*
* @covers \LaravelFreelancerNL\FluentAQL\Expressions\NullExpression
*/
class BindExpressionTest extends TestCase
{
public function testBindExpression()
{
$bindVar = '@myBind';
$data = 'myData';
$qb = new QueryBuilder();
$expression = new BindExpression($bindVar, $data);
$result = $expression->compile($qb);

self::assertEquals($bindVar, $result);
self::assertEquals($data, $expression->getData());
}

public function testBindExpressionIsAddedToNewQuery()
{
$bindVar = '@myBind';
$data = 'test';
$bindExpression = new BindExpression($bindVar, $data);

$qb = (new QueryBuilder())->filter('test', '==', $bindExpression);
$qb->get();

$this->assertCount(1, $qb->binds);
$this->assertSame($data, array_shift($qb->binds));
}
}
1 change: 1 addition & 0 deletions tests/Unit/SubqueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testSubQueryWithBinds()
->filter('u.active', '==', 'something to bind')
->return('u._key')
->get();

self::assertEquals(
'FOR u IN users FILTER u.active == @' . $subQuery->getQueryId() . '_1 RETURN u._key',
$subQuery->query
Expand Down

0 comments on commit 5325de6

Please sign in to comment.