Skip to content

Commit c0aba54

Browse files
committed
Added support for DELETE queries
1 parent a0808ce commit c0aba54

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ alpha-4
1111
- [node|shell] Most commands which accept a node path can also accept a UUID
1212
- [node] `node:list`: Show node primary item value
1313
- [query] Support for UPDATE queries
14+
- [query] Support for DELETE queries
1415

1516
### Bugs Fixes
1617

features/bootstrap/FeatureContext.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ public function thereShouldNotExistANodeTypeNamed($arg1)
628628
try {
629629
$nodeTypeManager->getNodeType($arg1);
630630
} catch (\Exception $e) {
631-
var_dump(get_class($e));die();
632631
}
633632

634633
throw new \Exception('Node type ' . $arg1 . ' exists');

features/phpcr_query_delete.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Feature: Execute a a raw DELETE query in JCR_SQL2
2+
In order to run an DELETE JCR_SQL2 query easily
3+
As a user logged into the shell
4+
I want to simply type the query like in a normal sql shell
5+
6+
Background:
7+
Given that I am logged in as "testuser"
8+
And the "cms.xml" fixtures are loaded
9+
10+
Scenario Outline: Execute query
11+
Given I execute the "<query>" command
12+
Then the command should not fail
13+
And there should exist a node at "<path>"
14+
And I save the session
15+
And there should not exist a node at "<path>"
16+
And I should see the following:
17+
"""
18+
1 row(s) affected
19+
"""
20+
Examples:
21+
| query | path |
22+
| DELETE FROM [nt:unstructured] AS a WHERE localname() = 'article1' | /cms/articles/article1 |
23+
| delete FROM [nt:unstructured] as a where localname() = 'article1' | /cms/articles/article1 |
24+
| DELETE FROM nt:unstructured AS a WHERE localname() = 'article1' | /cms/articles/article1 |

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ private function registerCommands()
164164
$this->add(new CommandPhpcr\QueryCommand());
165165
$this->add(new CommandPhpcr\QuerySelectCommand());
166166
$this->add(new CommandPhpcr\QueryUpdateCommand());
167+
$this->add(new CommandPhpcr\QueryDeleteCommand());
167168
$this->add(new CommandPhpcr\RetentionHoldAddCommand());
168169
$this->add(new CommandPhpcr\RetentionHoldListCommand());
169170
$this->add(new CommandPhpcr\RetentionHoldRemoveCommand());
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Console\Command\Phpcr;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;
9+
10+
class QueryDeleteCommand extends Command
11+
{
12+
protected function configure()
13+
{
14+
$this->setName('delete');
15+
$this->setDescription('Execute a literal JCR-SQL2 query');
16+
$this->addArgument('query');
17+
$this->setHelp(<<<EOT
18+
Execute a JCR-SQL2 query. Unlike other commands you can enter a query literally:
19+
20+
DELETE FROM [nt:unstructured] WHERE title = 'foo';
21+
22+
You must call <info>session:save</info> to persist changes.
23+
24+
Note that this command is not part of the JCR-SQL2 language but is implemented specifically
25+
for the PHPCR-Shell.
26+
EOT
27+
);
28+
}
29+
30+
public function execute(InputInterface $input, OutputInterface $output)
31+
{
32+
$sql = $input->getRawCommand();
33+
34+
// trim ";" for people used to MysQL
35+
if (substr($sql, -1) == ';') {
36+
$sql = substr($sql, 0, -1);
37+
}
38+
39+
$session = $this->getHelper('phpcr')->getSession();
40+
$qm = $session->getWorkspace()->getQueryManager();
41+
42+
if (!preg_match('{^delete from}', strtolower($sql))) {
43+
throw new \PHPCR\Query\InvalidQueryException(sprintf(
44+
'"FROM" not specified in DELETE query: "%s"', $sql
45+
));
46+
}
47+
48+
$sql = 'SELECT * FROM' . substr($sql, 11);
49+
50+
$selectParser = new Sql2ToQomQueryConverter($qm->getQOMFactory());
51+
$query = $selectParser->parse($sql);
52+
53+
$start = microtime(true);
54+
$result = $query->execute();
55+
56+
foreach ($result as $row) {
57+
$node = $row->getNode();
58+
$node->remove();
59+
}
60+
61+
$elapsed = microtime(true) - $start;
62+
$output->writeln(sprintf('%s row(s) affected in %ss', count($result), number_format($elapsed, 2)));
63+
}
64+
}

src/PHPCR/Shell/Console/Input/StringInput.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function __construct($command)
3333
$this->isQuery = true;
3434
}
3535

36+
if (strpos(strtolower($this->rawCommand), 'delete') === 0) {
37+
$command = 'delete' . substr($command, 6);
38+
$this->isQuery = true;
39+
}
40+
3641
parent::__construct($command);
3742
}
3843

0 commit comments

Comments
 (0)