Skip to content

Commit dabbb3a

Browse files
committed
Added query language validation
1 parent 0fe627d commit dabbb3a

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

src/PHPCR/Util/Console/Command/NodesUpdateCommand.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function configure()
4242
{
4343
parent::configure();
4444

45-
$this->configureNodeManipulationInput($this);
45+
$this->configureNodeManipulationInput();
4646

4747
$this->setName('phpcr:nodes:update')
4848
->addOption(
@@ -54,20 +54,24 @@ protected function configure()
5454
'query-language', 'l',
5555
InputOption::VALUE_OPTIONAL,
5656
'The query language (e.g. sql, jcr_sql2)',
57-
'jcr_sql2'
57+
'jcr-sql2'
5858
)
5959
->setDescription('Command to manipulate the nodes in the workspace.')
6060
->setHelp(<<<HERE
61-
The <info>nodes:update</info> command updates properties of nodes of type x matching
62-
the given select criteria.
61+
The <info>phpcr:nodes:update</info> can manipulate the properties of nodes
62+
found using the given query.
6363
64-
php bin/phpcr nodes:update --type="nt:unstructured" --where="foo='bar'" --set-prop=foo=bar
64+
For example, to set the property "foo" to "bar" on all unstructured nodes:
65+
66+
php bin/phpcr phpcr:nodes:update --query="SELECT * FROM [nt:unstructured]" --set-prop=foo=bar
67+
68+
Or to update only nodes matching a certain criteria:
69+
70+
php bin/phpcr nodes:update --query="SELECT * FROM [nt:unstructured] WHERE [phpcr:class]=\"Some\\Class\\Here\" --add-mixin=mix:mimetype
6571
6672
The options for manipulating nodes are the same as with the
6773
<info>node:touch</info> command and
6874
can be repeated to update multiple properties.
69-
70-
The <info>--where</info> option corresponds to the "where" part of a standard query.
7175
HERE
7276
);
7377
}
@@ -77,6 +81,8 @@ protected function configure()
7781
*/
7882
protected function execute(InputInterface $input, OutputInterface $output)
7983
{
84+
$this->dialog = new DialogHelper();
85+
8086
$query = $input->getOption('query');
8187
$queryLanguage = strtoupper($input->getOption('query-language'));
8288
$setProp = $input->getOption('set-prop');
@@ -87,8 +93,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
8793
$helper = $this->getPhpcrCliHelper();
8894
$session = $this->getPhpcrSession();
8995

90-
$this->dialog = new DialogHelper();
91-
9296
if (!$query) {
9397
throw new \InvalidArgumentException(
9498
'You must provide a SELECT query, e.g. --select="SELECT * FROM [nt:unstructured]"'

src/PHPCR/Util/Console/Command/WorkspaceQueryCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function configure()
4545

4646
$this->setName('phpcr:workspace:query')
4747
->addArgument('query', InputArgument::REQUIRED, 'A query statement to execute')
48-
->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'The query language (sql, jcr_sql2', 'jcr_sql2')
48+
->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'The query language (e.g. jcr-sql2', 'jcr-sql2')
4949
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The query limit', 0)
5050
->addOption('offset', null, InputOption::VALUE_OPTIONAL, 'The query offset', 0)
5151
->setDescription('Execute a JCR SQL2 statement')

src/PHPCR/Util/Console/Helper/PhpcrCliHelper.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,32 @@ public function processNode(OutputInterface $output, $node, $options)
142142
*/
143143
public function createQuery($language, $sql)
144144
{
145+
$this->validateQueryLanguage($language);
146+
145147
$session = $this->getSession();
146148
$qm = $session->getWorkspace()->getQueryManager();
147149
$language = strtoupper($language);
150+
$query = $qm->createQuery($sql, $language);
151+
152+
return $query;
153+
}
148154

149-
$constantName = '\PHPCR\Query\QueryInterface::'.$language;
150-
if (!defined($constantName)) {
151-
throw new \RuntimeException(sprintf(
152-
"Query language '\\PHPCR\\Query\\QueryInterface::%s' not defined.",
153-
$language
155+
/**
156+
* Validate the given query language.
157+
*
158+
* @param string Language type
159+
*
160+
* @return null
161+
*/
162+
protected function validateQueryLanguage($language)
163+
{
164+
$qm = $this->getSession()->getWorkspace()->getQueryManager();
165+
$langs = $qm->getSupportedQueryLanguages();
166+
if (!in_array($language, $langs)) {
167+
throw new \Exception(sprintf(
168+
'Query language "%s" not supported, available query languages: %s',
169+
$language, implode(',', $langs)
154170
));
155171
}
156-
157-
$query = $qm->createQuery($sql, constant($constantName));
158-
159-
return $query;
160172
}
161173
}

tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function setUp()
3636
$this->session = $this->getMock('PHPCR\SessionInterface');
3737
$this->workspace = $this->getMock('PHPCR\WorkspaceInterface');
3838
$this->repository = $this->getMock('PHPCR\RepositoryInterface');
39+
$this->queryManager = $this->getMock('PHPCR\Query\QueryManagerInterface');
3940

4041
$this->row1 = $this->getMock('PHPCR\Tests\Stubs\MockRow');
4142
$this->node1 = $this->getMock('PHPCR\Tests\Stubs\MockNode');
@@ -57,6 +58,14 @@ public function setUp()
5758
->method('getName')
5859
->will($this->returnValue('test'));
5960

61+
$this->workspace->expects($this->any())
62+
->method('getQueryManager')
63+
->will($this->returnValue($this->queryManager));
64+
65+
$this->queryManager->expects($this->any())
66+
->method('getSupportedQueryLanguages')
67+
->will($this->returnValue(array('JCR-SQL2')));
68+
6069
$this->application = new Application();
6170
$this->application->setHelperSet($this->helperSet);
6271
}

tests/PHPCR/Tests/Util/Console/Command/NodesUpdateCommandTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ public function setUp()
1212
{
1313
parent::setUp();
1414
$this->application->add(new NodesUpdateCommand());
15-
$this->queryManager = $this->getMock(
16-
'PHPCR\Query\QueryManagerInterface'
17-
);
1815
$this->query = $this->getMock('PHPCR\Query\QueryInterface');
1916
}
2017

tests/PHPCR/Tests/Util/Console/Command/WorkspaceQueryCommandTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ public function setUp()
1212
{
1313
parent::setUp();
1414
$this->application->add(new WorkspaceQueryCommand());
15-
$this->queryManager = $this->getMock(
16-
'PHPCR\Query\QueryManagerInterface'
17-
);
1815
$this->query = $this->getMock('PHPCR\Query\QueryInterface');
1916
}
2017

21-
public function testNodeTypeQuery()
18+
public function testQuery()
2219
{
23-
$this->session->expects($this->once())
20+
$this->queryManager->expects($this->any())
21+
->method('getSupportedQueryLanguages')
22+
->will($this->returnValue(array('JCR-SQL2')));
23+
$this->session->expects($this->any())
2424
->method('getWorkspace')
2525
->will($this->returnValue($this->workspace));
26-
$this->workspace->expects($this->once())
26+
$this->workspace->expects($this->any())
2727
->method('getQueryManager')
2828
->will($this->returnValue($this->queryManager));
2929
$this->queryManager->expects($this->once())

0 commit comments

Comments
 (0)