1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-02 14:24:10 +01:00

Reduce complexity of DB query calls

This commit is contained in:
Thomas Tanghus 2014-03-19 01:50:10 +01:00
parent 95c54b6bab
commit 1f4eec0ae5

View File

@ -890,159 +890,134 @@ class Database extends AbstractBackend {
return $uri; return $uri;
} }
/**
* Collect (nearly) all queries in one place
*
* @param string $identifier
* @param array $options Can be used for e.g. offset/limit
* @throws \Exception If $identifier isn't known
* @return \OC_DB_StatementWrapper
*/
protected function getPreparedQuery($identifier, array $options = array()) { protected function getPreparedQuery($identifier, array $options = array()) {
if (isset(self::$preparedQueries[$identifier])) { if (isset(self::$preparedQueries[$identifier])) {
return self::$preparedQueries[$identifier]; return self::$preparedQueries[$identifier];
} }
$args = array();
switch ($identifier) { switch ($identifier) {
case 'getaddressbooksforuser': case 'getaddressbooksforuser':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `displayname`, `description`, `ctag`'
\OCP\DB::prepare('SELECT `id`, `displayname`, `description`, `ctag`'
. ' AS `lastmodified`, `userid` AS `owner`, `uri` FROM `' . ' AS `lastmodified`, `userid` AS `owner`, `uri` FROM `'
. $this->addressBooksTableName . $this->addressBooksTableName
. '` WHERE `userid` = ? ORDER BY `displayname`'); . '` WHERE `userid` = ? ORDER BY `displayname`';
break; break;
case 'getaddressbook': case 'getaddressbook':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `displayname`, `description`, '
\OCP\DB::prepare('SELECT `id`, `displayname`, `description`, '
. '`userid` AS `owner`, `ctag` AS `lastmodified`, `uri` FROM `' . '`userid` AS `owner`, `ctag` AS `lastmodified`, `uri` FROM `'
. $this->addressBooksTableName . $this->addressBooksTableName
. '` WHERE `id` = ? AND `userid` = ?'); . '` WHERE `id` = ? AND `userid` = ?';
break; break;
case 'createaddressbook': case 'createaddressbook':
self::$preparedQueries[$identifier] = $args[] = 'INSERT INTO `'
\OCP\DB::prepare('INSERT INTO `'
. $this->addressBooksTableName . '` ' . $this->addressBooksTableName . '` '
. '(`userid`,`displayname`,`uri`,`description`,`ctag`) ' . '(`userid`,`displayname`,`uri`,`description`,`ctag`) '
. 'VALUES(?,?,?,?,?)'); . 'VALUES(?,?,?,?,?)';
break; break;
case 'deleteaddressbookcontacts': case 'deleteaddressbookcontacts':
self::$preparedQueries[$identifier] = $args[] = 'DELETE FROM `' . $this->cardsTableName
\OCP\DB::prepare('DELETE FROM `' . $this->cardsTableName . '` WHERE `addressbookid` = ?';
. '` WHERE `addressbookid` = ?');
break; break;
case 'deleteaddressbook': case 'deleteaddressbook':
self::$preparedQueries[$identifier] = $args[] = 'DELETE FROM `'
\OCP\DB::prepare('DELETE FROM `' . $this->addressBooksTableName . '` WHERE `id` = ?';
. $this->addressBooksTableName . '` WHERE `id` = ?');
break; break;
case 'touchaddressbook': case 'touchaddressbook':
self::$preparedQueries[$identifier] = $args[] = 'UPDATE `' . $this->addressBooksTableName
\OCP\DB::prepare('UPDATE `' . $this->addressBooksTableName . '` SET `ctag` = ? + 1 WHERE `id` = ?';
. '` SET `ctag` = ? + 1 WHERE `id` = ?');
break; break;
case 'counturi': case 'counturi':
self::$preparedQueries[$identifier] = $args[] = 'SELECT COUNT(*) AS `count` FROM `'
\OCP\DB::prepare('SELECT COUNT(*) AS `count` FROM `'
. $this->cardsTableName . $this->cardsTableName
. '` WHERE `addressbookid` = ? AND `uri` = ?'); . '` WHERE `addressbookid` = ? AND `uri` = ?';
break; break;
case 'addressbookuris': case 'addressbookuris':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `uri` FROM `'
\OCP\DB::prepare('SELECT `uri` FROM `' . $this->addressBooksTableName . '` WHERE `userid` = ? ';
. $this->addressBooksTableName
. '` WHERE `userid` = ? ');
break; break;
case 'contactidfromuri': case 'contactidfromuri':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id` FROM `'
\OCP\DB::prepare('SELECT `id` FROM `'
. $this->cardsTableName . $this->cardsTableName
. '` WHERE `uri` = ?'); . '` WHERE `uri` = ?';
break; break;
case 'deletecontact': case 'deletecontact':
self::$preparedQueries[$identifier] = $args[] = 'DELETE FROM `'
\OCP\DB::prepare('DELETE FROM `'
. $this->cardsTableName . $this->cardsTableName
. '` WHERE `id` = ? AND `addressbookid` = ?'); . '` WHERE `id` = ? AND `addressbookid` = ?';
break; break;
case 'updatecontact': case 'updatecontact':
self::$preparedQueries[$identifier] = $args[] = 'UPDATE `' . $this->cardsTableName
\OCP\DB::prepare('UPDATE `' . $this->cardsTableName
. '` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ?' . '` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ?'
. ' WHERE `id` = ? AND `addressbookid` = ?'); . ' WHERE `id` = ? AND `addressbookid` = ?';
break; break;
case 'createcontact': case 'createcontact':
self::$preparedQueries[$identifier] = $args[] = 'INSERT INTO `'
\OCP\DB::prepare('INSERT INTO `'
. $this->cardsTableName . $this->cardsTableName
. '` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) ' . '` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) '
. ' VALUES(?,?,?,?,?)'); . ' VALUES(?,?,?,?,?)';
break; break;
case 'getcontactbyid': $args[] = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, '
self::$preparedQueries[$identifier] =
\OCP\DB::prepare('SELECT `id`, `uri`, `carddata`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName
. '` WHERE `id` = ? AND `addressbookid` = ?' . '` WHERE `id` = ? AND `addressbookid` = ?';
);
break; break;
case 'getcontactbyuri': case 'getcontactbyuri':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, '
\OCP\DB::prepare('SELECT `id`, `uri`, `carddata`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName
. '` WHERE `uri` = ? AND `addressbookid` = ?' . '` WHERE `uri` = ? AND `addressbookid` = ?';
);
break; break;
case 'getcontactbyidnocollection': case 'getcontactbyidnocollection':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, '
\OCP\DB::prepare('SELECT `id`, `uri`, `carddata`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName . '` WHERE `id` = ?';
. '` WHERE `id` = ?'
);
break; break;
case 'getcontactbyurinocollection': case 'getcontactbyurinocollection':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, '
\OCP\DB::prepare('SELECT `id`, `uri`, `carddata`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName . '` WHERE `uri` = ?';
. '` WHERE `uri` = ?'
);
break; break;
case 'getcontactids': case 'getcontactids':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id` FROM `'
\OCP\DB::prepare('SELECT `id` FROM `' . $this->cardsTableName . '` WHERE `addressbookid` = ?';
. $this->cardsTableName
. '` WHERE `addressbookid` = ?'
);
break; break;
case 'getcontacts': case 'getcontacts':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, '
\OCP\DB::prepare('SELECT `id`, `uri`, `carddata`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName . '` WHERE `addressbookid` = ?';
. '` WHERE `addressbookid` = ?', $args[] = isset($options['limit']) ? $options['limit'] : null;
isset($options['limit']) ? $options['limit'] : null, $args[] = isset($options['offset']) ? $options['offset'] : null;
isset($options['offset']) ? $options['offset'] : null
);
break; break;
case 'getcontactsomitdata': case 'getcontactsomitdata':
self::$preparedQueries[$identifier] = $args[] = 'SELECT `id`, `uri`, `lastmodified`, '
\OCP\DB::prepare('SELECT `id`, `uri`, `lastmodified`, '
. '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' . '`addressbookid` AS `parent`, `fullname` AS `displayname` FROM `'
. $this->cardsTableName . $this->cardsTableName . '` WHERE `addressbookid` = ?';
. '` WHERE `addressbookid` = ?', $args[] = isset($options['limit']) ? $options['limit'] : null;
isset($options['limit']) ? $options['limit'] : null, $args[] = isset($options['offset']) ? $options['offset'] : null;
isset($options['offset']) ? $options['offset'] : null
);
break; break;
case 'numcontacts': case 'numcontacts':
self::$preparedQueries[$identifier] = $args[] = 'SELECT COUNT(*) AS `count` FROM `'
\OCP\DB::prepare('SELECT COUNT(*) AS `count` FROM `' . $this->cardsTableName . '` WHERE `addressbookid` = ?';
. $this->cardsTableName
. '` WHERE `addressbookid` = ?'
);
break; break;
default: default:
throw new \Exception('Unknown query identifier: ' . $identifier); throw new \Exception('Unknown query identifier: ' . $identifier);
} }
self::$preparedQueries[$identifier] = call_user_func_array('\OCP\DB::prepare', $args);
return self::$preparedQueries[$identifier]; return self::$preparedQueries[$identifier];
} }
} }