/
FaceClusterMapper.php
101 lines (91 loc) 路 3.22 KB
/
FaceClusterMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* Copyright (c) 2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Recognize\Db;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class FaceClusterMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'recognize_face_clusters', FaceCluster::class);
$this->db = $db;
}
/**
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function find(int $id): FaceCluster {
$qb = $this->db->getQueryBuilder();
$qb->select(FaceCluster::$columns)
->from('recognize_face_clusters')
->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
return $this->findEntity($qb);
}
/**
* @throws \OCP\DB\Exception
* @return list<\OCA\Recognize\Db\FaceCluster>
*/
public function findByUserId(string $userId): array {
$qb = $this->db->getQueryBuilder();
$columns = array_map(fn ($c) => 'fc.'.$c, FaceCluster::$columns);
$qb->selectDistinct($columns)
->from('recognize_face_clusters', 'fc')
->innerJoin('fc', 'recognize_face_detections', 'fd', 'fc.id = fd.cluster_id')
->where($qb->expr()->eq('fc.user_id', $qb->createPositionalParameter($userId)));
return $this->findEntities($qb);
}
/**
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function findByUserAndTitle(string $userId, string $title) : Entity {
$qb = $this->db->getQueryBuilder();
$qb->select(FaceCluster::$columns)
->from('recognize_face_clusters')
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
->andWhere($qb->expr()->orX(
$qb->expr()->eq('title', $qb->createPositionalParameter($title)),
$qb->expr()->andX(
$qb->expr()->eq('title', $qb->createPositionalParameter('')),
$qb->expr()->eq('id', $qb->createPositionalParameter((int) $title, IQueryBuilder::PARAM_INT))
)
));
return $this->findEntity($qb);
}
/**
* @param int $detectionId
* @throws \OCP\DB\Exception
* @return list<\OCA\Recognize\Db\FaceCluster>
*/
public function findByDetectionId(int $detectionId): array {
$qb = $this->db->getQueryBuilder();
$qb->select(array_map(function ($c) : string {
return 'f.'.$c;
}, FaceCluster::$columns))
->from('recognize_face_clusters', 'f')
->join('f', 'recognize_face_detections', 'd', 'f.id = d.cluster_id')
->where($qb->expr()->eq('d.id', $qb->createPositionalParameter($detectionId)));
return $this->findEntities($qb);
}
public function delete(Entity $entity): Entity {
$qb = $this->db->getQueryBuilder();
$qb->update('recognize_face_detections')
->set('cluster_id', -1)
->where($qb->expr()->eq('cluster_id', $qb->createPositionalParameter($entity->getId())));
$qb->executeStatement();
return parent::delete($entity);
}
/**
* @throws \OCP\DB\Exception
*/
public function deleteAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete('recognize_face_clusters')
->executeStatement();
}
}