You are here

function apdqc_admin_change_table_collation in Asynchronous Prefetch Database Query Cache 7

Convert cache tables collation to utf8_bin/ascii_bin.

Parameters

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

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

array $cache_tables: Pass in a set of tables if you do not wish to operate on all cache tables.

Return value

array Returns an array of tables and column names.

4 calls to apdqc_admin_change_table_collation()
apdqc_admin_convert_table_collations_to in ./apdqc.admin.inc
Convert cache table collations.
apdqc_admin_operations_form in ./apdqc.admin.inc
Form builder; perform apdqc operations.
apdqc_disable in ./apdqc.install
Implements hook_disable().
apdqc_modules_installed in ./apdqc.module
Implements hook_modules_installed().

File

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

Code

function apdqc_admin_change_table_collation($perform_alter = FALSE, $collation = 'ascii_bin', array $cache_tables = array()) {
  if (empty($cache_tables)) {
    $cache_tables = apdqc_get_cache_tables(TRUE, TRUE);
  }
  $db_type = Database::getConnection()
    ->databaseType();
  $tables_altered = array();
  if ($db_type === 'mysql') {
    $fields = array(
      'cid',
    );
    $charset = strtoupper(substr($collation, 0, strpos($collation, '_')));
    foreach ($cache_tables as $table_name => $schema) {

      // Skip cache_page if locale module is enabled and charset is ascii.
      if (strpos($table_name, 'cache_page') !== FALSE && module_exists('locale') && $charset === 'ASCII') {

        // Perform the operation, but use utf8_bin.
        $tables_altered += apdqc_admin_change_table_collation_queries($table_name, 'utf8_bin', $perform_alter, $fields, $schema['fields']);
        continue;
      }

      // Quick test to see if the charset will work without dataloss.
      // Only test if not UTF8.
      if ($charset !== 'UTF8' && db_table_exists($table_name)) {
        $results = db_query("SELECT COUNT(*) AS bad_translation FROM {{$table_name}} WHERE cid <> CONVERT(cid USING {$charset})")
          ->fetchAssoc();
        if (!empty($results['bad_translation'])) {
          continue;
        }
      }

      // Perform the operation.
      $tables_altered += apdqc_admin_change_table_collation_queries($table_name, $collation, $perform_alter, $fields, $schema['fields']);
    }
  }
  return $tables_altered;
}