1 charset_converter.inc public DatabaseCharsetConverter::convertTableFields($table_name, $charset = NULL, $collation = NULL)

Converts a table's field to a desired character set and collation.

Parameters

string $table_name: The table name to convert. This should be the actual table name in the database, with any table prefix already prepended. (Optional) The character set. Defaults to the constructor value.

string $collation: (Optional) The collation. Defaults to the constructor value.

Return value

bool: TRUE if the table fields are converted successfully, FALSE on failure.

Throws

PDOException

File

core/includes/database/charset_converter.inc, line 211

Class

DatabaseCharsetConverter
Character set converter for database tables.

Code

public function convertTableFields($table_name, $charset = NULL, $collation = NULL) {
  $return = TRUE;
  $results = $this->connection->query("SHOW FULL FIELDS FROM `$table_name`")->fetchAllAssoc('Field');
  $charset = $charset ? : $this->charset;
  $collation = $collation ? : $this->collation;
  foreach ($results as $row) {
    // Skip fields that don't have collation, as they are probably int or similar.
    // or if we are using that collation for this field already save a query
    // or is not binary.
    if (!$row->Collation || $row->Collation === $collation || strpos($row->Collation, '_bin') !== FALSE) {
      continue;
    }
    $default = '';
    if ($row->Default !== NULL) {
      $default = 'DEFAULT ' . ($row->Default == "CURRENT_TIMESTAMP" ? "CURRENT_TIMESTAMP" : ":default");
    }
    elseif ($row->Null == 'YES' && $row->Key == '') {
      if ($row->Type == 'timestamp') {
        $default = 'NULL ';
      }
      $default .= 'DEFAULT NULL';
    }

    $sql = "ALTER TABLE `$table_name`
              MODIFY `" . $row->Field . "` " .
      $row->Type . " " .
      "CHARACTER SET :charset COLLATE :collation " .
      ($row->Null == "YES" ? "" : "NOT NULL ") .
      $default . " " .
      $row->Extra . " " .
      "COMMENT :comment";

    $params = array(
      ':charset' => $charset,
      ':collation' => $collation,
      ':comment' => $row->Comment,
    );
    if (strstr($default, ':default')) {
      $params[':default'] = $row->Default;
    }
    $result = $this->connection->query($sql, $params);
    if (!$result) {
      $return = FALSE;
    }
  }
  return $return;
}