diff --git a/lib/private/DB/Adapter.php b/lib/private/DB/Adapter.php index e88e3cf09c6b5..77b1afb321a06 100644 --- a/lib/private/DB/Adapter.php +++ b/lib/private/DB/Adapter.php @@ -93,24 +93,36 @@ public function insertIfNotExist($table, $input, array $compare = null) { if (empty($compare)) { $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); + //Search for given compare-fields + $query_select = 'SELECT * FROM `' . $table . '` WHERE 1=1'; foreach($compare as $key) { - $query .= '`' . $key . '`'; + $query_select .= ' AND `' . $key . '`'; if (is_null($input[$key])) { - $query .= ' IS NULL AND '; + $query_select .= ' IS NULL'; } else { - $inserts[] = $input[$key]; - $query .= ' = ? AND '; - } + $query_select .= ' = "' . $input[$key] .'"'; + }; + }; + $result = $this->conn->executeQuery($query_select); + $data = $result->fetch(); + //took this from cache.php: + //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO + //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false + if ($data === null) { + $data = false; } - $query = substr($query, 0, -5); - $query .= ' HAVING COUNT(*) = 0'; + if ($data) { + return 0; //Data already there, only empty result returned + }; + //Do update + $inserts = array_values($input); + $query = 'INSERT INTO `' .$table . '` (`' + . implode('`,`', array_keys($input)) + . '`) VALUES (' + . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? + . ')'; return $this->conn->executeUpdate($query, $inserts); } }