Commit 9e3ae1fa authored by Bas de Nooijer's avatar Bas de Nooijer

Updated geospatial query helper functions to support dereferenced params, fixes issue #114

parent b2612442
...@@ -216,13 +216,14 @@ class Helper ...@@ -216,13 +216,14 @@ class Helper
* *
* Find all entries within the distance of a certain point. * Find all entries within the distance of a certain point.
* *
* @param $field
* @param $pointX * @param $pointX
* @param $pointY * @param $pointY
* @param $field
* @param $distance * @param $distance
* @param boolean $dereferenced
* @return string * @return string
*/ */
public function geofilt($pointX, $pointY, $field, $distance) public function geofilt($field, $pointX, $pointY, $distance, $dereferenced = false)
{ {
return $this->qparser( return $this->qparser(
'geofilt', 'geofilt',
...@@ -230,7 +231,8 @@ class Helper ...@@ -230,7 +231,8 @@ class Helper
'pt' => $pointX.','.$pointY, 'pt' => $pointX.','.$pointY,
'sfield' => $field, 'sfield' => $field,
'd' => $distance 'd' => $distance
) ),
$dereferenced
); );
} }
...@@ -242,13 +244,14 @@ class Helper ...@@ -242,13 +244,14 @@ class Helper
* guaranteed to encompass all of the points of interest, but it may also * guaranteed to encompass all of the points of interest, but it may also
* include other points that are slightly outside of the required distance. * include other points that are slightly outside of the required distance.
* *
* @param string $pointX * @param string $field
* @param string $pointY * @param string $pointX
* @param string $field * @param string $pointY
* @param string $distance * @param string $distance
* @param boolean $dereferenced
* @return string * @return string
*/ */
public function bbox($pointX, $pointY, $field, $distance) public function bbox($field, $pointX, $pointY, $distance, $dereferenced = false)
{ {
return $this->qparser( return $this->qparser(
'bbox', 'bbox',
...@@ -256,7 +259,8 @@ class Helper ...@@ -256,7 +259,8 @@ class Helper
'pt' => $pointX.','.$pointY, 'pt' => $pointX.','.$pointY,
'sfield' => $field, 'sfield' => $field,
'd' => $distance 'd' => $distance
) ),
$dereferenced
); );
} }
...@@ -269,16 +273,18 @@ class Helper ...@@ -269,16 +273,18 @@ class Helper
* or combining the distance with the relevancy score, * or combining the distance with the relevancy score,
* such as boosting by the inverse of the distance. * such as boosting by the inverse of the distance.
* *
* @param $field
* @param $pointX * @param $pointX
* @param $pointY * @param $pointY
* @param $field * @param boolean $dereferenced
* @return string * @return string
*/ */
public function geodist($pointX, $pointY, $field) public function geodist($field, $pointX, $pointY, $dereferenced = false)
{ {
return $this->functionCall( return $this->functionCall(
'geodist', 'geodist',
array($pointX, $pointY, $field) array('sfield' => $field, 'pt' => $pointX.','.$pointY),
$dereferenced
); );
} }
...@@ -289,9 +295,10 @@ class Helper ...@@ -289,9 +295,10 @@ class Helper
* @param string $name * @param string $name
* @param array $params * @param array $params
* @param boolean $dereferenced * @param boolean $dereferenced
* @param boolean $forceKeys
* @return string * @return string
*/ */
public function qparser($name, $params = array(), $dereferenced = false) public function qparser($name, $params = array(), $dereferenced = false, $forceKeys = false)
{ {
if ($dereferenced) { if ($dereferenced) {
...@@ -303,8 +310,12 @@ class Helper ...@@ -303,8 +310,12 @@ class Helper
} }
foreach ($params as $paramKey => $paramValue) { foreach ($params as $paramKey => $paramValue) {
$this->derefencedParamsLastKey++; if (is_int($paramKey) || $forceKeys) {
$derefKey = 'deref_' . $this->derefencedParamsLastKey; $this->derefencedParamsLastKey++;
$derefKey = 'deref_' . $this->derefencedParamsLastKey;
} else {
$derefKey = $paramKey;
}
$this->query->addParam($derefKey, $paramValue); $this->query->addParam($derefKey, $paramValue);
$params[$paramKey] = '$'.$derefKey; $params[$paramKey] = '$'.$derefKey;
} }
...@@ -312,7 +323,9 @@ class Helper ...@@ -312,7 +323,9 @@ class Helper
$output = '{!'.$name; $output = '{!'.$name;
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
$output .= ' ' . $key . '=' . $value; if (!$dereferenced || $forceKeys || is_int($key)) {
$output .= ' ' . $key . '=' . $value;
}
} }
$output .= '}'; $output .= '}';
...@@ -322,13 +335,21 @@ class Helper ...@@ -322,13 +335,21 @@ class Helper
/** /**
* Render a functionCall * Render a functionCall
* *
* @param string $name * @param string $name
* @param array $params * @param array $params
* @param boolean $dereferenced
* @return string * @return string
*/ */
public function functionCall($name, $params = array()) public function functionCall($name, $params = array(), $dereferenced = false)
{ {
return $name . '(' . implode($params, ',') . ')'; if ($dereferenced) {
foreach($params as $key => $value) {
$this->query->addParam($key, $value);
}
return $name . '()';
} else {
return $name . '(' . implode($params, ',') . ')';
}
} }
/** /**
...@@ -408,7 +429,7 @@ class Helper ...@@ -408,7 +429,7 @@ class Helper
*/ */
public function join($from, $to, $dereferenced = false) public function join($from, $to, $dereferenced = false)
{ {
return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced); return $this->qparser('join', array('from' => $from, 'to' => $to), $dereferenced, $dereferenced);
} }
} }
...@@ -81,7 +81,20 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -81,7 +81,20 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertEquals( $this->assertEquals(
'{!geofilt pt=45.15,-93.85 sfield=store d=5}', '{!geofilt pt=45.15,-93.85 sfield=store d=5}',
$this->helper->geofilt(45.15, -93.85, 'store', 5) $this->helper->geofilt('store', 45.15, -93.85, 5)
);
}
public function testGeofiltDereferenced()
{
$this->assertEquals(
'{!geofilt}',
$this->helper->geofilt('store', 45.15, -93.85, 5, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85', 'd' => 5),
$this->query->getParams()
); );
} }
...@@ -89,15 +102,41 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -89,15 +102,41 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertEquals( $this->assertEquals(
'{!bbox pt=45.15,-93.85 sfield=store d=5}', '{!bbox pt=45.15,-93.85 sfield=store d=5}',
$this->helper->bbox(45.15, -93.85, 'store', 5) $this->helper->bbox('store', 45.15, -93.85, 5)
);
}
public function testBboxDereferenced()
{
$this->assertEquals(
'{!bbox}',
$this->helper->bbox('store', 45.15, -93.85, 5, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85', 'd' => 5),
$this->query->getParams()
); );
} }
public function testGeodist() public function testGeodist()
{ {
$this->assertEquals( $this->assertEquals(
'geodist(45.15,-93.85,store)', 'geodist(store,45.15,-93.85)',
$this->helper->geodist(45.15, -93.85, 'store') $this->helper->geodist('store', 45.15, -93.85)
);
}
public function testGeodistDereferenced()
{
$this->assertEquals(
'geodist()',
$this->helper->geodist('store', 45.15, -93.85, true)
);
$this->assertEquals(
array('sfield' => 'store', 'pt' => '45.15,-93.85'),
$this->query->getParams()
); );
} }
...@@ -128,7 +167,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -128,7 +167,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertEquals( $this->assertEquals(
'{!join from=$deref_1 to=$deref_2}', '{!join from=$deref_1 to=$deref_2}',
$this->helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true) $this->helper->qparser('join', array('from' => 'manu_id', 'to' => 'id'), true, true)
); );
$this->assertEquals( $this->assertEquals(
...@@ -139,7 +178,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -139,7 +178,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
// second call, params should have updated counts // second call, params should have updated counts
$this->assertEquals( $this->assertEquals(
'{!join from=$deref_3 to=$deref_4}', '{!join from=$deref_3 to=$deref_4}',
$this->helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true) $this->helper->qparser('join', array('from' => 'cat_id', 'to' => 'prod_id'), true, true)
); );
// previous params should also still be there // previous params should also still be there
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment