mirror of
https://github.com/vrana/adminer.git
synced 2025-08-14 18:44:02 +02:00
MS SQL: Compute arrays in insertUpdate only once
This commit is contained in:
@@ -187,27 +187,28 @@ if (isset($_GET["mssql"])) {
|
|||||||
|
|
||||||
function insertUpdate($table, $rows, $primary) {
|
function insertUpdate($table, $rows, $primary) {
|
||||||
$fields = fields($table);
|
$fields = fields($table);
|
||||||
|
$update = array();
|
||||||
|
$where = array();
|
||||||
|
$set = reset($rows);
|
||||||
|
$columns = "c" . implode(", c", range(1, count($set)));
|
||||||
|
$c = 0;
|
||||||
|
foreach ($set as $key => $val) {
|
||||||
|
$c++;
|
||||||
|
$name = idf_unescape($key);
|
||||||
|
if (!$fields[$name]["auto_increment"]) {
|
||||||
|
$update[] = "$key = c$c";
|
||||||
|
}
|
||||||
|
if (isset($primary[$name])) {
|
||||||
|
$where[] = "$key = c$c";
|
||||||
|
}
|
||||||
|
}
|
||||||
$values = array();
|
$values = array();
|
||||||
foreach ($rows as $set) {
|
foreach ($rows as $set) {
|
||||||
$update = array();
|
|
||||||
$where = array();
|
|
||||||
$columns = "c" . implode(", c", range(1, count($set)));
|
|
||||||
$c = 0;
|
|
||||||
foreach ($set as $key => $val) {
|
|
||||||
$c++;
|
|
||||||
$name = idf_unescape($key);
|
|
||||||
if (!$fields[$name]["auto_increment"]) {
|
|
||||||
$update[] = "$key = c$c";
|
|
||||||
}
|
|
||||||
if (isset($primary[$name])) {
|
|
||||||
$where[] = "$key = c$c";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$values[] = "(" . implode(", ", $set) . ")";
|
$values[] = "(" . implode(", ", $set) . ")";
|
||||||
}
|
}
|
||||||
queries("SET IDENTITY_INSERT " . table($table) . " ON");
|
queries("SET IDENTITY_INSERT " . table($table) . " ON");
|
||||||
$return = queries("MERGE " . table($table) . " USING (VALUES\n\t" . implode(",\n\t", $values) . "\n) AS source ($columns) ON " . implode(" AND ", $where) //! source, c1 - possible conflict
|
$return = queries("MERGE " . table($table) . " USING (VALUES\n\t" . implode(",\n\t", $values) . "\n) AS source ($columns) ON " . implode(" AND ", $where) //! source, c1 - possible conflict
|
||||||
. "\nWHEN MATCHED THEN UPDATE SET " . implode(", ", $update)
|
. ($update ? "\nWHEN MATCHED THEN UPDATE SET " . implode(", ", $update) : "")
|
||||||
. "\nWHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES ($columns);" // ; is mandatory
|
. "\nWHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES ($columns);" // ; is mandatory
|
||||||
);
|
);
|
||||||
queries("SET IDENTITY_INSERT " . table($table) . " OFF");
|
queries("SET IDENTITY_INSERT " . table($table) . " OFF");
|
||||||
|
Reference in New Issue
Block a user