FIX Removing selected column detail only if having is empty (MySQL "feature")

This commit is contained in:
Guy Marriott 2019-04-26 14:13:43 +12:00 committed by Sam Minnee
parent f9fc4f6641
commit 3575070b9d
2 changed files with 11 additions and 6 deletions

View File

@ -464,7 +464,6 @@ class DataQuery
// Grab a statement selecting "everything" - the engine shouldn't care what's being selected in an "EXISTS" // Grab a statement selecting "everything" - the engine shouldn't care what's being selected in an "EXISTS"
// statement anyway // statement anyway
$statement = $this->getFinalisedQuery(); $statement = $this->getFinalisedQuery();
$statement->setSelect('*');
// Clear limit, distinct, and order as it's not relevant for an exists query // Clear limit, distinct, and order as it's not relevant for an exists query
$statement->setDistinct(false); $statement->setDistinct(false);
@ -472,8 +471,10 @@ class DataQuery
$statement->setLimit(null); $statement->setLimit(null);
// We can remove grouping if there's no "having" that might be relying on an aggregate // We can remove grouping if there's no "having" that might be relying on an aggregate
// Additionally, the columns being selected no longer matter
$having = $statement->getHaving(); $having = $statement->getHaving();
if (empty($having)) { if (empty($having)) {
$statement->setSelect('*');
$statement->setGroupBy(null); $statement->setGroupBy(null);
} }

View File

@ -6,6 +6,7 @@ use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\Tests\DataQueryTest\ObjectE;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
/** /**
@ -386,11 +387,7 @@ class DataQueryTest extends SapphireTest
$query = new DataQuery(DataQueryTest\ObjectC::class); $query = new DataQuery(DataQueryTest\ObjectC::class);
$query->sort('"SortOrder"'); $query->sort('"SortOrder"');
$query->where( $query->where(
[ ['"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => ['First', 2]]
'"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => [
'First', 2
]
]
); );
$result = $query->getFinalisedQuery(['Title']); $result = $query->getFinalisedQuery(['Title']);
$from = $result->getFrom(); $from = $result->getFrom();
@ -466,4 +463,11 @@ class DataQueryTest extends SapphireTest
$this->assertEquals('Second', $titles[1]); $this->assertEquals('Second', $titles[1]);
$this->assertEquals('Last', $titles[2]); $this->assertEquals('Last', $titles[2]);
} }
public function testExistsCreatesFunctionalQueries()
{
$this->assertTrue(ObjectE::get()->exists());
$this->assertFalse(ObjectE::get()->where(['"Title" = ?' => 'Foo'])->exists());
$this->assertTrue(ObjectE::get()->dataQuery()->groupby('"SortOrder"')->exists());
}
} }