Skip to content

Commit

Permalink
Merge pull request #63 from robwasripped/#62-filtering-columns-suffix…
Browse files Browse the repository at this point in the history
…ed-by-primary-key

#62 filtering columns suffixed by primary key
  • Loading branch information
TobiasHauck authored May 17, 2017
2 parents 01ff1bc + 84be033 commit a945a56
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Tests/Factory/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class RequestFactoryTest extends \PHPUnit_Framework_TestCase {
* @covers ::createOne
*/
public function createOne() {
$query = 'SELECT name FROM products WHERE id=1';
$query = 'SELECT name FROM products t0 WHERE t0.id=1';
$parser = new PHPSQLParser();
$factory = new RequestFactory();
$expected = new Request([
Expand Down
6 changes: 3 additions & 3 deletions Tests/Transformers/MysqlToRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function createMysqlToRequest($optionsOverride = []) {
* @covers ::<private>
*/
public function selectOne() {
$query = 'SELECT name FROM products WHERE id = 1';
$query = 'SELECT name FROM products t0 WHERE t0.id = 1';
$expected = new Request([
'method' => 'get',
'url' => $this->apiUrl . '/products/1',
Expand All @@ -120,7 +120,7 @@ public function selectOne() {
* @covers ::<private>
*/
public function selectOneBy() {
$query = 'SELECT name FROM products WHERE id=1 AND name=myName';
$query = 'SELECT name FROM products t0 WHERE t0.id=1 AND t0.name=myName';
$expected = new Request([
'method' => 'get',
'url' => $this->apiUrl . '/products/1',
Expand All @@ -139,7 +139,7 @@ public function selectOneBy() {
* @covers ::<private>
*/
public function selectBy() {
$query = 'SELECT name FROM products WHERE name=myName';
$query = 'SELECT name FROM products t0 WHERE t0.name=myName';
$expected = new Request([
'method' => 'get',
'url' => $this->apiUrl . '/products',
Expand Down
2 changes: 1 addition & 1 deletion Tests/Types/HttpQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HttpQueryTest extends \PHPUnit_Framework_TestCase {
*/
public function create() {
$parser = new PHPSQLParser();
$tokens = $parser->parse('SELECT name FROM products WHERE id=1 AND value="testvalue" AND name="testname"');
$tokens = $parser->parse('SELECT name FROM products t0 WHERE t0.id=1 AND t0.value="testvalue" AND t0.name="testname"');
$expected = 'value=testvalue&name=testname';

$this->assertSame($expected, HttpQuery::create($tokens));
Expand Down
15 changes: 9 additions & 6 deletions Types/HttpQuery.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of DoctrineRestDriver.
*
Expand All @@ -20,7 +21,6 @@

use Circle\DoctrineRestDriver\Enums\SqlOperations;
use Circle\DoctrineRestDriver\MetaData;
use Circle\DoctrineRestDriver\Validation\Assertions;

/**
* HttpQuery type
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function create(array $tokens, array $options = []) {
self::createPagination($tokens, $options),
]));

return preg_replace('/' . Identifier::column($tokens, new MetaData()) . '\=\d*&*/', '', $query);
return $query;
}

/**
Expand All @@ -66,12 +66,15 @@ public static function createConditionals(array $tokens) {
if(!isset($tokens['WHERE'])) return '';

$tableAlias = Table::alias($tokens);
$primaryKeyColumn = sprintf('%s.%s', $tableAlias, Identifier::column($tokens, new MetaData));

$conditionalString = array_reduce($tokens['WHERE'], function($query, $token) use ($tableAlias) {
return $query . str_replace('"', '', str_replace('OR', '|', str_replace('AND', '&', str_replace($tableAlias . '.', '', $token['base_expr']))));
// Get WHERE conditions as string including table alias and primary key column if present
$sqlWhereString = array_reduce($tokens['WHERE'], function($query, $token) use ($tableAlias) {
return $query . str_replace('"', '', str_replace('OR', '|', str_replace('AND', '&', $token['base_expr'])));
});

return $conditionalString;
// Remove primary key column before removing table alias and returning
return str_replace($tableAlias . '.', '', preg_replace('/' . preg_quote($primaryKeyColumn) . '=[\w\d]*&*/', '', $sqlWhereString));
}

/**
Expand All @@ -93,4 +96,4 @@ public static function createPagination(array $tokens, array $options) {

return $paginationParameters ? http_build_query($paginationParameters) : '';
}
}
}

0 comments on commit a945a56

Please sign in to comment.