field_sql_storage.module

  1. drupal
    1. 7 modules/field/modules/field_sql_storage/field_sql_storage.module
    2. 8 core/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

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. 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. $select_query->addTag('entity_field_access');
  475. $select_query->addMetaData('base_table', $tablename);
  476. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  477. $field_base_table = $table_alias;
  478. }
  479. if ($field['cardinality'] != 1 || $field['translatable']) {
  480. $select_query->distinct();
  481. }
  482. }
  483. // Add field conditions. We need a fresh grouping cache.
  484. drupal_static_reset('_field_sql_storage_query_field_conditions');
  485. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldConditions, $table_aliases, '_field_sql_storage_columnname');
  486. // Add field meta conditions.
  487. _field_sql_storage_query_field_conditions($query, $select_query, $query->fieldMetaConditions, $table_aliases, '_field_sql_storage_query_columnname');
  488. if (isset($query->deleted)) {
  489. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  490. }
  491. // Is there a need to sort the query by property?
  492. $has_property_order = FALSE;
  493. foreach ($query->order as $order) {
  494. if ($order['type'] == 'property') {
  495. $has_property_order = TRUE;
  496. }
  497. }
  498. if ($query->propertyConditions || $has_property_order) {
  499. if (empty($query->entityConditions['entity_type']['value'])) {
  500. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  501. }
  502. $entity_type = $query->entityConditions['entity_type']['value'];
  503. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  504. $query->entityConditions['entity_type']['operator'] = '=';
  505. foreach ($query->propertyConditions as $property_condition) {
  506. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  507. }
  508. }
  509. foreach ($query->entityConditions as $key => $condition) {
  510. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  511. }
  512. // Order the query.
  513. foreach ($query->order as $order) {
  514. if ($order['type'] == 'entity') {
  515. $key = $order['specifier'];
  516. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  517. }
  518. elseif ($order['type'] == 'field') {
  519. $specifier = $order['specifier'];
  520. $field = $specifier['field'];
  521. $table_alias = $table_aliases[$specifier['index']];
  522. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  523. $select_query->orderBy($sql_field, $order['direction']);
  524. }
  525. elseif ($order['type'] == 'property') {
  526. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  527. }
  528. }
  529. return $query->finishQuery($select_query, $id_key);
  530. }
  531. /**
  532. * Adds the base entity table to a field query object.
  533. *
  534. * @param SelectQuery $select_query
  535. * A SelectQuery containing at least one table as specified by
  536. * _field_sql_storage_tablename().
  537. * @param $entity_type
  538. * The entity type for which the base table should be joined.
  539. * @param $field_base_table
  540. * Name of a table in $select_query. As only INNER JOINs are used, it does
  541. * not matter which.
  542. *
  543. * @return
  544. * The name of the entity base table joined in.
  545. */
  546. function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity_type, $field_base_table) {
  547. $entity_info = entity_get_info($entity_type);
  548. $entity_base_table = $entity_info['base table'];
  549. $entity_field = $entity_info['entity keys']['id'];
  550. $select_query->join($entity_base_table, $entity_base_table, "$entity_base_table.$entity_field = $field_base_table.entity_id");
  551. return $entity_base_table;
  552. }
  553. /**
  554. * Adds field (meta) conditions to the given query objects respecting groupings.
  555. *
  556. * @param EntityFieldQuery $query
  557. * The field query object to be processed.
  558. * @param SelectQuery $select_query
  559. * The SelectQuery that should get grouping conditions.
  560. * @param condtions
  561. * The conditions to be added.
  562. * @param $table_aliases
  563. * An associative array of table aliases keyed by field index.
  564. * @param $column_callback
  565. * A callback that should return the column name to be used for the field
  566. * conditions. Accepts a field name and a field column name as parameters.
  567. */
  568. function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, SelectQuery $select_query, $conditions, $table_aliases, $column_callback) {
  569. $groups = &drupal_static(__FUNCTION__, array());
  570. foreach ($conditions as $key => $condition) {
  571. $table_alias = $table_aliases[$key];
  572. $field = $condition['field'];
  573. // Add the specified condition.
  574. $sql_field = "$table_alias." . $column_callback($field['field_name'], $condition['column']);
  575. $query->addCondition($select_query, $sql_field, $condition);
  576. // Add delta / language group conditions.
  577. foreach (array('delta', 'language') as $column) {
  578. if (isset($condition[$column . '_group'])) {
  579. $group_name = $condition[$column . '_group'];
  580. if (!isset($groups[$column][$group_name])) {
  581. $groups[$column][$group_name] = $table_alias;
  582. }
  583. else {
  584. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  585. }
  586. }
  587. }
  588. }
  589. }
  590. /**
  591. * Field meta condition column callback.
  592. */
  593. function _field_sql_storage_query_columnname($field_name, $column) {
  594. return $column;
  595. }
  596. /**
  597. * Implements hook_field_storage_delete_revision().
  598. *
  599. * This function actually deletes the data from the database.
  600. */
  601. function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) {
  602. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  603. if (isset($vid)) {
  604. foreach ($fields as $field_id) {
  605. $field = field_info_field_by_id($field_id);
  606. $revision_name = _field_sql_storage_revision_tablename($field);
  607. db_delete($revision_name)
  608. ->condition('entity_type', $entity_type)
  609. ->condition('entity_id', $id)
  610. ->condition('revision_id', $vid)
  611. ->execute();
  612. }
  613. }
  614. }
  615. /**
  616. * Implements hook_field_storage_delete_instance().
  617. *
  618. * This function simply marks for deletion all data associated with the field.
  619. */
  620. function field_sql_storage_field_storage_delete_instance($instance) {
  621. $field = field_info_field($instance['field_name']);
  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('deleted' => 1))
  626. ->condition('entity_type', $instance['entity_type'])
  627. ->condition('bundle', $instance['bundle'])
  628. ->execute();
  629. db_update($revision_name)
  630. ->fields(array('deleted' => 1))
  631. ->condition('entity_type', $instance['entity_type'])
  632. ->condition('bundle', $instance['bundle'])
  633. ->execute();
  634. }
  635. /**
  636. * Implements hook_field_attach_rename_bundle().
  637. */
  638. function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  639. // We need to account for deleted or inactive fields and instances.
  640. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE));
  641. foreach ($instances as $instance) {
  642. $field = field_info_field_by_id($instance['field_id']);
  643. if ($field['storage']['type'] == 'field_sql_storage') {
  644. $table_name = _field_sql_storage_tablename($field);
  645. $revision_name = _field_sql_storage_revision_tablename($field);
  646. db_update($table_name)
  647. ->fields(array('bundle' => $bundle_new))
  648. ->condition('entity_type', $entity_type)
  649. ->condition('bundle', $bundle_old)
  650. ->execute();
  651. db_update($revision_name)
  652. ->fields(array('bundle' => $bundle_new))
  653. ->condition('entity_type', $entity_type)
  654. ->condition('bundle', $bundle_old)
  655. ->execute();
  656. }
  657. }
  658. }
  659. /**
  660. * Implements hook_field_storage_purge_field().
  661. *
  662. * All field data items and instances have already been purged, so all
  663. * that is left is to delete the table.
  664. */
  665. function field_sql_storage_field_storage_purge_field($field) {
  666. $table_name = _field_sql_storage_tablename($field);
  667. $revision_name = _field_sql_storage_revision_tablename($field);
  668. db_drop_table($table_name);
  669. db_drop_table($revision_name);
  670. }
  671. /**
  672. * Implements hook_field_storage_details().
  673. */
  674. function field_sql_storage_field_storage_details($field) {
  675. $details = array();
  676. if (!empty($field['columns'])) {
  677. // Add field columns.
  678. foreach ($field['columns'] as $column_name => $attributes) {
  679. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  680. $columns[$column_name] = $real_name;
  681. }
  682. return array(
  683. 'sql' => array(
  684. FIELD_LOAD_CURRENT => array(
  685. _field_sql_storage_tablename($field) => $columns,
  686. ),
  687. FIELD_LOAD_REVISION => array(
  688. _field_sql_storage_revision_tablename($field) => $columns,
  689. ),
  690. ),
  691. );
  692. }
  693. }
Login or register to post comments