1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Add index properties to provide a solid search function

This commit is contained in:
LEDfan 2014-04-05 18:56:21 +02:00
parent 00cf07ec1a
commit 90a090af98
5 changed files with 144 additions and 15 deletions

View File

@ -411,4 +411,64 @@
</declaration> </declaration>
</table> </table>
<table>
<name>*dbprefix*contacts_ocu_cards_properties</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<unsigned>true</unsigned>
<length>4</length>
</field>
<field>
<name>addressbookid</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>contactid</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>name</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>64</length>
</field>
<field>
<name>value</name>
<type>text</type>
<default></default>
<notnull>false</notnull>
<length>255</length>
</field>
<field>
<name>preferred</name>
<type>integer</type>
<default>1</default>
<notnull>true</notnull>
<length>4</length>
</field>
</declaration>
</table>
</database> </database>

View File

@ -1 +1 @@
0.3.0.8 0.3.0.14

View File

@ -1018,4 +1018,9 @@ class Database extends AbstractBackend {
return self::$preparedQueries[$identifier]; return self::$preparedQueries[$identifier];
} }
public function getSearchProvider($addressbook){
return new \OCA\Contacts\AddressbookProvider($addressbook);
}
} }

View File

@ -50,7 +50,13 @@ class LocalUsers extends AbstractBackend {
* @var string * @var string
*/ */
private $cardsTableName = '*PREFIX*contacts_ocu_cards'; private $cardsTableName = '*PREFIX*contacts_ocu_cards';
private $indexTableName = '*PREFIX*contacts_ocu_cards_properties';
private $indexProperties = array(
'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME',
'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO');
public function __construct($userid){ public function __construct($userid){
$this->userid = $userid ? $userid : \OCP\User::getUser(); $this->userid = $userid ? $userid : \OCP\User::getUser();
@ -209,6 +215,9 @@ class LocalUsers extends AbstractBackend {
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false; return false;
} else { } else {
// All done
// now update the index table with all the properties
$this->updateIndex($user, $vcard);
return true; return true;
} }
} catch(\Exception $e) { } catch(\Exception $e) {
@ -268,7 +277,6 @@ class LocalUsers extends AbstractBackend {
$contact->REV = $now->format(\DateTime::W3C); $contact->REV = $now->format(\DateTime::W3C);
} }
$data = $contact->serialize();
try{ try{
$sql = 'UPDATE ' . $this->cardsTableName $sql = 'UPDATE ' . $this->cardsTableName
. ' SET ' . ' SET '
@ -279,12 +287,15 @@ class LocalUsers extends AbstractBackend {
. '`id` = ? ' . '`id` = ? '
. 'AND `addressbookid` = ? '; . 'AND `addressbookid` = ? ';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($contact->FN, $data, time(), $id, $this->userid)); $result = $query->execute(array($contact->FN, $contact->serialize(), time(), $id, $this->userid));
if (\OCP\DB::isError($result)) { if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false; return false;
} else { } else {
// All done
// now update the indexes in the DB
$this->updateIndex($id, $contact);
return true; return true;
} }
} catch(\Exception $e) { } catch(\Exception $e) {
@ -297,4 +308,55 @@ class LocalUsers extends AbstractBackend {
public function getSearchProvider(){ public function getSearchProvider(){
return new LocalUsersAddressbookProvider(); return new LocalUsersAddressbookProvider();
} }
private function updateIndex($contactId, $vcard){
// Utils\Properties::updateIndex($parameters['id'], $contact);
$this->purgeIndex($contactId);
$updatestmt = \OCP\DB::prepare('INSERT INTO `' . $this->indexTableName . '` '
. '(`addressbookid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)');
// Insert all properties in the table
foreach($vcard->children as $property) {
if(!in_array($property->name, $this->indexProperties)) {
continue;
}
$preferred = 0;
foreach($property->parameters as $parameter) {
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
$preferred = 1;
break;
}
}
try {
$result = $updatestmt->execute(
array(
\OCP\User::getUser(),
$contactId,
$property->name,
substr($property->value, 0, 254),
$preferred,
)
);
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
}
private function purgeIndex($id){
// Remove all indexes from the table
try {
$query = \OCP\DB::prepare('DELETE FROM `' . $this->indexTableName . '`'
. ' WHERE `contactid` = ?');
$query->execute(array($id));
} catch(\Exception $e) {
return false;
}
}
} }

View File

@ -15,18 +15,20 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
* @return array|false * @return array|false
*/ */
public function search($pattern, $searchProperties, $options) { public function search($pattern, $searchProperties, $options) {
if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){ if($pattern !== ''){
$query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) '; if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){
$stmt = \OCP\DB::prepare($query); $query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) ';
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); $stmt = \OCP\DB::prepare($query);
} elseif(in_array("FN", $searchProperties)){ $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%"));
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; } elseif(in_array("FN", $searchProperties)){
$stmt = \OCP\DB::prepare($query); $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? ';
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); $stmt = \OCP\DB::prepare($query);
} elseif(in_array("id", $searchProperties)){ $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%"));
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; } elseif(in_array("id", $searchProperties)){
$stmt = \OCP\DB::prepare($query); $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? ';
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); $stmt = \OCP\DB::prepare($query);
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%"));
}
} else { } else {
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?'; $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?';
$stmt = \OCP\DB::prepare($query); $stmt = \OCP\DB::prepare($query);