field_sql_storage.install 3.58 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?php

3 4
/**
 * @file
5
 * Install, update, and uninstall functions for the Field SQL Storage module.
6 7
 */

8
/**
9
 * Implements hook_schema().
10
 */
Dries's avatar
 
Dries committed
11 12 13 14 15
function field_sql_storage_schema() {
  $schema = array();

  // Dynamic (data) tables.
  if (db_table_exists('field_config')) {
16
    $fields = field_read_fields(array(), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
Dries's avatar
 
Dries committed
17 18
    drupal_load('module', 'field_sql_storage');
    foreach ($fields as $field) {
19 20 21
      if ($field['storage']['type'] == 'field_sql_storage') {
        $schema += _field_sql_storage_schema($field);
      }
Dries's avatar
 
Dries committed
22 23 24 25
    }
  }
  return $schema;
}
26 27

/**
28
 * Writes field data directly to SQL storage.
29
 *
30
 * @ingroup update_api
31
 */
32
function _update_8000_field_sql_storage_write($entity_type, $bundle, $entity_id, $revision_id, $field_name, $data) {
33 34 35 36
  $table_name = "field_data_{$field_name}";
  $revision_name = "field_revision_{$field_name}";

  db_delete($table_name)
37
    ->condition('entity_type', $entity_type)
38 39 40
    ->condition('entity_id', $entity_id)
    ->execute();
  db_delete($revision_name)
41
    ->condition('entity_type', $entity_type)
42 43 44 45 46 47 48 49
    ->condition('entity_id', $entity_id)
    ->condition('revision_id', $revision_id)
    ->execute();

  $columns = array();
  foreach ($data as $langcode => $items) {
    foreach ($items as $delta => $item) {
      $record = array(
50
        'entity_type' => $entity_type,
51 52 53 54
        'entity_id' => $entity_id,
        'revision_id' => $revision_id,
        'bundle' => $bundle,
        'delta' => $delta,
55
        'langcode' => $langcode,
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      );
      foreach ($item as $column => $value) {
        $record[_field_sql_storage_columnname($field_name, $column)] = $value;
      }

      $records[] = $record;
      // Record the columns used.
      $columns += $record;
    }
  }

  if ($columns) {
    $query = db_insert($table_name)->fields(array_keys($columns));
    $revision_query = db_insert($revision_name)->fields(array_keys($columns));
    foreach ($records as $record) {
      $query->values($record);
      if ($revision_id) {
        $revision_query->values($record);
      }
    }
    $query->execute();
    $revision_query->execute();
  }
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

/**
 * Changes field language into langcode.
 */
function field_sql_storage_update_8000(&$sandbox) {
  // Prepare updated schema data structures.
  $primary_key_data = array (
    'entity_type',
    'entity_id',
    'deleted',
    'delta',
    'langcode',
  );
  $primary_key_revision = array (
    'entity_type',
    'entity_id',
    'revision_id',
    'deleted',
    'delta',
    'langcode',
  );
  $langcode_index = array(
    'langcode',
  );
  $field_langcode = array(
    'type' => 'varchar',
    'length' => 32,
    'not null' => true,
    'default' => '',
  );

  // Retrieve field data.
112
  $fields = _update_7000_field_read_fields(array('storage_type' => 'field_sql_storage'));
113 114 115 116 117 118 119 120

  // Update schema.
  foreach ($fields as $field) {
    $data_table = _field_sql_storage_tablename($field);
    $revision_table = _field_sql_storage_revision_tablename($field);
    $table_info = array($data_table => $primary_key_data, $revision_table => $primary_key_revision);

    foreach ($table_info as $table => $primary_key) {
121 122 123 124 125 126 127 128 129
      // Do not attempt to rename the 'language' column for fields that already
      // contain it (created during the upgrade before this update function).
      if (db_field_exists($table, 'language')) {
        db_drop_primary_key($table);
        db_drop_index($table, 'language');
        db_change_field($table, 'language', 'langcode', $field_langcode);
        db_add_primary_key($table, $primary_key);
        db_add_index($table, 'langcode', $langcode_index);
      }
130 131 132
    }
  }
}