Merge pull request #3770 from nextcloud/faster-search-in-contacts
Factorize query for searching contacts
This commit is contained in:
commit
3e37a5f1c7
|
@ -882,16 +882,15 @@ class CardDavBackend implements BackendInterface, SyncSupport {
|
||||||
public function search($addressBookId, $pattern, $searchProperties) {
|
public function search($addressBookId, $pattern, $searchProperties) {
|
||||||
$query = $this->db->getQueryBuilder();
|
$query = $this->db->getQueryBuilder();
|
||||||
$query2 = $this->db->getQueryBuilder();
|
$query2 = $this->db->getQueryBuilder();
|
||||||
|
|
||||||
$query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp');
|
$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)));
|
$query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId)));
|
||||||
|
$or = $query2->expr()->orX();
|
||||||
|
foreach ($searchProperties as $property) {
|
||||||
|
$or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property)));
|
||||||
|
}
|
||||||
|
$query2->andWhere($or);
|
||||||
|
$query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%')));
|
||||||
|
|
||||||
$query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c')
|
$query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c')
|
||||||
->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL())));
|
->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL())));
|
||||||
|
|
Loading…
Reference in New Issue