Passed
Push — master ( d93244...31d6f2 )
by Pauli
03:37
created

GenreMapper::findUniqueEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020 - 2025
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IConfig;
16
use OCP\IDBConnection;
17
18
/**
19
 * @phpstan-extends BaseMapper<Genre>
20
 */
21
class GenreMapper extends BaseMapper {
22
	public function __construct(IDBConnection $db, IConfig $config) {
23
		parent::__construct($db, $config, 'music_genres', Genre::class, 'name', ['user_id', 'lower_name']);
24
	}
25
26
	/**
27
	 * Create SQL query which selects genres excluding any empty genres (having no tracks)
28
	 * @see \OCA\Music\Db\BaseMapper::selectEntities
29
	 * @return string SQL query
30
	 */
31
	protected function selectEntities(string $condition, ?string $extension=null) : string {
32
		return $this->selectGenres($condition, 'HAVING COUNT(`track`.`id`) > 0 ' . $extension);
33
	}
34
35
	/**
36
	 * Create SQL query to select genres. Unlike the function selectEntities used by the
37
	 * base class BaseMapper, this function returns also the genres with no tracks at all.
38
	 * @return string SQL query
39
	 */
40
	private function selectGenres(string $condition, ?string $extension=null) : string {
41
		return "SELECT
42
					`*PREFIX*music_genres`.`id`,
43
					`*PREFIX*music_genres`.`name`,
44
					`*PREFIX*music_genres`.`lower_name`,
45
					`*PREFIX*music_genres`.`created`,
46
					`*PREFIX*music_genres`.`updated`,
47
					COUNT(`track`.`id`) AS `trackCount`,
48
					COUNT(DISTINCT(`track`.`album_id`)) AS `albumCount`,
49
					COUNT(DISTINCT(`track`.`artist_id`)) AS `artistCount`
50
				FROM `*PREFIX*music_genres`
51
				LEFT JOIN `*PREFIX*music_tracks` `track`
52
				ON `track`.`genre_id` = `*PREFIX*music_genres`.`id`
53
				WHERE $condition
54
				GROUP BY
55
					`*PREFIX*music_genres`.`id`,
56
					`*PREFIX*music_genres`.`name`,
57
					`*PREFIX*music_genres`.`lower_name`,
58
					`*PREFIX*music_genres`.`created`,
59
					`*PREFIX*music_genres`.`updated`
60
				$extension";
61
	}
62
}
63