Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Исправление группировки по полю в Criteria через Projection::group #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/OSQL/QuerySkeleton.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected function resolveAliasByField($field, $alias)
$field instanceof SelectQuery
|| ($field instanceof DialectString && $field instanceof Aliased)
) {
return $field->getAlias();
return $field->getAlias() ?: $alias;
}
}

Expand Down
71 changes: 70 additions & 1 deletion test/db/CriteriaDBTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class CriteriaDBTest extends TestCaseDAO
public function testCriteria()
{
foreach (DBTestPool::me()->getPool() as $db) {
/* @var $db DB */
DBPool::me()->setDefault($db);
$this->getDBCreator()->fillDB();

Expand All @@ -14,5 +13,75 @@ public function testCriteria()
Cache::me()->clean();
}
}

public function testDialectsGroupByFunction()
{
$resultMap = [
'PgSQL' => 'SELECT date("test_user"."strange_time") AS "st" FROM "test_user" GROUP BY "st"',
'SQLitePDO' => 'SELECT date("test_user"."strange_time") AS "st" FROM "test_user" GROUP BY "st"',
'MySQL' => 'SELECT date(`test_user`.`strange_time`) AS `st` FROM `test_user` GROUP BY `st`',
'MySQLim' => 'SELECT date(`test_user`.`strange_time`) AS `st` FROM `test_user` GROUP BY `st`',
];

foreach (DBTestPool::me()->getPool() as $db) {
$result = $this->getResultByDb($db, $resultMap);

$criteria =
Criteria::create(TestUser::dao())->
addProjection(
Projection::property(
SQLFunction::create(
'date', 'strangeTime'
),

'st'
)
)->
addProjection(
Projection::group('st')
);

$this->assertEquals(
$result,
$criteria->toDialectString($db->getDialect())
);
}
}

public function testDialectsGroupByField()
{
$resultMap = [
'PgSQL' => 'SELECT "test_user"."strange_time" AS "st" FROM "test_user" GROUP BY "st", \'stt\' ORDER BY "st", \'stt\'',
'SQLitePDO' => 'SELECT "test_user"."strange_time" AS "st" FROM "test_user" GROUP BY "st", \'stt\' ORDER BY "st", \'stt\'',
'MySQL' => 'SELECT `test_user`.`strange_time` AS `st` FROM `test_user` GROUP BY `st`, \'stt\' ORDER BY `st`, \'stt\'',
'MySQLim' => 'SELECT `test_user`.`strange_time` AS `st` FROM `test_user` GROUP BY `st`, \'stt\' ORDER BY `st`, \'stt\'',
];

foreach (DBTestPool::me()->getPool() as $db) {
$result = $this->getResultByDb($db, $resultMap);
$criteria =
Criteria::create(TestUser::dao())->
addProjection(
Projection::property('strangeTime', 'st')
)->
addProjection(Projection::group('st'))->
addProjection(Projection::group('stt'))
->addOrder('st')
->addOrder('stt');

$this->assertEquals(
$result,
$criteria->toDialectString($db->getDialect())
);
}
}

private function getResultByDb(DB $db, $resultMap)
{
if (!array_key_exists($class = get_class($db), $resultMap)) {
$this->fail("Uknonwn SQL for db ".get_class($db));
}
return $resultMap[$class];
}
}
?>
5 changes: 4 additions & 1 deletion test/misc/DBTestPool.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function connect($persistent = false)
$connector->setPersistent($persistent)->connect();
}
}


/**
* @return DB[]
*/
public function getPool()
{
return $this->pool;
Expand Down