[Pkg-owncloud-commits] [owncloud] 06/10: Use an atomic implementation on sqlite for insertIfNotExist()

David Prévot taffit at moszumanska.debian.org
Wed Mar 11 15:50:04 UTC 2015


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

taffit pushed a commit to annotated tag v8.0.2
in repository owncloud.

commit e5aefc8bda802e97406bd78ee51a307a1fe60bcd
Author: Thomas Müller <thomas.mueller at tmit.eu>
Date:   Fri Mar 6 14:50:51 2015 +0100

    Use an atomic implementation on sqlite for insertIfNotExist()
---
 lib/private/db/adaptersqlite.php | 42 +++++++++-------------------------------
 1 file changed, 9 insertions(+), 33 deletions(-)

diff --git a/lib/private/db/adaptersqlite.php b/lib/private/db/adaptersqlite.php
index c5dfa85..df4a804 100644
--- a/lib/private/db/adaptersqlite.php
+++ b/lib/private/db/adaptersqlite.php
@@ -19,11 +19,13 @@ class AdapterSqlite extends Adapter {
 	}
 
 	public function insertIfNotExist($table, $input) {
-		// NOTE: For SQLite we have to use this clumsy approach
-		// otherwise all fieldnames used must have a unique key.
-		$query = 'SELECT COUNT(*) FROM `' . $table . '` WHERE ';
-		$inserts = array();
-		foreach ($input as $key => $value) {
+		$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) {
 			$query .= '`' . $key . '`';
 			if (is_null($value)) {
 				$query .= ' IS NULL AND ';
@@ -33,34 +35,10 @@ class AdapterSqlite extends Adapter {
 			}
 		}
 		$query = substr($query, 0, strlen($query) - 5);
+		$query .= ')';
 
 		try {
-			$stmt = $this->conn->prepare($query);
-			$result = $stmt->execute($inserts);
-		} catch(\Doctrine\DBAL\DBALException $e) {
-			$entry = 'DB Error: "'.$e->getMessage() . '"<br />';
-			$entry .= 'Offending command was: ' . $query . '<br />';
-			\OC_Log::write('core', $entry, \OC_Log::FATAL);
-			$l = \OC::$server->getL10N('lib');
-			throw new \OC\HintException(
-				$l->t('Database Error'),
-				$l->t('Please contact your system administrator.'),
-				0,
-				$e
-			);
-		}
-
-		if ($stmt->fetchColumn() === '0') {
-			$query = 'INSERT INTO `' . $table . '` (`'
-				. implode('`,`', array_keys($input)) . '`) VALUES('
-				. str_repeat('?,', count($input)-1).'? ' . ')';
-		} else {
-			return 0; //no rows updated
-		}
-
-		try {
-			$statement = $this->conn->prepare($query);
-			$result = $statement->execute(array_values($input));
+			return $this->conn->executeUpdate($query, $inserts);
 		} catch(\Doctrine\DBAL\DBALException $e) {
 			$entry = 'DB Error: "'.$e->getMessage() . '"<br />';
 			$entry .= 'Offending command was: ' . $query.'<br />';
@@ -73,7 +51,5 @@ class AdapterSqlite extends Adapter {
 				$e
 			);
 		}
-
-		return $result;
 	}
 }

-- 
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