Skip to content

Added support for DELETE queries #69

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
Jul 19, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ alpha-4
- [node|shell] Most commands which accept a node path can also accept a UUID
- [node] `node:list`: Show node primary item value
- [query] Support for UPDATE queries
- [query] Support for DELETE queries

### Bugs Fixes

Expand Down
1 change: 0 additions & 1 deletion features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,6 @@ public function thereShouldNotExistANodeTypeNamed($arg1)
try {
$nodeTypeManager->getNodeType($arg1);
} catch (\Exception $e) {
var_dump(get_class($e));die();
}

throw new \Exception('Node type ' . $arg1 . ' exists');
Expand Down
24 changes: 24 additions & 0 deletions features/phpcr_query_delete.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Execute a a raw DELETE query in JCR_SQL2
In order to run an DELETE JCR_SQL2 query easily
As a user logged into the shell
I want to simply type the query like in a normal sql shell

Background:
Given that I am logged in as "testuser"
And the "cms.xml" fixtures are loaded

Scenario Outline: Execute query
Given I execute the "<query>" command
Then the command should not fail
And there should exist a node at "<path>"
And I save the session
And there should not exist a node at "<path>"
And I should see the following:
"""
1 row(s) affected
"""
Examples:
| query | path |
| DELETE FROM [nt:unstructured] AS a WHERE localname() = 'article1' | /cms/articles/article1 |
| delete FROM [nt:unstructured] as a where localname() = 'article1' | /cms/articles/article1 |
| DELETE FROM nt:unstructured AS a WHERE localname() = 'article1' | /cms/articles/article1 |
1 change: 1 addition & 0 deletions src/PHPCR/Shell/Console/Application/ShellApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ private function registerCommands()
$this->add(new CommandPhpcr\QueryCommand());
$this->add(new CommandPhpcr\QuerySelectCommand());
$this->add(new CommandPhpcr\QueryUpdateCommand());
$this->add(new CommandPhpcr\QueryDeleteCommand());
$this->add(new CommandPhpcr\RetentionHoldAddCommand());
$this->add(new CommandPhpcr\RetentionHoldListCommand());
$this->add(new CommandPhpcr\RetentionHoldRemoveCommand());
Expand Down
64 changes: 64 additions & 0 deletions src/PHPCR/Shell/Console/Command/Phpcr/QueryDeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace PHPCR\Shell\Console\Command\Phpcr;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;

class QueryDeleteCommand extends Command
{
protected function configure()
{
$this->setName('delete');
$this->setDescription('Execute a literal JCR-SQL2 query');
$this->addArgument('query');
$this->setHelp(<<<EOT
Execute a JCR-SQL2 query. Unlike other commands you can enter a query literally:

DELETE FROM [nt:unstructured] WHERE title = 'foo';

You must call <info>session:save</info> to persist changes.

Note that this command is not part of the JCR-SQL2 language but is implemented specifically
for the PHPCR-Shell.
EOT
);
}

public function execute(InputInterface $input, OutputInterface $output)
{
$sql = $input->getRawCommand();

// trim ";" for people used to MysQL
if (substr($sql, -1) == ';') {
$sql = substr($sql, 0, -1);
}

$session = $this->getHelper('phpcr')->getSession();
$qm = $session->getWorkspace()->getQueryManager();

if (!preg_match('{^delete from}', strtolower($sql))) {
throw new \PHPCR\Query\InvalidQueryException(sprintf(
'"FROM" not specified in DELETE query: "%s"', $sql
));
}

$sql = 'SELECT * FROM' . substr($sql, 11);

$selectParser = new Sql2ToQomQueryConverter($qm->getQOMFactory());
$query = $selectParser->parse($sql);

$start = microtime(true);
$result = $query->execute();

foreach ($result as $row) {
$node = $row->getNode();
$node->remove();
}

$elapsed = microtime(true) - $start;
$output->writeln(sprintf('%s row(s) affected in %ss', count($result), number_format($elapsed, 2)));
}
}
5 changes: 5 additions & 0 deletions src/PHPCR/Shell/Console/Input/StringInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function __construct($command)
$this->isQuery = true;
}

if (strpos(strtolower($this->rawCommand), 'delete') === 0) {
$command = 'delete' . substr($command, 6);
$this->isQuery = true;
}

parent::__construct($command);
}

Expand Down