[Pkg-owncloud-commits] [owncloud] 45/118: Allow specifying the compare-array for insertIfNotExists()

David Prévot taffit at moszumanska.debian.org
Fri Mar 27 22:13:10 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit 4cfa4ecc2813b72e3d130aaa4c7259cb70b5d51e
Author: Joas Schilling <nickvergessen at owncloud.com>
Date:   Mon Mar 9 17:25:02 2015 +0100

    Allow specifying the compare-array for insertIfNotExists()
    
    Conflicts:
    	lib/private/appframework/db/db.php
---
 lib/private/allconfig.php        | 15 +++++++++++----
 lib/private/db.php               |  4 ++--
 lib/private/db/adapter.php       | 11 +++++++----
 lib/private/db/adaptersqlite.php | 11 +++++++----
 lib/private/db/connection.php    |  4 ++--
 lib/public/db.php                |  4 ++--
 lib/public/idbconnection.php     |  2 +-
 7 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/lib/private/allconfig.php b/lib/private/allconfig.php
index d729936..bf8a403 100644
--- a/lib/private/allconfig.php
+++ b/lib/private/allconfig.php
@@ -188,11 +188,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` = ? ';
 
@@ -205,8 +212,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/db.php b/lib/private/db.php
index dc25092..3993ae2 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 58b3514..ee6898d 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 df4a804..8b3c4eb 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 53935c1..04d3932 100644
--- a/lib/private/db/connection.php
+++ b/lib/private/db/connection.php
@@ -162,8 +162,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 e8fc817..50e519b 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 0d19b2c..d4ae9c4 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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list