Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
AbstractSparqlStore: readded getQueryType
Browse files Browse the repository at this point in the history
  • Loading branch information
k00ni committed Jul 3, 2018
1 parent d3b0b6f commit c73fc1e
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
45 changes: 45 additions & 0 deletions AbstractSparqlStore.php
Expand Up @@ -348,6 +348,51 @@ public function getMatchingStatements(Statement $statement, Node $graph = null,
return $this->statementIteratorFactory->createStatementIteratorFromArray($statementList);
}

/**
* Determines the type of a given query in a very basic way.
*
* Returns either:
* - construct,
* - select,
* - insert-data
* - update-data
* - null, if unknown
*
* @param string $query
*
* @return string|null
*
* @unstable
*
* @since 2.0.0
*/
public function getQueryType(string $query): ?string
{
// remove PREFIX entries at the beginning
$query = \preg_replace('/PREFIX\s+[a-z]+:\s*<.*?>/im', '', $query);

$query = \ltrim(\strtolower($query));

// CONSTRUCT
if ('construct' == \substr($query, 0, 9)) {
return 'construct';

// SELECT
} elseif ('select' == \substr($query, 0, 6)) {
return 'select';

// INSERT DATA
} elseif ('insert data' == \substr($query, 0, 11)) {
return 'insert-data';

// DELETE DATA
} elseif ('delete data' == \substr($query, 0, 11)) {
return 'delete-data';
}

return null;
}

/**
* Returns true or false depending on whether or not the statements pattern has any matches in the given graph.
*
Expand Down
92 changes: 92 additions & 0 deletions Test/AbstractSparqlStoreTest.php
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of Saft.
*
* (c) Konrad Abicht <hi@inspirito.de>
* (c) Natanael Arndt <arndt@informatik.uni-leipzig.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Saft\Store\Test;

use Saft\Rdf\Test\TestCase;
use Saft\Sparql\Result\Result;
use Saft\Sparql\Result\ResultFactoryImpl;
use Saft\Store\ChainableStore;
use Saft\Store\AbstractSparqlStore;

/**
* The only purpose of this class to make AbstractSparqlStore testable. Using mocks/stubs
* lead to problems, therefore this "workaround".
*/
class TestableSparqlStore extends AbstractSparqlStore
{
public function query(string $query, array $options = []): Result { }
}

/**
* The actual test
*/
class AbstractSparqlStoreTest extends TestCase
{
public function setUp()
{
parent::setUp();

$this->fixture = new TestableSparqlStore(
$this->nodeFactory,
$this->statementFactory,
new ResultFactoryImpl(),
$this->statementIteratorFactory,
$this->rdfHelpers
);
}

public function testGetQueryTypeConstruct()
{
$query = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
CONSTRUCT { <http://example.org/person#Alice> vcard:FN ?name }
WHERE { ?x foaf:name ?name }';

$this->assertEquals('construct', $this->fixture->getQueryType($query));
}

public function testGetQueryTypeSelect()
{
$query = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
WHERE
{ ?x foaf:knows ?y ;
foaf:name ?nameX .
?y foaf:name ?nameY .
OPTIONAL { ?y foaf:nick ?nickY }
}';
$this->assertEquals('select', $this->fixture->getQueryType($query));
}

public function testGetQueryTypeUpdate()
{
// INSERT DATA
$query = 'PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "A new book" ;
dc:creator "A.N.Other" .
}';
$this->assertEquals('insert-data', $this->fixture->getQueryType($query));

// DELETE DATA
$query = 'PREFIX dc: <http://purl.org/dc/elements/1.1/>
DELETE DATA
{
<http://example/book2> dc:title "David Copperfield" ;
dc:creator "Edmund Wells" .
}';
$this->assertEquals('delete-data', $this->fixture->getQueryType($query));
}
}

0 comments on commit c73fc1e

Please sign in to comment.