Projet

Général

Profil

Paste
Télécharger (28,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / field / modules / field_sql_storage / field_sql_storage.module @ c7768a53

1
<?php
2

    
3
/**
4
 * @file
5
 * Default implementation of the field storage API.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function field_sql_storage_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#field_sql_storage':
14
      $output = '';
15
      $output .= '<h3>' . t('About') . '</h3>';
16
      $output .= '<p>' . t('The Field SQL storage module stores field data in the database. It is the default field storage module; other field storage mechanisms may be available as contributed modules. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
17
      return $output;
18
  }
19
}
20

    
21
/**
22
 * Implements hook_field_storage_info().
23
 */
24
function field_sql_storage_field_storage_info() {
25
  return array(
26
    'field_sql_storage' => array(
27
      'label' => t('Default SQL storage'),
28
      'description' => t('Stores fields in the local SQL database, using per-field tables.'),
29
    ),
30
  );
31
}
32

    
33
/**
34
 * Generate a table name for a field data table.
35
 *
36
 * @param $field
37
 *   The field structure.
38
 * @return
39
 *   A string containing the generated name for the database table
40
 */
41
function _field_sql_storage_tablename($field) {
42
  if ($field['deleted']) {
43
    return "field_deleted_data_{$field['id']}";
44
  }
45
  else {
46
    return "field_data_{$field['field_name']}";
47
  }
48
}
49

    
50
/**
51
 * Generate a table name for a field revision archive table.
52
 *
53
 * @param $name
54
 *   The field structure.
55
 * @return
56
 *   A string containing the generated name for the database table
57
 */
58
function _field_sql_storage_revision_tablename($field) {
59
  if ($field['deleted']) {
60
    return "field_deleted_revision_{$field['id']}";
61
  }
62
  else {
63
    return "field_revision_{$field['field_name']}";
64
  }
65
}
66

    
67
/**
68
 * Generates a table alias for a field data table.
69
 *
70
 * The table alias is unique for each unique combination of field name
71
 * (represented by $tablename), delta_group and language_group.
72
 *
73
 * @param $tablename
74
 *   The name of the data table for this field.
75
 * @param $field_key
76
 *   The numeric key of this field in this query.
77
 * @param $query
78
 *   The EntityFieldQuery that is executed.
79
 *
80
 * @return
81
 *   A string containing the generated table alias.
82
 */
83
function _field_sql_storage_tablealias($tablename, $field_key, EntityFieldQuery $query) {
84
  // No conditions present: use a unique alias.
85
  if (empty($query->fieldConditions[$field_key])) {
86
    return $tablename . $field_key;
87
  }
88

    
89
  // Find the delta and language condition values and append them to the alias.
90
  $condition = $query->fieldConditions[$field_key];
91
  $alias = $tablename;
92
  $has_group_conditions = FALSE;
93

    
94
  foreach (array('delta', 'language') as $column) {
95
    if (isset($condition[$column . '_group'])) {
96
      $alias .= '_' . $column . '_' . $condition[$column . '_group'];
97
      $has_group_conditions = TRUE;
98
    }
99
  }
100

    
101
  // Return the alias when it has delta/language group conditions.
102
  if ($has_group_conditions) {
103
    return $alias;
104
  }
105

    
106
  // Return a unique alias in other cases.
107
  return $tablename . $field_key;
108
}
109

    
110
/**
111
 * Generate a column name for a field data table.
112
 *
113
 * @param $name
114
 *   The name of the field
115
 * @param $column
116
 *   The name of the column
117
 * @return
118
 *   A string containing a generated column name for a field data
119
 *   table that is unique among all other fields.
120
 */
121
function _field_sql_storage_columnname($name, $column) {
122
  return $name . '_' . $column;
123
}
124

    
125
/**
126
 * Generate an index name for a field data table.
127
 *
128
 * @param $name
129
 *   The name of the field
130
 * @param $column
131
 *   The name of the index
132
 * @return
133
 *   A string containing a generated index name for a field data
134
 *   table that is unique among all other fields.
135
 */
136
function _field_sql_storage_indexname($name, $index) {
137
  return $name . '_' . $index;
138
}
139

    
140
/**
141
 * Return the database schema for a field. This may contain one or
142
 * more tables. Each table will contain the columns relevant for the
143
 * specified field. Leave the $field's 'columns' and 'indexes' keys
144
 * empty to get only the base schema.
145
 *
146
 * @param $field
147
 *   The field structure for which to generate a database schema.
148
 * @return
149
 *   One or more tables representing the schema for the field.
150
 */
151
function _field_sql_storage_schema($field) {
152
  $deleted = $field['deleted'] ? 'deleted ' : '';
153
  $current = array(
154
    'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
155
    'fields' => array(
156
      'entity_type' => array(
157
        'type' => 'varchar',
158
        'length' => 128,
159
        'not null' => TRUE,
160
        'default' => '',
161
        'description' => 'The entity type this data is attached to',
162
      ),
163
      'bundle' => array(
164
        'type' => 'varchar',
165
        'length' => 128,
166
        'not null' => TRUE,
167
        'default' => '',
168
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
169
      ),
170
      'deleted' => array(
171
        'type' => 'int',
172
        'size' => 'tiny',
173
        'not null' => TRUE,
174
        'default' => 0,
175
        'description' => 'A boolean indicating whether this data item has been deleted'
176
      ),
177
      'entity_id' => array(
178
        'type' => 'int',
179
        'unsigned' => TRUE,
180
        'not null' => TRUE,
181
        'description' => 'The entity id this data is attached to',
182
      ),
183
      'revision_id' => array(
184
        'type' => 'int',
185
        'unsigned' => TRUE,
186
        'not null' => FALSE,
187
        'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
188
      ),
189
      // @todo Consider storing language as integer.
190
      'language' => array(
191
        'type' => 'varchar',
192
        'length' => 32,
193
        'not null' => TRUE,
194
        'default' => '',
195
        'description' => 'The language for this data item.',
196
      ),
197
      'delta' => array(
198
        'type' => 'int',
199
        'unsigned' => TRUE,
200
        'not null' => TRUE,
201
        'description' => 'The sequence number for this data item, used for multi-value fields',
202
      ),
203
    ),
204
    'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
205
    'indexes' => array(
206
      'entity_type' => array('entity_type'),
207
      'bundle' => array('bundle'),
208
      'deleted' => array('deleted'),
209
      'entity_id' => array('entity_id'),
210
      'revision_id' => array('revision_id'),
211
      'language' => array('language'),
212
    ),
213
  );
214

    
215
  $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
216
  // Add field columns.
217
  foreach ($field['columns'] as $column_name => $attributes) {
218
    $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
219
    $current['fields'][$real_name] = $attributes;
220
  }
221

    
222
  // Add indexes.
223
  foreach ($field['indexes'] as $index_name => $columns) {
224
    $real_name = _field_sql_storage_indexname($field['field_name'], $index_name);
225
    foreach ($columns as $column_name) {
226
      // Indexes can be specified as either a column name or an array with
227
      // column name and length. Allow for either case.
228
      if (is_array($column_name)) {
229
        $current['indexes'][$real_name][] = array(
230
          _field_sql_storage_columnname($field['field_name'], $column_name[0]),
231
          $column_name[1],
232
        );
233
      }
234
      else {
235
        $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name);
236
      }
237
    }
238
  }
239

    
240
  // Add foreign keys.
241
  foreach ($field['foreign keys'] as $specifier => $specification) {
242
    $real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
243
    $current['foreign keys'][$real_name]['table'] = $specification['table'];
244
    foreach ($specification['columns'] as $column_name => $referenced) {
245
      $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
246
      $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
247
    }
248
  }
249

    
250
  // Construct the revision table.
251
  $revision = $current;
252
  $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
253
  $revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
254
  $revision['fields']['revision_id']['not null'] = TRUE;
255
  $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
256

    
257
  return array(
258
    _field_sql_storage_tablename($field) => $current,
259
    _field_sql_storage_revision_tablename($field) => $revision,
260
  );
261
}
262

    
263
/**
264
 * Implements hook_field_storage_create_field().
265
 */
