summaryrefslogtreecommitdiffstats
path: root/lib/private/db/adapter.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/db/adapter.php')
-rw-r--r--lib/private/db/adapter.php40
1 files changed, 17 insertions, 23 deletions
diff --git a/lib/private/db/adapter.php b/lib/private/db/adapter.php
index 58b3514b922..8c251c82075 100644
--- a/lib/private/db/adapter.php
+++ b/lib/private/db/adapter.php
@@ -40,44 +40,38 @@ class Adapter {
}
/**
- * insert the @input values when they do not exist yet
- * @param string $table name
- * @param array $input key->value pair, key has to be sanitized properly
- * @throws \OC\HintException
- * @return int count of inserted rows
+ * Insert a row if the matching row does not exists.
+ *
+ * @param string $table The table name (will replace *PREFIX* with the actual prefix)
+ * @param array $input data that should be inserted into the table (column name => value)
+ * @param array|null $compare List of values that should be checked for "if not exists"
+ * If this is null or an empty array, all keys of $input will be compared
+ * Please note: text fields (clob) must not be used in the compare array
+ * @return int number of inserted rows
+ * @throws \Doctrine\DBAL\DBALException
*/
- public function insertIfNotExist($table, $input) {
+ 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);
- 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 ';
}
}
$query = substr($query, 0, strlen($query) - 5);
$query .= ' HAVING COUNT(*) = 0';
- try {
- return $this->conn->executeUpdate($query, $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
- );
- }
+ return $this->conn->executeUpdate($query, $inserts);
}
}