/*
Update a row

$data array A structured array to be inserted into the KVS.
The array must be completely key-value at its top level.  Below
that, anything that can be serialized into JSON is allowed.
977639
public function update($data, $changepk=false) {
        $map = $this->extractMap($data);
        $keys = $this->extractKeys($data);
        $where = false;
        $wheremap = array();
        $more = '';
        if ( $keys->id ) {
            $where = 'id=:id';
            $wheremap[':id'] = $keys->id;
            // TODO: Can we set this to null?
            if ( $changepk || $keys->uk1 ) {
                $more = ', uk1=:uk1';
            } else {
                unset($map[':uk1']); // Leave uk1 alone
            }
        } else if ( $keys->uk1 ) { // Update using uk1 as where clause
            $where = 'uk1=:uk1';
            $wheremap[':uk1'] = $keys->uk1;
        }
        if ( ! $where ) throw new \Exception('update requires id or pk1 field');

        $wheremap[':foreign_key'] = $this->KVS_FK;
        $wheremap[':secondary_key'] = $this->KVS_SK;
        $sql = "SELECT json_body FROM $this->KVS_TABLE
            WHERE $this->KVS_FK_NAME = :foreign_key AND $this->KVS_SK_NAME = :secondary_key AND $where";
        $rows = $this->PDOX->allRowsDie($sql, $wheremap);
        if ( count($rows) == 0 ) return 0;
        if ( count($rows) > 1 ) {
            throw new \Exception('Where clause selected more than one row');
        }

        $updatemap = array_merge($map, $wheremap);
        $row = $rows[0];
        $old_json = json_decode($row['json_body'], true);
        $copy = array_merge($old_json, $data);
        $copy = self::preStoreCleanup($copy);
        $updatemap[':json_body'] = json_encode($copy);


        $sql = "UPDATE $this->KVS_TABLE SET sk1=:sk1, tk1=:tk1, co1=:co1,
            co2=:co2, json_body=:json_body, updated_at=$this->NOW $more
            WHERE $this->KVS_FK_NAME = :foreign_key AND $this->KVS_SK_NAME = :secondary_key AND $where";

        $stmt = $this->PDOX->queryDie($sql, $updatemap);

        if ( ! $stmt->success ) return false;
        return $stmt->rowCount();
    }
955210
protected function _setData(array $data)
    {
        $pmk = $this->getPrimaryKey();
        if (!empty($data)) {
            if (!isset($data[$pmk]) && !is_null($this->$pmk)) {
                $data[$pmk] = $this->$pmk;
            }
            return $data;
        }
        $columns = Manager::getInstance()->get($this)->getColumns();
        foreach (array_keys($columns) as $property) {
            $key = trim($property, '_');
            if ($key == $pmk && !$this->$pmk) {
                continue;
            }
            $data[$key] = $this->$property;
        }

        return $data;
    }
378536
private function setData(array $data)
    {
        $_documentClass = $this->_documentClass;
        $_edgeClass = $this->_edgeClass;

        if (isset($this->_options[self::ENTRY_FLAT]) && $this->_options[self::ENTRY_FLAT]) {
            $this->_result = $data;
        } else {
            $this->_result = [];

            if ($this->_options[self::ENTRY_TYPE] === Collection::TYPE_EDGE) {
                foreach ($data as $row) {
                    $this->_result[] = $_edgeClass::createFromArray($row, $this->_options);
                }
            } else {
                foreach ($data as $row) {
                    $this->_result[] = $_documentClass::createFromArray($row, $this->_options);
                }
            }
        }
    }
1046695
function insertSiblingData($FieldValueArray=array(),$ConditionString=null,$Rest=null)
    {
		$this->lock();
    	//Find the Sibling
        $Arguments=func_get_args();
        array_shift($Arguments); //first argument, the array
        array_shift($Arguments);
        if ($ConditionString) $ConditionString="WHERE $ConditionString";
        $Query="SELECT {$this->right()} AS `Right`".
        	" FROM {$this->table()} $ConditionString";

        array_unshift($Arguments,$Query);
        $Sibl=call_user_func_array("Jf::sql",$Arguments);

        $Sibl=$Sibl[0];
        if ($Sibl==null)
        {
            $Sibl["Left"]=$Sibl["Right"]=0;
        }
        Jf::sql("UPDATE {$this->table()} SET {$this->right()} = {$this->right()} + 2 WHERE {$this->right()} > ?",$Sibl["Right"]);
        Jf::sql("UPDATE {$this->table()} SET {$this->left()} = {$this->left()} + 2 WHERE {$this->left()} > ?",$Sibl["Right"]);

        $FieldsString=$ValuesString="";
        $Values=array();
        if ($FieldValueArray)
        foreach($FieldValueArray as $k=>$v)
        {
            $FieldsString.=",";
            $FieldsString.="`".$k."`";
            $ValuesString.=",?";
            $Values[]=$v;
        }

        $Query= "INSERT INTO {$this->table()} ({$this->left()},{$this->right()} $FieldsString) ".
        	"VALUES(?,? $ValuesString)";
        array_unshift($Values,$Sibl["Right"]+2);
        array_unshift($Values,$Sibl["Right"]+1);
        array_unshift($Values,$Query);

        $Res=call_user_func_array("Jf::sql",$Values);
		$this->unlock();
        return $Res;
    }
664172
public function upsert($table, $insertFieldValues, $updateFieldValues = [], $primaryKey = null)
    {
        if (!$primaryKey) {
            $primaryKey = (string)key($insertFieldValues);
        }

        if (!$updateFieldValues) {
            $updateFieldValues = $insertFieldValues;
        }

        $fields = array_keys($insertFieldValues);

        $bind = $insertFieldValues;
        $updates = [];
        foreach ($updateFieldValues as $k => $v) {
            $field = is_string($k) ? $k : $v;
            if ($primaryKey === $field) {
                continue;
            }

            if (is_int($k)) {
                $updates[] = "[$field]=:{$field}_dku";
                $bind["{$field}_dku"] = $insertFieldValues[$field];
            } elseif ($v instanceof AssignmentInterface) {
                $v->setFieldName($k);
                $updates[] = $v->getSql();
                /** @noinspection SlowArrayOperationsInLoopInspection */
                $bind = array_merge($bind, $v->getBind());
            } else {
                $updates[] = $v;
            }
        }

        $insertFieldsSql = '[' . implode('],[', $fields) . ']';
        $insertValuesSql = ':' . implode(',:', $fields);

        $updateFieldsSql = implode(',', $updates);

        $sql = 'INSERT' . " INTO {$this->_escapeIdentifier($table)}($insertFieldsSql) VALUES($insertValuesSql) ON DUPLICATE KEY UPDATE $updateFieldsSql";

        return $this->execute('insert', $sql, $bind);
    }