266
function field_sql_storage_field_storage_create_field($field) {
267
  $schema = _field_sql_storage_schema($field);
268
  foreach ($schema as $name => $table) {
269
    db_create_table($name, $table);
270
  }
271
  drupal_get_schema(NULL, TRUE);
272
}
273

    
274
/**
275
 * Implements hook_field_update_forbid().
276
 *
277
 * Forbid any field update that changes column definitions if there is
278
 * any data.
279
 */
280
function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) {
281
  if ($has_data && $field['columns'] != $prior_field['columns']) {
282
    throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
283
  }
284
}
285

    
286
/**
287
 * Implements hook_field_storage_update_field().
288
 */
289
function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
290
  if (! $has_data) {
291
    // There is no data. Re-create the tables completely.
292

    
293
    if (Database::getConnection()->supportsTransactionalDDL()) {
294
      // If the database supports transactional DDL, we can go ahead and rely
295
      // on it. If not, we will have to rollback manually if something fails.
296
      $transaction = db_transaction();
297
    }
298

    
299
    try {
300
      $prior_schema = _field_sql_storage_schema($prior_field);
301
      foreach ($prior_schema as $name => $table) {
302
        db_drop_table($name, $table);
303
      }
304
      $schema = _field_sql_storage_schema($field);
305
      foreach ($schema as $name => $table) {
306
        db_create_table($name, $table);
307
      }
308
    }
309
    catch (Exception $e) {
310
      if (Database::getConnection()->supportsTransactionalDDL()) {
311
        $transaction->rollback();
312
      }
313
      else {
314
        // Recreate tables.
315
        $prior_schema = _field_sql_storage_schema($prior_field);
316
        foreach ($prior_schema as $name => $table) {
317
          if (!db_table_exists($name)) {
318
            db_create_table($name, $table);
319
          }
320
        }
321
      }
322
      throw $e;
323
    }
324
  }
