Skip navigation
Help

field_sql_storage.module

  1. drupal
    1. 7 drupal/modules/field/modules/field_sql_storage/field_sql_storage.module

Default implementation of the field storage API.

Functions & methods

NameDescription
field_sql_storage_field_attach_rename_bundleImplements hook_field_attach_rename_bundle().
field_sql_storage_field_storage_create_fieldImplements hook_field_storage_create_field().
field_sql_storage_field_storage_deleteImplements hook_field_storage_delete().
field_sql_storage_field_storage_delete_fieldImplements hook_field_storage_delete_field().
field_sql_storage_field_storage_delete_instanceImplements hook_field_storage_delete_instance().
field_sql_storage_field_storage_delete_revisionImplements hook_field_storage_delete_revision().
field_sql_storage_field_storage_detailsImplements hook_field_storage_details().
field_sql_storage_field_storage_infoImplements hook_field_storage_info().
field_sql_storage_field_storage_loadImplements hook_field_storage_load().
field_sql_storage_field_storage_purgeImplements hook_field_storage_purge().
field_sql_storage_field_storage_purge_fieldImplements hook_field_storage_purge_field().
field_sql_storage_field_storage_queryImplements hook_field_storage_query().
field_sql_storage_field_storage_update_fieldImplements hook_field_storage_update_field().
field_sql_storage_field_storage_writeImplements hook_field_storage_write().
field_sql_storage_field_update_forbidImplements hook_field_update_forbid().
field_sql_storage_helpImplements hook_help().
_field_sql_storage_columnnameGenerate a column name for a field data table.
_field_sql_storage_indexnameGenerate an index name for a field data table.
_field_sql_storage_query_columnnameField meta condition column callback.
_field_sql_storage_query_field_conditionsAdds field (meta) conditions to the given query objects respecting groupings.
_field_sql_storage_query_join_entityAdds the base entity table to a field query object.
_field_sql_storage_revision_tablenameGenerate a table name for a field revision archive table.
_field_sql_storage_schemaReturn 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_tablenameGenerate a table name for a field data table.

File

