Skip to content

Commit

Permalink
Update for compatibility with MySQL 5.7+ and PostgreSQL
Browse files Browse the repository at this point in the history
getUniqueResources, getUniqueTags, and getUniqueUsers was failing due to mySQL5.7 compatibility issues. The error being thrown by mySQL was: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

To fix, we have to either turn off the only_full_group_by option or specifically select the columns that are used instead of selecting *.

See: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html for more info.

PostgreSQL was failing due to fields being present in ORDER but missing from GROUP BY. This has also been corrected.
  • Loading branch information
lmgonzales authored and demiankatz committed Jul 5, 2016
1 parent 32ed597 commit ec63614
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions module/VuFind/src/VuFind/Db/Table/ResourceTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@ public function getUniqueResources(
$userId = null, $resourceId = null, $tagId = null
) {
$callback = function ($select) use ($userId, $resourceId, $tagId) {
$select->columns(
[
'resource_id' => new Expression(
'MAX(?)', ['resource_tags.resource_id'],
[Expression::TYPE_IDENTIFIER]
),
'tag_id' => new Expression(
'MAX(?)', ['resource_tags.tag_id'],
[Expression::TYPE_IDENTIFIER]
),
'list_id' => new Expression(
'MAX(?)', ['resource_tags.list_id'],
[Expression::TYPE_IDENTIFIER]
),
'user_id' => new Expression(
'MAX(?)', ['resource_tags.user_id'],
[Expression::TYPE_IDENTIFIER]
),
'id' => new Expression(
'MAX(?)', ['resource_tags.id'],
[Expression::TYPE_IDENTIFIER]
)
]
);
$select->join(
['r' => 'resource'],
'resource_tags.resource_id = r.id',
Expand All @@ -313,8 +337,8 @@ public function getUniqueResources(
if (!is_null($tagId)) {
$select->where->equalTo('resource_tags.tag_id', $tagId);
}
$select->group(["resource_id"]);
$select->order(["title"]);
$select->group(['resource_id', 'title']);
$select->order(['title']);
};
return $this->select($callback);
}
Expand All @@ -332,6 +356,30 @@ public function getUniqueTags($userId = null, $resourceId = null, $tagId = null)
{

$callback = function ($select) use ($userId, $resourceId, $tagId) {
$select->columns(
[
'resource_id' => new Expression(
'MAX(?)', ['resource_tags.resource_id'],
[Expression::TYPE_IDENTIFIER]
),
'tag_id' => new Expression(
'MAX(?)', ['resource_tags.tag_id'],
[Expression::TYPE_IDENTIFIER]
),
'list_id' => new Expression(
'MAX(?)', ['resource_tags.list_id'],
[Expression::TYPE_IDENTIFIER]
),
'user_id' => new Expression(
'MAX(?)', ['resource_tags.user_id'],
[Expression::TYPE_IDENTIFIER]
),
'id' => new Expression(
'MAX(?)', ['resource_tags.id'],
[Expression::TYPE_IDENTIFIER]
)
]
);
$select->join(
['t' => 'tags'],
'resource_tags.tag_id = t.id',
Expand All @@ -346,8 +394,8 @@ public function getUniqueTags($userId = null, $resourceId = null, $tagId = null)
if (!is_null($tagId)) {
$select->where->equalTo('resource_tags.tag_id', $tagId);
}
$select->group(["tag_id"]);
$select->order(["tag"]);
$select->group(['tag_id', 'tag']);
$select->order(['tag']);
};
return $this->select($callback);
}
Expand All @@ -364,6 +412,30 @@ public function getUniqueTags($userId = null, $resourceId = null, $tagId = null)
public function getUniqueUsers($userId = null, $resourceId = null, $tagId = null)
{
$callback = function ($select) use ($userId, $resourceId, $tagId) {
$select->columns(
[
'resource_id' => new Expression(
'MAX(?)', ['resource_tags.resource_id'],
[Expression::TYPE_IDENTIFIER]
),
'tag_id' => new Expression(
'MAX(?)', ['resource_tags.tag_id'],
[Expression::TYPE_IDENTIFIER]
),
'list_id' => new Expression(
'MAX(?)', ['resource_tags.list_id'],
[Expression::TYPE_IDENTIFIER]
),
'user_id' => new Expression(
'MAX(?)', ['resource_tags.user_id'],
[Expression::TYPE_IDENTIFIER]
),
'id' => new Expression(
'MAX(?)', ['resource_tags.id'],
[Expression::TYPE_IDENTIFIER]
)
]
);
$select->join(
['u' => 'user'],
'resource_tags.user_id = u.id',
Expand All @@ -378,8 +450,8 @@ public function getUniqueUsers($userId = null, $resourceId = null, $tagId = null
if (!is_null($tagId)) {
$select->where->equalTo('resource_tags.tag_id', $tagId);
}
$select->group(["user_id"]);
$select->order(["username"]);
$select->group(['user_id', 'username']);
$select->order(['username']);
};
return $this->select($callback);
}
Expand Down

0 comments on commit ec63614

Please sign in to comment.