You are here

function _field_sql_norevisions_schema in Field SQL norevisions 7

Return the database schema for a field. This may contain one or more tables. Each table will contain the columns relevant for the specified field. Leave the $field's 'columns' and 'indexes' keys empty to get only the base schema.

Parameters

$field: The field structure for which to generate a database schema.

Return value

One or more tables representing the schema for the field.

4 calls to _field_sql_norevisions_schema()
FieldSqlNoRevisionsTestCase::testFieldUpdateFailure in ./field_sql_norevisions.test
Test that failure to create fields is handled gracefully.
field_sql_norevisions_field_storage_create_field in ./field_sql_norevisions.module
Implements hook_field_storage_create_field().
field_sql_norevisions_field_storage_update_field in ./field_sql_norevisions.module
Implements hook_field_storage_update_field().
field_sql_norevisions_schema in ./field_sql_norevisions.install
Implements hook_schema().

File

./field_sql_norevisions.module, line 95
Default implementation of the field storage API.

Code

function _field_sql_norevisions_schema($field) {
  $deleted = $field['deleted'] ? 'deleted ' : '';
  $current = array(
    'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
    'fields' => array(
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The entity type this data is attached to',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
      ),
      'deleted' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'A boolean indicating whether this data item has been deleted',
      ),
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
      ),
      // @todo Consider storing language as integer.
      'language' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The language for this data item.',
      ),
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The sequence number for this data item, used for multi-value fields',
      ),
    ),
    'primary key' => array(
      'entity_type',
      'entity_id',
      'deleted',
      'delta',
      'language',
    ),
    'indexes' => array(
      'entity_type' => array(
        'entity_type',
      ),
      'bundle' => array(
        'bundle',
      ),
      'deleted' => array(
        'deleted',
      ),
      'entity_id' => array(
        'entity_id',
      ),
      'revision_id' => array(
        'revision_id',
      ),
      'language' => array(
        'language',
      ),
    ),
  );
  $field += array(
    'columns' => array(),
    'indexes' => array(),
    'foreign keys' => array(),
  );

  // Add field columns.
  foreach ($field['columns'] as $column_name => $attributes) {
    $real_name = _field_sql_norevisions_columnname($field['field_name'], $column_name);
    $current['fields'][$real_name] = $attributes;
  }

  // Add indexes.
  foreach ($field['indexes'] as $index_name => $columns) {
    $real_name = _field_sql_norevisions_indexname($field['field_name'], $index_name);
    foreach ($columns as $column_name) {
      $current['indexes'][$real_name][] = _field_sql_norevisions_columnname($field['field_name'], $column_name);
    }
  }

  // Add foreign keys.
  foreach ($field['foreign keys'] as $specifier => $specification) {
    $real_name = _field_sql_norevisions_indexname($field['field_name'], $specifier);
    $current['foreign keys'][$real_name]['table'] = $specification['table'];
    foreach ($specification['columns'] as $column => $referenced) {
      $sql_storage_column = _field_sql_norevisions_columnname($field['field_name'], $column_name);
      $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
    }
  }
  return array(
    _field_sql_norevisions_tablename($field) => $current,
  );
}