Pass primary key to insert_update function

This commit is contained in:
Jakub Vrana 2010-07-22 17:57:26 +02:00
parent 2ad8ab4770
commit 6e50eb8ec0
4 changed files with 12 additions and 18 deletions

View file

@ -767,10 +767,10 @@ if (!defined("DRIVER")) {
/** Insert or update data in the table /** Insert or update data in the table
* @param string * @param string
* @param array * @param array
* @param array * @param array columns in keys
* @return bool * @return bool
*/ */
function insert_update($table, $set, $indexes) { function insert_update($table, $set, $primary) {
foreach ($set as $key => $val) { foreach ($set as $key => $val) {
$set[$key] = "$key = $val"; $set[$key] = "$key = $val";
} }

View 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")); 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; global $connection;
$primary = array();
foreach ($indexes as $index) {
if ($index["type"] == "PRIMARY") {
$primary = array_map("idf_escape", $index["columns"]);
break;
}
}
$update = array(); $update = array();
$where = array(); $where = array();
foreach ($set as $key => $val) { foreach ($set as $key => $val) {
$update[] = "$key = $val"; $update[] = "$key = $val";
if (in_array($key, $primary)) { if (isset($primary[$key])) {
$where[] = "$key = $val"; $where[] = "$key = $val";
} }
} }

View 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")); 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) . ")"); return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
} }

View file

@ -28,17 +28,18 @@ $group_by = ($group && count($group) < count($select) ? "\nGROUP BY " . implode(
if ($_POST && !$error) { if ($_POST && !$error) {
$where_check = "(" . implode(") OR (", array_map('where_check', (array) $_POST["check"])) . ")"; $where_check = "(" . implode(") OR (", array_map('where_check', (array) $_POST["check"])) . ")";
$primary = null; $primary = $unselected = null;
foreach ($indexes as $index) { foreach ($indexes as $index) {
if ($index["type"] == "PRIMARY") { 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; break;
} }
} }
foreach ($select as $key => $val) { foreach ($select as $key => $val) {
$val = $_GET["columns"][$key]; $val = $_GET["columns"][$key];
if (!$val["fun"]) { if (!$val["fun"]) {
unset($primary[$val["col"]]); unset($unselected[$val["col"]]);
} }
} }
if ($_POST["export"]) { if ($_POST["export"]) {
@ -54,7 +55,7 @@ if ($_POST && !$error) {
} }
dump_csv($row); dump_csv($row);
} }
if (!is_array($_POST["check"]) || $primary === array()) { if (!is_array($_POST["check"]) || $unselected === array()) {
$where2 = $where; $where2 = $where;
if (is_array($_POST["check"])) { if (is_array($_POST["check"])) {
$where2[] = "($where_check)"; $where2[] = "($where_check)";
@ -99,7 +100,7 @@ if ($_POST && !$error) {
$command = "INSERT"; $command = "INSERT";
$query = "INTO $query"; $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")); $result = queries($command . " $query" . ($_POST["all"] ? ($where ? "\nWHERE " . implode(" AND ", $where) : "") : "\nWHERE $where_check"));
$affected = $connection->affected_rows; $affected = $connection->affected_rows;
} else { } else {
@ -154,7 +155,7 @@ if ($_POST && !$error) {
foreach ($matches2[1] as $i => $col) { foreach ($matches2[1] as $i => $col) {
$set[idf_escape($cols[$i])] = ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $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) { if (!$result) {
break; break;
} }