]> git.joonet.de Git - adminer.git/commitdiff
MS SQL: Import all CSV rows with one command
authorJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:27:43 +0000 (22:27 +0100)
committerJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:27:43 +0000 (22:27 +0100)
adminer/drivers/mssql.inc.php
adminer/select.inc.php

index d559b4ef548120365a045dbbf3b3aad09638faa5..033a09b2db0a893c2baeb2838a37579224a86a36 100644 (file)
@@ -187,29 +187,31 @@ if (isset($_GET["mssql"])) {
 
                function insertUpdate($table, $rows, $primary) {
                        $fields = fields($table);
-                       queries("SET IDENTITY_INSERT " . table($table) . " ON");
+                       $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 = $val";
+                                               $update[] = "$key = c$c";
                                        }
                                        if (isset($primary[$name])) {
-                                               $where[] = "$key = $val";
+                                               $where[] = "$key = c$c";
                                        }
                                }
-                               //! can use only one query for all rows
-                               if (!queries("MERGE " . table($table) . " USING (VALUES(" . implode(", ", $set) . ")) AS source (c" . implode(", c", range(1, count($set))) . ") ON " . implode(" AND ", $where) //! source, c1 - possible conflict
-                                       . " WHEN MATCHED THEN UPDATE SET " . implode(", ", $update)
-                                       . " WHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ");" // ; is mandatory
-                               )) {
-                                       return false;
-                               }
+                               $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)
+                               . "\nWHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES ($columns);" // ; is mandatory
+                       );
                        queries("SET IDENTITY_INSERT " . table($table) . " OFF");
-                       return true;
+                       return $return;
                }
 
                function begin() {
index c82cfcea7439db493ed2c5ef5a45eb06ec721ff6..288924ff1bd545af2fb05056e3fafa54afc52648 100644 (file)
@@ -205,7 +205,7 @@ if ($_POST && !$error) {
                        }
                        $result = (!$rows || $driver->insertUpdate($TABLE, $rows, $primary));
                        if ($result) {
-                               $result = $driver->commit();
+                               $driver->commit();
                        }
                        queries_redirect(remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result);
                        $driver->rollback(); // after queries_redirect() to not overwrite error