You are here

function apdqc_admin_sessions_table_duplicates in Asynchronous Prefetch Database Query Cache 7

See if the sessions table needs to be updated.

Can alter forwards and backwards as well.

Parameters

bool $perform_alter: Set the TRUE to alter the database.

bool $reverse: Set to TRUE to revert back to standard schema.

array $schema: Array of schema.

Return value

array Returns empty array if a perform alter was done. Otherwise returns the schema array for the table that needs to be altered.

3 calls to apdqc_admin_sessions_table_duplicates()
apdqc_admin_operations_form in ./apdqc.admin.inc
Form builder; perform apdqc operations.
apdqc_admin_sessions_table_update_schema in ./apdqc.admin.inc
Update the sessions table schema.
apdqc_modules_installed in ./apdqc.module
Implements hook_modules_installed().

File

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

Code

function apdqc_admin_sessions_table_duplicates($perform_alter = FALSE, $reverse = FALSE, $schema = array()) {
  if ($reverse) {
    if (empty($schema)) {
      $schema = apdqc_get_full_schema(FALSE);
    }
    $collation = 'utf8_general_ci';
  }
  else {
    if (empty($schema)) {
      $schema = apdqc_get_full_schema(TRUE);
    }
    $collation = 'ascii_bin';
  }
  $needs_conversion = array();
  foreach ($schema as $table => $values) {
    if ($table === 'sessions') {
      continue;
    }
    foreach ($values['fields'] as $column => $attributes) {

      // See if the schema looks very simalr to sessions table.
      if (($column === 'sid' || $column === 'ssid') && !empty($attributes['length']) && $attributes['length'] >= 43 && !empty($attributes['type']) && stripos($attributes['type'], 'char') !== FALSE && !empty($attributes['description']) && stripos($attributes['description'], 'session') !== FALSE) {
        $table_name = Database::getConnection()
          ->prefixTables('{' . db_escape_table($table) . '}');
        $results = db_query("SHOW FULL FIELDS FROM {$table_name} WHERE Field = :column", array(
          ':column' => $column,
        ))
          ->fetchAllAssoc('Field');

        // If yes see if the Collation is not the target collation.
        if ($results[$column]->Collation !== $collation) {
          $needs_conversion[$table] = $values;
          if ($perform_alter) {
            $columns = array(
              $column,
            );
            apdqc_admin_change_table_collation_queries($table, $collation, TRUE, $columns);
          }
        }
        if ($results[$column]->Type !== $schema[$table]['fields'][$column]['type'] . '(' . $schema[$table]['fields'][$column]['length'] . ')') {
          $needs_conversion[$table] = $values;
          if ($perform_alter) {
            db_change_field($table, $column, $column, $schema[$table]['fields'][$column]);
          }
        }
      }
    }
  }
  return $needs_conversion;
}