Skip to content

Commit 5325de6

Browse files
Bind expressions contain their data; even when added to a new query as an expression
1 parent 142d58c commit 5325de6

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

src/Expressions/BindExpression.php

+33
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,42 @@
44

55
namespace LaravelFreelancerNL\FluentAQL\Expressions;
66

7+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
8+
79
/**
810
* AQL literal expression.
911
*/
1012
class BindExpression extends LiteralExpression implements ExpressionInterface
1113
{
14+
protected string $bindVariable;
15+
16+
protected mixed $data = null;
17+
18+
public function __construct(string $bindVariable, mixed $data = null)
19+
{
20+
$this->bindVariable = $bindVariable;
21+
22+
$this->data = $data;
23+
}
24+
25+
/**
26+
* Compile expression output.
27+
*
28+
* @param QueryBuilder $queryBuilder
29+
* @return string
30+
*/
31+
public function compile(QueryBuilder $queryBuilder): string
32+
{
33+
return $this->bindVariable;
34+
}
35+
36+
public function getBindVariable(): string
37+
{
38+
return $this->bindVariable;
39+
}
40+
41+
public function getData(): mixed
42+
{
43+
return $this->data;
44+
}
1245
}

src/Expressions/LiteralExpression.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LiteralExpression extends Expression implements ExpressionInterface
1717
* @param QueryBuilder $queryBuilder
1818
* @return string
1919
*/
20-
public function compile(QueryBuilder $queryBuilder = null): string
20+
public function compile(QueryBuilder $queryBuilder): string
2121
{
2222
return (string) $this->expression;
2323
}

src/QueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function bind(
203203

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

206-
return new BindExpression($to);
206+
return new BindExpression($to, $data);
207207
}
208208

209209
/**

src/Traits/NormalizesExpressions.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
trait NormalizesExpressions
2020
{
21-
2221
/**
2322
* @param object|array<mixed>|string|int|float|bool|null $data
2423
*/
@@ -36,6 +35,8 @@ public function normalizeArgument(
3635
array|string $allowedExpressionTypes = null
3736
): Expression {
3837
if ($argument instanceof Expression) {
38+
$argument = $this->processBindExpression($argument);
39+
3940
return $argument;
4041
}
4142

@@ -266,4 +267,16 @@ protected function normalizeObject(
266267

267268
return new ObjectExpression($this->normalizeIterable((array) $argument, $allowedExpressionTypes));
268269
}
270+
271+
public function processBindExpression(Expression $argument): Expression
272+
{
273+
if ($argument instanceof BindExpression) {
274+
$bindKey = ltrim($argument->getBindVariable(), '@');
275+
276+
if (!isset($this->binds[$bindKey])) {
277+
$this->binds[$bindKey] = $argument->getData();
278+
}
279+
}
280+
return $argument;
281+
}
269282
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tests\Unit\Expressions;
4+
5+
use LaravelFreelancerNL\FluentAQL\Expressions\BindExpression;
6+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7+
use LaravelFreelancerNL\FluentAQL\Tests\TestCase;
8+
9+
/**
10+
* Class QueryExpressionTest
11+
*
12+
* @covers \LaravelFreelancerNL\FluentAQL\Expressions\NullExpression
13+
*/
14+
class BindExpressionTest extends TestCase
15+
{
16+
public function testBindExpression()
17+
{
18+
$bindVar = '@myBind';
19+
$data = 'myData';
20+
$qb = new QueryBuilder();
21+
$expression = new BindExpression($bindVar, $data);
22+
$result = $expression->compile($qb);
23+
24+
self::assertEquals($bindVar, $result);
25+
self::assertEquals($data, $expression->getData());
26+
}
27+
28+
public function testBindExpressionIsAddedToNewQuery()
29+
{
30+
$bindVar = '@myBind';
31+
$data = 'test';
32+
$bindExpression = new BindExpression($bindVar, $data);
33+
34+
$qb = (new QueryBuilder())->filter('test', '==', $bindExpression);
35+
$qb->get();
36+
37+
$this->assertCount(1, $qb->binds);
38+
$this->assertSame($data, array_shift($qb->binds));
39+
}
40+
}

tests/Unit/SubqueryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testSubQueryWithBinds()
4444
->filter('u.active', '==', 'something to bind')
4545
->return('u._key')
4646
->get();
47+
4748
self::assertEquals(
4849
'FOR u IN users FILTER u.active == @' . $subQuery->getQueryId() . '_1 RETURN u._key',
4950
$subQuery->query

0 commit comments

Comments
 (0)