Default implementation of the field storage API.

File

modules/field/modules/field_sql_storage/field_sql_storage.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Default implementation of the field storage API.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function field_sql_storage_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#field_sql_storage':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $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>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * Implements hook_field_storage_info().
  20. */
  21. function field_sql_storage_field_storage_info() {
  22. return array(
  23. 'field_sql_storage' => array(
  24. 'label' => t('Default SQL storage'),
  25. 'description' => t('Stores fields in the local SQL database, using per-field tables.'),
  26. ),
  27. );
  28. }
  29. /**
  30. * Generate a table name for a field data table.
  31. *
  32. * @param $field
  33. * The field structure.
  34. * @return
  35. * A string containing the generated name for the database table
  36. */
  37. function _field_sql_storage_tablename($field) {
  38. if ($field['deleted']) {
  39. return "field_deleted_data_{$field['id']}";
  40. }
  41. else {
  42. return "field_data_{$field['field_name']}";
  43. }
  44. }
  45. /**
  46. * Generate a table name for a field revision archive table.
  47. *
  48. * @param $name
  49. * The field structure.
  50. * @return
  51. * A string containing the generated name for the database table
  52. */
  53. function _field_sql_storage_revision_tablename($field) {
  54. if ($field['deleted']) {
  55. return "field_deleted_revision_{$field['id']}";
  56. }
  57. else {
  58. return "field_revision_{$field['field_name']}";
  59. }
  60. }
  61. /**
  62. * Generates a table alias for a field data table.
  63. *
  64. * The table alias is unique for each unique combination of field name
  65. * (represented by $tablename), delta_group and language_group.
  66. *
  67. * @param $tablename
  68. * The name of the data table for this field.
  69. * @param $field_key
  70. * The numeric key of this field in this query.
  71. * @param $query
  72. * The EntityFieldQuery that is executed.
  73. *
  74. * @return
  75. * A string containing the generated table alias.
  76. */
  77. function _field_sql_storage_tablealias($tablename, $field_key, EntityFieldQuery $query) {
  78. // No conditions present: use a unique alias.
  79. if (empty($query->fieldConditions[$field_key])) {
  80. return $tablename . $field_key;
  81. }
  82. // Find the delta and language condition values and append them to the alias.
  83. $condition = $query->fieldConditions[$field_key];
  84. $alias = $tablename;
  85. $has_group_conditions = FALSE;
  86. foreach (array('delta', 'language') as $column) {
  87. if (isset($condition[$column . '_group'])) {
  88. $alias .= '_' . $column . '_' . $condition[$column . '_group'];
  89. $has_group_conditions = TRUE;
  90. }
  91. }
  92. // Return the alias when it has delta/language group conditions.
  93. if ($has_group_conditions) {
  94. return $alias;
  95. }
  96. // Return a unique alias in other cases.
  97. return $tablename . $field_key;
  98. }
  99. /**
  100. * Generate a column name for a field data table.
  101. *
  102. * @param $name
  103. * The name of the field
  104. * @param $column
  105. * The name of the column
  106. * @return
  107. * A string containing a generated column name for a field data
  108. * table that is unique among all other fields.
  109. */
  110. function _field_sql_storage_columnname($name, $column) {
  111. return $name . '_' . $column;
  112. }
  113. /**
  114. * Generate an index name for a field data table.
  115. *
  116. * @param $name
  117. * The name of the field
  118. * @param $column
  119. * The name of the index
  120. * @return
  121. * A string containing a generated index name for a field data
  122. * table that is unique among all other fields.
  123. */
  124. function _field_sql_storage_indexname($name, $index) {
  125. return $name . '_' . $index;
  126. }
  127. /**
  128. * Return the database schema for a field. This may contain one or
  129. * more tables. Each table will contain the columns relevant for the
  130. * specified field. Leave the $field's 'columns' and 'indexes' keys
  131. * empty to get only the base schema.
  132. *
  133. * @param $field
  134. * The field structure for which to generate a database schema.
  135. * @return
  136. * One or more tables representing the schema for the field.
  137. */
  138. function _field_sql_storage_schema($field) {
  139. $deleted = $field['deleted'] ? 'deleted ' : '';
  140. $current = array(
  141. 'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
  142. 'fields' => array(
  143. 'entity_type' => array(
  144. 'type' => 'varchar',
  145. 'length' => 128,
  146. 'not null' => TRUE,
  147. 'default' => '',
  148. 'description' => 'The entity type this data is attached to',
  149. ),
  150. 'bundle' => array(
  151. 'type' => 'varchar',
  152. 'length' => 128,
  153. 'not null' => TRUE,
  154. 'default' => '',
  155. 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
  156. ),
  157. 'deleted' => array(
  158. 'type' => 'int',
  159. 'size' => 'tiny',
  160. 'not null' => TRUE,
  161. 'default' => 0,
  162. 'description' => 'A boolean indicating whether this data item has been deleted'
  163. ),
  164. 'entity_id' => array(
  165. 'type' => 'int',
  166. 'unsigned' => TRUE,
  167. 'not null' => TRUE,
  168. 'description' => 'The entity id this data is attached to',
  169. ),
  170. 'revision_id' => array(
  171. 'type' => 'int',
  172. 'unsigned' => TRUE,
  173. 'not null' => FALSE,
  174. 'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
  175. ),
  176. // @todo Consider storing language as integer.
  177. 'language' => array(
  178. 'type' => 'varchar',
  179. 'length' => 32,
  180. 'not null' => TRUE,
  181. 'default' => '',
  182. 'description' => 'The language for this data item.',
  183. ),
  184. 'delta' => array(
  185. 'type' => 'int',
  186. 'unsigned' => TRUE,
  187. 'not null' => TRUE,
  188. 'description' => 'The sequence number for this data item, used for multi-value fields',
  189. ),
  190. ),
  191. 'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
  192. 'indexes' => array(
  193. 'entity_type' => array('entity_type'),
  194. 'bundle' => array('bundle'),
  195. 'deleted' => array('deleted'),
  196. 'entity_id' => array('entity_id'),
  197. 'revision_id' => array('revision_id'),
  198. 'language' => array('language'),
  199. ),
  200. );
  201. $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
  202. // Add field columns.
  203. foreach ($field['columns'] as $column_name => $attributes) {
  204. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  205. $current['fields'][$real_name] = $attributes;
  206. }
  207. // Add indexes.
  208. foreach ($field['indexes'] as $index_name => $columns) {
  209. $real_name = _field_sql_storage_indexname($field['field_name'], $index_name);
  210. foreach ($columns as $column_name) {
  211. // Indexes can be specified as either a column name or an array with
  212. // column name and length. Allow for either case.
  213. if (is_array($column_name)) {
  214. $current['indexes'][$real_name][] = array(
  215. _field_sql_storage_columnname($field['field_name'], $column_name[0]),
  216. $column_name[1],
  217. );
  218. }
  219. else {
  220. $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name);
  221. }
  222. }
  223. }
  224. // Add foreign keys.
  225. foreach ($field['foreign keys'] as $specifier => $specification) {
  226. $real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
  227. $current['foreign keys'][$real_name]['table'] = $specification['table'];
  228. foreach ($specification['columns'] as $column_name => $referenced) {
  229. $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
  230. $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
  231. }
  232. }
  233. // Construct the revision table.
  234. $revision = $current;
  235. $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
  236. $revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
  237. $revision['fields']['revision_id']['not null'] = TRUE;
  238. $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
  239. return array(
  240. _field_sql_storage_tablename($field) => $current,
  241. _field_sql_storage_revision_tablename($field) => $revision,
  242. );
  243. }
  244. /**
  245. * Implements hook_field_storage_create_field().
  246. */
  247. function field_sql_storage_field_storage_create_field($field) {
  248. $schema = _field_sql_storage_schema($field);
  249. foreach ($schema as $name => $table) {
  250. db_create_table($name, $table);
  251. }
  252. drupal_get_schema(NULL, TRUE);
  253. }
  254. /**
  255. * Implements hook_field_update_forbid().
  256. *
  257. * Forbid any field update that changes column definitions if there is
  258. * any data.
  259. */
  260. function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) {
  261. if ($has_data && $field['columns'] != $prior_field['columns']) {
  262. throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
  263. }
  264. }
  265. /**
  266. * Implements hook_field_storage_update_field().
  267. */
  268. function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
  269. if (! $has_data) {
  270. // There is no data. Re-create the tables completely.
  271. if (Database::getConnection()->supportsTransactionalDDL()) {
  272. // If the database supports transactional DDL, we can go ahead and rely
  273. // on it. If not, we will have to rollback manually if something fails.
  274. $transaction = db_transaction();
  275. }
  276. try {
  277. $prior_schema = _field_sql_storage_schema($prior_field);
  278. foreach ($prior_schema as $name => $table) {
  279. db_drop_table($name, $table);
  280. }
  281. $schema = _field_sql_storage_schema($field);
  282. foreach ($schema as $name => $table) {
  283. db_create_table($name, $table);
  284. }
  285. }
  286. catch (Exception $e) {
  287. if (Database::getConnection()->supportsTransactionalDDL()) {
  288. $transaction->rollback();
  289. }
  290. else {
  291. // Recreate tables.
  292. $prior_schema = _field_sql_storage_schema($prior_field);
  293. foreach ($prior_schema as $name => $table) {
  294. if (!db_table_exists($name)) {
  295. db_create_table($name, $table);
  296. }
  297. }
  298. }
  299. throw $e;
  300. }
  301. }
  302. else {
  303. // There is data, so there are no column changes. Drop all the
  304. // prior indexes and create all the new ones, except for all the
  305. // priors that exist unchanged.
  306. $table = _field_sql_storage_tablename($prior_field);
  307. $revision_table = _field_sql_storage_revision_tablename($prior_field);
  308. foreach ($prior_field['indexes'] as $name => $columns) {
  309. if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
  310. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  311. db_drop_index($table, $real_name);
  312. db_drop_index($revision_table, $real_name);
  313. }
  314. }
  315. $table = _field_sql_storage_tablename($field);
  316. $revision_table = _field_sql_storage_revision_tablename($field);
  317. foreach ($field['indexes'] as $name => $columns) {
  318. if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
  319. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  320. $real_columns = array();
  321. foreach ($columns as $column_name) {
  322. // Indexes can be specified as either a column name or an array with
  323. // column name and length. Allow for either case.
  324. if (is_array($column_name)) {
  325. $real_columns[] = array(
  326. _field_sql_storage_columnname($field['field_name'], $column_name[0]),
  327. $column_name[1],
  328. );
  329. }
  330. else {
  331. $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
  332. }
  333. }
  334. db_add_index($table, $real_name, $real_columns);
  335. db_add_index($revision_table, $real_name, $real_columns);
  336. }
  337. }
  338. }
  339. drupal_get_schema(NULL, TRUE);
  340. }
  341. /**
  342. * Implements hook_field_storage_delete_field().
  343. */
  344. function field_sql_storage_field_storage_delete_field($field) {
  345. // Mark all data associated with the field for deletion.
  346. $field['deleted'] = 0;
  347. $table = _field_sql_storage_tablename($field);
  348. $revision_table = _field_sql_storage_revision_tablename($field);
  349. db_update($table)
  350. ->fields(array('deleted' => 1))
  351. ->execute();
  352. // Move the table to a unique name while the table contents are being deleted.
  353. $field['deleted'] = 1;
  354. $new_table = _field_sql_storage_tablename($field);
  355. $revision_new_table = _field_sql_storage_revision_tablename($field);
  356. db_rename_table($table, $new_table);
  357. db_rename_table($revision_table, $revision_new_table);
  358. drupal_get_schema(NULL, TRUE);
  359. }
  360. /**
  361. * Implements hook_field_storage_load().
  362. */
  363. function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  364. $load_current = $age == FIELD_LOAD_CURRENT;
  365. foreach ($fields as $field_id => $ids) {
  366. // By the time this hook runs, the relevant field definitions have been
  367. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  368. // on each field individually is more efficient than loading all fields in
  369. // memory upfront with field_info_field_by_ids().
  370. $field = field_info_field_by_id($field_id);
  371. $field_name = $field['field_name'];
  372. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  373. $query = db_select($table, 't')
  374. ->fields('t')
  375. ->condition('entity_type', $entity_type)
  376. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  377. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  378. ->orderBy('delta');
  379. if (empty($options['deleted'])) {
  380. $query->condition('deleted', 0);
  381. }
  382. $results = $query->execute();
  383. $delta_count = array();
  384. foreach ($results as $row) {
  385. if (!isset($delta_count[$row->entity_id][$row->language])) {
  386. $delta_count[$row->entity_id][$row->language] = 0;
  387. }
  388. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  389. $item = array();
  390. // For each column declared by the field, populate the item
  391. // from the prefixed database column.
  392. foreach ($field['columns'] as $column => $attributes) {
  393. $column_name = _field_sql_storage_columnname($field_name, $column);
  394. $item[$column] = $row->$column_name;
  395. }
  396. // Add the item to the field values for the entity.
  397. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  398. $delta_count[$row->entity_id][$row->language]++;
  399. }
  400. }
  401. }
  402. }
  403. /**
  404. * Implements hook_field_storage_write().
  405. */
  406. function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
  407. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  408. if (!isset($vid)) {
  409. $vid = $id;
  410. }
  411. foreach ($fields as $field_id) {
  412. $field = field_info_field_by_id($field_id);
  413. $field_name = $field['field_name'];
  414. $table_name = _field_sql_storage_tablename($field);
  415. $revision_name = _field_sql_storage_revision_tablename($field);
  416. $all_languages = field_available_languages($entity_type, $field);
  417. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  418. // Delete and insert, rather than update, in case a value was added.
  419. if ($op == FIELD_STORAGE_UPDATE) {
  420. // Delete languages present in the incoming $entity->$field_name.
  421. // Delete all languages if $entity->$field_name is empty.
  422. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  423. if ($languages) {
  424. db_delete($table_name)
  425. ->condition('entity_type', $entity_type)
  426. ->condition('entity_id', $id)
  427. ->condition('language', $languages, 'IN')
  428. ->execute();
  429. db_delete($revision_name)
  430. ->condition('entity_type', $entity_type)
  431. ->condition('entity_id', $id)
  432. ->condition('revision_id', $vid)
  433. ->condition('language', $languages, 'IN')
  434. ->execute();
  435. }
  436. }
  437. // Prepare the multi-insert query.
  438. $do_insert = FALSE;
  439. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  440. foreach ($field['columns'] as $column => $attributes) {
  441. $columns[] = _field_sql_storage_columnname($field_name, $column);
  442. }
  443. $query = db_insert($table_name)->fields($columns);
  444. $revision_query = db_insert($revision_name)->fields($columns);
  445. foreach ($field_languages as $langcode) {
  446. $items = (array) $entity->{$field_name}[$langcode];
  447. $delta_count = 0;
  448. foreach ($items as $delta => $item) {
  449. // We now know we have something to insert.
  450. $do_insert = TRUE;
  451. $record = array(
  452. 'entity_type' => $entity_type,
  453. 'entity_id' => $id,
  454. 'revision_id' => $vid,
  455. 'bundle' => $bundle,
  456. 'delta' => $delta,
  457. 'language' => $langcode,
  458. );
  459. foreach ($field['columns'] as $column => $attributes) {
  460. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  461. }
  462. $query->values($record);
  463. if (isset($vid)) {
  464. $revision_query->values($record);
  465. }
  466. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  467. break;
  468. }
  469. }
  470. }
  471. // Execute the query if we have values to insert.
  472. if ($do_insert) {
  473. $query->execute();
  474. $revision_query->execute();
  475. }
  476. }
  477. }
  478. /**
  479. * Implements hook_field_storage_delete().
  480. *
  481. * This function deletes data for all fields for an entity from the database.
  482. */
  483. function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
  484. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  485. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  486. if (isset($fields[$instance['field_id']])) {
  487. $field = field_info_field_by_id($instance['field_id']);
  488. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  489. }
  490. }
  491. }
  492. /**
  493. * Implements hook_field_storage_purge().
  494. *
  495. * This function deletes data from the database for a single field on
  496. * an entity.
  497. */
  498. function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
  499. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  500. $table_name = _field_sql_storage_tablename($field);
  501. $revision_name = _field_sql_storage_revision_tablename($field);
  502. db_delete($table_name)
  503. ->condition('entity_type', $entity_type)
  504. ->condition('entity_id', $id)
  505. ->execute();
  506. db_delete($revision_name)
  507. ->condition('entity_type', $entity_type)
  508. ->condition('entity_id', $id)
  509. ->execute();
  510. }
  511. /**
  512. * Implements hook_field_storage_query().
  513. */
  514. function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
  515. if ($query->age == FIELD_LOAD_CURRENT) {
  516. $tablename_function = '_field_sql_storage_tablename';
  517. $id_key = 'entity_id';
  518. }
  519. else {
  520. $tablename_function = '_field_sql_storage_revision_tablename';
  521. $id_key = 'revision_id';
  522. }
  523. $table_aliases = array();
  524. $query_tables = NULL;
  525. // Add tables for the fields used.
  526. foreach ($query->fields as $key => $field) {
  527. $tablename = $tablename_function($field);
  528. $table_alias = _field_sql_storage_tablealias($tablename, $key, $query);
  529. $table_aliases[$key] = $table_alias;
  530. if ($key) {
  531. if (!isset($query_tables[$table_alias])) {
  532. $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");
  533. }
  534. }
  535. else {
  536. $select_query = db_select($tablename, $table_alias);
  537. // Store a reference to the list of joined tables.
  538. $query_tables =& $select_query->getTables();
  539. // Allow queries internal to the Field API to opt out of the access
  540. // check, for situations where the query's results should not depend on
  541. // the access grants for the current user.
  542. if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
  543. $select_query->addTag('entity_field_access');
  544. }
  545. $select_query->addMetaData('base_table', $tablename);
  546. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  547. $field_base_table = $table_alias;
  548. }
  549. if ($field['cardinality'] != 1 || $field['translatable']) {
  550. $select_query->distinct();
  551. }
  552. }
  553. // Add field conditions. We need a fresh grouping cache.
  554. drupal_static_reset('_field_sql_storage_query_field_conditions');
  555. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  556. // Add field meta conditions.
  557. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  558. if (isset($query->deleted)) {
  559. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  560. }
  561. // Is there a need to sort the query by property?
  562. $has_property_order = FALSE;
  563. foreach ($query->order as $order) {
  564. if ($order['type'] == 'property') {
  565. $has_property_order = TRUE;
  566. }
  567. }
  568. if ($query->propertyConditions || $has_property_order) {
  569. if (empty($query->entityConditions['entity_type']['value'])) {
  570. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  571. }
  572. $entity_type = $query->entityConditions['entity_type']['value'];
  573. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  574. $query->entityConditions['entity_type']['operator'] = '=';
  575. foreach ($query->propertyConditions as $property_condition) {
  576. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  577. }
  578. }
  579. foreach ($query->entityConditions as $key => $condition) {
  580. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  581. }
  582. // Order the query.
  583. foreach ($query->order as $order) {
  584. if ($order['type'] == 'entity') {
  585. $key = $order['specifier'];
  586. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  587. }
  588. elseif ($order['type'] == 'field') {
  589. $specifier = $order['specifier'];
  590. $field = $specifier['field'];
  591. $table_alias = $table_aliases[$specifier['index']];
  592. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  593. $select_query->orderBy($sql_field, $order['direction']);
  594. }
  595. elseif ($order['type'] == 'property') {
  596. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  597. }
  598. }
  599. return $query->finishQuery($select_query, $id_key);
  600. }
  601. /**
  602. * Adds the base entity table to a field query object.
  603. *
  604. * @param SelectQuery $select_query
  605. * A SelectQuery containing at least one table as specified by
  606. * _field_sql_storage_tablename().
  607. * @param $entity_type
  608. * The entity type for which the base table should be joined.
  609. * @param $field_base_table
  610. * Name of a table in $select_query. As only INNER JOINs are used, it does
  611. * not matter which.
  612. *
  613. * @return
  614. * The name of the entity base table joined in.
  615. */
  616. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  617. $entity_info = entity_get_info($entity_type);
  618. $entity_base_table = $entity_info['base table'];
  619. $entity_field = $entity_info['entity keys']['id'];
  620. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  621. return $entity_base_table;
  622. }
  623. /**
  624. * Adds field (meta) conditions to the given query objects respecting groupings.
  625. *
  626. * @param EntityFieldQuery $query
  627. * The field query object to be processed.
  628. * @param SelectQuery $select_query
  629. * The SelectQuery that should get grouping conditions.
  630. * @param condtions
  631. * The conditions to be added.
  632. * @param $table_aliases
  633. * An associative array of table aliases keyed by field index.
  634. * @param $column_callback
  635. * A callback that should return the column name to be used for the field
  636. * conditions. Accepts a field name and a field column name as parameters.
  637. */
  638. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  639. $groups = &drupal_static(__FUNCTION__, array());
  640. foreach ($conditions as $key => $condition) {
  641. $table_alias = $table_aliases[$key];
  642. $field = $condition['field'];
  643. // Add the specified condition.
  644. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  645. $query->addCondition($select_query, $sql_field, $condition);
  646. // Add delta / language group conditions.
  647. foreach (array('delta', 'language') as $column) {
  648. if (isset($condition[$column . '_group'])) {
  649. $group_name = $condition[$column . '_group'];
  650. if (!isset($groups[$column][$group_name])) {
  651. $groups[$column][$group_name] = $table_alias;
  652. }
  653. else {
  654. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  655. }
  656. }
  657. }
  658. }
  659. }
  660. /**
  661. * Field meta condition column callback.
  662. */
  663. function _field_sql_storage_query_columnname($field_name, $column) {
  664. return $column;
  665. }
  666. /**
  667. * Implements hook_field_storage_delete_revision().
  668. *
  669. * This function actually deletes the data from the database.
  670. */
  671. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  672. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  673. if (isset($vid)) {
  674. foreach ($fields as $field_id) {
  675. $field = field_info_field_by_id($field_id);
  676. $revision_name = _field_sql_storage_revision_tablename($field);
  677. db_delete($revision_name)
  678. ->condition('entity_type', $entity_type)
  679. ->condition('entity_id', $id)
  680. ->condition('revision_id', $vid)
  681. ->execute();
  682. }
  683. }
  684. }
  685. /**
  686. * Implements hook_field_storage_delete_instance().
  687. *
  688. * This function simply marks for deletion all data associated with the field.
  689. */
  690. function field_sql_storage_field_storage_delete_instance($instance) {
  691. $field = field_info_field($instance['field_name']);
  692. $table_name = _field_sql_storage_tablename($field);
  693. $revision_name = _field_sql_storage_revision_tablename($field);
  694. db_update($table_name)
  695. ->fields(array('deleted' => 1))
  696. ->condition('entity_type', $instance['entity_type'])
  697. ->condition('bundle', $instance['bundle'])
  698. ->execute();
  699. db_update($revision_name)
  700. ->fields(array('deleted' => 1))
  701. ->condition('entity_type', $instance['entity_type'])
  702. ->condition('bundle', $instance['bundle'])
  703. ->execute();
  704. }
  705. /**
  706. * Implements hook_field_attach_rename_bundle().
  707. */
  708. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  709. // We need to account for deleted or inactive fields and instances.
  710. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  711. foreach ($instances as $instance) {
  712. $field = field_info_field_by_id($instance['field_id']);
  713. if ($field['storage']['type'] == 'field_sql_storage') {
  714. $table_name = _field_sql_storage_tablename($field);
  715. $revision_name = _field_sql_storage_revision_tablename($field);
  716. db_update($table_name)
  717. ->fields(array('bundle' => $bundle_new))
  718. ->condition('entity_type', $entity_type)
  719. ->condition('bundle', $bundle_old)
  720. ->execute();
  721. db_update($revision_name)
  722. ->fields(array('bundle' => $bundle_new))
  723. ->condition('entity_type', $entity_type)
  724. ->condition('bundle', $bundle_old)
  725. ->execute();
  726. }
  727. }
  728. }
  729. /**
  730. * Implements hook_field_storage_purge_field().
  731. *
  732. * All field data items and instances have already been purged, so all
  733. * that is left is to delete the table.
  734. */
  735. function field_sql_storage_field_storage_purge_field($field) {
  736. $table_name = _field_sql_storage_tablename($field);
  737. $revision_name = _field_sql_storage_revision_tablename($field);
  738. db_drop_table($table_name);
  739. db_drop_table($revision_name);
  740. }
  741. /**
  742. * Implements hook_field_storage_details().
  743. */
  744. function field_sql_storage_field_storage_details($field) {
  745. $details = array();
  746. if (!empty($field['columns'])) {
  747. // Add field columns.
  748. foreach ($field['columns'] as $column_name => $attributes) {
  749. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  750. $columns[$column_name] = $real_name;
  751. }
  752. return array(
  753. 'sql' => array(
  754. FIELD_LOAD_CURRENT => array(
  755. _field_sql_storage_tablename($field) => $columns,
  756. ),
  757. FIELD_LOAD_REVISION => array(
  758. _field_sql_storage_revision_tablename($field) => $columns,
  759. ),
  760. ),
  761. );
  762. }
  763. }

