From ffe04182a86a326861763a5e6afa3577c52e07a5 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Mon, 17 Sep 2012 16:01:25 +0200 Subject: [PATCH] Added methods OC_DB::insertIfNotExist() and OCP\DB::insertIfNotExist(). --- lib/db.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++ lib/public/db.php | 21 ++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/db.php b/lib/db.php index 4d8e5a1a86..8598f659ca 100644 --- a/lib/db.php +++ b/lib/db.php @@ -512,6 +512,55 @@ class OC_DB { return true; } + /** + * @brief Insert a row if a matching row doesn't exists. + * @returns true/false + * + */ + public static function insertIfNotExist($table, $input) { + self::connect(); + $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); + $table = str_replace( '*PREFIX*', $prefix, $table ); + + if(is_null(self::$type)) { + self::$type=OC_Config::getValue( "dbtype", "sqlite" ); + } + $type = self::$type; + + $query = ''; + // differences in escaping of table names ('`' for mysql) and getting the current timestamp + if( $type == 'sqlite' || $type == 'sqlite3' ) { + $query = 'REPLACE OR INSERT INTO "' . $table . '" ("' + . implode('","', array_keys($input)) . '") VALUES("' + . implode('","', array_values($input)) . '")'; + } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql') { + $query = 'INSERT INTO `' .$table . '` (' + . implode(',', array_keys($input)) . ') SELECT \'' + . implode('\',\'', array_values($input)) . '\' FROM ' . $table . ' WHERE '; + + foreach($input as $key => $value) { + $query .= $key . " = '" . $value . '\' AND '; + } + $query = substr($query, 0, strlen($query) - 5); + $query .= ' HAVING COUNT(*) = 0'; + } + // TODO: oci should be use " (quote) instead of ` (backtick). + //OC_Log::write('core', __METHOD__ . ', type: ' . $type . ', query: ' . $query, OC_Log::DEBUG); + + try { + $result=self::$connection->prepare($query); + } catch(PDOException $e) { + $entry = 'DB Error: "'.$e->getMessage().'"
'; + $entry .= 'Offending command was: '.$query.'
'; + OC_Log::write('core', $entry,OC_Log::FATAL); + error_log('DB error: '.$entry); + die( $entry ); + } + + $result = new PDOStatementWrapper($result); + $result->execute(); + } + /** * @brief does minor chages to query * @param $query Query string diff --git a/lib/public/db.php b/lib/public/db.php index 6ce62b27ca..b9e56985e9 100644 --- a/lib/public/db.php +++ b/lib/public/db.php @@ -45,6 +45,27 @@ class DB { return(\OC_DB::prepare($query,$limit,$offset)); } + /** + * @brief Insert a row if a matching row doesn't exists. + * @param $table string The table name (will replace *PREFIX*) to perform the replace on. + * @param $input array + * + * The input array if in the form: + * + * array ( 'id' => array ( 'value' => 6, + * 'key' => true + * ), + * 'name' => array ('value' => 'Stoyan'), + * 'family' => array ('value' => 'Stefanov'), + * 'birth_date' => array ('value' => '1975-06-20') + * ); + * @returns true/false + * + */ + public static function insertIfNotExist($table, $input) { + return(\OC_DB::insertIfNotExist($table, $input)); + } + /** * @brief gets last value of autoincrement * @param $table string The optional table name (will replace *PREFIX*) and add sequence suffix