325
  else {
326
    // There is data, so there are no column changes. Drop all the
327
    // prior indexes and create all the new ones, except for all the
328
    // priors that exist unchanged.
329
    $table = _field_sql_storage_tablename($prior_field);
330
    $revision_table = _field_sql_storage_revision_tablename($prior_field);
331
    foreach ($prior_field['indexes'] as $name => $columns) {
332
      if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
333
        $real_name = _field_sql_storage_indexname($field['field_name'], $name);
334
        db_drop_index($table, $real_name);
335
        db_drop_index($revision_table, $real_name);
336
      }
337
    }
338
    $table = _field_sql_storage_tablename($field);
339
    $revision_table = _field_sql_storage_revision_tablename($field);
340
    foreach ($field['indexes'] as $name => $columns) {
341
      if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
342
        $real_name = _field_sql_storage_indexname($field['field_name'], $name);
343
        $real_columns = array();
344
        foreach ($columns as $column_name) {
345
          // Indexes can be specified as either a column name or an array with
346
          // column name and length. Allow for either case.
347
          if (is_array($column_name)) {
348
            $real_columns[] = array(
349
              _field_sql_storage_columnname($field['field_name'], $column_name[0]),
350
              $column_name[1],
351
            );
352
          }
353
          else {
354
            $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
355
          }
356
        }
357
        db_add_index($table, $real_name, $real_columns);
358
        db_add_index($revision_table, $real_name, $real_columns);
359
      }
360
    }
361
  }
362
  drupal_get_schema(NULL, TRUE);
363
}
364

    
365
/**
366
 * Implements hook_field_storage_delete_field().
367
 */
