Skip to content

Commit

Permalink
adding groupBy for PosteSQL
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <[email protected]>
  • Loading branch information
dartcafe committed May 27, 2024
1 parent dd20c6b commit 8cf458d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
13 changes: 3 additions & 10 deletions lib/Db/CommentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,9 @@ protected function buildQuery(): IQueryBuilder {
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE);
$anonAlias = $this->joinAnon($qb, self::TABLE);

$qb->groupBy(
self::TABLE . '.id',
$anonAlias . '.anonymous',
$anonAlias . '.owner',
$anonAlias . '.show_results',
$anonAlias . '.expire',
);
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id');
$this->joinAnon($qb, self::TABLE);

return $qb;
}
Expand Down
11 changes: 2 additions & 9 deletions lib/Db/OptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,14 @@ protected function buildQuery(bool $hideVotes = false): IQueryBuilder {

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id')
->orderBy('order', 'ASC');

$this->joinVotesCount($qb, self::TABLE, $hideVotes);
$this->joinPollForLimits($qb, self::TABLE);
$this->joinCurrentUserVote($qb, self::TABLE, $currentUserId);
$this->joinCurrentUserVoteCount($qb, self::TABLE, $currentUserId);
$anonAlias = $this->joinAnon($qb, self::TABLE);

$qb->groupBy(
self::TABLE . '.id',
$anonAlias . '.anonymous',
$anonAlias . '.owner',
$anonAlias . '.show_results',
$anonAlias . '.expire',
);
$this->joinAnon($qb, self::TABLE);

return $qb;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/Db/PollMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ protected function buildQuery(): IQueryBuilder {
*/
protected function joinUserRole(IQueryBuilder &$qb, string $fromAlias, string $currentUserId): void {
$joinAlias = 'shares';
$qb->addSelect($qb->createFunction('coalesce(' . $joinAlias . '.type, "") AS user_role'));
$qb->addSelect($qb->createFunction('coalesce(' . $joinAlias . '.type, "") AS user_role'))
->addGroupBy($joinAlias . '.type');

$qb->selectAlias($joinAlias . '.locked', 'is_current_user_locked')
->addGroupBy($joinAlias . '.locked');

$qb->leftJoin(
$fromAlias,
Expand Down
7 changes: 6 additions & 1 deletion lib/Db/QBMapperWithUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ protected function joinAnon(IQueryBuilder &$qb, string $fromAlias): string {
->selectAlias($joinAlias . '.owner', 'poll_owner_id')
->selectAlias($joinAlias . '.show_results', 'poll_show_results')
->selectAlias($joinAlias . '.expire', 'poll_expire')
;
->addGroupBy(
$joinAlias . '.anonymous',
$joinAlias . '.owner',
$joinAlias . '.show_results',
$joinAlias . '.expire',
);

$qb->leftJoin(
$fromAlias,
Expand Down
10 changes: 7 additions & 3 deletions lib/Db/ShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function findByPoll(int $pollId, bool $getDeleted = false): array {
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE)
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id')
->where($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)));

Expand All @@ -83,6 +83,7 @@ public function findByPollNotInvited(int $pollId, bool $getDeleted = false): arr

$qb->select('*')
->from($this->getTableName())
->groupBy(self::TABLE . '.id')
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('invitation_sent', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));

Expand All @@ -103,6 +104,7 @@ public function findByPollUnreminded(int $pollId, bool $getDeleted = false): arr

$qb->select('*')
->from($this->getTableName())
->groupBy(self::TABLE . '.id')
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('reminder_sent', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));

Expand All @@ -120,7 +122,8 @@ public function findByPollAndUser(int $pollId, string $userId, bool $findDeleted
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE)
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id')
->where($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq(self::TABLE . '.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->isNotNull(self::TABLE . '.id'));
Expand Down Expand Up @@ -160,7 +163,8 @@ public function findByToken(string $token, bool $getDeleted = false): Share {
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE)
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id')
->where($qb->expr()->eq(self::TABLE . '.token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR)));

if (!$getDeleted) {
Expand Down
19 changes: 7 additions & 12 deletions lib/Db/VoteMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function findParticipantsByPoll(int $pollId): array {

$qb->selectDistinct([self::TABLE . '.user_id', self::TABLE . '.poll_id'])
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.user_id', self::TABLE . '.poll_id')
->where(
$qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
);
Expand Down Expand Up @@ -187,7 +188,8 @@ protected function buildQuery(bool $findOrphaned = false): IQueryBuilder {
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE);
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id');

$optionAlias = $this->joinOption($qb, self::TABLE);

Expand All @@ -197,17 +199,9 @@ protected function buildQuery(bool $findOrphaned = false): IQueryBuilder {
} else {
$qb->where($qb->expr()->isNotNull($optionAlias . '.id'));
}
$anonAlias = $this->joinAnon($qb, self::TABLE);

$qb->groupBy(
self::TABLE . '.id',
$optionAlias . '.id',
$anonAlias . '.anonymous',
$anonAlias . '.owner',
$anonAlias . '.show_results',
$anonAlias . '.expire',
);

$this->joinAnon($qb, self::TABLE);

return $qb;
}

Expand All @@ -218,7 +212,8 @@ protected function buildQuery(bool $findOrphaned = false): IQueryBuilder {
protected function joinOption(IQueryBuilder &$qb, string $fromAlias): string {
$joinAlias = 'options';

$qb->selectAlias($joinAlias . '.id', 'option_id');
$qb->selectAlias($joinAlias . '.id', 'option_id')
->addGroupBy($joinAlias . '.id');

$qb->leftJoin(
$fromAlias,
Expand Down

0 comments on commit 8cf458d

Please sign in to comment.