Skip to content
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
8 changes: 8 additions & 0 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public function sqlParameter(string $subject, &$matches = null)
*/
public function tableAlias(string $subject, &$matches = null)
{
if (
preg_match(
'/^\s*(\(.*\))\s+(?:AS\s+)?([' . self::SQLCHARS . ']+)\s+ON\s/uis',
$subject,
$matches
)
)
return 1;
return preg_match(
'/`?([' . self::SQLCHARS . ']+[.:]?[' . self::SQLCHARS . '*]*)`?(\s+AS)?(\s+`?([' . self::SQLCHARS . ']*)`?)?/ui',
$subject,
Expand Down
18 changes: 17 additions & 1 deletion tests/Queries/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,20 @@ public function testFetchColumn()
self::assertEquals(false, $statement);
self::assertEquals(false, $statement2);
}
}

public function testMulitpleSubselectJoinClauses(): void
{
$query = $this->fluent->from('user')
-> disableSmartJoin()
-> leftJoin(
"(SELECT user_id, count(*) AS count FROM comment GROUP BY user_id) comments ON user.id = comments.user_id"
)
-> leftJoin(
"(SELECT user_id, count(*) AS count FROM article GROUP BY user_id) articles ON user.id = articles.user_id"
)
->select("comments.count AS total_comments")
->select("articles.count AS total_articles");
self::assertEquals('SELECT user.*, comments.count AS total_comments, articles.count AS total_articles FROM user LEFT JOIN (SELECT user_id, count(*) AS count FROM comment GROUP BY user_id) comments ON user.id = comments.user_id LEFT JOIN (SELECT user_id, count(*) AS count FROM article GROUP BY user_id) articles ON user.id = articles.user_id',
$query->getQuery(false));
}
}