Skip to content

Commit 0565e85

Browse files
committed
Introduce expression language in UPDATE queries
1 parent cf0154f commit 0565e85

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

features/all/phpcr_query_update.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,30 @@ Feature: Execute a a raw UPDATE query in JCR_SQL2
153153
| UPDATE [nt:unstructured] mixin_foo('bar') |
154154
| UPDATE [nt:unstructured] APPLY mixin_foo('bar') |
155155
| UPDATE [nt:unstructured] mixin_foo'bar') |
156+
157+
Scenario Outline: Execute update query with expressions
158+
Given I execute the "<query>" command
159+
Then the command should not fail
160+
And I save the session
161+
And the node at "<path>" should have the property "<property>" with value "<expectedValue>"
162+
And I should see the following:
163+
"""
164+
1 row(s) affected
165+
"""
166+
Examples:
167+
| query | path | property | expectedValue |
168+
| UPDATE [nt:unstructured] AS a SET a.title = expr('row.getNode().getName()') WHERE localname() = 'article1' | /cms/articles/article1 | title | article1 |
169+
| UPDATE [nt:unstructured] AS a SET a.title = expr('row.getPath()') WHERE localname() = 'article1' | /cms/articles/article1 | title | /cms/articles/article1 |
170+
171+
Scenario: Execute an update with a quoted expression (can't do this in Examples above)
172+
Given I execute the following command:
173+
"""
174+
UPDATE [nt:unstructured] AS a SET a.weight = expr('row.getNode().getPropertyValue("weight") * 2') WHERE a.name = 'Product One'
175+
"""
176+
And the command should not fail
177+
And I save the session
178+
And the node at "/cms/products/product1" should have the property "weight" with value "20"
179+
And I should see the following:
180+
"""
181+
1 row(s) affected
182+
"""

features/fixtures/cms.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
<sv:property sv:name="title" sv:type="String">
114114
<sv:value>Article 1</sv:value>
115115
</sv:property>
116+
<sv:property sv:name="number" sv:type="Long">
117+
<sv:value>2</sv:value>
118+
</sv:property>
116119
<sv:property sv:name="tags" sv:type="String" sv:multiple="true">
117120
<sv:value>Planes</sv:value>
118121
<sv:value>Trains</sv:value>

src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function execute(InputInterface $input, OutputInterface $output)
9090
$result = $query->execute();
9191
$rows = 0;
9292

93-
$updateProcessor = new UpdateProcessor();
93+
$updateProcessor = $this->get('query.update.processor');
9494

9595
foreach ($result as $row) {
9696
$rows++;

src/PHPCR/Shell/DependencyInjection/Container.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct($mode = PhpcrShell::MODE_STANDALONE)
4040
$this->registerPhpcr();
4141
$this->registerEvent();
4242
$this->registerConsole();
43+
$this->registerQuery();
4344
}
4445

4546
public function registerHelpers()
@@ -167,6 +168,13 @@ public function registerConsole()
167168
->addArgument(new Reference('phpcr.session'));
168169
}
169170

171+
public function registerQuery()
172+
{
173+
$this->register('query.update.expression_language', 'Symfony\Component\ExpressionLanguage\ExpressionLanguage');
174+
$this->register('query.update.processor', 'PHPCR\Shell\Query\UpdateProcessor')
175+
->addArgument(new Reference('query.update.expression_language'));
176+
}
177+
170178
public function getMode()
171179
{
172180
return $this->mode;

src/PHPCR/Shell/Query/UpdateProcessor.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHPCR\Shell\Query;
1313

1414
use PHPCR\Query\RowInterface;
15+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1516

1617
/**
1718
* Processor for node updates
@@ -25,8 +26,9 @@ class UpdateProcessor
2526
*/
2627
private $functionMap = array();
2728

28-
public function __construct()
29+
public function __construct(ExpressionLanguage $expressionLanguage)
2930
{
31+
3032
$this->functionMapApply = array(
3133
'mixin_add' => function ($operand, $row, $mixinName) {
3234
$node = $row->getNode();
@@ -42,6 +44,14 @@ public function __construct()
4244
);
4345

4446
$this->functionMapSet = array(
47+
'expr' => function ($operand, $row, $expression) use ($expressionLanguage) {
48+
return $expressionLanguage->evaluate(
49+
$expression,
50+
array(
51+
'row' => $row,
52+
)
53+
);
54+
},
4555
'array_replace' => function ($operand, $row, $v, $x, $y) {
4656
$operand->validateScalarArray($v);
4757
foreach ($v as $key => $value) {

src/PHPCR/Shell/Test/ContextBase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ public function iExecuteTheCommand($args)
151151
$this->executeCommand($args);
152152
}
153153

154+
/**
155+
* @Given I execute the following command:
156+
*/
157+
public function iExecuteTheFollowingCommand(PyStringNode $command)
158+
{
159+
$this->executeCommand($command);
160+
}
161+
154162
/**
155163
* @Given /^I execute the following commands:$/
156164
*/

0 commit comments

Comments
 (0)