Get rid of the SELECT query. Try UPDATE, on failure INSERT.

This commit is contained in:
Andreas Fischer 2013-10-31 15:44:19 +01:00
parent 43d71aada8
commit d6ddb12c20
1 changed files with 4 additions and 8 deletions

View File

@ -69,18 +69,14 @@ class OC_OCS_Privatedata {
$key = addslashes(strip_tags($parameters['key'])); $key = addslashes(strip_tags($parameters['key']));
$value = OC_OCS::readData('post', 'value', 'text'); $value = OC_OCS::readData('post', 'value', 'text');
// check if key is already set // update in DB
$query = \OCP\DB::prepare('SELECT `value` FROM `*PREFIX*privatedata` WHERE `user` = ? AND `app` = ? AND `key` = ? '); $query = \OCP\DB::prepare('UPDATE `*PREFIX*privatedata` SET `value` = ? WHERE `user` = ? AND `app` = ? AND `key` = ?');
$result = $query->execute(array($user, $app, $key)); $numRows = $query->execute(array($value, $user, $app, $key));
if ($result->numRows()==0) { if ($numRows === false || $numRows === 0) {
// store in DB // store in DB
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*privatedata` (`user`, `app`, `key`, `value`)' . ' VALUES(?, ?, ?, ?)'); $query = \OCP\DB::prepare('INSERT INTO `*PREFIX*privatedata` (`user`, `app`, `key`, `value`)' . ' VALUES(?, ?, ?, ?)');
$query->execute(array($user, $app, $key, $value)); $query->execute(array($user, $app, $key, $value));
} else {
// update in DB
$query = \OCP\DB::prepare('UPDATE `*PREFIX*privatedata` SET `value` = ? WHERE `user` = ? AND `app` = ? AND `key` = ? ');
$query->execute(array($value, $user, $app, $key ));
} }
return new OC_OCS_Result(null, 100); return new OC_OCS_Result(null, 100);