1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-15 02:54:28 +02:00

MS SQL: Compute arrays in insertUpdate only once

This commit is contained in:
Jakub Vrana
2025-02-26 22:35:00 +01:00
parent 89b097c699
commit 81340ff7e5

View File

@@ -187,10 +187,9 @@ if (isset($_GET["mssql"])) {
function insertUpdate($table, $rows, $primary) { function insertUpdate($table, $rows, $primary) {
$fields = fields($table); $fields = fields($table);
$values = array();
foreach ($rows as $set) {
$update = array(); $update = array();
$where = array(); $where = array();
$set = reset($rows);
$columns = "c" . implode(", c", range(1, count($set))); $columns = "c" . implode(", c", range(1, count($set)));
$c = 0; $c = 0;
foreach ($set as $key => $val) { foreach ($set as $key => $val) {
@@ -203,11 +202,13 @@ if (isset($_GET["mssql"])) {
$where[] = "$key = c$c"; $where[] = "$key = c$c";
} }
} }
$values = array();
foreach ($rows as $set) {
$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");