You are here

function apdqc_admin_change_table_collation_queries in Asynchronous Prefetch Database Query Cache 7

Convert the table to the specified collation.

Parameters

string $table_name: Perform the operation on this table.

string $collation: The db collation to change to table columns to.

bool $perform_alter: Set to TRUE to actually perform the alter.

array $fields: An array of field names.

array $schema_fields: An array of field definitions.

Return value

array Returns an array of tables and column names.

4 calls to apdqc_admin_change_table_collation_queries()
apdqc_admin_change_table_collation in ./apdqc.admin.inc
Convert cache tables collation to utf8_bin/ascii_bin.
apdqc_admin_semaphore_table_update_schema in ./apdqc.admin.inc
Update the semaphore table schema.
apdqc_admin_sessions_table_duplicates in ./apdqc.admin.inc
See if the sessions table needs to be updated.
apdqc_admin_sessions_table_update_schema in ./apdqc.admin.inc
Update the sessions table schema.

File

./apdqc.admin.inc, line 825
Admin page callbacks for the apdqc module.

Code

function apdqc_admin_change_table_collation_queries($table_name, $collation, $perform_alter, $fields, $schema_fields = array()) {
  $db_type = Database::getConnection()
    ->databaseType();
  $tables_altered = array();
  if ($db_type !== 'mysql' || !db_table_exists($table_name)) {
    return $tables_altered;
  }
  if (empty($schema_fields)) {
    $schema = apdqc_get_full_schema();
    if (isset($schema[$table_name])) {
      $schema_fields = $schema[$table_name]['fields'];
    }
    else {
      $perform_alter = FALSE;
    }
  }
  $table_name = Database::getConnection()
    ->prefixTables('{' . db_escape_table($table_name) . '}');
  $results = db_query("SHOW FULL FIELDS FROM {$table_name}")
    ->fetchAllAssoc('Field');
  $db_schema = Database::getConnection()
    ->schema();
  foreach ($results as $row) {
    if (!in_array($row->Field, $fields) || $row->Collation === $collation) {
      continue;
    }
    if (!$perform_alter) {
      $tables_altered[$table_name][$row->Field] = $row->Collation;
    }
    else {
      $charset = strtolower(substr($collation, 0, strpos($collation, '_')));
      $query = "ALTER TABLE {$table_name} CHANGE `{$row->Field}` `{$row->Field}` {$row->Type}";
      $query .= " CHARACTER SET {$charset} COLLATE {$collation}";
      if (isset($schema_fields[$row->Field]['not null'])) {
        if ($schema_fields[$row->Field]['not null']) {
          $query .= ' NOT NULL';
        }
        else {
          $query .= ' NULL';
        }
      }

      // $schema_fields[$row->Field]['default'] can be NULL, so we explicitly
      // check for the key here.
      if (isset($schema_fields[$row->Field]) && is_array($schema_fields[$row->Field]) && array_key_exists('default', $schema_fields[$row->Field])) {
        $default = $schema_fields[$row->Field]['default'];
        if (is_string($default)) {
          $default = "'" . $default . "'";
        }
        elseif (!isset($default)) {
          $default = 'NULL';
        }
        $query .= ' DEFAULT ' . $default;
      }
      if (empty($schema_fields[$row->Field]['not null']) && !isset($schema_fields[$row->Field]['default'])) {
        $query .= ' DEFAULT NULL';
      }

      // Add column comment.
      if (!empty($schema_fields[$row->Field]['description'])) {
        $query .= ' COMMENT ' . $db_schema
          ->prepareComment($schema_fields[$row->Field]['description'], 255);
      }
      if (function_exists('apdqc_query')) {
        $mysqli = apdqc_query(array(
          $table_name,
        ), array(
          '*',
        ), $query, array(
          'async' => TRUE,
          'log' => FALSE,
          'get_mysqli' => TRUE,
        ));
        $good = TRUE;
        if (isset($mysqli->thread_id)) {
          if (apdqc_kill_metadata_lock($mysqli->thread_id)) {
            $tables_altered[$table_name][$row->Field] = 'metadata lock';
            $good = FALSE;
          }
        }
        if ($good) {
          $tables_altered[$table_name][$row->Field] = 'done';
        }
      }
      else {
        db_query($query);
        $tables_altered[$table_name][$row->Field] = 'done';
      }
    }
  }
  if (function_exists('apdqc_get_db_object')) {
    apdqc_get_db_object(array(), array(), array(
      'async' => FALSE,
      'reap' => TRUE,
    ));
  }
  return $tables_altered;
}