-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldValueMapper.php
More file actions
129 lines (106 loc) · 3.7 KB
/
FieldValueMapper.php
File metadata and controls
129 lines (106 loc) · 3.7 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\ProfileFields\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;
/** @template-extends QBMapper<FieldValue> */
class FieldValueMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'profile_fields_values', FieldValue::class);
}
public function findByFieldDefinitionIdAndUserUid(int $fieldDefinitionId, string $userUid): ?FieldValue {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('profile_fields_values')
->where($qb->expr()->eq('field_definition_id', $qb->createNamedParameter($fieldDefinitionId)))
->andWhere($qb->expr()->eq('user_uid', $qb->createNamedParameter($userUid)));
try {
return $this->findEntity($qb);
} catch (DoesNotExistException|MultipleObjectsReturnedException) {
return null;
}
}
/**
* @return list<FieldValue>
*/
public function findByFieldDefinitionId(int $fieldDefinitionId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('profile_fields_values')
->where($qb->expr()->eq('field_definition_id', $qb->createNamedParameter($fieldDefinitionId)))
->orderBy('user_uid', 'ASC')
->addOrderBy('id', 'ASC');
return $this->findEntities($qb);
}
/**
* @return list<FieldValue>
*/
public function findAllOrdered(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('profile_fields_values')
->orderBy('field_definition_id', 'ASC')
->addOrderBy('user_uid', 'ASC')
->addOrderBy('id', 'ASC');
return $this->findEntities($qb);
}
/**
* @return list<FieldValue>
*/
public function findByUserUid(string $userUid): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('profile_fields_values')
->where($qb->expr()->eq('user_uid', $qb->createNamedParameter($userUid)))
->orderBy('field_definition_id', 'ASC');
return $this->findEntities($qb);
}
public function hasValuesForFieldDefinitionId(int $fieldDefinitionId): bool {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('*', 'value_count'))
->from('profile_fields_values')
->where($qb->expr()->eq('field_definition_id', $qb->createNamedParameter($fieldDefinitionId)));
$cursor = $qb->executeQuery();
$result = $cursor->fetchOne();
$cursor->closeCursor();
return (int)$result > 0;
}
/**
* @return list<string>
*/
public function findDistinctUserUids(): array {
$qb = $this->db->getQueryBuilder();
$qb->selectDistinct('user_uid')
->from('profile_fields_values')
->orderBy('user_uid', 'ASC');
$cursor = $qb->executeQuery();
$userUids = $cursor->fetchFirstColumn();
$cursor->closeCursor();
return array_values(array_map(static fn (mixed $uid): string => (string)$uid, $userUids));
}
public function deleteByUserUid(string $userUid): int {
$qb = $this->db->getQueryBuilder();
$qb->delete('profile_fields_values')
->where($qb->expr()->eq('user_uid', $qb->createNamedParameter($userUid)));
return $qb->executeStatement();
}
/**
* @return list<FieldValue>
*/
public function findByFieldDefinitionIdAndValueJson(int $fieldDefinitionId, string $valueJson): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('profile_fields_values')
->where($qb->expr()->eq('field_definition_id', $qb->createNamedParameter($fieldDefinitionId)))
->andWhere($qb->expr()->eq('value_json', $qb->createNamedParameter($valueJson)))
->orderBy('user_uid', 'ASC')
->addOrderBy('id', 'ASC');
return $this->findEntities($qb);
}
}