Postgres::editRow PHP Method

editRow() public method

Updates a row in a table
public editRow ( $table, $vars, $nulls, $format, $types, $keyarr ) : -1
$table The table in which to update
$vars An array mapping new values for the row
$nulls An array mapping column => something if it is to be null
$format An array of the data type (VALUE or EXPRESSION)
$types An array of field types
$keyarr An array mapping column => value to update
return -1
    function editRow($table, $vars, $nulls, $format, $types, $keyarr)
    {
        if (!is_array($vars) || !is_array($nulls) || !is_array($format) || !is_array($types)) {
            return -1;
        } else {
            $f_schema = $this->_schema;
            $this->fieldClean($f_schema);
            $this->fieldClean($table);
            // Build clause
            if (sizeof($vars) > 0) {
                foreach ($vars as $key => $value) {
                    $this->fieldClean($key);
                    // Handle NULL values
                    if (isset($nulls[$key])) {
                        $tmp = 'NULL';
                    } else {
                        $tmp = $this->formatValue($types[$key], $format[$key], $value);
                    }
                    if (isset($sql)) {
                        $sql .= ", \"{$key}\"={$tmp}";
                    } else {
                        $sql = "UPDATE \"{$f_schema}\".\"{$table}\" SET \"{$key}\"={$tmp}";
                    }
                }
                $first = true;
                foreach ($keyarr as $k => $v) {
                    $this->fieldClean($k);
                    $this->clean($v);
                    if ($first) {
                        $sql .= " WHERE \"{$k}\"='{$v}'";
                        $first = false;
                    } else {
                        $sql .= " AND \"{$k}\"='{$v}'";
                    }
                }
            }
            // Begin transaction.  We do this so that we can ensure only one row is
            // edited
            $status = $this->beginTransaction();
            if ($status != 0) {
                $this->rollbackTransaction();
                return -1;
            }
            $status = $this->execute($sql);
            if ($status != 0) {
                // update failed
                $this->rollbackTransaction();
                return -1;
            } elseif ($this->conn->Affected_Rows() != 1) {
                // more than one row could be updated
                $this->rollbackTransaction();
                return -2;
            }
            // End transaction
            return $this->endTransaction();
        }
    }
Postgres