]> git.joonet.de Git - adminer.git/commitdiff
Pass primary key to insert_update function
authorJakub Vrana <jakub@vrana.cz>
Thu, 22 Jul 2010 15:57:26 +0000 (17:57 +0200)
committerJakub Vrana <jakub@vrana.cz>
Thu, 22 Jul 2010 15:57:26 +0000 (17:57 +0200)
adminer/drivers/mysql.inc.php
adminer/drivers/pgsql.inc.php
adminer/drivers/sqlite.inc.php
adminer/select.inc.php

index b3c21d00f5a144fbbe17d071a2a47a15979beeb2..b64d204540a10b405966d872621199351aaf6b08 100644 (file)
@@ -767,10 +767,10 @@ if (!defined("DRIVER")) {
        /** Insert or update data in the table
        * @param string
        * @param array
-       * @param array
+       * @param array columns in keys
        * @return bool
        */
-       function insert_update($table, $set, $indexes) {
+       function insert_update($table, $set, $primary) {
                foreach ($set as $key => $val) {
                        $set[$key] = "$key = $val";
                }
index 34c969a8d3f3d7fe144f5d989b972f47550d1f37..96526726d019d0ca95e5211b80d315765e0f3377 100644 (file)
@@ -467,20 +467,13 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = " . $connection->qu
                return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
        }
        
-       function insert_update($table, $set, $indexes) {
+       function insert_update($table, $set, $primary) {
                global $connection;
-               $primary = array();
-               foreach ($indexes as $index) {
-                       if ($index["type"] == "PRIMARY") {
-                               $primary = array_map("idf_escape", $index["columns"]);
-                               break;
-                       }
-               }
                $update = array();
                $where = array();
                foreach ($set as $key => $val) {
                        $update[] = "$key = $val";
-                       if (in_array($key, $primary)) {
+                       if (isset($primary[$key])) {
                                $where[] = "$key = $val";
                        }
                }
index 43f7c70f9d9bce6a4807d75e5445b4b6d64daafa..bf5c30970b8abf9ee42670f836c8709b8cca2890 100644 (file)
@@ -492,7 +492,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
                return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
        }
        
-       function insert_update($table, $set, $indexes) {
+       function insert_update($table, $set, $primary) {
                return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
        }
        
index 9ec8a9457638ea33e82a6375c9bc2ed1f178a1b3..8b76c3b0bdf8027a5f90621fc0d1fb1866cbeb8e 100644 (file)
@@ -28,17 +28,18 @@ $group_by = ($group && count($group) < count($select) ? "\nGROUP BY " . implode(
 
 if ($_POST && !$error) {
        $where_check = "(" . implode(") OR (", array_map('where_check', (array) $_POST["check"])) . ")";
-       $primary = null;
+       $primary = $unselected = null;
        foreach ($indexes as $index) {
                if ($index["type"] == "PRIMARY") {
-                       $primary = ($select ? array_flip($index["columns"]) : array()); // empty array means that all primary fields are selected
+                       $primary = array_flip($index["columns"]);
+                       $unselected = ($select ? $primary : array());
                        break;
                }
        }
        foreach ($select as $key => $val) {
                $val = $_GET["columns"][$key];
                if (!$val["fun"]) {
-                       unset($primary[$val["col"]]);
+                       unset($unselected[$val["col"]]);
                }
        }
        if ($_POST["export"]) {
@@ -54,7 +55,7 @@ if ($_POST && !$error) {
                        }
                        dump_csv($row);
                }
-               if (!is_array($_POST["check"]) || $primary === array()) {
+               if (!is_array($_POST["check"]) || $unselected === array()) {
                        $where2 = $where;
                        if (is_array($_POST["check"])) {
                                $where2[] = "($where_check)";
@@ -99,7 +100,7 @@ if ($_POST && !$error) {
                                        $command = "INSERT";
                                        $query = "INTO $query";
                                }
-                               if ($_POST["all"] || ($primary === array() && $_POST["check"]) || count($group) < count($select)) {
+                               if ($_POST["all"] || ($unselected === array() && $_POST["check"]) || count($group) < count($select)) {
                                        $result = queries($command . " $query" . ($_POST["all"] ? ($where ? "\nWHERE " . implode(" AND ", $where) : "") : "\nWHERE $where_check"));
                                        $affected = $connection->affected_rows;
                                } else {
@@ -154,7 +155,7 @@ if ($_POST && !$error) {
                                        foreach ($matches2[1] as $i => $col) {
                                                $set[idf_escape($cols[$i])] = ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
                                        }
-                                       $result = insert_update($TABLE, $set, $indexes);
+                                       $result = insert_update($TABLE, $set, $primary);
                                        if (!$result) {
                                                break;
                                        }