diff options
Diffstat (limited to 'lib/db/adapter.php')
-rw-r--r-- | lib/db/adapter.php | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/db/adapter.php b/lib/db/adapter.php index 3b83853289a..3b950331191 100644 --- a/lib/db/adapter.php +++ b/lib/db/adapter.php @@ -22,4 +22,32 @@ class Adapter { public function fixupStatement($statement) { return $statement; } + + public function insertIfNotExist($table, $input) { + $query = 'INSERT INTO `' .$table . '` (`' + . implode('`,`', array_keys($input)) . '`) SELECT ' + . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? + . 'FROM `' . $table . '` WHERE '; + + foreach($input as $key => $value) { + $query .= '`' . $key . '` = ? AND '; + } + $query = substr($query, 0, strlen($query) - 5); + $query .= ' HAVING COUNT(*) = 0'; + $inserts = array_values($input); + $inserts = array_merge($inserts, $inserts); + + try { + $statement = $this->conn->prepare($query); + $result = $statement->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); + error_log('DB error: ' . $entry); + OC_Template::printErrorPage( $entry ); + } + + return $result; + } } |