115565
public function update($table, $data, $keys, $options = []) {
		return $this->object->update($table, $data, $keys, $options);
	}
665665
function updateTableData( columnIndex ) {
  let cell;
  const rows = this.ui.tableBody.querySelectorAll( 'tr' );
  this.tableData = [];
  columnIndex = columnIndex || this.sortColumnIndex;

  for ( let i = 0, len = rows.length; i < len; ++i ) {
    cell = rows[i].cells[columnIndex];
    if ( cell ) {
      cell = cell.textContent.trim();
    }
    this.tableData.push( [ cell, rows[i] ] );
  }

  const sortType = this.ui.sortButton.getAttribute( 'data-sort_type' );
  this.tableData.sort( this.tableDataSorter( this.sortDirection, sortType ) );

  return this.tableData;
}
932260
public function updateRows(array $rows): array
    {
        if (null === $this->data) {
            $this->buildIndex();
        }

        $list = [];
        foreach ($rows as $key => $row) {
            $key = (string)$key;
            if ($this->hasKey($key)) {
                $this->data[$key] = $list[$key] = $row;
            }
        }

        if ($list) {
            $this->save();
        }

        return $list;
    }
821822
public function insertRows($data, $onDuplicateKeyUpdate = null, $params = [], $safeColumns = [])
    {
        $col = array();
        if ($safeColumns) {
            foreach ($safeColumns as $c) {
                $col[trim($c)] = true;
            }
        }
        $inserts = $cols = [];
        if ($params) {
            $this->params = array_merge($params, $this->params);
        }
        foreach ($data as $pos => $fieldGroup) {
            $inss = [];
            foreach ($fieldGroup as $k => $v) { // generates inserts and saves values to escape
                if (isset($col[$k]) || (!$safeColumns)) {
                    $cols[$k] = '`' . $k . '`';
                    if (isset($applyMethods[$k])) {
                        $inss[] = $applyMethods[$k] . "(:v$pos$k)";
                    } else {
                        $inss[] = ":v$pos$k";
                    }
                    $this->params[":v$pos$k"] = $v;
                }
            }
            if ($inss) {
                $inserts[] = '(' . implode(', ', $inss) . ')';
            }
        }
        if (!count($inserts)) {
            return 0;
        }
        if ($onDuplicateKeyUpdate !== 'ignore') {
            $onDuplicateKeyUpdate = $onDuplicateKeyUpdate ? ' ON DUPLICATE KEY UPDATE ' . $onDuplicateKeyUpdate : '';
            $sql = 'INSERT INTO `' . $this->table . '` (' . implode(', ', $cols) . ') VALUES ' . implode(', ', $inserts) . $onDuplicateKeyUpdate; // generates full sql command
        } else {
            $sql = 'INSERT IGNORE INTO `' . $this->table . '` (' . implode(', ', $cols) . ') VALUES ' . implode(', ', $inserts); // generates full sql command
        }
        return $this->connection->execQuery($sql, $this->params);
    }