drupal/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. * Generate a column name for a field data table.
  63. *
  64. * @param $name
  65. * The name of the field
  66. * @param $column
  67. * The name of the column
  68. * @return
  69. * A string containing a generated column name for a field data
  70. * table that is unique among all other fields.
  71. */
  72. function _field_sql_storage_columnname($name, $column) {
  73. return $name . '_' . $column;
  74. }
  75. /**
  76. * Generate an index name for a field data table.
  77. *
  78. * @param $name
  79. * The name of the field
  80. * @param $column
  81. * The name of the index
  82. * @return
  83. * A string containing a generated index name for a field data
  84. * table that is unique among all other fields.
  85. */
  86. function _field_sql_storage_indexname($name, $index) {
  87. return $name . '_' . $index;
  88. }
  89. /**
  90. * Return the database schema for a field. This may contain one or
  91. * more tables. Each table will contain the columns relevant for the
  92. * specified field. Leave the $field's 'columns' and 'indexes' keys
  93. * empty to get only the base schema.
  94. *
  95. * @param $field
  96. * The field structure for which to generate a database schema.
  97. * @return
  98. * One or more tables representing the schema for the field.
  99. */
  100. function _field_sql_storage_schema($field) {
  101. $deleted = $field['deleted'] ? 'deleted ' : '';
  102. $current = array(
  103. 'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})",
  104. 'fields' => array(
  105. 'entity_type' => array(
  106. 'type' => 'varchar',
  107. 'length' => 128,
  108. 'not null' => TRUE,
  109. 'default' => '',
  110. 'description' => 'The entity type this data is attached to',
  111. ),
  112. 'bundle' => array(
  113. 'type' => 'varchar',
  114. 'length' => 128,
  115. 'not null' => TRUE,
  116. 'default' => '',
  117. 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
  118. ),
  119. 'deleted' => array(
  120. 'type' => 'int',
  121. 'size' => 'tiny',
  122. 'not null' => TRUE,
  123. 'default' => 0,
  124. 'description' => 'A boolean indicating whether this data item has been deleted'
  125. ),
  126. 'entity_id' => array(
  127. 'type' => 'int',
  128. 'unsigned' => TRUE,
  129. 'not null' => TRUE,
  130. 'description' => 'The entity id this data is attached to',
  131. ),
  132. 'revision_id' => array(
  133. 'type' => 'int',
  134. 'unsigned' => TRUE,
  135. 'not null' => FALSE,
  136. 'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
  137. ),
  138. // @todo Consider storing language as integer.
  139. 'language' => array(
  140. 'type' => 'varchar',
  141. 'length' => 32,
  142. 'not null' => TRUE,
  143. 'default' => '',
  144. 'description' => 'The language for this data item.',
  145. ),
  146. 'delta' => array(
  147. 'type' => 'int',
  148. 'unsigned' => TRUE,
  149. 'not null' => TRUE,
  150. 'description' => 'The sequence number for this data item, used for multi-value fields',
  151. ),
  152. ),
  153. 'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'),
  154. 'indexes' => array(
  155. 'entity_type' => array('entity_type'),
  156. 'bundle' => array('bundle'),
  157. 'deleted' => array('deleted'),
  158. 'entity_id' => array('entity_id'),
  159. 'revision_id' => array('revision_id'),
  160. 'language' => array('language'),
  161. ),
  162. );
  163. $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
  164. // Add field columns.
  165. foreach ($field['columns'] as $column_name => $attributes) {
  166. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  167. $current['fields'][$real_name] = $attributes;
  168. }
  169. // Add indexes.
  170. foreach ($field['indexes'] as $index_name => $columns) {
  171. $real_name = _field_sql_storage_indexname($field['field_name'], $index_name);
  172. foreach ($columns as $column_name) {
  173. $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name);
  174. }
  175. }
  176. // Add foreign keys.
  177. foreach ($field['foreign keys'] as $specifier => $specification) {
  178. $real_name = _field_sql_storage_indexname($field['field_name'], $specifier);
  179. $current['foreign keys'][$real_name]['table'] = $specification['table'];
  180. foreach ($specification['columns'] as $column => $referenced) {
  181. $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name);
  182. $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced;
  183. }
  184. }
  185. // Construct the revision table.
  186. $revision = $current;
  187. $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})";
  188. $revision['primary key'] = array('entity_type', 'entity_id', 'revision_id', 'deleted', 'delta', 'language');
  189. $revision['fields']['revision_id']['not null'] = TRUE;
  190. $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to';
  191. return array(
  192. _field_sql_storage_tablename($field) => $current,
  193. _field_sql_storage_revision_tablename($field) => $revision,
  194. );
  195. }
  196. /**
  197. * Implements hook_field_storage_create_field().
  198. */
  199. function field_sql_storage_field_storage_create_field($field) {
  200. $schema = _field_sql_storage_schema($field);
  201. foreach ($schema as $name => $table) {
  202. db_create_table($name, $table);
  203. }
  204. drupal_get_schema(NULL, TRUE);
  205. }
  206. /**
  207. * Implements hook_field_update_forbid().
  208. *
  209. * Forbid any field update that changes column definitions if there is
  210. * any data.
  211. */
  212. function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) {
  213. if ($has_data && $field['columns'] != $prior_field['columns']) {
  214. throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
  215. }
  216. }
  217. /**
  218. * Implements hook_field_storage_update_field().
  219. */
  220. function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
  221. if (! $has_data) {
  222. // There is no data. Re-create the tables completely.
  223. $prior_schema = _field_sql_storage_schema($prior_field);
  224. foreach ($prior_schema as $name => $table) {
  225. db_drop_table($name, $table);
  226. }
  227. $schema = _field_sql_storage_schema($field);
  228. foreach ($schema as $name => $table) {
  229. db_create_table($name, $table);
  230. }
  231. }
  232. else {
  233. // There is data, so there are no column changes. Drop all the
  234. // prior indexes and create all the new ones, except for all the
  235. // priors that exist unchanged.
  236. $table = _field_sql_storage_tablename($prior_field);
  237. $revision_table = _field_sql_storage_revision_tablename($prior_field);
  238. foreach ($prior_field['indexes'] as $name => $columns) {
  239. if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
  240. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  241. db_drop_index($table, $real_name);
  242. db_drop_index($revision_table, $real_name);
  243. }
  244. }
  245. $table = _field_sql_storage_tablename($field);
  246. $revision_table = _field_sql_storage_revision_tablename($field);
  247. foreach ($field['indexes'] as $name => $columns) {
  248. if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
  249. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  250. $real_columns = array();
  251. foreach ($columns as $column_name) {
  252. $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
  253. }
  254. db_add_index($table, $real_name, $real_columns);
  255. db_add_index($revision_table, $real_name, $real_columns);
  256. }
  257. }
  258. }
  259. drupal_get_schema(NULL, TRUE);
  260. }
  261. /**
  262. * Implements hook_field_storage_delete_field().
  263. */
  264. function field_sql_storage_field_storage_delete_field($field) {
  265. // Mark all data associated with the field for deletion.
  266. $field['deleted'] = 0;
  267. $table = _field_sql_storage_tablename($field);
  268. $revision_table = _field_sql_storage_revision_tablename($field);
  269. db_update($table)
  270. ->fields(array('deleted' => 1))
  271. ->execute();
  272. // Move the table to a unique name while the table contents are being deleted.
  273. $field['deleted'] = 1;
  274. $new_table = _field_sql_storage_tablename($field);
  275. $revision_new_table = _field_sql_storage_revision_tablename($field);
  276. db_rename_table($table, $new_table);
  277. db_rename_table($revision_table, $revision_new_table);
  278. drupal_get_schema(NULL, TRUE);
  279. }
  280. /**
  281. * Implements hook_field_storage_load().
  282. */
  283. function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  284. $field_info = field_info_field_by_ids();
  285. $load_current = $age == FIELD_LOAD_CURRENT;
  286. foreach ($fields as $field_id => $ids) {
  287. $field = $field_info[$field_id];
  288. $field_name = $field['field_name'];
  289. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  290. $query = db_select($table, 't')
  291. ->fields('t')
  292. ->condition('entity_type', $entity_type)
  293. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  294. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  295. ->orderBy('delta');
  296. if (empty($options['deleted'])) {
  297. $query->condition('deleted', 0);
  298. }
  299. $results = $query->execute();
  300. $delta_count = array();
  301. foreach ($results as $row) {
  302. if (!isset($delta_count[$row->entity_id][$row->language])) {
  303. $delta_count[$row->entity_id][$row->language] = 0;
  304. }
  305. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  306. $item = array();
  307. // For each column declared by the field, populate the item
  308. // from the prefixed database column.
  309. foreach ($field['columns'] as $column => $attributes) {
  310. $column_name = _field_sql_storage_columnname($field_name, $column);
  311. $item[$column] = $row->$column_name;
  312. }
  313. // Add the item to the field values for the entity.
  314. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  315. $delta_count[$row->entity_id][$row->language]++;
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * Implements hook_field_storage_write().
  322. */
  323. function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
  324. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  325. if (!isset($vid)) {
  326. $vid = $id;
  327. }
  328. foreach ($fields as $field_id) {
  329. $field = field_info_field_by_id($field_id);
  330. $field_name = $field['field_name'];
  331. $table_name = _field_sql_storage_tablename($field);
  332. $revision_name = _field_sql_storage_revision_tablename($field);
  333. $all_languages = field_available_languages($entity_type, $field);
  334. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  335. // Delete and insert, rather than update, in case a value was added.
  336. if ($op == FIELD_STORAGE_UPDATE) {
  337. // Delete languages present in the incoming $entity->$field_name.
  338. // Delete all languages if $entity->$field_name is empty.
  339. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  340. if ($languages) {
  341. db_delete($table_name)
  342. ->condition('entity_type', $entity_type)
  343. ->condition('entity_id', $id)
  344. ->condition('language', $languages, 'IN')
  345. ->execute();
  346. db_delete($revision_name)
  347. ->condition('entity_type', $entity_type)
  348. ->condition('entity_id', $id)
  349. ->condition('revision_id', $vid)
  350. ->condition('language', $languages, 'IN')
  351. ->execute();
  352. }
  353. }
  354. // Prepare the multi-insert query.
  355. $do_insert = FALSE;
  356. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  357. foreach ($field['columns'] as $column => $attributes) {
  358. $columns[] = _field_sql_storage_columnname($field_name, $column);
  359. }
  360. $query = db_insert($table_name)->fields($columns);
  361. $revision_query = db_insert($revision_name)->fields($columns);
  362. foreach ($field_languages as $langcode) {
  363. $items = (array) $entity->{$field_name}[$langcode];
  364. $delta_count = 0;
  365. foreach ($items as $delta => $item) {
  366. // We now know we have someting to insert.
  367. $do_insert = TRUE;
  368. $record = array(
  369. 'entity_type' => $entity_type,
  370. 'entity_id' => $id,
  371. 'revision_id' => $vid,
  372. 'bundle' => $bundle,
  373. 'delta' => $delta,
  374. 'language' => $langcode,
  375. );
  376. foreach ($field['columns'] as $column => $attributes) {
  377. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  378. }
  379. $query->values($record);
  380. if (isset($vid)) {
  381. $revision_query->values($record);
  382. }
  383. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  384. break;
  385. }
  386. }
  387. }
  388. // Execute the query if we have values to insert.
  389. if ($do_insert) {
  390. $query->execute();
  391. $revision_query->execute();
  392. }
  393. }
  394. }
  395. /**
  396. * Implements hook_field_storage_delete().
  397. *
  398. * This function deletes data for all fields for an entity from the database.
  399. */
  400. function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
  401. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  402. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  403. if (isset($fields[$instance['field_id']])) {
  404. $field = field_info_field_by_id($instance['field_id']);
  405. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  406. }
  407. }
  408. }
  409. /**
  410. * Implements hook_field_storage_purge().
  411. *
  412. * This function deletes data from the database for a single field on
  413. * an entity.
  414. */
  415. function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
  416. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  417. $table_name = _field_sql_storage_tablename($field);
  418. $revision_name = _field_sql_storage_revision_tablename($field);
  419. db_delete($table_name)
  420. ->condition('entity_type', $entity_type)
  421. ->condition('entity_id', $id)
  422. ->execute();
  423. db_delete($revision_name)
  424. ->condition('entity_type', $entity_type)
  425. ->condition('entity_id', $id)
  426. ->execute();
  427. }
  428. /**
  429. * Implements hook_field_storage_query().
  430. */
  431. function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
  432. if ($query->age == FIELD_LOAD_CURRENT) {
  433. $tablename_function = '_field_sql_storage_tablename';
  434. $id_key = 'entity_id';
  435. }
  436. else {
  437. $tablename_function = '_field_sql_storage_revision_tablename';
  438. $id_key = 'revision_id';
  439. }
  440. $table_aliases = array();
  441. // Add tables for the fields used.
  442. foreach ($query->fields as $key => $field) {
  443. $tablename = $tablename_function($field);
  444. // Every field needs a new table.
  445. $table_alias = $tablename . $key;
  446. $table_aliases[$key] = $table_alias;
  447. if ($key) {
  448. $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");
  449. }
  450. else {
  451. $select_query = db_select($tablename, $table_alias);
  452. $select_query->addTag('entity_field_access');
  453. $select_query->addMetaData('base_table', $tablename);
  454. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  455. $field_base_table = $table_alias;
  456. }
  457. if ($field['cardinality'] != 1) {
  458. $select_query->distinct();
  459. }
  460. }
  461. // Add field conditions. We need a fresh grouping cache.
  462. drupal_static_reset('_field_sql_storage_query_field_conditions');
  463. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  464. // Add field meta conditions.
  465. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  466. if (isset($query->deleted)) {
  467. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  468. }
  469. // Is there a need to sort the query by property?
  470. $has_property_order = FALSE;
  471. foreach ($query->order as $order) {
  472. if ($order['type'] == 'property') {
  473. $has_property_order = TRUE;
  474. }
  475. }
  476. if ($query->propertyConditions || $has_property_order) {
  477. if (empty($query->entityConditions['entity_type']['value'])) {
  478. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  479. }
  480. $entity_type = $query->entityConditions['entity_type']['value'];
  481. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  482. $query->entityConditions['entity_type']['operator'] = '=';
  483. foreach ($query->propertyConditions as $property_condition) {
  484. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  485. }
  486. }
  487. foreach ($query->entityConditions as $key => $condition) {
  488. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  489. }
  490. // Order the query.
  491. foreach ($query->order as $order) {
  492. if ($order['type'] == 'entity') {
  493. $key = $order['specifier'];
  494. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  495. }
  496. elseif ($order['type'] == 'field') {
  497. $specifier = $order['specifier'];
  498. $field = $specifier['field'];
  499. $table_alias = $table_aliases[$specifier['index']];
  500. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  501. $select_query->orderBy($sql_field, $order['direction']);
  502. }
  503. elseif ($order['type'] == 'property') {
  504. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  505. }
  506. }
  507. return $query->finishQuery($select_query, $id_key);
  508. }
  509. /**
  510. * Adds the base entity table to a field query object.
  511. *
  512. * @param SelectQuery $select_query
  513. * A SelectQuery containing at least one table as specified by
  514. * _field_sql_storage_tablename().
  515. * @param $entity_type
  516. * The entity type for which the base table should be joined.
  517. * @param $field_base_table
  518. * Name of a table in $select_query. As only INNER JOINs are used, it does
  519. * not matter which.
  520. *
  521. * @return
  522. * The name of the entity base table joined in.
  523. */
  524. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  525. $entity_info = entity_get_info($entity_type);
  526. $entity_base_table = $entity_info['base table'];
  527. $entity_field = $entity_info['entity keys']['id'];
  528. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  529. return $entity_base_table;
  530. }
  531. /**
  532. * Adds field (meta) conditions to the given query objects respecting groupings.
  533. *
  534. * @param EntityFieldQuery $query
  535. * The field query object to be processed.
  536. * @param SelectQuery $select_query
  537. * The SelectQuery that should get grouping conditions.
  538. * @param condtions
  539. * The conditions to be added.
  540. * @param $table_aliases
  541. * An associative array of table aliases keyed by field index.
  542. * @param $column_callback
  543. * A callback that should return the column name to be used for the field
  544. * conditions. Accepts a field name and a field column name as parameters.
  545. */
  546. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  547. $groups = &drupal_static(__FUNCTION__, array());
  548. foreach ($conditions as $key => $condition) {
  549. $table_alias = $table_aliases[$key];
  550. $field = $condition['field'];
  551. // Add the specified condition.
  552. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  553. $query->addCondition($select_query, $sql_field, $condition);
  554. // Add delta / language group conditions.
  555. foreach (array('delta', 'language') as $column) {
  556. if (isset($condition[$column . '_group'])) {
  557. $group_name = $condition[$column . '_group'];
  558. if (!isset($groups[$column][$group_name])) {
  559. $groups[$column][$group_name] = $table_alias;
  560. }
  561. else {
  562. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  563. }
  564. }
  565. }
  566. }
  567. }
  568. /**
  569. * Field meta condition column callback.
  570. */
  571. function _field_sql_storage_query_columnname($field_name, $column) {
  572. return $column;
  573. }
  574. /**
  575. * Implements hook_field_storage_delete_revision().
  576. *
  577. * This function actually deletes the data from the database.
  578. */
  579. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  580. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  581. if (isset($vid)) {
  582. foreach ($fields as $field_id) {
  583. $field = field_info_field_by_id($field_id);
  584. $revision_name = _field_sql_storage_revision_tablename($field);
  585. db_delete($revision_name)
  586. ->condition('entity_type', $entity_type)
  587. ->condition('entity_id', $id)
  588. ->condition('revision_id', $vid)
  589. ->execute();
  590. }
  591. }
  592. }
  593. /**
  594. * Implements hook_field_storage_delete_instance().
  595. *
  596. * This function simply marks for deletion all data associated with the field.
  597. */
  598. function field_sql_storage_field_storage_delete_instance($instance) {
  599. $field = field_info_field($instance['field_name']);
  600. $table_name = _field_sql_storage_tablename($field);
  601. $revision_name = _field_sql_storage_revision_tablename($field);
  602. db_update($table_name)
  603. ->fields(array('deleted' => 1))
  604. ->condition('entity_type', $instance['entity_type'])
  605. ->condition('bundle', $instance['bundle'])
  606. ->execute();
  607. db_update($revision_name)
  608. ->fields(array('deleted' => 1))
  609. ->condition('entity_type', $instance['entity_type'])
  610. ->condition('bundle', $instance['bundle'])
  611. ->execute();
  612. }
  613. /**
  614. * Implements hook_field_attach_rename_bundle().
  615. */
  616. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  617. // We need to account for deleted or inactive fields and instances.
  618. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  619. foreach ($instances as $instance) {
  620. $field = field_info_field_by_id($instance['field_id']);
  621. if ($field['storage']['type'] == 'field_sql_storage') {
  622. $table_name = _field_sql_storage_tablename($field);
  623. $revision_name = _field_sql_storage_revision_tablename($field);
  624. db_update($table_name)
  625. ->fields(array('bundle' => $bundle_new))
  626. ->condition('entity_type', $entity_type)
  627. ->condition('bundle', $bundle_old)
  628. ->execute();
  629. db_update($revision_name)
  630. ->fields(array('bundle' => $bundle_new))
  631. ->condition('entity_type', $entity_type)
  632. ->condition('bundle', $bundle_old)
  633. ->execute();
  634. }
  635. }
  636. }
  637. /**
  638. * Implements hook_field_storage_purge_field().
  639. *
  640. * All field data items and instances have already been purged, so all
  641. * that is left is to delete the table.
  642. */
  643. function field_sql_storage_field_storage_purge_field($field) {
  644. $table_name = _field_sql_storage_tablename($field);
  645. $revision_name = _field_sql_storage_revision_tablename($field);
  646. db_drop_table($table_name);
  647. db_drop_table($revision_name);
  648. }
  649. /**
  650. * Implements hook_field_storage_details().
  651. */
  652. function field_sql_storage_field_storage_details($field) {
  653. $details = array();
  654. if (!empty($field['columns'])) {
  655. // Add field columns.
  656. foreach ($field['columns'] as $column_name => $attributes) {
  657. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  658. $columns[$column_name] = $real_name;
  659. }
  660. return array(
  661. 'sql' => array(
  662. FIELD_LOAD_CURRENT => array(
  663. _field_sql_storage_tablename($field) => $columns,
  664. ),
  665. FIELD_LOAD_REVISION => array(
  666. _field_sql_storage_revision_tablename($field) => $columns,
  667. ),
  668. ),
  669. );
  670. }
  671. }