You are here

function apdqc_schema_alter in Asynchronous Prefetch Database Query Cache 7

Implements hook_schema_alter().

File

./apdqc.module, line 362
Asynchronous Prefetch Database Query Cache module.

Code

function apdqc_schema_alter(&$schema) {
  $collation = variable_get('apdqc_table_collations', APDQC_TABLE_COLLATIONS);
  $table_indexes = variable_get('apdqc_table_indexes', APDQC_TABLE_INDEXES);
  $ascii = array(
    'binary' => TRUE,
    'collation' => 'ascii_bin',
    'charset' => 'ascii',
    'mysql_character_set' => 'ascii',
  );
  $utf8 = array(
    'binary' => TRUE,
    'collation' => 'utf8_bin',
    'charset' => 'utf8',
    'mysql_character_set' => 'utf8',
  );
  foreach ($schema as $table_name => &$values) {
    if (strpos($table_name, 'semaphore') === 0) {
      $schema[$table_name]['primary key'] = array(
        'name',
        'value',
        'expire',
      );
      $schema[$table_name]['fields']['name'] += $ascii;
      if (function_exists('apdqc_lock_base85_encode')) {

        // Length of _apdqc_lock_id() is always 20 or less.
        $schema[$table_name]['fields']['value']['length'] = 20;
      }
      else {

        // Length of _lock_id() is always 33 or less.
        $schema[$table_name]['fields']['value']['length'] = 33;
      }
      $schema[$table_name]['fields']['value'] += $ascii;
      continue;
    }
    if (strpos($table_name, 'session') === 0) {

      // Length of drupal_random_key() is always 43.
      $schema[$table_name]['fields']['sid']['length'] = 43;
      $schema[$table_name]['fields']['sid']['type'] = 'char';
      $schema[$table_name]['fields']['sid'] += $ascii;
      $schema[$table_name]['fields']['ssid']['length'] = 43;
      $schema[$table_name]['fields']['ssid']['type'] = 'char';
      $schema[$table_name]['fields']['ssid'] += $ascii;

      // Max length of IPv6 tunneled is 45.
      $schema[$table_name]['fields']['hostname']['length'] = 45;
      $schema[$table_name]['fields']['hostname'] += $ascii;
      continue;
    }
    foreach ($values['fields'] as $column => &$attributes) {

      // Convert other session schemas to match the new session schema.
      if (($column === 'sid' || $column === 'ssid') && !empty($attributes['length']) && $attributes['length'] >= 43 && stripos($attributes['type'], 'char') !== FALSE) {
        $attributes['length'] = 43;
        $attributes['type'] = 'char';
        $attributes += $ascii;
      }
    }
    if (strpos($table_name, 'cache') !== 0) {

      // Skip if not a cache table.
      continue;
    }
    if (empty($schema[$table_name]['fields']['cid'])) {

      // Skip if not a cache table.
      continue;
    }
    if (!empty($collation)) {
      if (!empty($schema[$table_name]['fields']['cid'])) {

        // Force db collation to be *_bin for the cid of cache tables.
        $schema[$table_name]['fields']['cid']['binary'] = TRUE;
        if ($collation === 'ascii_bin') {
          $schema[$table_name]['fields']['cid'] += $ascii;
        }
        else {
          $schema[$table_name]['fields']['cid'] += $utf8;
        }
      }
    }
    if (!empty($table_indexes)) {
      if (!empty($schema[$table_name]['indexes']['expire'])) {

        // Create a expire_created index.
        $schema[$table_name]['indexes']['expire_created'] = array(
          'expire',
          'created',
        );
        unset($schema[$table_name]['indexes']['expire']);
      }
    }

    // Add cache*__truncated_table to the schema.
    if (strpos(strrev($table_name), strrev('__truncated_table')) !== 0 && db_table_exists($table_name . '__truncated_table')) {
      $schema[$table_name . '__truncated_table'] = $schema[$table_name];
    }
  }
}