374335
function addRow($row, $append = true) {
        if ($append) {
            $this->_data[] = array_values($row);
        } else {
            array_unshift($this->_data, array_values($row));
        }

        $this->_updateRowsCols($row);
    }
1296628
public function updateRows(array $rows): array
    {
        $list = [];
        foreach ($rows as $key => $row) {
            $key = (string)$key;
            if (!$this->hasKey($key)) {
                $list[$key] = null;
            } else {
                $path = $this->getPathFromKey($key);
                $file = $this->getFile($path);
                $list[$key] = $this->saveFile($file, $row);
            }
        }

        return $list;
    }
1121966
private function SetObjData($_table, $_fields, $_data)
    {
        $this->table = $_table;
        $this->fields = $_fields;
        $this->data[] = array();

        if (isset($_data[0]) and is_array($_data[0])) {
            for ($i=0; $i<count($_data); $i++) {
                foreach ($this->fields as $field) {
                    if (isset($_data[$i][$field]) and !$this->IsFieldLocked($field)) {
                        $this->data[$i][$field] = $this->Escape($_data[$i][$field]);
                    }
                }
            }
        } else {
            foreach ($this->fields as $field) {
                if (isset($_data[$field]) and !$this->IsFieldLocked($field)) {
                    $this->data[0][$field] = $this->Escape($_data[$field]);
                }
            }
        }
    }
911619
public function update(array $data)
    {
        $affected = 0;
        foreach ($this->table->data['data'] as &$rs) {
            if ($this->matchWhere($rs)) {
                $rs = array_merge($rs, $data);
                $affected++;
            }
        }

        if ($affected) {
            $this->table->save();
        }

        return $affected;
    }
1160616
public function insert($data) {
        $map = $this->extractMap($data);
        $sql = "INSERT INTO $this->KVS_TABLE ($this->KVS_FK_NAME, $this->KVS_SK_NAME,
            uk1, sk1, tk1, co1, co2, json_body, created_at)
            VALUES (:foreign_key, :secondary_key, :uk1, :sk1, :tk1, :co1, :co2, :json_body, $this->NOW)";
        $map[':foreign_key'] = $this->KVS_FK;
        $map[':secondary_key'] = $this->KVS_SK;

        $copy = self::preStoreCleanup($data);
        $map[':json_body'] = json_encode($copy);
        $stmt = $this->PDOX->queryDie($sql, $map);

        if ( $stmt->success) return(intval($this->PDOX->lastInsertId()));
        return false;
    }
501043
protected function setData($data = [])
    {
        if (!isset($data[self::RS_KEY_CODE])) {
            $data[self::RS_KEY_CODE] = 0;
        }
        if (!isset($data[self::RS_KEY_MSG])) {
            $data[self::RS_KEY_MSG] = 'Success';
        }
        if (!isset($data[self::RS_KEY_TYPE])) {
            $data[self::RS_KEY_TYPE] = self::RESPONSE_XHTML;
        }

        if (!isset($data[self::RS_KEY_DATA])) {
            $data[self::RS_KEY_DATA] = new \stdClass();
        } else {
            if (!$data instanceof \stdClass) {
                $data[self::RS_KEY_DATA] = new \stdClass();
            }
        }
        $this->data = $data;
    }
190886
public function update(array $data = [])
    {
        $sql = 'update `' . $this->table . '` set ';
        $sql .= Util::rangeField(Util::add2points(array_keys($data)));

        if (!is_null($this->where)) {
            $sql .= ' where ' . $this->where;

            $this->where = null;

            $data = array_merge($data, $this->where_data_binding);

            $this->where_data_binding = [];
        }

        $stmt = $this->connection->prepare($sql);

        $data = Sanitize::make($data, true);

        $this->bind($stmt, $data);

        // Execution of the request
        $stmt->execute();

        $r = $stmt->rowCount();

        return (int) $r;
    }
313918
public function update($data = array()) {
    foreach ($data as $key => $value) {
      $this->{$key} = $value;
    }

    $postData = array();
    foreach (static::$properties as $name) {
      $postData[$name] = $this->data[$name];
    }

    $this->client->put(static::$path . '/' . $this->id(), $postData);

    return $this;
  }