aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db
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
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')
-rw-r--r--lib/db/adapter.php28
-rw-r--r--lib/db/adaptersqlite.php41
-rw-r--r--lib/db/connection.php12
3 files changed, 80 insertions, 1 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;
+ }
}
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;
+ }
}
diff --git a/lib/db/connection.php b/lib/db/connection.php
index 8442efc47c9..e18062d78f3 100644
--- a/lib/db/connection.php
+++ b/lib/db/connection.php
@@ -138,8 +138,18 @@ class Connection extends \Doctrine\DBAL\Connection {
return parent::lastInsertId($seqName);
}
+ /**
+ * @brief Insert a row if a matching row doesn't exists.
+ * @param string $table. The table to insert into in the form '*PREFIX*tableName'
+ * @param array $input. An array of fieldname/value pairs
+ * @returns bool The return value from execute()
+ */
+ public function insertIfNotExist($table, $input) {
+ return $this->adapter->insertIfNotExist($table, $input);
+ }
+
// internal use
- public function replaceTablePrefix($statement) {
+ protected function replaceTablePrefix($statement) {
return str_replace( '*PREFIX*', $this->table_prefix, $statement );
}