Functions

Namesort descending Description
++field_sql_storage_field_storage_write Implements hook_field_storage_write().
field_sql_storage_field_attach_rename_bundle Implements hook_field_attach_rename_bundle().
field_sql_storage_field_storage_create_field Implements hook_field_storage_create_field().
field_sql_storage_field_storage_delete Implements hook_field_storage_delete().
field_sql_storage_field_storage_delete_field Implements hook_field_storage_delete_field().
field_sql_storage_field_storage_delete_instance Implements hook_field_storage_delete_instance().
field_sql_storage_field_storage_delete_revision Implements hook_field_storage_delete_revision().
field_sql_storage_field_storage_details Implements hook_field_storage_details().
field_sql_storage_field_storage_info Implements hook_field_storage_info().
field_sql_storage_field_storage_load Implements hook_field_storage_load().
field_sql_storage_field_storage_purge Implements hook_field_storage_purge().
field_sql_storage_field_storage_purge_field Implements hook_field_storage_purge_field().
field_sql_storage_field_storage_query Implements hook_field_storage_query().
field_sql_storage_field_storage_update_field Implements hook_field_storage_update_field().
field_sql_storage_field_update_forbid Implements hook_field_update_forbid().
field_sql_storage_help Implements hook_help().
_field_sql_storage_columnname Generate a column name for a field data table.
_field_sql_storage_indexname Generate an index name for a field data table.
_field_sql_storage_query_columnname Field meta condition column callback.
_field_sql_storage_query_field_conditions Adds field (meta) conditions to the given query objects respecting groupings.
_field_sql_storage_query_join_entity Adds the base entity table to a field query object.
_field_sql_storage_revision_tablename Generate a table name for a field revision archive table.
_field_sql_storage_schema 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.
_field_sql_storage_tablealias Generates a table alias for a field data table.
_field_sql_storage_tablename Generate a table name for a field data table.