368
function field_sql_storage_field_storage_delete_field($field) {
369
  // Mark all data associated with the field for deletion.
370
  $field['deleted'] = 0;
371
  $table = _field_sql_storage_tablename($field);
372
  $revision_table = _field_sql_storage_revision_tablename($field);
373
  db_update($table)
374
    ->fields(array('deleted' => 1))
375
    ->execute();
376

    
377
  // Move the table to a unique name while the table contents are being deleted.
378
  $field['deleted'] = 1;
379
  $new_table = _field_sql_storage_tablename($field);
380
  $revision_new_table = _field_sql_storage_revision_tablename($field);
381
  db_rename_table($table, $new_table);
382
  db_rename_table($revision_table, $revision_new_table);
383
  drupal_get_schema(NULL, TRUE);
384
}
385

    
386
/**
387
 * Implements hook_field_storage_load().
388
 */
389
function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
390
  $load_current = $age == FIELD_LOAD_CURRENT;
391

    
392
  foreach ($fields as $field_id => $ids) {
393
    // By the time this hook runs, the relevant field definitions have been
394
    // populated and cached in FieldInfo, so calling field_info_field_by_id()
395
    // on each field individually is more efficient than loading all fields in
396
    // memory upfront with field_info_field_by_ids().
397
    $field = field_info_field_by_id($field_id);
398
    $field_name = $field['field_name'];
399
    $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
400

    
401
    $query = db_select($table, 't')
402
      ->fields('t')
403
      ->condition('entity_type', $entity_type)
404
      ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
405
      ->condition('language', field_available_languages($entity_type, $field), 'IN')
406
      ->orderBy('delta');
407

    
408
    if (empty($options['deleted'])) {
409
      $query->condition('deleted', 0);
410
    }
411

    
412
    $results = $query->execute();
413

    
414
    $delta_count = array();
415
    foreach ($results as $row) {
416
      if (!isset($delta_count[$row->entity_id][$row->language])) {
417
        $delta_count[$row->entity_id][$row->language] = 0;
418
      }
419

    
420
      if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
421
        $item = array();
422
        // For each column declared by the field, populate the item
423
        // from the prefixed database column.
424
        foreach ($field['columns'] as $column => $attributes) {
425
          $column_name = _field_sql_storage_columnname($field_name, $column);
426
          $item[$column] = $row->$column_name;
427
        }
428

    
429
        // Add the item to the field values for the entity.
430
        $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
431
        $delta_count[$row->entity_id][$row->language]++;
432
      }
433
    }
434
  }
435
}
436

    
437
/**
438
 * Implements hook_field_storage_write().
439
 */
440
function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
441
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
442
  if (!isset($vid)) {
443
    $vid = $id;
444
  }
