You are here

function advagg_install_change_table_collation in Advanced CSS/JS Aggregation 7.2

Convert the table to the specified collation.

Parameters

string $table_name: Perform the operation on this table.

array $fields: An array of field names.

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

array $schema_fields: An array of field definitions.

Return value

array Returns an array of tables and column names.

6 calls to advagg_install_change_table_collation()
advagg_install in ./advagg.install
Implements hook_install().
advagg_sri_install in advagg_sri/advagg_sri.install
Implements hook_install().
advagg_update_7207 in ./advagg.install
Update schema making the varchar columns char. Change utf8_bin to ascii_bin.
advagg_update_7209 in ./advagg.install
Update schema making it match the definition.
advagg_validator_install in advagg_validator/advagg_validator.install
Implements hook_install().

... See full list

File

./advagg.install, line 2671
Handles Advanced Aggregation installation and upgrade tasks.

Code

function advagg_install_change_table_collation($table_name, array $fields, $collation, array $schema_fields) {
  $db_type = Database::getConnection()
    ->databaseType();

  // Skip if not MySQL.
  if ($db_type !== 'mysql') {
    return 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)) {
      continue;
    }
    $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);
    }
    db_query($query);
  }
}