Completed
Pull Request — master (#97)
by Roeland
02:12
created

ItemMapper::insert()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 24
cp 0
rs 9.328
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Copyright (c) 2015 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus\Db;
10
11
use OCP\AppFramework\Db\DoesNotExistException;
12
use OCP\AppFramework\Db\Entity;
13
use OCP\AppFramework\Db\Mapper;
14
use OCP\IDBConnection;
15
16
class ItemMapper extends Mapper {
17
	public function __construct(IDBConnection $db) {
18
		parent::__construct($db, 'files_antivirus', Item::class);
19
	}
20
21
	/**
22
	 * Find rule by id
23
	 * @param int $fileid
24
	 * @return Rule
25
	 * @throws DoesNotExistException
26
	 */
27
	public function findByFileId($fileid){
28
		$sql = 'SELECT * FROM ' . $this->getTableName() .' WHERE fileid = ?';
29
		return $this->findEntity($sql, [$fileid]);
30
	}
31
32
	public function delete(Entity $entity) {
33
		if (!($entity instanceof Item)) {
34
			throw new \InvalidArgumentException();
35
		}
36
37
		$qb = $this->db->getQueryBuilder();
38
39
		$qb->delete('files_antivirus')
40
			->where(
41
				$qb->expr()->eq('fileid', $qb->createNamedParameter($entity->getFileid()))
42
			);
43
		$qb->execute();
44
45
		return $entity;
46
	}
47
48
	/**
49
	 * Creates a new entry in the db from an entity
50
	 * @param Entity $entity the entity that should be created
51
	 * @return Entity the saved entity with the set id
52
	 * @since 7.0.0
53
	 * @deprecated 14.0.0 Move over to QBMapper
54
	 */
55
	public function insert(Entity $entity){
56
		// get updated fields to save, fields have to be set using a setter to
57
		// be saved
58
		$properties = $entity->getUpdatedFields();
59
		$values = '';
60
		$columns = '';
61
		$params = [];
62
63
		// build the fields
64
		$i = 0;
65
		foreach($properties as $property => $updated) {
66
			$column = $entity->propertyToColumn($property);
67
			$getter = 'get' . ucfirst($property);
68
69
			$columns .= '`' . $column . '`';
70
			$values .= '?';
71
72
			// only append colon if there are more entries
73
			if($i < count($properties)-1){
74
				$columns .= ',';
75
				$values .= ',';
76
			}
77
78
			$params[] = $entity->$getter();
79
			$i++;
80
81
		}
82
83
		$sql = 'INSERT INTO `' . $this->tableName . '`(' .
84
			$columns . ') VALUES(' . $values . ')';
85
86
		$stmt = $this->execute($sql, $params);
87
88
		$stmt->closeCursor();
89
90
		return $entity;
91
	}
92
}
93