445

    
446
  foreach ($fields as $field_id) {
447
    $field = field_info_field_by_id($field_id);
448
    $field_name = $field['field_name'];
449
    $table_name = _field_sql_storage_tablename($field);
450
    $revision_name = _field_sql_storage_revision_tablename($field);
451

    
452
    $all_languages = field_available_languages($entity_type, $field);
453
    $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
454

    
455
    // Delete and insert, rather than update, in case a value was added.
456
    if ($op == FIELD_STORAGE_UPDATE) {
457
      // Delete languages present in the incoming $entity->$field_name.
458
      // Delete all languages if $entity->$field_name is empty.
459
      $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
460
      if ($languages) {
461
        db_delete($table_name)
462
          ->condition('entity_type', $entity_type)
463
          ->condition('entity_id', $id)
464
          ->condition('language', $languages, 'IN')
465
          ->execute();
466
        db_delete($revision_name)
467
          ->condition('entity_type', $entity_type)
468
          ->condition('entity_id', $id)
469
          ->condition('revision_id', $vid)
470
          ->condition('language', $languages, 'IN')
471
          ->execute();
472
      }
473
    }
474

    
475
    // Prepare the multi-insert query.
476
    $do_insert = FALSE;
477
    $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
478
    foreach ($field['columns'] as $column => $attributes) {
479
      $columns[] = _field_sql_storage_columnname($field_name, $column);
480
    }
481
    $query = db_insert($table_name)->fields($columns);
482
    $revision_query = db_insert($revision_name)->fields($columns);
483

    
484
    foreach ($field_languages as $langcode) {
485
      $items = (array) $entity->{$field_name}[$langcode];
486
      $delta_count = 0;
487
      foreach ($items as $delta => $item) {
488
        // We now know we have something to insert.
489
        $do_insert = TRUE;
490
        $record = array(
491
          'entity_type' => $entity_type,
492
          'entity_id' => $id,
493
          'revision_id' => $vid,
494
          'bundle' => $bundle,
495
          'delta' => $delta,
496
          'language' => $langcode,
497
        );
498
        foreach ($field['columns'] as $column => $attributes) {
499
          $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
500
        }
501
        $query->values($record);
502
        if (isset($vid)) {
503
          $revision_query->values($record);
504
        }
505

    
506
        if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
507
          break;
508
        }
509
      }
510
    }
511

    
512
    // Execute the query if we have values to insert.
513
    if ($do_insert) {
514
      $query->execute();
515
      $revision_query->execute();
516
    }
517
  }
518
}
519

    
520
/**
521
 * Implements hook_field_storage_delete().
522
 *
523
 * This function deletes data for all fields for an entity from the database.
524
 */
525
function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
526
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
527

    
528
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
529
    if (isset($fields[$instance['field_id']])) {
530
      $field = field_info_field_by_id($instance['field_id']);
531
      field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
532
    }
533
  }
534
}
535

    
536
/**
537
 * Implements hook_field_storage_purge().
538
 *
539
 * This function deletes data from the database for a single field on
540
 * an entity.
541
 */
542
function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
543
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
544

    
545
  $table_name = _field_sql_storage_tablename($field);
546
  $revision_name = _field_sql_storage_revision_tablename($field);
547
  db_delete($table_name)
548
    ->condition('entity_type', $entity_type)
549
    ->condition('entity_id', $id)
550
    ->execute();
551
  db_delete($revision_name)
552
    ->condition('entity_type', $entity_type)
553
    ->condition('entity_id', $id)
554
    ->execute();
555
}
556

    
557
/**
558
 * Implements hook_field_storage_query().
559
 */
560
function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
561
  if ($query->age == FIELD_LOAD_CURRENT) {
562
    $tablename_function = '_field_sql_storage_tablename';
563
    $id_key = 'entity_id';
564
  }
565
  else {
566
    $tablename_function = '_field_sql_storage_revision_tablename';
567
    $id_key = 'revision_id';
568
  }
569
  $table_aliases = array();
570
  $query_tables = NULL;
571
  // Add tables for the fields used.
572
  foreach ($query->fields as $key => $field) {
573
    $tablename = $tablename_function($field);
574
    $table_alias = _field_sql_storage_tablealias($tablename, $key, $query);
575
    $table_aliases[$key] = $table_alias;
576
    if ($key) {
577
      if (!isset($query_tables[$table_alias])) {
578
        $select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
579
      }
580
    }
581
    else {
582
      $select_query = db_select($tablename, $table_alias);
583
      // Store a reference to the list of joined tables.
584
      $query_tables =& $select_query->getTables();
585
      // Allow queries internal to the Field API to opt out of the access
586
      // check, for situations where the query's results should not depend on
587
      // the access grants for the current user.
588
      if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
589
        $select_query->addTag('entity_field_access');
590
      }
591
      $select_query->addMetaData('base_table', $tablename);
592
      $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
593
      $field_base_table = $table_alias;
594
    }
595
    if ($field['cardinality'] != 1 || $field['translatable']) {
596
      $select_query->distinct();
597
    }
