diff --git a/lib/private/allconfig.php b/lib/private/allconfig.php index 00defd920d758b3d8f36afdfd72f0f322a34d38f..b8bba7986e60beb56a685934bcf73e1d9cdede04 100644 --- a/lib/private/allconfig.php +++ b/lib/private/allconfig.php @@ -189,11 +189,18 @@ class AllConfig implements \OCP\IConfig { return; } - $data = array($value, $userId, $appName, $key); + $affectedRows = 0; if (!$exists && $preCondition === null) { - $sql = 'INSERT INTO `*PREFIX*preferences` (`configvalue`, `userid`, `appid`, `configkey`)'. - 'VALUES (?, ?, ?, ?)'; + $this->connection->insertIfNotExist('*PREFIX*preferences', [ + 'configvalue' => $value, + 'userid' => $userId, + 'appid' => $appName, + 'configkey' => $key, + ], ['configvalue', 'userid', 'appid']); + $affectedRows = 1; } elseif ($exists) { + $data = array($value, $userId, $appName, $key); + $sql = 'UPDATE `*PREFIX*preferences` SET `configvalue` = ? '. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ? '; @@ -206,8 +213,8 @@ class AllConfig implements \OCP\IConfig { } $data[] = $preCondition; } + $affectedRows = $this->connection->executeUpdate($sql, $data); } - $affectedRows = $this->connection->executeUpdate($sql, $data); // only add to the cache if we already loaded data for the user if ($affectedRows > 0 && isset($this->userCache[$userId])) { diff --git a/lib/private/appframework/db/db.php b/lib/private/appframework/db/db.php index 5387e36d627cf5e7b44313cfdc677f92efcba5d3..18c32c948c54a73cb12ad365c306d46fa6f6ca4f 100644 --- a/lib/private/appframework/db/db.php +++ b/lib/private/appframework/db/db.php @@ -138,8 +138,8 @@ class Db implements IDb { * @return bool * */ - public function insertIfNotExist($table, $input) { - return $this->connection->insertIfNotExist($table, $input); + public function insertIfNotExist($table, $input, $compare = null) { + return $this->connection->insertIfNotExist($table, $input, $compare); } /** diff --git a/lib/private/db.php b/lib/private/db.php index dc25092e2769da7e1a6f86303150314749bd6c50..3993ae277481190806e47a636bcc91fcdb96ae0b 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -172,8 +172,8 @@ class OC_DB { * @param array $input An array of fieldname/value pairs * @return boolean number of updated rows */ - public static function insertIfNotExist($table, $input) { - return \OC::$server->getDatabaseConnection()->insertIfNotExist($table, $input); + public static function insertIfNotExist($table, $input, $compare = null) { + return \OC::$server->getDatabaseConnection()->insertIfNotExist($table, $input, $compare); } /** diff --git a/lib/private/db/adapter.php b/lib/private/db/adapter.php index 58b3514b9225a94a3fb2aed2cf1b46c98370d88b..ee6898dde855bcce73430aa7cf25eb6b1746ca53 100644 --- a/lib/private/db/adapter.php +++ b/lib/private/db/adapter.php @@ -46,19 +46,22 @@ class Adapter { * @throws \OC\HintException * @return int count of inserted rows */ - public function insertIfNotExist($table, $input) { + public function insertIfNotExist($table, $input, $compare = null) { + if ($compare === null) { + $compare = array_keys($input); + } $query = 'INSERT INTO `' .$table . '` (`' . implode('`,`', array_keys($input)) . '`) SELECT ' . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? . 'FROM `' . $table . '` WHERE '; $inserts = array_values($input); - foreach($input as $key => $value) { + foreach($compare as $key) { $query .= '`' . $key . '`'; - if (is_null($value)) { + if (is_null($input[$key])) { $query .= ' IS NULL AND '; } else { - $inserts[] = $value; + $inserts[] = $input[$key]; $query .= ' = ? AND '; } } diff --git a/lib/private/db/adaptersqlite.php b/lib/private/db/adaptersqlite.php index df4a804feb1d5dc0b529d61e42d2cb8829c128b4..8b3c4ebc839a85d2dbef3716d2d5a0d9882800e8 100644 --- a/lib/private/db/adaptersqlite.php +++ b/lib/private/db/adaptersqlite.php @@ -18,19 +18,22 @@ class AdapterSqlite extends Adapter { return $statement; } - public function insertIfNotExist($table, $input) { + public function insertIfNotExist($table, $input, $compare = null) { + if ($compare === null) { + $compare = array_keys($input); + } $fieldList = '`' . implode('`,`', array_keys($input)) . '`'; $query = "INSERT INTO `$table` ($fieldList) SELECT " . str_repeat('?,', count($input)-1).'? ' . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE "; $inserts = array_values($input); - foreach($input as $key => $value) { + foreach($compare as $key) { $query .= '`' . $key . '`'; - if (is_null($value)) { + if (is_null($input[$key])) { $query .= ' IS NULL AND '; } else { - $inserts[] = $value; + $inserts[] = $input[$key]; $query .= ' = ? AND '; } } diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index 6ba29fc2ccfdada7a67c2e6a2aca7045effeba18..cc94c862b85e0b12c67cfe894c7c043f4c0a77d4 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -164,8 +164,8 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { * @throws \OC\HintException * @return bool The return value from execute() */ - public function insertIfNotExist($table, $input) { - return $this->adapter->insertIfNotExist($table, $input); + public function insertIfNotExist($table, $input, $compare = null) { + return $this->adapter->insertIfNotExist($table, $input, $compare); } /** diff --git a/lib/public/db.php b/lib/public/db.php index e8fc817106e96d7466531c719ee61600dfb9a5f5..50e519bbe91390a75bdc67bd1a1df7f1f0f7c8ff 100644 --- a/lib/public/db.php +++ b/lib/public/db.php @@ -64,8 +64,8 @@ class DB { * @return bool * */ - public static function insertIfNotExist($table, $input) { - return(\OC_DB::insertIfNotExist($table, $input)); + public static function insertIfNotExist($table, $input, $compare = null) { + return(\OC_DB::insertIfNotExist($table, $input, $compare)); } /** diff --git a/lib/public/idbconnection.php b/lib/public/idbconnection.php index 0d3274d90eb8f507afbd3fad520a4655bcc8b559..3cc7ff3248bd3bfbc1cc66dcc130f8d06f78d3a7 100644 --- a/lib/public/idbconnection.php +++ b/lib/public/idbconnection.php @@ -94,7 +94,7 @@ interface IDBConnection { * @return bool * */ - public function insertIfNotExist($table, $input); + public function insertIfNotExist($table, $input, $compare = null); /** * Start a transaction