[DATABASE] Fix remaining misuses of SQL's GROUP BY

This commit is contained in:
Alexei Sorokin
2020-08-10 19:29:04 +03:00
committed by Diogo Peralta Cordeiro
parent b0b10cf186
commit 03e69e8c31
5 changed files with 94 additions and 76 deletions

View File

@@ -62,20 +62,25 @@ class GroupFavoritedAction extends ShowgroupAction
$groupId = (int)$this->group->id;
$weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
$cutoff = sprintf(
"fave.modified > TIMESTAMP '%s'",
common_sql_date(time() - common_config('popular', 'cutoff'))
"fave.modified > CURRENT_TIMESTAMP - INTERVAL '%d' SECOND",
common_config('popular', 'cutoff')
);
$offset = ($this->page - 1) * NOTICES_PER_PAGE;
$limit = NOTICES_PER_PAGE + 1;
$limit = NOTICES_PER_PAGE + 1;
$qry = 'SELECT notice.*, ' . $weightexpr . ' AS weight ' .
'FROM notice ' .
'INNER JOIN group_inbox ON notice.id = group_inbox.notice_id ' .
'INNER JOIN fave ON notice.id = fave.notice_id ' .
'WHERE ' . $cutoff . ' AND group_id = ' . $groupId . ' ' .
'GROUP BY id, profile_id, uri, content, rendered, url, created, notice.modified, reply_to, is_local, source, notice.conversation ' .
'ORDER BY weight DESC LIMIT ' . $limit . ' OFFSET ' . $offset;
$qry = <<<END
SELECT *
FROM notice INNER JOIN (
SELECT notice_id AS id, {$weightexpr} AS weight
FROM fave
INNER JOIN group_inbox USING (notice_id)
WHERE {$cutoff} AND group_inbox.group_id = {$groupId}
GROUP BY notice_id
) AS t1 USING (id)
ORDER BY weight DESC
LIMIT {$limit} OFFSET {$offset};
END;
$notice = Memcached_DataObject::cachedQuery('Notice', $qry, 600);

View File

@@ -77,18 +77,25 @@ class FavoritedSliceAction extends FavoritedAction
$weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
$cutoff = sprintf(
"fave.modified > TIMESTAMP '%s'",
common_sql_date(time() - common_config('popular', 'cutoff'))
"fave.modified > CURRENT_TIMESTAMP - INTERVAL '%d' SECOND",
common_config('popular', 'cutoff')
);
$offset = ($this->page - 1) * NOTICES_PER_PAGE;
$limit = NOTICES_PER_PAGE + 1;
$qry = 'SELECT notice.*, ' . $weightexpr . ' AS weight ' .
'FROM notice INNER JOIN fave ON notice.id = fave.notice_id ' .
'WHERE ' . $cutoff . ' AND ' . $slice . ' ' .
'GROUP BY id, profile_id, uri, content, rendered, url, created, notice.modified, reply_to, is_local, source, notice.conversation ' .
'ORDER BY weight DESC LIMIT ' . $limit . ' OFFSET ' . $offset;
$qry = <<<END
SELECT *
FROM notice INNER JOIN (
SELECT notice.id, {$weightexpr} AS weight
FROM notice
INNER JOIN fave ON notice.id = fave.notice_id
WHERE {$cutoff} AND {$slice}
GROUP BY notice.id
) AS t1 USING (id)
ORDER BY weight DESC
LIMIT {$limit} OFFSET {$offset};
END;
$notice = Memcached_DataObject::cachedQuery('Notice', $qry, 600);
@@ -113,17 +120,15 @@ class FavoritedSliceAction extends FavoritedAction
$include = $this->nicknamesToIds($this->includeUsers);
$exclude = $this->nicknamesToIds($this->excludeUsers);
if (count($include) == 1) {
return "profile_id = " . intval($include[0]);
} elseif (count($include) > 1) {
return "profile_id IN (" . implode(',', $include) . ")";
} elseif (count($exclude) === 1) {
return "profile_id != " . intval($exclude[0]);
} elseif (count($exclude) > 1) {
return "profile_id NOT IN (" . implode(',', $exclude) . ")";
} else {
return false;
$sql = [];
if (count($include) > 0) {
$sql[] = 'notice.profile_id IN (' . implode(',', $include) . ')';
}
if (count($exclude) > 0) {
$sql[] = 'notice.profile_id NOT IN (' . implode(',', $exclude) . ')';
}
return implode(' AND ', $sql) ?: false;
}
/**