field_sql_storage.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. $field_info = field_info_field_by_ids();
  307. $load_current = $age == FIELD_LOAD_CURRENT;
  308. foreach ($fields as $field_id => $ids) {
  309. $field = $field_info[$field_id];
  310. $field_name = $field['field_name'];
  311. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  312. $query = db_select($table, 't')
  313. ->fields('t')
  314. ->condition('entity_type', $entity_type)
  315. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  316. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  317. ->orderBy('delta');
  318. if (empty($options['deleted'])) {
  319. $query->condition('deleted', 0);
  320. }
  321. $results = $query->execute();
  322. $delta_count = array();
  323. foreach ($results as $row) {
  324. if (!isset($delta_count[$row->entity_id][$row->language])) {
  325. $delta_count[$row->entity_id][$row->language] = 0;
  326. }
  327. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  328. $item = array();
  329. // For each column declared by the field, populate the item
  330. // from the prefixed database column.
  331. foreach ($field['columns'] as $column => $attributes) {
  332. $column_name = _field_sql_storage_columnname($field_name, $column);
  333. $item[$column] = $row->$column_name;
  334. }
  335. // Add the item to the field values for the entity.
  336. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  337. $delta_count[$row->entity_id][$row->language]++;
  338. }
  339. }
  340. }
  341. }
  342. /**
  343. * Implements hook_field_storage_write().
  344. */
  345. function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fields) {
  346. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  347. if (!isset($vid)) {
  348. $vid = $id;
  349. }
  350. foreach ($fields as $field_id) {
  351. $field = field_info_field_by_id($field_id);
  352. $field_name = $field['field_name'];
  353. $table_name = _field_sql_storage_tablename($field);
  354. $revision_name = _field_sql_storage_revision_tablename($field);
  355. $all_languages = field_available_languages($entity_type, $field);
  356. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  357. // Delete and insert, rather than update, in case a value was added.
  358. if ($op == FIELD_STORAGE_UPDATE) {
  359. // Delete languages present in the incoming $entity->$field_name.
  360. // Delete all languages if $entity->$field_name is empty.
  361. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  362. if ($languages) {
  363. db_delete($table_name)
  364. ->condition('entity_type', $entity_type)
  365. ->condition('entity_id', $id)
  366. ->condition('language', $languages, 'IN')
  367. ->execute();
  368. db_delete($revision_name)
  369. ->condition('entity_type', $entity_type)
  370. ->condition('entity_id', $id)
  371. ->condition('revision_id', $vid)
  372. ->condition('language', $languages, 'IN')
  373. ->execute();
  374. }
  375. }
  376. // Prepare the multi-insert query.
  377. $do_insert = FALSE;
  378. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  379. foreach ($field['columns'] as $column => $attributes) {
  380. $columns[] = _field_sql_storage_columnname($field_name, $column);
  381. }
  382. $query = db_insert($table_name)->fields($columns);
  383. $revision_query = db_insert($revision_name)->fields($columns);
  384. foreach ($field_languages as $langcode) {
  385. $items = (array) $entity->{$field_name}[$langcode];
  386. $delta_count = 0;
  387. foreach ($items as $delta => $item) {
  388. // We now know we have someting to insert.
  389. $do_insert = TRUE;
  390. $record = array(
  391. 'entity_type' => $entity_type,
  392. 'entity_id' => $id,
  393. 'revision_id' => $vid,
  394. 'bundle' => $bundle,
  395. 'delta' => $delta,
  396. 'language' => $langcode,
  397. );
  398. foreach ($field['columns'] as $column => $attributes) {
  399. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  400. }
  401. $query->values($record);
  402. if (isset($vid)) {
  403. $revision_query->values($record);
  404. }
  405. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  406. break;
  407. }
  408. }
  409. }
  410. // Execute the query if we have values to insert.
  411. if ($do_insert) {
  412. $query->execute();
  413. $revision_query->execute();
  414. }
  415. }
  416. }
  417. /**
  418. * Implements hook_field_storage_delete().
  419. *
  420. * This function deletes data for all fields for an entity from the database.
  421. */
  422. function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) {
  423. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  424. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  425. if (isset($fields[$instance['field_id']])) {
  426. $field = field_info_field_by_id($instance['field_id']);
  427. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  428. }
  429. }
  430. }
  431. /**
  432. * Implements hook_field_storage_purge().
  433. *
  434. * This function deletes data from the database for a single field on
  435. * an entity.
  436. */
  437. function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) {
  438. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  439. $table_name = _field_sql_storage_tablename($field);
  440. $revision_name = _field_sql_storage_revision_tablename($field);
  441. db_delete($table_name)
  442. ->condition('entity_type', $entity_type)
  443. ->condition('entity_id', $id)
  444. ->execute();
  445. db_delete($revision_name)
  446. ->condition('entity_type', $entity_type)
  447. ->condition('entity_id', $id)
  448. ->execute();
  449. }
  450. /**
  451. * Implements hook_field_storage_query().
  452. */
  453. function field_sql_storage_field_storage_query(EntityFieldQuery $query) {
  454. if ($query->age == FIELD_LOAD_CURRENT) {
  455. $tablename_function = '_field_sql_storage_tablename';
  456. $id_key = 'entity_id';
  457. }
  458. else {
  459. $tablename_function = '_field_sql_storage_revision_tablename';
  460. $id_key = 'revision_id';
  461. }
  462. $table_aliases = array();
  463. // Add tables for the fields used.
  464. foreach ($query->fields as $key => $field) {
  465. $tablename = $tablename_function($field);
  466. // Every field needs a new table.
  467. $table_alias = $tablename . $key;
  468. $table_aliases[$key] = $table_alias;
  469. if ($key) {
  470. $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");
  471. }
  472. else {
  473. $select_query = db_select($tablename, $table_alias);
  474. // Allow queries internal to the Field API to opt out of the access
  475. // check, for situations where the query's results should not depend on
  476. // the access grants for the current user.
  477. if (!isset($query->tags['DANGEROUS_ACCESS_CHECK_OPT_OUT'])) {
  478. $select_query->addTag('entity_field_access');
  479. }
  480. $select_query->addMetaData('base_table', $tablename);
  481. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  482. $field_base_table = $table_alias;
  483. }
  484. if ($field['cardinality'] != 1 || $field['translatable']) {
  485. $select_query->distinct();
  486. }
  487. }
  488. // Add field conditions. We need a fresh grouping cache.
  489. drupal_static_reset('_field_sql_storage_query_field_conditions');
  490. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  491. // Add field meta conditions.
  492. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  493. if (isset($query->deleted)) {
  494. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  495. }
  496. // Is there a need to sort the query by property?
  497. $has_property_order = FALSE;
  498. foreach ($query->order as $order) {
  499. if ($order['type'] == 'property') {
  500. $has_property_order = TRUE;
  501. }
  502. }
  503. if ($query->propertyConditions || $has_property_order) {
  504. if (empty($query->entityConditions['entity_type']['value'])) {
  505. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  506. }
  507. $entity_type = $query->entityConditions['entity_type']['value'];
  508. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  509. $query->entityConditions['entity_type']['operator'] = '=';
  510. foreach ($query->propertyConditions as $property_condition) {
  511. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  512. }
  513. }
  514. foreach ($query->entityConditions as $key => $condition) {
  515. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  516. }
  517. // Order the query.
  518. foreach ($query->order as $order) {
  519. if ($order['type'] == 'entity') {
  520. $key = $order['specifier'];
  521. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  522. }
  523. elseif ($order['type'] == 'field') {
  524. $specifier = $order['specifier'];
  525. $field = $specifier['field'];
  526. $table_alias = $table_aliases[$specifier['index']];
  527. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  528. $select_query->orderBy($sql_field, $order['direction']);
  529. }
  530. elseif ($order['type'] == 'property') {
  531. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  532. }
  533. }
  534. return $query->finishQuery($select_query, $id_key);
  535. }
  536. /**
  537. * Adds the base entity table to a field query object.
  538. *
  539. * @param SelectQuery $select_query
  540. * A SelectQuery containing at least one table as specified by
  541. * _field_sql_storage_tablename().
  542. * @param $entity_type
  543. * The entity type for which the base table should be joined.
  544. * @param $field_base_table
  545. * Name of a table in $select_query. As only INNER JOINs are used, it does
  546. * not matter which.
  547. *
  548. * @return
  549. * The name of the entity base table joined in.
  550. */
  551. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  552. $entity_info = entity_get_info($entity_type);
  553. $entity_base_table = $entity_info['base table'];
  554. $entity_field = $entity_info['entity keys']['id'];
  555. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  556. return $entity_base_table;
  557. }
  558. /**
  559. * Adds field (meta) conditions to the given query objects respecting groupings.
  560. *
  561. * @param EntityFieldQuery $query
  562. * The field query object to be processed.
  563. * @param SelectQuery $select_query
  564. * The SelectQuery that should get grouping conditions.
  565. * @param condtions
  566. * The conditions to be added.
  567. * @param $table_aliases
  568. * An associative array of table aliases keyed by field index.
  569. * @param $column_callback
  570. * A callback that should return the column name to be used for the field
  571. * conditions. Accepts a field name and a field column name as parameters.
  572. */
  573. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  574. $groups = &drupal_static(__FUNCTION__, array());
  575. foreach ($conditions as $key => $condition) {
  576. $table_alias = $table_aliases[$key];
  577. $field = $condition['field'];
  578. // Add the specified condition.
  579. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  580. $query->addCondition($select_query, $sql_field, $condition);
  581. // Add delta / language group conditions.
  582. foreach (array('delta', 'language') as $column) {
  583. if (isset($condition[$column . '_group'])) {
  584. $group_name = $condition[$column . '_group'];
  585. if (!isset($groups[$column][$group_name])) {
  586. $groups[$column][$group_name] = $table_alias;
  587. }
  588. else {
  589. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  590. }
  591. }
  592. }
  593. }
  594. }
  595. /**
  596. * Field meta condition column callback.
  597. */
  598. function _field_sql_storage_query_columnname($field_name, $column) {
  599. return $column;
  600. }
  601. /**
  602. * Implements hook_field_storage_delete_revision().
  603. *
  604. * This function actually deletes the data from the database.
  605. */
  606. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  607. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  608. if (isset($vid)) {
  609. foreach ($fields as $field_id) {
  610. $field = field_info_field_by_id($field_id);
  611. $revision_name = _field_sql_storage_revision_tablename($field);
  612. db_delete($revision_name)
  613. ->condition('entity_type', $entity_type)
  614. ->condition('entity_id', $id)
  615. ->condition('revision_id', $vid)
  616. ->execute();
  617. }
  618. }
  619. }
  620. /**
  621. * Implements hook_field_storage_delete_instance().
  622. *
  623. * This function simply marks for deletion all data associated with the field.
  624. */
  625. function field_sql_storage_field_storage_delete_instance($instance) {
  626. $field = field_info_field($instance['field_name']);
  627. $table_name = _field_sql_storage_tablename($field);
  628. $revision_name = _field_sql_storage_revision_tablename($field);
  629. db_update($table_name)
  630. ->fields(array('deleted' => 1))
  631. ->condition('entity_type', $instance['entity_type'])
  632. ->condition('bundle', $instance['bundle'])
  633. ->execute();
  634. db_update($revision_name)
  635. ->fields(array('deleted' => 1))
  636. ->condition('entity_type', $instance['entity_type'])
  637. ->condition('bundle', $instance['bundle'])
  638. ->execute();
  639. }
  640. /**
  641. * Implements hook_field_attach_rename_bundle().
  642. */
  643. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  644. // We need to account for deleted or inactive fields and instances.
  645. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  646. foreach ($instances as $instance) {
  647. $field = field_info_field_by_id($instance['field_id']);
  648. if ($field['storage']['type'] == 'field_sql_storage') {
  649. $table_name = _field_sql_storage_tablename($field);
  650. $revision_name = _field_sql_storage_revision_tablename($field);
  651. db_update($table_name)
  652. ->fields(array('bundle' => $bundle_new))
  653. ->condition('entity_type', $entity_type)
  654. ->condition('bundle', $bundle_old)
  655. ->execute();
  656. db_update($revision_name)
  657. ->fields(array('bundle' => $bundle_new))
  658. ->condition('entity_type', $entity_type)
  659. ->condition('bundle', $bundle_old)
  660. ->execute();
  661. }
  662. }
  663. }
  664. /**
  665. * Implements hook_field_storage_purge_field().
  666. *
  667. * All field data items and instances have already been purged, so all
  668. * that is left is to delete the table.
  669. */
  670. function field_sql_storage_field_storage_purge_field($field) {
  671. $table_name = _field_sql_storage_tablename($field);
  672. $revision_name = _field_sql_storage_revision_tablename($field);
  673. db_drop_table($table_name);
  674. db_drop_table($revision_name);
  675. }
  676. /**
  677. * Implements hook_field_storage_details().
  678. */
  679. function field_sql_storage_field_storage_details($field) {
  680. $details = array();
  681. if (!empty($field['columns'])) {
  682. // Add field columns.
  683. foreach ($field['columns'] as $column_name => $attributes) {
  684. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  685. $columns[$column_name] = $real_name;
  686. }
  687. return array(
  688. 'sql' => array(
  689. FIELD_LOAD_CURRENT => array(
  690. _field_sql_storage_tablename($field) => $columns,
  691. ),
  692. FIELD_LOAD_REVISION => array(
  693. _field_sql_storage_revision_tablename($field) => $columns,
  694. ),
  695. ),
  696. );
  697. }
  698. }