Skip to content
Browse files

Gremlin queries can pass named parameters. #32

Alos, updated the Gremlin example script to demonstrate this.
  • Loading branch information...
1 parent 6f64970 commit 3ee630e505188a132cd6ee8aa547cef5034efbd9 @jadell committed
View
5 examples/gremlin.php
@@ -69,8 +69,9 @@ function loaderTestAutoloader($sClass)
// Find all actors in a movie
} else if ($cmd == 'actors') {
- $queryTemplate = "g.V.in('IN').uniqueObject.sort{it.name}.toList()";
- $query = new Gremlin\Query($client, $queryTemplate);
+ $queryTemplate = "g.V.in(type).uniqueObject.sort{it.name}.toList()";
+ $params = array('type' => 'IN');
+ $query = new Gremlin\Query($client, $queryTemplate, $params);
$result = $query->getResultSet();
foreach ($result as $row) {
View
8 lib/Everyman/Neo4j/Command/ExecuteGremlinQuery.php
@@ -33,8 +33,12 @@ public function __construct(Client $client, Query $query)
*/
protected function getData()
{
- $queryString = $this->query->getQuery();
- return array('script'=>$queryString);
+ $data = array('script' => $this->query->getQuery());
+ $params = $this->query->getParameters();
+ if ($params) {
+ $data['params'] = $params;
+ }
+ return $data;
}
/**
View
15 lib/Everyman/Neo4j/Gremlin/Query.php
@@ -15,6 +15,7 @@ class Query implements Neo4j\Query
{
protected $client = null;
protected $script = null;
+ protected $vars = array();
protected $result = null;
@@ -23,11 +24,13 @@ class Query implements Neo4j\Query
*
* @param Neo4j\Client $client
* @param string $script A Gremlin query script
+ * @param array $vars Replacement vars to inject into the query
*/
- public function __construct(Neo4j\Client $client, $script)
+ public function __construct(Neo4j\Client $client, $script, $vars=array())
{
$this->client = $client;
$this->script = $script;
+ $this->vars = $vars;
}
/**
@@ -41,6 +44,16 @@ public function getQuery()
}
/**
+ * Get the template parameters
+ *
+ * @return array
+ */
+ public function getParameters()
+ {
+ return $this->vars;
+ }
+
+ /**
* Retrieve the query results
*
* @return Neo4j\Query\ResultSet
View
14 tests/unit/lib/Everyman/Neo4j/Client_GremlinTest.php
@@ -14,8 +14,11 @@ public function setUp()
public function testGremlinQuery_ServerReturnsErrorCode_ReturnsFalse()
{
- $props = array('script' => 'i=g.foo();');
- $query = new Gremlin\Query($this->client, $props['script']);
+ $props = array(
+ 'script' => 'i=g.foo(start);',
+ 'params' => array('start' => 123),
+ );
+ $query = new Gremlin\Query($this->client, $props['script'], $props['params']);
$this->transport->expects($this->once())
->method('post')
@@ -28,8 +31,11 @@ public function testGremlinQuery_ServerReturnsErrorCode_ReturnsFalse()
public function testGremlinQuery_DataAndColumnsReturned_ReturnsResultSet()
{
- $props = array('script' => 'i=g.foo();');
- $query = new Gremlin\Query($this->client, $props['script']);
+ $props = array(
+ 'script' => 'i=g.foo(start);',
+ 'params' => array('start' => 123),
+ );
+ $query = new Gremlin\Query($this->client, $props['script'], $props['params']);
$this->transport->expects($this->once())
->method('post')
View
12 tests/unit/lib/Everyman/Neo4j/Gremlin/QueryTest.php
@@ -7,13 +7,15 @@ class QueryTest extends \PHPUnit_Framework_TestCase
protected $query = null;
protected $queryString = null;
+ protected $params = null;
public function setUp()
{
$this->client = $this->getMock('Everyman\Neo4j\Client', array(), array(), '', false);
- $this->queryString = 'i = g.v(1);i.outE.inV';
+ $this->queryString = 'i = g.v(start);i.outE.inV';
+ $this->params = array('start' => 123);
- $this->query = new Query($this->client, $this->queryString);
+ $this->query = new Query($this->client, $this->queryString, $this->params);
}
public function testGetQuery_ReturnsString()
@@ -22,6 +24,12 @@ public function testGetQuery_ReturnsString()
$this->assertEquals($result, $this->queryString);
}
+ public function testGetParameters_ReturnsArray()
+ {
+ $result = $this->query->getParameters();
+ $this->assertEquals($result, $this->params);
+ }
+
public function testGetResultSet_OnlyExecutesOnce_ReturnsResultSet()
{
$return = $this->getMock('Everyman\Neo4j\Query\ResultSet', array(), array(), '', false);

0 comments on commit 3ee630e

Please sign in to comment.
Something went wrong with that request. Please try again.