Skip to content
Snippets Groups Projects
Commit d809efc1 authored by Thomas Tanghus's avatar Thomas Tanghus
Browse files

Change insertIfNotExist() for sqlite. Not fast, but more reliable than previous attempt.

parent 42b871dc
Branches
No related tags found
No related merge requests found
......@@ -543,8 +543,9 @@ class OC_DB {
/**
* @brief Insert a row if a matching row doesn't exists.
* @returns true/false
*
* @param string $table. The table to insert into in the form '*PREFIX*tableName'
* @param array $input. An array of fieldname/value pairs
* @returns The return value from PDOStatementWrapper->execute()
*/
public static function insertIfNotExist($table, $input) {
self::connect();
......@@ -559,9 +560,31 @@ class OC_DB {
$query = '';
// differences in escaping of table names ('`' for mysql) and getting the current timestamp
if( $type == 'sqlite' || $type == 'sqlite3' ) {
$query = 'INSERT OR REPLACE INTO "' . $table . '" ("'
// NOTE: For SQLite we have to use this clumsy approach
// otherwise all fieldnames used must have a unique key.
$query = 'SELECT * FROM "' . $table . '" WHERE ';
foreach($input as $key => $value) {
$query .= $key . " = '" . $value . '\' AND ';
}
$query = substr($query, 0, strlen($query) - 5);
try {
$stmt = self::prepare($query);
$result = $stmt->execute();
} catch(PDOException $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);
die( $entry );
}
if($result->numRows() == 0) {
$query = 'INSERT INTO "' . $table . '" ("'
. implode('","', array_keys($input)) . '") VALUES("'
. implode('","', array_values($input)) . '")';
} else {
return true;
}
} elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql') {
$query = 'INSERT INTO `' .$table . '` ('
. implode(',', array_keys($input)) . ') SELECT \''
......@@ -573,6 +596,7 @@ class OC_DB {
$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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment