Skip to content

Prevent invalid behavior on UPDATE #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ dev-master

### Bug Fixes

- [query] Disabled updating multivalue properties where properties have more
than one value with the UPDATE, as currently other items are overwritten and
data is lost. See: https://github.com/phpcr/phpcr-shell/issues/85
- [shell] Multivalue (and so multiline) property values are truncated as a single string (#70)

alpha-4
Expand Down
17 changes: 17 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,23 @@ public function theNodeAtShouldHaveThePropertyWithValue($arg1, $arg2, $arg3)
PHPUnit_Framework_Assert::assertEquals($arg3, $propertyType);
}

/**
* @Given /^the node at "([^"]*)" should have the property "([^"]*)" with value "([^"]*)" at index "([^"]*)"$/
*/
public function theNodeAtShouldHaveThePropertyWithValueAtIndex($arg1, $arg2, $arg3, $index)
{
$session = $this->getSession();
$node = $session->getNode($arg1);
$property = $node->getProperty($arg2);
if (!$property->isMultiple()) {
throw new \Exception('Property is not multiple and you wanted to check an index');
}

$propertyType = $property->getValue();
PHPUnit_Framework_Assert::assertEquals($arg3, $propertyType[$index]);
}


/**
* @Given /^the property "([^"]*)" should have type "([^"]*)" and value "([^"]*)"$/
*/
Expand Down
8 changes: 8 additions & 0 deletions features/fixtures/cms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
<sv:property sv:name="title" sv:type="String">
<sv:value>Article 1</sv:value>
</sv:property>
<sv:property sv:name="tags" sv:type="String" sv:multiple="true">
<sv:value>Planes</sv:value>
<sv:value>Trains</sv:value>
<sv:value>Automobiles</sv:value>
</sv:property>
<sv:property sv:name="tag" sv:type="String" sv:multiple="true">
<sv:value>Planes</sv:value>
</sv:property>
</sv:node>
</sv:node>
</sv:node>
Expand Down
21 changes: 20 additions & 1 deletion features/phpcr_query_update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Execute a a raw UPDATE query in JCR_SQL2
Given that I am logged in as "testuser"
And the "cms.xml" fixtures are loaded

Scenario Outline: Execute query
Scenario Outline: Execute update query
Given I execute the "<query>" command
Then the command should not fail
And I save the session
Expand All @@ -23,3 +23,22 @@ Feature: Execute a a raw UPDATE query in JCR_SQL2
| UPDATE nt:unstructured AS a SET a.title = 'DTL' WHERE localname() = 'article1' | /cms/articles/article1 | title | DTL |
| UPDATE nt:unstructured AS a SET title = 'DTL' WHERE localname() = 'article1' | /cms/articles/article1 | title | DTL |
| UPDATE nt:unstructured AS a SET title = 'DTL', foobar='barfoo' WHERE localname() = 'article1' | /cms/articles/article1 | foobar | barfoo |

Scenario: Update multivalue index by value
Given I execute the "UPDATE [nt:unstructured] AS a SET a.tags = 'Rockets' WHERE a.tags = 'Trains'" command
And I save the session
Then the command should not fail
And I should see the following:
"""
Cannot update property "tags". Updating multi-value nodes with more than one element not currently supported
"""

Scenario: Update single multivalue
Given I execute the "UPDATE [nt:unstructured] AS a SET a.tag = 'Rockets' WHERE a.tags = 'Planes'" command
And I save the session
Then the command should not fail
And I should see the following:
"""
1 row(s) affected
"""
And the node at "/cms/articles/article1" should have the property "tag" with value "Rockets" at index "0"
22 changes: 22 additions & 0 deletions src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ public function execute(InputInterface $input, OutputInterface $output)
$rows++;
foreach ($updates as $field => $property) {
$node = $row->getNode($property['selector']);

if ($node->hasProperty($property['name'])) {
$phpcrProperty = $node->getProperty($property['name']);

if ($phpcrProperty->isMultiple()) {
$currentValue = $phpcrProperty->getValue();

if (sizeof($currentValue) > 1) {
$output->writeln(sprintf(
'<error>Cannot update property "%s". Updating multi-value nodes with more than one element not currently supported</error>',
$phpcrProperty->getName()
));
$output->writeln(sprintf(
'<error>See: https://github.com/phpcr/phpcr-shell/issues/81</error>',
$phpcrProperty->getName()
));
}

$property['value'] = (array) $property['value'];
}
}

$node->setProperty($property['name'], $property['value']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PHPCR/Shell/Query/UpdateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function parse($sql2)

$query = $this->factory->createQuery($source, $constraint);

$res = new \ArrayObject(array($query, $updates));
$res = new \ArrayObject(array($query, $updates, $constraint));

return $res;
}
Expand Down