PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/field/modules/field_sql_storage/field_sql_storage.module

https://bitbucket.org/antisocnet/drupal
Unknown | 758 lines | 701 code | 57 blank | 0 comment | 0 complexity | e32a2f65b48ef478f27c2ee6c232a821 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. if (Database::getConnection()->supportsTransactionalDDL()) {
  224. // If the database supports transactional DDL, we can go ahead and rely
  225. // on it. If not, we will have to rollback manually if something fails.
  226. $transaction = db_transaction();
  227. }
  228. try {
  229. $prior_schema = _field_sql_storage_schema($prior_field);
  230. foreach ($prior_schema as $name => $table) {
  231. db_drop_table($name, $table);
  232. }
  233. $schema = _field_sql_storage_schema($field);
  234. foreach ($schema as $name => $table) {
  235. db_create_table($name, $table);
  236. }
  237. }
  238. catch (Exception $e) {
  239. if (Database::getConnection()->supportsTransactionalDDL()) {
  240. $transaction->rollback();
  241. }
  242. else {
  243. // Recreate tables.
  244. $prior_schema = _field_sql_storage_schema($prior_field);
  245. foreach ($prior_schema as $name => $table) {
  246. if (!db_table_exists($name)) {
  247. db_create_table($name, $table);
  248. }
  249. }
  250. }
  251. throw $e;
  252. }
  253. }
  254. else {
  255. // There is data, so there are no column changes. Drop all the
  256. // prior indexes and create all the new ones, except for all the
  257. // priors that exist unchanged.
  258. $table = _field_sql_storage_tablename($prior_field);
  259. $revision_table = _field_sql_storage_revision_tablename($prior_field);
  260. foreach ($prior_field['indexes'] as $name => $columns) {
  261. if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
  262. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  263. db_drop_index($table, $real_name);
  264. db_drop_index($revision_table, $real_name);
  265. }
  266. }
  267. $table = _field_sql_storage_tablename($field);
  268. $revision_table = _field_sql_storage_revision_tablename($field);
  269. foreach ($field['indexes'] as $name => $columns) {
  270. if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) {
  271. $real_name = _field_sql_storage_indexname($field['field_name'], $name);
  272. $real_columns = array();
  273. foreach ($columns as $column_name) {
  274. $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
  275. }
  276. db_add_index($table, $real_name, $real_columns);
  277. db_add_index($revision_table, $real_name, $real_columns);
  278. }
  279. }
  280. }
  281. drupal_get_schema(NULL, TRUE);
  282. }
  283. /**
  284. * Implements hook_field_storage_delete_field().
  285. */
  286. function field_sql_storage_field_storage_delete_field($field) {
  287. // Mark all data associated with the field for deletion.
  288. $field['deleted'] = 0;
  289. $table = _field_sql_storage_tablename($field);
  290. $revision_table = _field_sql_storage_revision_tablename($field);
  291. db_update($table)
  292. ->fields(array('deleted' => 1))
  293. ->execute();
  294. // Move the table to a unique name while the table contents are being deleted.
  295. $field['deleted'] = 1;
  296. $new_table = _field_sql_storage_tablename($field);
  297. $revision_new_table = _field_sql_storage_revision_tablename($field);
  298. db_rename_table($table, $new_table);
  299. db_rename_table($revision_table, $revision_new_table);
  300. drupal_get_schema(NULL, TRUE);
  301. }
  302. /**
  303. * Implements hook_field_storage_load().
  304. */
  305. function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  306. $load_current = $age == FIELD_LOAD_CURRENT;
  307. foreach ($fields as $field_id => $ids) {
  308. // By the time this hook runs, the relevant field definitions have been
  309. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  310. // on each field individually is more efficient than loading all fields in
  311. // memory upfront with field_info_field_by_ids().
  312. $field = field_info_field_by_id($field_id);
  313. $field_name = $field['field_name'];
  314. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  315. $query = db_select($table, 't')
  316. ->fields('t')
  317. ->condition('entity_type', $entity_type)
  318. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  319. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  320. ->orderBy('delta');
  321. if (empty($options['deleted'])) {
  322. $query->condition('deleted', 0);
  323. }
  324. $results = $query->execute();
  325. $delta_count = array();
  326. foreach ($results as $row) {
  327. if (!isset($delta_count[$row->entity_id][$row->language])) {
  328. $delta_count[$row->entity_id][$row->language] = 0;
  329. }
  330. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  331. $item = array();
  332. // For each column declared by the field, populate the item
  333. // from the prefixed database column.
  334. foreach ($field['columns'] as $column => $attributes) {
  335. $column_name = _field_sql_storage_columnname($field_name, $column);
  336. $item[$column] = $row->$column_name;
  337. }
  338. // Add the item to the field values for the entity.
  339. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  340. $delta_count[$row->entity_id][$row->language]++;
  341. }
  342. }
  343. }
  344. }
  345. /**
  346. * Implements hook_field_storage_write().
  347. */
  348. function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
  349. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  350. if (!isset($vid)) {
  351. $vid = $id;
  352. }
  353. foreach ($fields as $field_id) {
  354. $field = field_info_field_by_id($field_id);
  355. $field_name = $field['field_name'];
  356. $table_name = _field_sql_storage_tablename($field);
  357. $revision_name = _field_sql_storage_revision_tablename($field);
  358. $all_languages = field_available_languages($entity_type, $field);
  359. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  360. // Delete and insert, rather than update, in case a value was added.
  361. if ($op == FIELD_STORAGE_UPDATE) {
  362. // Delete languages present in the incoming $entity->$field_name.
  363. // Delete all languages if $entity->$field_name is empty.
  364. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  365. if ($languages) {
  366. db_delete($table_name)
  367. ->condition('entity_type', $entity_type)
  368. ->condition('entity_id', $id)
  369. ->condition('language', $languages, 'IN')
  370. ->execute();
  371. db_delete($revision_name)
  372. ->condition('entity_type', $entity_type)
  373. ->condition('entity_id', $id)
  374. ->condition('revision_id', $vid)
  375. ->condition('language', $languages, 'IN')
  376. ->execute();
  377. }
  378. }
  379. // Prepare the multi-insert query.
  380. $do_insert = FALSE;
  381. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  382. foreach ($field['columns'] as $column => $attributes) {
  383. $columns[] = _field_sql_storage_columnname($field_name, $column);
  384. }
  385. $query = db_insert($table_name)->fields($columns);
  386. $revision_query = db_insert($revision_name)->fields($columns);
  387. foreach ($field_languages as $langcode) {
  388. $items = (array) $entity->{$field_name}[$langcode];
  389. $delta_count = 0;
  390. foreach ($items as $delta => $item) {
  391. // We now know we have someting to insert.
  392. $do_insert = TRUE;
  393. $record = array(
  394. 'entity_type' => $entity_type,
  395. 'entity_id' => $id,
  396. 'revision_id' => $vid,
  397. 'bundle' => $bundle,
  398. 'delta' => $delta,
  399. 'language' => $langcode,
  400. );
  401. foreach ($field['columns'] as $column => $attributes) {
  402. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  403. }
  404. $query->values($record);
  405. if (isset($vid)) {
  406. $revision_query->values($record);
  407. }
  408. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  409. break;
  410. }
  411. }
  412. }
  413. // Execute the query if we have values to insert.
  414. if ($do_insert) {
  415. $query->execute();
  416. $revision_query->execute();
  417. }
  418. }
  419. }
  420. /**
  421. * Implements hook_field_storage_delete().
  422. *
  423. * This function deletes data for all fields for an entity from the database.
  424. */
  425. function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
  426. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  427. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  428. if (isset($fields[$instance['field_id']])) {
  429. $field = field_info_field_by_id($instance['field_id']);
  430. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  431. }
  432. }
  433. }
  434. /**
  435. * Implements hook_field_storage_purge().
  436. *
  437. * This function deletes data from the database for a single field on
  438. * an entity.
  439. */
  440. function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
  441. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  442. $table_name = _field_sql_storage_tablename($field);
  443. $revision_name = _field_sql_storage_revision_tablename($field);
  444. db_delete($table_name)
  445. ->condition('entity_type', $entity_type)
  446. ->condition('entity_id', $id)
  447. ->execute();
  448. db_delete($revision_name)
  449. ->condition('entity_type', $entity_type)
  450. ->condition('entity_id', $id)
  451. ->execute();
  452. }
  453. /**
  454. * Implements hook_field_storage_query().
  455. */
  456. function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
  457. if ($query->age == FIELD_LOAD_CURRENT) {
  458. $tablename_function = '_field_sql_storage_tablename';
  459. $id_key = 'entity_id';
  460. }
  461. else {
  462. $tablename_function = '_field_sql_storage_revision_tablename';
  463. $id_key = 'revision_id';
  464. }
  465. $table_aliases = array();
  466. // Add tables for the fields used.
  467. foreach ($query->fields as $key => $field) {
  468. $tablename = $tablename_function($field);
  469. // Every field needs a new table.
  470. $table_alias = $tablename . $key;
  471. $table_aliases[$key] = $table_alias;
  472. if ($key) {
  473. $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");
  474. }
  475. else {
  476. $select_query = db_select($tablename, $table_alias);
  477. // Allow queries internal to the Field API to opt out of the access
  478. // check, for situations where the query's results should not depend on
  479. // the access grants for the current user.
  480. if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
  481. $select_query->addTag('entity_field_access');
  482. }
  483. $select_query->addMetaData('base_table', $tablename);
  484. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  485. $field_base_table = $table_alias;
  486. }
  487. if ($field['cardinality'] != 1 || $field['translatable']) {
  488. $select_query->distinct();
  489. }
  490. }
  491. // Add field conditions. We need a fresh grouping cache.
  492. drupal_static_reset('_field_sql_storage_query_field_conditions');
  493. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  494. // Add field meta conditions.
  495. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  496. if (isset($query->deleted)) {
  497. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  498. }
  499. // Is there a need to sort the query by property?
  500. $has_property_order = FALSE;
  501. foreach ($query->order as $order) {
  502. if ($order['type'] == 'property') {
  503. $has_property_order = TRUE;
  504. }
  505. }
  506. if ($query->propertyConditions || $has_property_order) {
  507. if (empty($query->entityConditions['entity_type']['value'])) {
  508. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  509. }
  510. $entity_type = $query->entityConditions['entity_type']['value'];
  511. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  512. $query->entityConditions['entity_type']['operator'] = '=';
  513. foreach ($query->propertyConditions as $property_condition) {
  514. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  515. }
  516. }
  517. foreach ($query->entityConditions as $key => $condition) {
  518. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  519. }
  520. // Order the query.
  521. foreach ($query->order as $order) {
  522. if ($order['type'] == 'entity') {
  523. $key = $order['specifier'];
  524. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  525. }
  526. elseif ($order['type'] == 'field') {
  527. $specifier = $order['specifier'];
  528. $field = $specifier['field'];
  529. $table_alias = $table_aliases[$specifier['index']];
  530. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  531. $select_query->orderBy($sql_field, $order['direction']);
  532. }
  533. elseif ($order['type'] == 'property') {
  534. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  535. }
  536. }
  537. return $query->finishQuery($select_query, $id_key);
  538. }
  539. /**
  540. * Adds the base entity table to a field query object.
  541. *
  542. * @param SelectQuery $select_query
  543. * A SelectQuery containing at least one table as specified by
  544. * _field_sql_storage_tablename().
  545. * @param $entity_type
  546. * The entity type for which the base table should be joined.
  547. * @param $field_base_table
  548. * Name of a table in $select_query. As only INNER JOINs are used, it does
  549. * not matter which.
  550. *
  551. * @return
  552. * The name of the entity base table joined in.
  553. */
  554. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  555. $entity_info = entity_get_info($entity_type);
  556. $entity_base_table = $entity_info['base table'];
  557. $entity_field = $entity_info['entity keys']['id'];
  558. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  559. return $entity_base_table;
  560. }
  561. /**
  562. * Adds field (meta) conditions to the given query objects respecting groupings.
  563. *
  564. * @param EntityFieldQuery $query
  565. * The field query object to be processed.
  566. * @param SelectQuery $select_query
  567. * The SelectQuery that should get grouping conditions.
  568. * @param condtions
  569. * The conditions to be added.
  570. * @param $table_aliases
  571. * An associative array of table aliases keyed by field index.
  572. * @param $column_callback
  573. * A callback that should return the column name to be used for the field
  574. * conditions. Accepts a field name and a field column name as parameters.
  575. */
  576. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  577. $groups = &drupal_static(__FUNCTION__, array());
  578. foreach ($conditions as $key => $condition) {
  579. $table_alias = $table_aliases[$key];
  580. $field = $condition['field'];
  581. // Add the specified condition.
  582. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  583. $query->addCondition($select_query, $sql_field, $condition);
  584. // Add delta / language group conditions.
  585. foreach (array('delta', 'language') as $column) {
  586. if (isset($condition[$column . '_group'])) {
  587. $group_name = $condition[$column . '_group'];
  588. if (!isset($groups[$column][$group_name])) {
  589. $groups[$column][$group_name] = $table_alias;
  590. }
  591. else {
  592. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  593. }
  594. }
  595. }
  596. }
  597. }
  598. /**
  599. * Field meta condition column callback.
  600. */
  601. function _field_sql_storage_query_columnname($field_name, $column) {
  602. return $column;
  603. }
  604. /**
  605. * Implements hook_field_storage_delete_revision().
  606. *
  607. * This function actually deletes the data from the database.
  608. */
  609. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  610. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  611. if (isset($vid)) {
  612. foreach ($fields as $field_id) {
  613. $field = field_info_field_by_id($field_id);
  614. $revision_name = _field_sql_storage_revision_tablename($field);
  615. db_delete($revision_name)
  616. ->condition('entity_type', $entity_type)
  617. ->condition('entity_id', $id)
  618. ->condition('revision_id', $vid)
  619. ->execute();
  620. }
  621. }
  622. }
  623. /**
  624. * Implements hook_field_storage_delete_instance().
  625. *
  626. * This function simply marks for deletion all data associated with the field.
  627. */
  628. function field_sql_storage_field_storage_delete_instance($instance) {
  629. $field = field_info_field($instance['field_name']);
  630. $table_name = _field_sql_storage_tablename($field);
  631. $revision_name = _field_sql_storage_revision_tablename($field);
  632. db_update($table_name)
  633. ->fields(array('deleted' => 1))
  634. ->condition('entity_type', $instance['entity_type'])
  635. ->condition('bundle', $instance['bundle'])
  636. ->execute();
  637. db_update($revision_name)
  638. ->fields(array('deleted' => 1))
  639. ->condition('entity_type', $instance['entity_type'])
  640. ->condition('bundle', $instance['bundle'])
  641. ->execute();
  642. }
  643. /**
  644. * Implements hook_field_attach_rename_bundle().
  645. */
  646. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  647. // We need to account for deleted or inactive fields and instances.
  648. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  649. foreach ($instances as $instance) {
  650. $field = field_info_field_by_id($instance['field_id']);
  651. if ($field['storage']['type'] == 'field_sql_storage') {
  652. $table_name = _field_sql_storage_tablename($field);
  653. $revision_name = _field_sql_storage_revision_tablename($field);
  654. db_update($table_name)
  655. ->fields(array('bundle' => $bundle_new))
  656. ->condition('entity_type', $entity_type)
  657. ->condition('bundle', $bundle_old)
  658. ->execute();
  659. db_update($revision_name)
  660. ->fields(array('bundle' => $bundle_new))
  661. ->condition('entity_type', $entity_type)
  662. ->condition('bundle', $bundle_old)
  663. ->execute();
  664. }
  665. }
  666. }
  667. /**
  668. * Implements hook_field_storage_purge_field().
  669. *
  670. * All field data items and instances have already been purged, so all
  671. * that is left is to delete the table.
  672. */
  673. function field_sql_storage_field_storage_purge_field($field) {
  674. $table_name = _field_sql_storage_tablename($field);
  675. $revision_name = _field_sql_storage_revision_tablename($field);
  676. db_drop_table($table_name);
  677. db_drop_table($revision_name);
  678. }
  679. /**
  680. * Implements hook_field_storage_details().
  681. */
  682. function field_sql_storage_field_storage_details($field) {
  683. $details = array();
  684. if (!empty($field['columns'])) {
  685. // Add field columns.
  686. foreach ($field['columns'] as $column_name => $attributes) {
  687. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  688. $columns[$column_name] = $real_name;
  689. }
  690. return array(
  691. 'sql' => array(
  692. FIELD_LOAD_CURRENT => array(
  693. _field_sql_storage_tablename($field) => $columns,
  694. ),
  695. FIELD_LOAD_REVISION => array(
  696. _field_sql_storage_revision_tablename($field) => $columns,
  697. ),
  698. ),
  699. );
  700. }
  701. }