Completed
Push — master ( 448b39...a46d9a )
by Maxence
02:01
created

IndexesRequest::getIndexes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 12
nc 4
nop 2
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Db;
28
29
30
use OCA\FullTextSearch\Exceptions\IndexDoesNotExistException;
31
use OCA\FullTextSearch\IFullTextSearchProvider;
32
use OCA\FullTextSearch\Model\ExtendedIndex;
33
use OCA\FullTextSearch\Model\Index;
34
35
class IndexesRequest extends IndexesRequestBuilder {
36
37
38
	/**
39
	 * @param Index $index
40
	 *
41
	 * @return bool
42
	 * @throws \Exception
43
	 */
44
	public function create(Index $index) {
45
46
		try {
47
			$qb = $this->getIndexesInsertSql();
48
			$qb->setValue('owner_id', $qb->createNamedParameter($index->getOwnerId()))
49
			   ->setValue('provider_id', $qb->createNamedParameter($index->getProviderId()))
50
			   ->setValue('document_id', $qb->createNamedParameter($index->getDocumentId()))
51
			   ->setValue('source', $qb->createNamedParameter($index->getSource()))
52
			   ->setValue('err', $qb->createNamedParameter($index->getError()))
53
			   ->setValue('message', $qb->createNamedParameter($index->getMessage()))
54
			   ->setValue('status', $qb->createNamedParameter($index->getStatus()))
55
			   ->setValue('options', $qb->createNamedParameter(json_encode($index->getOptions())))
56
			   ->setValue('indexed', $qb->createNamedParameter($index->getLastIndex()));
57
58
			$qb->execute();
59
60
			return true;
61
		} catch (\Exception $e) {
62
			throw $e;
63
		}
64
	}
65
66
67
	/**
68
	 * @param Index $index
69
	 *
70
	 * @return bool
71
	 */
72
	public function update(Index $index) {
73
74
		try {
75
			$this->getIndex($index->getProviderId(), $index->getDocumentId());
76
		} catch (IndexDoesNotExistException $e) {
77
			return false;
78
		}
79
80
		$qb = $this->getIndexesUpdateSql();
81
		$qb->set('status', $qb->createNamedParameter($index->getStatus()));
82
		$qb->set('source', $qb->createNamedParameter($index->getSource()));
83
		$qb->set('options', $qb->createNamedParameter(json_encode($index->getOptions())));
84
85
		if ($index->getOwnerId() !== '') {
86
			$qb->set('owner_id', $qb->createNamedParameter($index->getOwnerId()));
87
		}
88
89
		if ($index->getLastIndex() > 0) {
90
			$qb->set('indexed', $qb->createNamedParameter($index->getLastIndex()));
91
		}
92
93
		$qb->set('message', $qb->createNamedParameter($index->getMessage()));
94
95
		$this->limitToProviderId($qb, $index->getProviderId());
96
		$this->limitToDocumentId($qb, $index->getDocumentId());
97
98
		$qb->execute();
99
100
		return true;
101
	}
102
103
104
	public function updateStatus($providerId, $indexes, $status) {
105
		$qb = $this->getIndexesUpdateSql();
106
		$qb->set('status', $qb->createNamedParameter($status));
107
108
		$this->limitToProviderId($qb, $providerId);
109
		$this->limitToDocumentId($qb, $indexes);
110
111
		$qb->execute();
112
	}
113
114
115
	/**
116
	 * @param Index $index
117
	 */
118
	public function deleteIndex(Index $index) {
119
		$qb = $this->getIndexesDeleteSql();
120
		$this->limitToProviderId($qb, $index->getProviderId());
121
		$this->limitToDocumentId($qb, $index->getDocumentId());
122
123
		$qb->execute();
124
	}
125
126
127
	/**
128
	 * @param string $providerId
129
	 */
130
	public function deleteFromProviderId($providerId) {
131
		$qb = $this->getIndexesDeleteSql();
132
		$this->limitToProviderId($qb, $providerId());
133
134
		$qb->execute();
135
	}
136
137
138
	/**
139
	 *
140
	 */
141
	public function reset() {
142
		$qb = $this->getIndexesDeleteSql();
143
144
		$qb->execute();
145
	}
146
147
148
	/**
149
	 * return index.
150
	 *
151
	 * @param string $providerId
152
	 * @param string|int $documentId
153
	 *
154
	 * @return ExtendedIndex
155
	 * @throws IndexDoesNotExistException
156
	 */
157 View Code Duplication
	public function getIndex($providerId, $documentId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
		$qb = $this->getIndexesSelectSql();
159
		$this->limitToProviderId($qb, $providerId);
160
		$this->limitToDocumentId($qb, $documentId);
161
162
		$cursor = $qb->execute();
163
		$data = $cursor->fetch();
164
		$cursor->closeCursor();
165
166
		if ($data === false) {
167
			throw new IndexDoesNotExistException($this->l10n->t('Index not found'));
168
		}
169
170
		return $this->parseIndexesSelectSql($data);
171
	}
172
173
174
	/**
175
	 * return index.
176
	 *
177
	 * @param string $providerId
178
	 * @param array $documentIds
179
	 *
180
	 * @return ExtendedIndex[]
181
	 * @throws IndexDoesNotExistException
182
	 */
183
	public function getIndexes($providerId, $documentIds) {
184
		$qb = $this->getIndexesSelectSql();
185
		$this->limitToProviderId($qb, $providerId);
186
		$this->limitToDocumentIds($qb, $documentIds);
187
188
		$indexes = [];
189
		$cursor = $qb->execute();
190
		while ($data = $cursor->fetch()) {
191
			$indexes[] = $this->parseIndexesSelectSql($data);
192
		}
193
		$cursor->closeCursor();
194
195
		if (sizeof($indexes) === 0) {
196
			throw new IndexDoesNotExistException($this->l10n->t('Index not found'));
197
		}
198
199
		return $indexes;
200
	}
201
202
203
	/**
204
	 * @return Index[]
205
	 */
206 View Code Duplication
	public function getQueuedIndexes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
		$qb = $this->getIndexesSelectSql();
208
		$this->limitToQueuedIndexes($qb);
209
210
		$indexes = [];
211
		$cursor = $qb->execute();
212
		while ($data = $cursor->fetch()) {
213
			$indexes[] = $this->parseIndexesSelectSql($data);
214
		}
215
		$cursor->closeCursor();
216
217
		return $indexes;
218
	}
219
220
221
	/**
222
	 * return list of last indexes from a providerId.
223
	 *
224
	 * @param IFullTextSearchProvider $provider
225
	 *
226
	 * @return Index[]
227
	 */
228 View Code Duplication
	public function getIndexesFromProvider(IFullTextSearchProvider $provider) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
		$qb = $this->getIndexesSelectSql();
230
		$this->limitToProviderId($qb, $provider->getId());
231
232
		$indexes = [];
233
		$cursor = $qb->execute();
234
		while ($data = $cursor->fetch()) {
235
			$indexes[] = $this->parseIndexesSelectSql($data);
236
		}
237
		$cursor->closeCursor();
238
239
		return $indexes;
240
	}
241
242
243
}