function _mysql_make_update_tp(&$stack, $qstruct, &$cb_list) { global $ERROR; $mode = $qstruct["MODE"]; if ($mode == "DELETE") { // this need not depend on $qstruct["DATA"] return _mysql_make_delete_tp($stack, $qstruct, $cb_list); } $res = ""; $table = $qstruct["TABLE"]; $realtable = _db_realname($table); $realtable_tp = _db_2temporal($realtable); $autoinc = _db_autoinc($table); $version = _db_version($table); $deleted = _db_extfield($table, "deleted"); $fields = implode(", ", _db_realname($table, $qstruct["ALL_FIELDS"])); $count = 0; foreach ($qstruct["DATA"] as $row) { $idcond = _mysql_make_idcond_base($qstruct, $row); if ($count++) { $res .= "; "; } //$res .= "replace into $realtable_tp($fields) "; $res .= "insert into {$realtable_tp}({$fields}) "; $where = _mysql_make_idwhere($qstruct, $row); if ($ERROR) { return null; } $newdata = ""; $rowcount = 0; $oldcount = 0; foreach ($qstruct["ALL_FIELDS"] as $field) { $realfield = _db_realname($table, $field); if ($rowcount++) { $newdata .= ", "; } if ($field == $deleted) { $row[$field] = false; } if ($mode == "INSERT" && $field == $autoinc || $field == $version) { // give AUTO_INCREMENT / versioning a chance $newdata .= "null"; } elseif (array_key_exists($field, $row) && in_array($field, $qstruct["UPDATE_FIELDS"])) { // take new value $value = @$row[$field]; if ($field == $autoinc && $mode == "INSERT") { $value = null; } if (!@$qstruct["RAW_MODE"] && !_mysql_check_allref($stack, $table, $field, $value, $mode, $idcond)) { return null; } $newdata .= db_esc_sql($value); } elseif ($mode == "REPLACE") { // caution: we cannot be sure that the data already exists, handle that case $newdata .= "case when exists(select {$realfield} from {$realtable} where {$where}) then (select max({$realfield}) from {$realtable} where {$where}) else null end"; } else { // fallback to old value from the db $oldcount++; $newdata .= $realfield; } } if ($mode == "REPLACE") { $res .= "select {$newdata}"; } elseif (($oldcount || $mode == "UPDATE") && $mode != "INSERT") { $res .= "select {$newdata} from {$realtable} where {$where}"; } else { $res .= "values ({$newdata})"; } $cb_list[] = $qstruct["CB"]; } return $res; }
function _db_realname($tp_table, $field = null) { global $SCHEMA; global $RAW_ID; if (is_array($field)) { // structured case $res = array(); foreach ($field as $item) { $res[] = _db_realname($tp_table, $item); } return $res; } if (is_string($field) && preg_match("/^\\\\(.*)/", $field, $matches)) { return $matches[1]; } if (is_string($field) && preg_match("/^({$RAW_ID})\\.({$RAW_ID})\$/", $field, $matches)) { //return $matches[1] . "." . _db_realname($matches[1], $matches[2]); return $field; } if (is_array($tp_table)) { // no _exact_ table given -> search for one $tlist = ""; $res = ""; $res2 = ""; $count = 0; // number of matches foreach ($tp_table as $alias => $test) { if (is_array($test)) { $test = $alias; } $tlist .= "[{$test}]"; $test2 = _db_realname($test, $field); if ($test2) { $count++; if (!$res) { $res = _db_realname($test); $res2 = $test2; } } } if ($res2) { if ($count > 1 && $field) { // resolve ambiguity by prepending return "{$res}.{$res2}"; } return $res2; } die("no table found for field '{$field}' ({$tlist})\n"); } // normal case _db_temporal($tp_table, $table); if (!$table || !@$SCHEMA[$table]) { // cannot translate return ""; } if ($field) { return @$SCHEMA[$table]["FIELDS"][$field]["REALNAME"]; } return @$SCHEMA[$table]["REALNAME"]; }