Skip to content

Commit d3163ca

Browse files
committed
Re-added query command
1 parent e8514dc commit d3163ca

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private function registerCommands()
142142
$this->add(new CommandPhpcr\NodePropertyShowCommand());
143143
$this->add(new CommandPhpcr\SessionRefreshCommand());
144144
$this->add(new CommandPhpcr\SessionSaveCommand());
145+
$this->add(new CommandPhpcr\QueryCommand());
145146
$this->add(new CommandPhpcr\QuerySelectCommand());
146147
$this->add(new CommandPhpcr\RetentionHoldAddCommand());
147148
$this->add(new CommandPhpcr\RetentionHoldListCommand());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Input\InputOption;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class QueryCommand extends Command
11+
{
12+
protected function configure()
13+
{
14+
$this->setName('query');
15+
$this->setDescription('Execute a query ');
16+
$this->addArgument('query');
17+
$this->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'The query language (e.g. jcr-sql2', 'JCR-SQL2');
18+
$this->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The query limit', 0);
19+
$this->addOption('offset', null, InputOption::VALUE_OPTIONAL, 'The query offset', 0);
20+
$this->setHelp(<<<EOT
21+
Execute an SQL query. This command differs from <info>select</info> in that it
22+
is executed conventionally and not literally. The advantage is that you can
23+
specify a specific query language and additional options:
24+
25+
<info>query "SELECT * FROM [nt:unstructured]" --language=JCR_SQL2 --limit=5 --offset=4</info>
26+
EOT
27+
);
28+
}
29+
30+
public function execute(InputInterface $input, OutputInterface $output)
31+
{
32+
$language = $input->getOption('language');
33+
$limit = $input->getOption('limit');
34+
$offset = $input->getOption('offset');
35+
$query = $input->getArgument('query');
36+
37+
$session = $this->getHelper('phpcr')->getSession();
38+
$workspace = $session->getWorkspace();;
39+
$supportedQueryLanguages = $workspace->getQueryManager()->getSupportedQueryLanguages();
40+
41+
if (!in_array($language, $supportedQueryLanguages)) {
42+
throw new \InvalidArgumentException(sprintf(
43+
'"%s" is an invalid query language, valid query languages are:%s',
44+
$language,
45+
PHP_EOL . ' -' . implode(PHP_EOL . ' - ', $supportedQueryLanguages)
46+
));
47+
}
48+
49+
$qm = $session->getWorkspace()->getQueryManager();
50+
$query = $qm->createQuery($query, $language);
51+
52+
if ($limit) {
53+
$query->setLimit($limit);
54+
}
55+
56+
if ($offset) {
57+
$query->setOffset($offset);
58+
}
59+
60+
$start = microtime(true);
61+
$result = $query->execute();
62+
$elapsed = microtime(true) - $start;
63+
64+
$this->getHelper('result_formatter')->formatQueryResult($result, $output, $elapsed);
65+
}
66+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class QuerySelectCommand extends Command
1111
protected function configure()
1212
{
1313
$this->setName('select');
14-
$this->setDescription('Execute an JCR-SQL2 query');
14+
$this->setDescription('Execute a literal JCR-SQL2 query');
1515
$this->addArgument('query');
1616
$this->setHelp(<<<EOT
1717
Execute a JCR-SQL2 query. Unlike other commands you can enter a query literally:

0 commit comments

Comments
 (0)