598
  }
599

    
600
  // Add field conditions. We need a fresh grouping cache.
601
  drupal_static_reset('_field_sql_storage_query_field_conditions');
602
  _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
603

    
604
  // Add field meta conditions.
605
  _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
606

    
607
  if (isset($query->deleted)) {
608
    $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
609
  }
610

    
611
  // Is there a need to sort the query by property?
612
  $has_property_order = FALSE;
613
  foreach ($query->order as $order) {
614
    if ($order['type'] == 'property') {
615
      $has_property_order = TRUE;
616
    }
617
  }
618

    
619
  if ($query->propertyConditions || $has_property_order) {
620
    if (empty($query->entityConditions['entity_type']['value'])) {
621
      throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
622
    }
623
    $entity_type = $query->entityConditions['entity_type']['value'];
624
    $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
625
    $query->entityConditions['entity_type']['operator'] = '=';
626
    foreach ($query->propertyConditions as $property_condition) {
627
      $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
628
    }
629
  }
630
  foreach ($query->entityConditions as $key => $condition) {
631
    $query->addCondition($select_query, "$field_base_table.$key", $condition);
632
  }
633

    
634
  // Order the query.
635
  foreach ($query->order as $order) {
636
    if ($order['type'] == 'entity') {
637
      $key = $order['specifier'];
638
      $select_query->orderBy("$field_base_table.$key", $order['direction']);
639
    }
640
    elseif ($order['type'] == 'field') {
641
      $specifier = $order['specifier'];
642
      $field = $specifier['field'];
643
      $table_alias = $table_aliases[$specifier['index']];
644
      $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
645
      $select_query->orderBy($sql_field, $order['direction']);
646
    }
647
    elseif ($order['type'] == 'property') {
648
      $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
649
    }
650
  }
651

    
652
  return $query->finishQuery($select_query, $id_key);
653
}
654

    
655
/**
656
 * Adds the base entity table to a field query object.
657
 *
658
 * @param SelectQuery $select_query
659
 *   A SelectQuery containing at least one table as specified by
660
 *   _field_sql_storage_tablename().
661
 * @param $entity_type
662
 *   The entity type for which the base table should be joined.
663
 * @param $field_base_table
664
 *   Name of a table in $select_query. As only INNER JOINs are used, it does
665
 *   not matter which.
666
 *
667
 * @return
668
 *   The name of the entity base table joined in.
669
 */
670
function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
671
  $entity_info = entity_get_info($entity_type);
672
  $entity_base_table = $entity_info['base table'];
673
  $entity_field = $entity_info['entity keys']['id'];
674
  $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
675
  return $entity_base_table;
676
}
677

    
678
/**
679
 * Adds field (meta) conditions to the given query objects respecting groupings.
680
 *
681
 * @param EntityFieldQuery $query
682
 *   The field query object to be processed.
683
 * @param SelectQuery $select_query
684
 *   The SelectQuery that should get grouping conditions.
685
 * @param condtions
686
 *   The conditions to be added.
687
 * @param $table_aliases
688
 *   An associative array of table aliases keyed by field index.
689
 * @param $column_callback
690
 *   A callback that should return the column name to be used for the field
691
 *   conditions. Accepts a field name and a field column name as parameters.
692
 */
693
function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
694
  $groups = &drupal_static(__FUNCTION__, array());
695
  foreach ($conditions as $key => $condition) {
696
    $table_alias = $table_aliases[$key];
697
    $field = $condition['field'];
698
    // Add the specified condition.
699
    $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
700
    $query->addCondition($select_query, $sql_field, $condition);
701
    // Add delta / language group conditions.
702
    foreach (array('delta', 'language') as $column) {
703
      if (isset($condition[$column . '_group'])) {
704
        $group_name = $condition[$column . '_group'];
705
        if (!isset($groups[$column][$group_name])) {
706
          $groups[$column][$group_name] = $table_alias;
707
        }
708
        else {
709
          $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
710
        }
711
      }
712
    }
713
  }
