Scalr\Upgrade\Updates\Update20150116130622::recrypt PHP Метод

recrypt() публичный Метод

Reencrypts specified fields
public recrypt ( string $table, string[] $fields, string $where = '', string[] $pks = ['id'], Scalr\Util\CryptoTool $source = null ) : integer
$table string Table name
$fields string[] Fields name
$where string WHERE statement for SELECT query
$pks string[] Primary keys names
$source Scalr\Util\CryptoTool
Результат integer Returns number of affected rows
    public function recrypt($table, $fields, $where = '', $pks = ['id'], CryptoTool $source = null)
    {
        if ($source === null) {
            $source = $this->source;
        }
        $this->console->out("Reencrypting table '{$table}' fields:\n\t" . implode("\n\t", $fields));
        $names = '`' . implode('`, `', array_merge($pks, $fields)) . '`';
        $data = $this->db->Execute("SELECT {$names} FROM `{$table}` {$where} FOR UPDATE;");
        $params = '`' . implode('` = ?, `', $fields) . '` = ?';
        $where = '`' . implode('` = ? AND `', $pks) . '` = ?';
        $stmt = $this->db->Prepare("UPDATE `{$table}` SET {$params} WHERE {$where};");
        $affected = 0;
        foreach ($data as $entry) {
            $in = [];
            foreach ($fields as $field) {
                $in[] = $this->target->encrypt($source->_decrypt($entry[$field]));
            }
            foreach ($pks as $pk) {
                $in[] = $entry[$pk];
            }
            $this->db->Execute($stmt, $in);
            $affected += $this->db->Affected_Rows();
        }
        $this->console->out("Updated {$affected} rows!\n");
        return $affected;
    }