Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions lib/private/DB/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,36 @@ public function insertIfNotExist($table, $input, array $compare = null) {
if (empty($compare)) {
$compare = array_keys($input);
}
$query = 'INSERT INTO `' .$table . '` (`'
. implode('`,`', array_keys($input)) . '`) SELECT '
. str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative?
. 'FROM `' . $table . '` WHERE ';

$inserts = array_values($input);
//Search for given compare-fields
$query_select = 'SELECT * FROM `' . $table . '` WHERE 1=1';
foreach($compare as $key) {
$query .= '`' . $key . '`';
$query_select .= ' AND `' . $key . '`';
if (is_null($input[$key])) {
$query .= ' IS NULL AND ';
$query_select .= ' IS NULL';
} else {
$inserts[] = $input[$key];
$query .= ' = ? AND ';
}
$query_select .= ' = "' . $input[$key] .'"';
};
};
$result = $this->conn->executeQuery($query_select);
$data = $result->fetch();
//took this from cache.php:
//FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
//PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false
if ($data === null) {
$data = false;
}
$query = substr($query, 0, -5);
$query .= ' HAVING COUNT(*) = 0';
if ($data) {
return 0; //Data already there, only empty result returned
};

//Do update
$inserts = array_values($input);
$query = 'INSERT INTO `' .$table . '` (`'
. implode('`,`', array_keys($input))
. '`) VALUES ('
. str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative?
. ')';
return $this->conn->executeUpdate($query, $inserts);
}
}