714
}
715

    
716
/**
717
 * Field meta condition column callback.
718
 */
719
function _field_sql_storage_query_columnname($field_name, $column) {
720
  return $column;
721
}
722

    
723
/**
724
 * Implements hook_field_storage_delete_revision().
725
 *
726
 * This function actually deletes the data from the database.
727
 */
728
function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
729
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
730

    
731
  if (isset($vid)) {
732
    foreach ($fields as $field_id) {
733
      $field = field_info_field_by_id($field_id);
734
      $revision_name = _field_sql_storage_revision_tablename($field);
735
      db_delete($revision_name)
736
        ->condition('entity_type', $entity_type)
737
        ->condition('entity_id', $id)
738
        ->condition('revision_id', $vid)
739
        ->execute();
740
    }
741
  }
742
}
743

    
744
/**
745
 * Implements hook_field_storage_delete_instance().
746
 *
747
 * This function simply marks for deletion all data associated with the field.
748
 */
749
function field_sql_storage_field_storage_delete_instance($instance) {
750
  $field = field_info_field($instance['field_name']);
751
  $table_name = _field_sql_storage_tablename($field);
752
  $revision_name = _field_sql_storage_revision_tablename($field);
753
  db_update($table_name)
754
    ->fields(array('deleted' => 1))
755
    ->condition('entity_type', $instance['entity_type'])
756
    ->condition('bundle', $instance['bundle'])
757
    ->execute();
758
  db_update($revision_name)
759
    ->fields(array('deleted' => 1))
760
    ->condition('entity_type', $instance['entity_type'])
761
    ->condition('bundle', $instance['bundle'])
762
    ->execute();
763
}
764

    
765
/**
766
 * Implements hook_field_attach_rename_bundle().
767
 */
768
function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
769
  // We need to account for deleted or inactive fields and instances.
770
  $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
771
  foreach ($instances as $instance) {
772
    $field = field_info_field_by_id($instance['field_id']);
773
    if ($field['storage']['type'] == 'field_sql_storage') {
774
      $table_name = _field_sql_storage_tablename($field);
775
      $revision_name = _field_sql_storage_revision_tablename($field);
776
      db_update($table_name)
777
        ->fields(array('bundle' => $bundle_new))
778
        ->condition('entity_type', $entity_type)
779
        ->condition('bundle', $bundle_old)
780
        ->execute();
781
      db_update($revision_name)
782
        ->fields(array('bundle' => $bundle_new))
783
        ->condition('entity_type', $entity_type)
784
        ->condition('bundle', $bundle_old)
785
        ->execute();
786
    }
787
  }
788
}
789

    
790
/**
791
 * Implements hook_field_storage_purge_field().
792
 *
793
 * All field data items and instances have already been purged, so all
794
 * that is left is to delete the table.
795
 */
796
function field_sql_storage_field_storage_purge_field($field) {
797
  $table_name = _field_sql_storage_tablename($field);
798
  $revision_name = _field_sql_storage_revision_tablename($field);
799
  db_drop_table($table_name);
800
  db_drop_table($revision_name);
801
}
802

    
803
/**
804
 * Implements hook_field_storage_details().
805
 */
806
function field_sql_storage_field_storage_details($field) {
807
  $details = array();
808
  if (!empty($field['columns'])) {
809
     // Add field columns.
810
    foreach ($field['columns'] as $column_name => $attributes) {
811
      $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
812
      $columns[$column_name] = $real_name;
813
    }
814
    return array(
815
      'sql' => array(
816
        FIELD_LOAD_CURRENT => array(
817
          _field_sql_storage_tablename($field) => $columns,
818
        ),
819
        FIELD_LOAD_REVISION => array(
820
          _field_sql_storage_revision_tablename($field) => $columns,
821
        ),
822
      ),
823
    );
824
  }
825
}