summaryrefslogtreecommitdiffstats
path: root/lib/db/adaptersqlite.php
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2013-02-26 08:30:42 +0100
committerBart Visscher <bartv@thisnet.nl>2013-07-21 23:17:37 +0200
commit19b9c896340c572d9a42d033cd8824f33d18efa3 (patch)
tree5125454219343ed1686a634e91cd7d454007c006 /lib/db/adaptersqlite.php
parent58991150ab17846640907d43a6d4b0ff502d4986 (diff)
downloadnextcloud-server-19b9c896340c572d9a42d033cd8824f33d18efa3.tar.gz
nextcloud-server-19b9c896340c572d9a42d033cd8824f33d18efa3.zip
Move insertIfNotExist to Connection wrapper
Real implementation is in DB\Adapter* classes
Diffstat (limited to 'lib/db/adaptersqlite.php')
-rw-r--r--lib/db/adaptersqlite.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/db/adaptersqlite.php b/lib/db/adaptersqlite.php
index f0057ab489f..252fd94b0c0 100644
--- a/lib/db/adaptersqlite.php
+++ b/lib/db/adaptersqlite.php
@@ -16,4 +16,45 @@ class AdapterSqlite extends Adapter {
$statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
return $statement;
}
+
+ 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 ';
+ foreach($input as $key => $value) {
+ $query .= '`' . $key . '` = ? AND ';
+ }
+ $query = substr($query, 0, strlen($query) - 5);
+ try {
+ $stmt = $this->conn->prepare($query);
+ $result = $stmt->execute(array($input));
+ } 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 );
+ }
+
+ 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));
+ } 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;
+ }
}