Completed
Push — php-7.1 ( 77242e...1bf6d8 )
by Pauli
14:40
created

BaseMapper::questionMarks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
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 2016
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
/**
19
 * Common base class for data access classes of the Music app
20
 */
21
class BaseMapper extends Mapper {
22
23
	public function __construct(IDBConnection $db, $tableName, $entityClass=null){
24
		parent::__construct($db, $tableName, $entityClass);
25
	}
26
27
	/**
28
	 * @param integer[] $ids  IDs of the entities to be deleted
29
	 */
30
	public function deleteById($ids){
31
		$count = count($ids);
32
		if($count === 0) {
33
			return;
34
		}
35
		$sql = 'DELETE FROM `' . $this->getTableName() . '` WHERE `id` IN '. $this->questionMarks($count);
36
		$this->execute($sql, $ids);
37
	}
38
39
	/**
40
	 * @param string $userId
41
	 */
42 View Code Duplication
	public function count($userId){
43
		$sql = 'SELECT COUNT(*) AS count FROM `' . $this->getTableName() . '` '.
44
			'WHERE `user_id` = ?';
45
		$params = array($userId);
46
		$result = $this->execute($sql, $params);
47
		$row = $result->fetch();
48
		return $row['count'];
49
	}
50
51
	/**
52
	 * helper creating a string like '(?,?,?)' with the specified number of elements
53
	 * @param int $count
54
	 */
55
	protected function questionMarks($count) {
56
		$questionMarks = array();
57
		for($i = 0; $i < $count; $i++){
58
			$questionMarks[] = '?';
59
		}
60
		return '(' . implode(',', $questionMarks) . ')';
61
	}
62
63
}
64