Skip to content

Commit cf56769

Browse files
committed
Merge pull request #141 from phpcr/expression_language
Introduce expression language in UPDATE queries
2 parents cf0154f + eca0508 commit cf56769

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dev-master
88
### Features
99

1010
- [profile:show] Added command to display current profile
11+
- [query:update] Introduced `expr()` function to allow setting poperty values using expression language.
1112

1213
### Bug fixes
1314

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
"name": "phpcr/phpcr-shell",
33
"description": "Shell for PHPCR",
44
"require": {
5-
"symfony/console": "~2.3",
5+
"symfony/console": "~2.4",
66
"jackalope/jackalope": "~1.1",
77
"phpcr/phpcr": "~2.1",
88
"phpcr/phpcr-utils": "~1.2",
99
"symfony/finder": "~2.3",
1010
"symfony/serializer": "~2.3",
1111
"symfony/yaml": "~2.3",
1212
"symfony/dependency-injection": "~2.3",
13+
"symfony/expression-language": "~2.4",
1314
"dantleech/glob-finder": "~1.0"
1415
},
1516
"minimum-stability": "dev",
1617
"require-dev": {
1718
"symfony/symfony": "2.6",
18-
"symfony/process": "~2.3",
19-
"symfony/filesystem": "~2.3",
19+
"symfony/process": "~2.4",
20+
"symfony/filesystem": "~2.4",
2021
"phpunit/phpunit": "~3.7.28",
2122
"behat/behat": "~3.0.0",
2223
"phpspec/phpspec": "2.0",

features/all/phpcr_query_update.feature

+27
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+
When I execute the "<query>" command
159+
Then the command should not fail
160+
And I save the session
161+
Then 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+
When 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+
Then the command should not fail
177+
And I save the session
178+
Then 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+
"""

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

+1-1
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

+8
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

+10-1
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,7 +26,7 @@ class UpdateProcessor
2526
*/
2627
private $functionMap = array();
2728

28-
public function __construct()
29+
public function __construct(ExpressionLanguage $expressionLanguage)
2930
{
3031
$this->functionMapApply = array(
3132
'mixin_add' => function ($operand, $row, $mixinName) {
@@ -42,6 +43,14 @@ public function __construct()
4243
);
4344

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

src/PHPCR/Shell/Test/ContextBase.php

+8
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)