Factorize query for searching contacts

This commit is contained in:
Thomas Citharel 2017-03-10 09:40:57 +01:00 committed by Roeland Jago Douma
parent 867b3ee234
commit ecba3722da
No known key found for this signature in database
GPG Key ID: F941078878347C0C
1 changed files with 5 additions and 8 deletions

View File

@ -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())));