From ecba3722da8b2bcb4987acb13c9bbe912f8e711e Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 10 Mar 2017 09:40:57 +0100 Subject: [PATCH] Factorize query for searching contacts --- apps/dav/lib/CardDAV/CardDavBackend.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index 397a77c1d1..65b8176aed 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -869,16 +869,13 @@ class CardDavBackend implements BackendInterface, SyncSupport { public function search($addressBookId, $pattern, $searchProperties) { $query = $this->db->getQueryBuilder(); $query2 = $this->db->getQueryBuilder(); + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); - foreach ($searchProperties as $property) { - $query2->orWhere( - $query2->expr()->andX( - $query2->expr()->eq('cp.name', $query->createNamedParameter($property)), - $query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')) - ) - ); - } $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); + foreach ($searchProperties as $property) { + $query2->expr()->orX($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); + } + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL())));