]> git.joonet.de Git - adminer.git/commitdiff
MS SQL: Compute arrays in insertUpdate only once
authorJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:35:00 +0000 (22:35 +0100)
committerJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:35:00 +0000 (22:35 +0100)
adminer/drivers/mssql.inc.php

index 033a09b2db0a893c2baeb2838a37579224a86a36..5dc15a5cf1179add2d604f79d155f6c387b11328 100644 (file)
@@ -187,27 +187,28 @@ if (isset($_GET["mssql"])) {
 
                function insertUpdate($table, $rows, $primary) {
                        $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();
                        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) . ")";
                        }
                        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
-                               . "\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
                        );
                        queries("SET IDENTITY_INSERT " . table($table) . " OFF");