Permalink
Browse files

Pass primary key to insert_update function

  • Loading branch information...
1 parent 2ad8ab4 commit 6e50eb8ec0478ecf382bd6ca482fb9617aae091a @vrana committed Jul 22, 2010
Showing with 12 additions and 18 deletions.
  1. +2 −2 adminer/drivers/mysql.inc.php
  2. +2 −9 adminer/drivers/pgsql.inc.php
  3. +1 −1 adminer/drivers/sqlite.inc.php
  4. +7 −6 adminer/select.inc.php
@@ -767,10 +767,10 @@ function insert_into($table, $set) {
/** 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";
}
@@ -467,20 +467,13 @@ function insert_into($table, $set) {
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";
}
}
@@ -492,7 +492,7 @@ function insert_into($table, $set) {
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) . ")");
}
@@ -28,17 +28,18 @@
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 @@
}
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 @@
$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 @@
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;
}

0 comments on commit 6e50eb8

Please sign in to comment.