/
QueryUpdateCommand.php
61 lines (47 loc) · 1.87 KB
/
QueryUpdateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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\Shell\Query\UpdateParser;
class QueryUpdateCommand extends Command
{
protected function configure()
{
$this->setName('update');
$this->setDescription('Execute an UPDATE JCR-SQL2 query');
$this->addArgument('query');
$this->setHelp(<<<EOT
Execute a JCR-SQL2 update query. Unlike other commands you can enter a query literally:
UPDATE [nt:unstructured] AS a SET title = 'foobar' WHERE a.title = 'barfoo';
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();
$updateParser = new UpdateParser($qm->getQOMFactory());
$res = $updateParser->parse($sql);
$query = $res->offsetGet(0);
$updates = $res->offsetGet(1);
$start = microtime(true);
$result = $query->execute();
foreach ($result as $row) {
foreach ($updates as $field => $property) {
$node = $row->getNode($property['selector']);
$node->setProperty($property['name'], $property['value']);
}
}
$elapsed = microtime(true) - $start;
$output->writeln(sprintf('%s row(s) affected in %ss', count($result), number_format($elapsed, 2)));
}
}