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

fix PollMapper for PostgreSQL usage #3394

Merged
merged 8 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 4 additions & 11 deletions lib/Db/CommentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,10 @@ 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;
}
}
13 changes: 4 additions & 9 deletions lib/Db/OptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,16 @@ 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
24 changes: 16 additions & 8 deletions lib/Db/PollMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ protected function buildQuery(): IQueryBuilder {
$qb = $this->db->getQueryBuilder();

$qb->select(self::TABLE . '.*')
// TODO: check if this is necessary, in case of empty table to avoid possibly nulled columns
// ->groupBy(self::TABLE . '.id')
->from($this->getTableName(), self::TABLE);
->from($this->getTableName(), self::TABLE)
->groupBy(self::TABLE . '.id');


$this->joinOptionsForMaxDate($qb, self::TABLE);
$this->joinCurrentUserVotes($qb, self::TABLE, $currentUserId);
$this->joinUserRole($qb, self::TABLE, $currentUserId);
$qb->groupBy(self::TABLE . '.id');
return $qb;
}

Expand All @@ -192,8 +192,13 @@ 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->selectAlias($joinAlias . '.locked', 'is_current_user_locked');
$emptyString = $qb->createNamedParameter("", IQueryBuilder::PARAM_STR);

$qb->addSelect($qb->createFunction('coalesce(' . $joinAlias . '.type, '. $emptyString . ') AS user_role'))
->addGroupBy($joinAlias . '.type');

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

$qb->leftJoin(
$fromAlias,
Expand All @@ -205,6 +210,7 @@ protected function joinUserRole(IQueryBuilder &$qb, string $fromAlias, string $c
$qb->expr()->eq($joinAlias . '.deleted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)),
)
);

}

/**
Expand All @@ -215,9 +221,11 @@ protected function joinUserRole(IQueryBuilder &$qb, string $fromAlias, string $c
*/
protected function joinOptionsForMaxDate(IQueryBuilder &$qb, string $fromAlias): void {
$joinAlias = 'options';
$saveMin = (string) time();

$qb->addSelect($qb->createFunction('coalesce(MAX(' . $joinAlias . '.timestamp), 0) AS max_date'))
$zero = $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT);
$saveMin = $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT);

$qb->addSelect($qb->createFunction('coalesce(MAX(' . $joinAlias . '.timestamp), '. $zero . ') AS max_date'))
->addSelect($qb->createFunction('coalesce(MIN(' . $joinAlias . '.timestamp), ' . $saveMin . ') AS min_date'));

$qb->leftJoin(
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct() {
}

public function getPreferences_decoded(): mixed {
return json_decode($this->getPreferences());
return json_decode($this->getPreferences() ?? '');
}

/**
Expand Down
11 changes: 8 additions & 3 deletions lib/Db/QBMapperWithUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ public function __construct(
/**
* Joins anonymous setting of poll
*/
protected function joinAnon(IQueryBuilder &$qb, string $fromAlias): string {
protected function joinAnon(IQueryBuilder &$qb, string $fromAlias): void {
$joinAlias = 'anon';

$qb->selectAlias($joinAlias . '.anonymous', 'anonymized')
->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,
Poll::TABLE,
$joinAlias,
$qb->expr()->eq($joinAlias . '.id', $fromAlias . '.poll_id'),
);
return $joinAlias;

}
}
12 changes: 6 additions & 6 deletions lib/Db/ShareMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 @@ -80,7 +80,7 @@ public function findGroupShareByPoll(int $pollId, bool $getDeleted = false): arr
$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 . '.type', $qb->createNamedParameter(Share::TYPE_GROUP, IQueryBuilder::PARAM_STR)));
Expand Down Expand Up @@ -141,7 +141,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 All @@ -165,7 +166,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 Expand Up @@ -223,8 +225,6 @@ protected function joinUserVoteCount(IQueryBuilder &$qb, string $fromAlias): voi
$qb->expr()->eq($fromAlias . '.user_id', $joinAlias . '.user_id'),
)
);
// avoid result with nulled columns
$qb->groupBy($fromAlias . '.id');
}

}
23 changes: 8 additions & 15 deletions lib/Db/VoteMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ 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))
);
$qb->addGroupBy(self::TABLE . '.user_id', self::TABLE . '.poll_id');

return $this->findEntities($qb);
}
Expand Down Expand Up @@ -197,28 +197,20 @@ protected function find(int $id): Vote {

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);


if ($findOrphaned) {
$qb->where($qb->expr()->isNull($optionAlias . '.id'));
} 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 @@ -230,7 +222,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
9 changes: 9 additions & 0 deletions tests/Unit/Db/PollMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function testUpdate() {
unset($poll);
}

/**
* Delete the previously created entry from the database.
*/
public function testFind() {
foreach ($this->polls as $poll) {
$this->assertInstanceOf(Poll::class, $this->pollMapper->find($poll->getId()));
}
}

/**
* Delete the previously created entry from the database.
*/
Expand Down