diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-03-09 17:25:02 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-03-09 22:37:49 +0100 |
commit | 8fa692388b29979c56ff6d0229d20e09e8ecba65 (patch) | |
tree | e3d0e47582c55aa0b4f29ef935396a7747684dac /lib/private/db | |
parent | 94b7fa17c55ed5c194b506ca6ab426fa38119b3c (diff) | |
download | nextcloud-server-8fa692388b29979c56ff6d0229d20e09e8ecba65.tar.gz nextcloud-server-8fa692388b29979c56ff6d0229d20e09e8ecba65.zip |
Allow specifying the compare-array for insertIfNotExists()
Diffstat (limited to 'lib/private/db')
-rw-r--r-- | lib/private/db/adapter.php | 11 | ||||
-rw-r--r-- | lib/private/db/adaptersqlite.php | 11 | ||||
-rw-r--r-- | lib/private/db/connection.php | 4 |
3 files changed, 16 insertions, 10 deletions
diff --git a/lib/private/db/adapter.php b/lib/private/db/adapter.php index 58b3514b922..ee6898dde85 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 df4a804feb1..8b3c4ebc839 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 6ba29fc2ccf..cc94c862b85 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); } /** |