Merge pull request #41474 from nextcloud/refactor/carddav/earlier-return-sql-where-in

refactor(carddav): Use even earlier return and SQL's WHERE IN
This commit is contained in:
Christoph Wurst 2023-11-15 17:16:53 +01:00 committed by GitHub
commit d06ee45b80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1106,7 +1106,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
}
/**
* @param array $addressBookIds
* @param int[] $addressBookIds
* @param string $pattern
* @param array $searchProperties
* @param array $options
@ -1126,19 +1126,11 @@ class CardDavBackend implements BackendInterface, SyncSupport {
string $pattern,
array $searchProperties,
array $options = []): array {
$escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false;
$useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false;
$query2 = $this->db->getQueryBuilder();
$addressBookOr = $query2->expr()->orX();
foreach ($addressBookIds as $addressBookId) {
$addressBookOr->add($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId)));
}
if ($addressBookOr->count() === 0) {
if (empty($addressBookIds)) {
return [];
}
$escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false;
$useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false;
if ($escapePattern) {
$searchProperties = array_filter($searchProperties, function ($property) use ($pattern) {
@ -1161,9 +1153,10 @@ class CardDavBackend implements BackendInterface, SyncSupport {
return [];
}
$query2 = $this->db->getQueryBuilder();
$query2->selectDistinct('cp.cardid')
->from($this->dbCardsPropertiesTable, 'cp')
->andWhere($addressBookOr)
->where($query2->expr()->in('cp.addressbookid', $query2->createNamedParameter($addressBookIds, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY))
->andWhere($query2->expr()->in('cp.name', $query2->createNamedParameter($searchProperties, IQueryBuilder::PARAM_STR_ARRAY)));
// No need for like when the pattern is empty