File tree 6 files changed +90
-3
lines changed 6 files changed +90
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
namespace LaravelFreelancerNL \FluentAQL \Expressions ;
6
6
7
+ use LaravelFreelancerNL \FluentAQL \QueryBuilder ;
8
+
7
9
/**
8
10
* AQL literal expression.
9
11
*/
10
12
class BindExpression extends LiteralExpression implements ExpressionInterface
11
13
{
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
+ }
12
45
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ class LiteralExpression extends Expression implements ExpressionInterface
17
17
* @param QueryBuilder $queryBuilder
18
18
* @return string
19
19
*/
20
- public function compile (QueryBuilder $ queryBuilder = null ): string
20
+ public function compile (QueryBuilder $ queryBuilder ): string
21
21
{
22
22
return (string ) $ this ->expression ;
23
23
}
Original file line number Diff line number Diff line change @@ -203,7 +203,7 @@ public function bind(
203
203
204
204
$ to = $ this ->grammar ->formatBind ($ to , false );
205
205
206
- return new BindExpression ($ to );
206
+ return new BindExpression ($ to, $ data );
207
207
}
208
208
209
209
/**
Original file line number Diff line number Diff line change 18
18
19
19
trait NormalizesExpressions
20
20
{
21
-
22
21
/**
23
22
* @param object|array<mixed>|string|int|float|bool|null $data
24
23
*/
@@ -36,6 +35,8 @@ public function normalizeArgument(
36
35
array |string $ allowedExpressionTypes = null
37
36
): Expression {
38
37
if ($ argument instanceof Expression) {
38
+ $ argument = $ this ->processBindExpression ($ argument );
39
+
39
40
return $ argument ;
40
41
}
41
42
@@ -266,4 +267,16 @@ protected function normalizeObject(
266
267
267
268
return new ObjectExpression ($ this ->normalizeIterable ((array ) $ argument , $ allowedExpressionTypes ));
268
269
}
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
+ }
269
282
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ public function testSubQueryWithBinds()
44
44
->filter ('u.active ' , '== ' , 'something to bind ' )
45
45
->return ('u._key ' )
46
46
->get ();
47
+
47
48
self ::assertEquals (
48
49
'FOR u IN users FILTER u.active == @ ' . $ subQuery ->getQueryId () . '_1 RETURN u._key ' ,
49
50
$ subQuery ->query
You can’t perform that action at this time.
0 commit comments