PMA\libraries\controllers\table\TableSearchController::replace PHP Метод

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

Replaces a given string in a column with a give replacement
public replace ( integer $columnIndex, string $find, string $replaceWith, boolean $useRegex, string $charSet ) : void
$columnIndex integer index of the column
$find string string to find in the column
$replaceWith string string to replace with
$useRegex boolean to use Regex replace or not
$charSet string character set of the connection
Результат void
    public function replace($columnIndex, $find, $replaceWith, $useRegex, $charSet)
    {
        $column = $this->_columnNames[$columnIndex];
        if ($useRegex) {
            $toReplace = $this->_getRegexReplaceRows($columnIndex, $find, $replaceWith, $charSet);
            $sql_query = "UPDATE " . Util::backquote($this->table) . " SET " . Util::backquote($column) . " = CASE";
            if (is_array($toReplace)) {
                foreach ($toReplace as $row) {
                    $sql_query .= "\n WHEN " . Util::backquote($column) . " = '" . $GLOBALS['dbi']->escapeString($row[0]) . "' THEN '" . $GLOBALS['dbi']->escapeString($row[1]) . "'";
                }
            }
            $sql_query .= " END" . " WHERE " . Util::backquote($column) . " RLIKE '" . $GLOBALS['dbi']->escapeString($find) . "' COLLATE " . $charSet . "_bin";
            // here we
            // change the collation of the 2nd operand to a case sensitive
            // binary collation to make sure that the comparison
            // is case sensitive
        } else {
            $sql_query = "UPDATE " . Util::backquote($this->table) . " SET " . Util::backquote($column) . " =" . " REPLACE(" . Util::backquote($column) . ", '" . $find . "', '" . $replaceWith . "')" . " WHERE " . Util::backquote($column) . " LIKE '%" . $find . "%' COLLATE " . $charSet . "_bin";
            // here we
            // change the collation of the 2nd operand to a case sensitive
            // binary collation to make sure that the comparison
            // is case sensitive
        }
        $this->dbi->query($sql_query, null, DatabaseInterface::QUERY_STORE);
        $GLOBALS['sql_query'] = $sql_query;
    }

Usage Example

 /**
  * Test for replace
  *
  * @return void
  */
 public function testReplace()
 {
     $tableSearch = new TableSearchController("zoom", null);
     $columnIndex = 0;
     $find = "Field";
     $replaceWith = "Column";
     $useRegex = false;
     $charSet = "UTF-8";
     $tableSearch->replace($columnIndex, $find, $replaceWith, $useRegex, $charSet);
     $sql_query = $GLOBALS['sql_query'];
     $result = "UPDATE `PMA_BookMark` SET `Field1` = " . "REPLACE(`Field1`, 'Field', 'Column') " . "WHERE `Field1` LIKE '%Field%' COLLATE UTF-8_bin";
     $this->assertEquals($result, $sql_query);
 }