Passed
Pull Request — master (#1490)
by René
04:02
created

OptionMapper::renameUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author René Gieling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Polls\Db;
26
27
use OCP\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\QBMapper;
30
use Doctrine\DBAL\Exception\TableNotFoundException;
31
32
/**
33
 * @template-extends QBMapper<Option>
34
 */
35
class OptionMapper extends QBMapper {
36
	public function __construct(IDBConnection $db) {
37 1
		parent::__construct($db, 'polls_options', '\OCA\Polls\Db\Option');
38 1
	}
39 1
40
	/**
41
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
42
	 * @return Option
43
	 */
44
	public function find(int $id): Option {
45
		$qb = $this->db->getQueryBuilder();
46
47
		$qb->select('*')
48
		   ->from($this->getTableName())
49
		   ->where(
50
			   $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
51
		   );
52
53
		return $this->findEntity($qb);
54
	}
55
56
	/**
57
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
58
	 * @return Option[]
59
	 */
60
	public function findByPoll(int $pollId): array {
61
		$qb = $this->db->getQueryBuilder();
62
63
		$qb->select('*')
64
		   ->from($this->getTableName())
65
		   ->where(
66
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
67
		   )
68
		   ->orderBy('order', 'ASC');
69
70
		return $this->findEntities($qb);
71
	}
72
73
	/**
74
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
75
	 * @return Option
76
	 */
77
	public function findByPollAndText($pollId, $pollOptionText): Option {
78
		$qb = $this->db->getQueryBuilder();
79
80
		$qb->select('*')
81
		   ->from($this->getTableName())
82
		   ->where(
83
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
84
		   )
85
		   ->andWhere(
86
			   $qb->expr()->eq('poll_option_text', $qb->createNamedParameter($pollOptionText, IQueryBuilder::PARAM_STR))
87
		   )
88
		   ->orderBy('order', 'ASC');
89
90
		return $this->findEntity($qb);
91
	}
92
93
	public function remove($optionId): void {
94
		$qb = $this->db->getQueryBuilder();
95
96
		$qb->delete($this->getTableName())
97
		   ->where(
98
			   $qb->expr()->eq('id', $qb->createNamedParameter($optionId, IQueryBuilder::PARAM_INT))
99
		   );
100
101
		$qb->execute();
102
	}
103
104
	public function deleteByPoll($pollId): void {
105
		$qb = $this->db->getQueryBuilder();
106
107
		$qb->delete($this->getTableName())
108
		   ->where(
109
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
110
		   );
111
112
		$qb->execute();
113
	}
114
115
	/**
116
	 * @return void
117
	 */
118
	public function removeDuplicates() {
119
		try {
120
			// remove duplicates from oc_polls_options
121
			// preserve the first entry
122
			$query = $this->db->getQueryBuilder();
123
			$query->select('id', 'poll_id', 'poll_option_text', 'timestamp')
124
				->from($this->getTableName());
125
			$foundEntries = $query->execute();
126
127
			$delete = $this->db->getQueryBuilder();
128
			$delete->delete($this->getTableName())
129
				->where('id = :id');
130
131
			$entries2Keep = [];
132
133
			while ($row = $foundEntries->fetch()) {
134
				$currentRecord = [
135
					$row['poll_id'],
136
					$row['poll_option_text'],
137
					$row['timestamp']
138
				];
139
				if (in_array($currentRecord, $entries2Keep)) {
140
					$delete->setParameter('id', $row['id']);
141
					$delete->execute();
142
				} else {
143
					$entries2Keep[] = $currentRecord;
144
				}
145
			}
146
		} catch (TableNotFoundException $e) {
147
			// ignore
148
		}
149
	}
150
151
	/**
152
	 * @return void
153
	 */
154
	public function renameUserId(string $userId, string $replacementName): void {
155
		$query = $this->db->getQueryBuilder();
156
		$query->update($this->getTableName())
157
			->set('owner', $query->createNamedParameter($replacementName))
158
			->where($query->expr()->eq('owner', $query->createNamedParameter($userId)))
159
			->execute();
160
	}
161
}
162