diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-12-14 19:50:05 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-12-14 19:50:05 +0000 |
commit | 36278f01d83f857ecb39ebfad067d5d2ee346973 (patch) | |
tree | 41212e6a0760a0bab47a89b5a70afb583f6c25fc /modules | |
parent | 53c10fbf0f6dc03dead544c4f44bfcbfb18e0525 (diff) | |
download | brdo-36278f01d83f857ecb39ebfad067d5d2ee346973.tar.gz brdo-36278f01d83f857ecb39ebfad067d5d2ee346973.tar.bz2 |
- Patch #986992 by yched, sun: fixed insane etid / {field_config_entity_type()} abstraction.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/comment/comment.install | 3 | ||||
-rw-r--r-- | modules/field/field.api.php | 235 | ||||
-rw-r--r-- | modules/field/field.install | 5 | ||||
-rw-r--r-- | modules/field/modules/field_sql_storage/field_sql_storage.install | 105 | ||||
-rw-r--r-- | modules/field/modules/field_sql_storage/field_sql_storage.module | 79 | ||||
-rw-r--r-- | modules/field/modules/field_sql_storage/field_sql_storage.test | 26 | ||||
-rw-r--r-- | modules/node/node.module | 5 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.install | 6 |
8 files changed, 220 insertions, 244 deletions
diff --git a/modules/comment/comment.install b/modules/comment/comment.install index 6a7b03427..23f9f9cf0 100644 --- a/modules/comment/comment.install +++ b/modules/comment/comment.install @@ -286,7 +286,6 @@ function comment_update_7006(&$sandbox) { $comments = (bool) db_query_range('SELECT 1 FROM {comment}', 0, 1)->fetchField(); $sandbox['types'] = array(); if ($comments) { - $sandbox['etid'] = _field_sql_storage_etid('comment'); $sandbox['types'] = array_keys(_update_7000_node_get_types()); } $sandbox['total'] = count($sandbox['types']); @@ -299,7 +298,7 @@ function comment_update_7006(&$sandbox) { $query->innerJoin('node', 'n', 'c.nid = n.nid AND n.type = :type', array(':type' => $type)); $query->addField('c', 'cid', 'entity_id'); $query->addExpression("'comment_node_$type'", 'bundle'); - $query->addExpression($sandbox['etid'], 'etid'); + $query->addExpression("'comment'", 'entity_type'); $query->addExpression('0', 'deleted'); $query->addExpression("'" . LANGUAGE_NONE . "'", 'language'); $query->addExpression('0', 'delta'); diff --git a/modules/field/field.api.php b/modules/field/field.api.php index ed0d63fd1..bc31b8ac3 100644 --- a/modules/field/field.api.php +++ b/modules/field/field.api.php @@ -1583,7 +1583,6 @@ function hook_field_storage_details_alter(&$details, $field) { */ function hook_field_storage_load($entity_type, &$entities, $age, $fields, $options) { $field_info = field_info_field_by_ids(); - $etid = _field_sql_storage_etid($entity_type); $load_current = $age == FIELD_LOAD_CURRENT; foreach ($fields as $field_id => $ids) { @@ -1593,7 +1592,7 @@ function hook_field_storage_load($entity_type, &$entities, $age, $fields, $optio $query = db_select($table, 't') ->fields('t') - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN') ->condition('language', field_available_languages($entity_type, $field), 'IN') ->orderBy('delta'); @@ -1646,7 +1645,9 @@ function hook_field_storage_load($entity_type, &$entities, $age, $fields, $optio */ function hook_field_storage_write($entity_type, $entity, $op, $fields) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); + if (!isset($vid)) { + $vid = $id; + } foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); @@ -1664,31 +1665,27 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) { $languages = !empty($entity->$field_name) ? $field_languages : $all_languages; if ($languages) { db_delete($table_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->condition('language', $languages, 'IN') ->execute(); - if (isset($vid)) { - db_delete($revision_name) - ->condition('etid', $etid) - ->condition('entity_id', $id) - ->condition('revision_id', $vid) - ->condition('language', $languages, 'IN') - ->execute(); - } + db_delete($revision_name) + ->condition('entity_type', $entity_type) + ->condition('entity_id', $id) + ->condition('revision_id', $vid) + ->condition('language', $languages, 'IN') + ->execute(); } } // Prepare the multi-insert query. $do_insert = FALSE; - $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); + $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); foreach ($field['columns'] as $column => $attributes) { $columns[] = _field_sql_storage_columnname($field_name, $column); } $query = db_insert($table_name)->fields($columns); - if (isset($vid)) { - $revision_query = db_insert($revision_name)->fields($columns); - } + $revision_query = db_insert($revision_name)->fields($columns); foreach ($field_languages as $langcode) { $items = (array) $entity->{$field_name}[$langcode]; @@ -1697,7 +1694,7 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) { // We now know we have someting to insert. $do_insert = TRUE; $record = array( - 'etid' => $etid, + 'entity_type' => $entity_type, 'entity_id' => $id, 'revision_id' => $vid, 'bundle' => $bundle, @@ -1721,9 +1718,7 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) { // Execute the query if we have values to insert. if ($do_insert) { $query->execute(); - if (isset($vid)) { - $revision_query->execute(); - } + $revision_query->execute(); } } } @@ -1744,7 +1739,6 @@ function hook_field_storage_write($entity_type, $entity, $op, $fields) { */ function hook_field_storage_delete($entity_type, $entity, $fields) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); foreach (field_info_instances($entity_type, $bundle) as $instance) { if (isset($fields[$instance['field_id']])) { @@ -1775,14 +1769,13 @@ function hook_field_storage_delete($entity_type, $entity, $fields) { */ function hook_field_storage_delete_revision($entity_type, $entity, $fields) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); if (isset($vid)) { foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); $revision_name = _field_sql_storage_revision_tablename($field); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->condition('revision_id', $vid) ->execute(); @@ -1805,126 +1798,104 @@ function hook_field_storage_delete_revision($entity_type, $entity, $fields) { * See EntityFieldQuery::execute() for the return values. */ function hook_field_storage_query($query) { - $load_current = $options['age'] == FIELD_LOAD_CURRENT; - - $field = field_info_field_by_id($field_id); - $field_name = $field['field_name']; - $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); - $field_columns = array_keys($field['columns']); - - // Build the query. - $query = db_select($table, 't'); - $query->join('field_config_entity_type', 'e', 't.etid = e.etid'); - - // Add conditions. - foreach ($conditions as $condition) { - // A condition is either a (column, value, operator) triple, or a - // (column, value) pair with implied operator. - @list($column, $value, $operator) = $condition; - // Translate operator and value if needed. - switch ($operator) { - case 'STARTS_WITH': - $operator = 'LIKE'; - $value = db_like($value) . '%'; - break; - - case 'ENDS_WITH': - $operator = 'LIKE'; - $value = '%' . db_like($value); - break; - - case 'CONTAINS': - $operator = 'LIKE'; - $value = '%' . db_like($value) . '%'; - break; + $groups = array(); + if ($query->age == FIELD_LOAD_CURRENT) { + $tablename_function = '_field_sql_storage_tablename'; + $id_key = 'entity_id'; + } + else { + $tablename_function = '_field_sql_storage_revision_tablename'; + $id_key = 'revision_id'; + } + $table_aliases = array(); + // Add tables for the fields used. + foreach ($query->fields as $key => $field) { + $tablename = $tablename_function($field); + // Every field needs a new table. + $table_alias = $tablename . $key; + $table_aliases[$key] = $table_alias; + if ($key) { + $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"); + } + else { + $select_query = db_select($tablename, $table_alias); + $select_query->addTag('entity_field_access'); + $select_query->addMetaData('base_table', $tablename); + $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle')); + $field_base_table = $table_alias; } - // Translate field columns into prefixed db columns. - if (in_array($column, $field_columns)) { - $column = _field_sql_storage_columnname($field_name, $column); + if ($field['cardinality'] != 1) { + $select_query->distinct(); } - // Translate entity types into numeric ids. Expressing the condition on the - // local 'etid' column rather than the JOINed 'type' column avoids a - // filesort. - if ($column == 'type') { - $column = 't.etid'; - if (is_array($value)) { - foreach (array_keys($value) as $key) { - $value[$key] = _field_sql_storage_etid($value[$key]); + } + + // Add field conditions. + foreach ($query->fieldConditions as $key => $condition) { + $table_alias = $table_aliases[$key]; + $field = $condition['field']; + // Add the specified condition. + $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']); + $query->addCondition($select_query, $sql_field, $condition); + // Add delta / language group conditions. + foreach (array('delta', 'language') as $column) { + if (isset($condition[$column . '_group'])) { + $group_name = $condition[$column . '_group']; + if (!isset($groups[$column][$group_name])) { + $groups[$column][$group_name] = $table_alias; + } + else { + $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column"); } } - else { - $value = _field_sql_storage_etid($value); - } - } - // Track condition on 'deleted'. - if ($column == 'deleted') { - $condition_deleted = TRUE; } - - $query->condition($column, $value, $operator); } - // Exclude deleted data unless we have a condition on it. - if (!isset($condition_deleted)) { - $query->condition('deleted', 0); + if (isset($query->deleted)) { + $select_query->condition("$field_base_table.deleted", (int) $query->deleted); } - // For a count query, return the count now. - if ($options['count']) { - return $query - ->fields('t', array('etid', 'entity_id', 'revision_id')) - ->distinct() - ->countQuery() - ->execute() - ->fetchField(); + // Is there a need to sort the query by property? + $has_property_order = FALSE; + foreach ($query->order as $order) { + if ($order['type'] == 'property') { + $has_property_order = TRUE; + } } - // For a data query, add fields. - $query - ->fields('t', array('bundle', 'entity_id', 'revision_id')) - ->fields('e', array('type')) - // We need to ensure entities arrive in a consistent order for the - // range() operation to work. - ->orderBy('t.etid') - ->orderBy('t.entity_id'); - - // Initialize results array - $return = array(); - - // Getting $count entities possibly requires reading more than $count rows - // since fields with multiple values span over several rows. We query for - // batches of $count rows until we've either read $count entities or received - // less rows than asked for. - $entity_count = 0; - do { - if ($options['limit'] != FIELD_QUERY_NO_LIMIT) { - $query->range($options['cursor'], $options['limit']); + if ($query->propertyConditions || $has_property_order) { + if (empty($query->entityConditions['entity_type']['value'])) { + throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.'); } - $results = $query->execute(); - - $row_count = 0; - foreach ($results as $row) { - $row_count++; - $options['cursor']++; - // If querying all revisions and the entity type has revisions, we need - // to key the results by revision_ids. - $entity_type = entity_get_info($row->type); - $id = ($load_current || empty($entity_type['entity keys']['revision'])) ? $row->entity_id : $row->revision_id; - - if (!isset($return[$row->type][$id])) { - $return[$row->type][$id] = entity_create_stub_entity($row->type, array($row->entity_id, $row->revision_id, $row->bundle)); - $entity_count++; - } + $entity_type = $query->entityConditions['entity_type']['value']; + $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table); + $query->entityConditions['entity_type']['operator'] = '='; + foreach ($query->propertyConditions as $property_condition) { + $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition); } - } while ($options['limit'] != FIELD_QUERY_NO_LIMIT && $row_count == $options['limit'] && $entity_count < $options['limit']); + } + foreach ($query->entityConditions as $key => $condition) { + $query->addCondition($select_query, "$field_base_table.$key", $condition); + } - // The query is complete when the last batch returns less rows than asked - // for. - if ($row_count < $options['limit']) { - $options['cursor'] = FIELD_QUERY_COMPLETE; + // Order the query. + foreach ($query->order as $order) { + if ($order['type'] == 'entity') { + $key = $order['specifier']; + $select_query->orderBy("$field_base_table.$key", $order['direction']); + } + elseif ($order['type'] == 'field') { + $specifier = $order['specifier']; + $field = $specifier['field']; + $table_alias = $table_aliases[$specifier['index']]; + $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']); + $select_query->orderBy($sql_field, $order['direction']); + } + elseif ($order['type'] == 'property') { + $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']); + } } - return $return; + return $query->finishQuery($select_query, $id_key); } /** @@ -1982,18 +1953,17 @@ function hook_field_storage_delete_field($field) { * The instance being deleted. */ function hook_field_storage_delete_instance($instance) { - $etid = _field_sql_storage_etid($instance['entity_type']); $field = field_info_field($instance['field_name']); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) ->fields(array('deleted' => 1)) - ->condition('etid', $etid) + ->condition('entity_type', $instance['entity_type']) ->condition('bundle', $instance['bundle']) ->execute(); db_update($revision_name) ->fields(array('deleted' => 1)) - ->condition('etid', $etid) + ->condition('entity_type', $instance['entity_type']) ->condition('bundle', $instance['bundle']) ->execute(); } @@ -2547,16 +2517,15 @@ function hook_field_storage_purge_field_instance($instance) { */ function hook_field_storage_purge($entity_type, $entity, $field, $instance) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_delete($table_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute(); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute(); } diff --git a/modules/field/field.install b/modules/field/field.install index 34afd5ad1..a95f4c639 100644 --- a/modules/field/field.install +++ b/modules/field/field.install @@ -296,13 +296,12 @@ function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) ->execute(); // Nuke data. - $etid = _field_sql_storage_etid($entity_type); db_delete('field_data_' . $field_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('bundle', $bundle) ->execute(); db_delete('field_revision_' . $field_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('bundle', $bundle) ->execute(); } diff --git a/modules/field/modules/field_sql_storage/field_sql_storage.install b/modules/field/modules/field_sql_storage/field_sql_storage.install index 92692a7e5..efb734fb5 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.install +++ b/modules/field/modules/field_sql_storage/field_sql_storage.install @@ -12,26 +12,6 @@ function field_sql_storage_schema() { $schema = array(); - // Static (meta-data) tables. - $schema['field_config_entity_type'] = array( - 'fields' => array( - 'etid' => array( - 'type' => 'serial', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'description' => 'The unique id for this entity type', - ), - 'type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'An entity type', - ), - ), - 'primary key' => array('etid'), - 'unique keys' => array('type' => array('type')), - ); - // Dynamic (data) tables. if (db_table_exists('field_config')) { $fields = field_read_fields(array(), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); @@ -54,16 +34,15 @@ function field_sql_storage_schema() { * @ingroup update-api-6.x-to-7.x */ function _update_7000_field_sql_storage_write($entity_type, $bundle, $entity_id, $revision_id, $field_name, $data) { - $etid = _field_sql_storage_etid($entity_type); $table_name = "field_data_{$field_name}"; $revision_name = "field_revision_{$field_name}"; db_delete($table_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $entity_id) ->execute(); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $entity_id) ->condition('revision_id', $revision_id) ->execute(); @@ -72,7 +51,7 @@ function _update_7000_field_sql_storage_write($entity_type, $bundle, $entity_id, foreach ($data as $langcode => $items) { foreach ($items as $delta => $item) { $record = array( - 'etid' => $etid, + 'entity_type' => $entity_type, 'entity_id' => $entity_id, 'revision_id' => $revision_id, 'bundle' => $bundle, @@ -122,5 +101,83 @@ function field_sql_storage_update_7000() { } /** + * Remove the field_config_entity_type table and store 'entity_type' strings. + */ +function field_sql_storage_update_7001(&$sandbox) { + if (!isset($sandbox['progress'])) { + // Collect current etids. + $sandbox['etids'] = db_query('SELECT etid, type FROM {field_config_entity_type}')->fetchAllKeyed(); + + // Collect affected tables: field data, field revision data, 'deleted' + // tables. + $sandbox['tables'] = array(); + $results = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC)) + ->fields('fc') + ->execute(); + foreach ($results as $field) { + if ($field['deleted']) { + $sandbox['tables'][] = "field_deleted_data_{$field['id']}"; + $sandbox['tables'][] = "field_deleted_revision_{$field['id']}"; + } + else { + $sandbox['tables'][] = "field_data_{$field['field_name']}"; + $sandbox['tables'][] = "field_revision_{$field['field_name']}"; + } + } + + $sandbox['total'] = count($sandbox['tables']); + $sandbox['progress'] = 0; + } + + if ($sandbox['tables']) { + $table = array_pop($sandbox['tables']); + + if (db_table_exists($table)) { + // Add the 'entity_type' column. + $column = array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The entity type this data is attached to.', + ); + db_add_field($table, 'entity_type', $column); + + // Populate the 'entity_type' column based on the 'etid' column. + foreach ($sandbox['etids'] as $etid => $entity_type) { + db_update($table) + ->fields(array('entity_type' => $entity_type)) + ->condition('etid', $etid) + ->execute(); + } + + // Add indexes for the 'entity_type' column. + db_drop_primary_key($table); + db_add_primary_key($table, array('entity_type', 'entity_id', 'deleted', 'delta', 'language')); + db_add_index($table, 'entity_type', array('entity_type')); + + // Drop the 'etid' column. + db_drop_field($table, 'etid'); + } + + // Report progress. + $sandbox['progress']++; + $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['total']); + } + else { + // No more tables left: drop the field_config_entity_type table. + db_drop_table('field_config_entity_type'); + + // Drop the previous 'field_sql_storage_ENTITYTYPE_etid' system variables. + foreach ($sandbox['etids'] as $etid => $entity_type) { + variable_del('field_sql_storage_' . $entity_type . '_etid'); + } + + // We're done. + $sandbox['#finished'] = 1; + } +} + +/** * @} End of "defgroup field-updates-6.x-to-7.x" */ diff --git a/modules/field/modules/field_sql_storage/field_sql_storage.module b/modules/field/modules/field_sql_storage/field_sql_storage.module index 19401fcb4..26f3d8fe4 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.module +++ b/modules/field/modules/field_sql_storage/field_sql_storage.module @@ -96,24 +96,6 @@ function _field_sql_storage_indexname($name, $index) { } /** - * Retrieve or assign an entity type id for an entity type. - * - * @param $entity_type - * The entity type, such as 'node' or 'user'. - * @return - * The entity type id. - * - */ -function _field_sql_storage_etid($entity_type) { - $etid = variable_get('field_sql_storage_' . $entity_type . '_etid', NULL); - if (!isset($etid)) { - $etid = db_insert('field_config_entity_type')->fields(array('type' => $entity_type))->execute(); - variable_set('field_sql_storage_' . $entity_type . '_etid', $etid); - } - return $etid; -} - -/** * Return the database schema for a field. This may contain one or * more tables. Each table will contain the columns relevant for the * specified field. Leave the $field's 'columns' and 'indexes' keys @@ -129,11 +111,12 @@ function _field_sql_storage_schema($field) { $current = array( 'description' => "Data storage for {$deleted}field {$field['id']} ({$field['field_name']})", 'fields' => array( - 'etid' => array( - 'type' => 'int', - 'unsigned' => TRUE, + 'entity_type' => array( + 'type' => 'varchar', + 'length' => 128, 'not null' => TRUE, - 'description' => 'The entity type id this data is attached to', + 'default' => '', + 'description' => 'The entity type this data is attached to', ), 'bundle' => array( 'type' => 'varchar', @@ -177,9 +160,9 @@ function _field_sql_storage_schema($field) { ), ), // @todo Is the primary key needed at all ? - 'primary key' => array('etid', 'entity_id', 'deleted', 'delta', 'language'), + 'primary key' => array('entity_type', 'entity_id', 'deleted', 'delta', 'language'), 'indexes' => array( - 'etid' => array('etid'), + 'entity_type' => array('entity_type'), 'bundle' => array('bundle'), 'deleted' => array('deleted'), 'entity_id' => array('entity_id'), @@ -218,7 +201,7 @@ function _field_sql_storage_schema($field) { // use the IN operator. $revision = $current; $revision['description'] = "Revision archive storage for {$deleted}field {$field['id']} ({$field['field_name']})"; - $revision['primary key'] = array('etid', 'revision_id', 'deleted', 'delta', 'language'); + $revision['primary key'] = array('entity_type', 'revision_id', 'deleted', 'delta', 'language'); $revision['fields']['revision_id']['not null'] = TRUE; $revision['fields']['revision_id']['description'] = 'The entity revision id this data is attached to'; @@ -322,7 +305,6 @@ function field_sql_storage_field_storage_delete_field($field) { */ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) { $field_info = field_info_field_by_ids(); - $etid = _field_sql_storage_etid($entity_type); $load_current = $age == FIELD_LOAD_CURRENT; foreach ($fields as $field_id => $ids) { @@ -332,7 +314,7 @@ function field_sql_storage_field_storage_load($entity_type, $entities, $age, $fi $query = db_select($table, 't') ->fields('t') - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN') ->condition('language', field_available_languages($entity_type, $field), 'IN') ->orderBy('delta'); @@ -374,7 +356,6 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel if (!isset($vid)) { $vid = $id; } - $etid = _field_sql_storage_etid($entity_type); foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); @@ -392,12 +373,12 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel $languages = !empty($entity->$field_name) ? $field_languages : $all_languages; if ($languages) { db_delete($table_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->condition('language', $languages, 'IN') ->execute(); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->condition('revision_id', $vid) ->condition('language', $languages, 'IN') @@ -407,7 +388,7 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel // Prepare the multi-insert query. $do_insert = FALSE; - $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); + $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); foreach ($field['columns'] as $column => $attributes) { $columns[] = _field_sql_storage_columnname($field_name, $column); } @@ -421,7 +402,7 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel // We now know we have someting to insert. $do_insert = TRUE; $record = array( - 'etid' => $etid, + 'entity_type' => $entity_type, 'entity_id' => $id, 'revision_id' => $vid, 'bundle' => $bundle, @@ -457,7 +438,6 @@ function field_sql_storage_field_storage_write($entity_type, $entity, $op, $fiel */ function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); foreach (field_info_instances($entity_type, $bundle) as $instance) { if (isset($fields[$instance['field_id']])) { @@ -475,16 +455,15 @@ function field_sql_storage_field_storage_delete($entity_type, $entity, $fields) */ function field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_delete($table_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute(); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute(); } @@ -510,18 +489,13 @@ function field_sql_storage_field_storage_query(EntityFieldQuery $query) { $table_alias = $tablename . $key; $table_aliases[$key] = $table_alias; if ($key) { - $select_query->join($tablename, $table_alias, "$table_alias.etid = $field_base_table.etid AND $table_alias.$id_key = $field_base_table.$id_key"); + $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"); } else { $select_query = db_select($tablename, $table_alias); $select_query->addTag('entity_field_access'); $select_query->addMetaData('base_table', $tablename); - $select_query->fields($table_alias, array('entity_id', 'revision_id', 'bundle')); - // As only a numeric ID is stored instead of the entity type add the - // field_config_entity_type table to resolve the etid to a more readable - // name. - $select_query->join('field_config_entity_type', 'fcet', "fcet.etid = $table_alias.etid"); - $select_query->addField('fcet', 'type', 'entity_type'); + $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle')); $field_base_table = $table_alias; } if ($field['cardinality'] != 1) { @@ -574,16 +548,14 @@ function field_sql_storage_field_storage_query(EntityFieldQuery $query) { } } foreach ($query->entityConditions as $key => $condition) { - $sql_field = $key == 'entity_type' ? 'fcet.type' : "$field_base_table.$key"; - $query->addCondition($select_query, $sql_field, $condition); + $query->addCondition($select_query, "$field_base_table.$key", $condition); } // Order the query. foreach ($query->order as $order) { if ($order['type'] == 'entity') { $key = $order['specifier']; - $sql_field = $key == 'entity_type' ? 'fcet.type' : "$field_base_table.$key"; - $select_query->orderBy($sql_field, $order['direction']); + $select_query->orderBy("$field_base_table.$key", $order['direction']); } elseif ($order['type'] == 'field') { $specifier = $order['specifier']; @@ -630,14 +602,13 @@ function _field_sql_storage_query_join_entity(SelectQuery $select_query, $entity */ function field_sql_storage_field_storage_delete_revision($entity_type, $entity, $fields) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); - $etid = _field_sql_storage_etid($entity_type); if (isset($vid)) { foreach ($fields as $field_id) { $field = field_info_field_by_id($field_id); $revision_name = _field_sql_storage_revision_tablename($field); db_delete($revision_name) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->condition('revision_id', $vid) ->execute(); @@ -651,18 +622,17 @@ function field_sql_storage_field_storage_delete_revision($entity_type, $entity, * This function simply marks for deletion all data associated with the field. */ function field_sql_storage_field_storage_delete_instance($instance) { - $etid = _field_sql_storage_etid($instance['entity_type']); $field = field_info_field($instance['field_name']); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) ->fields(array('deleted' => 1)) - ->condition('etid', $etid) + ->condition('entity_type', $instance['entity_type']) ->condition('bundle', $instance['bundle']) ->execute(); db_update($revision_name) ->fields(array('deleted' => 1)) - ->condition('etid', $etid) + ->condition('entity_type', $instance['entity_type']) ->condition('bundle', $instance['bundle']) ->execute(); } @@ -671,7 +641,6 @@ function field_sql_storage_field_storage_delete_instance($instance) { * Implements hook_field_attach_rename_bundle(). */ function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { - $etid = _field_sql_storage_etid($entity_type); // We need to account for deleted or inactive fields and instances. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); foreach ($instances as $instance) { @@ -681,12 +650,12 @@ function field_sql_storage_field_attach_rename_bundle($entity_type, $bundle_old, $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) ->fields(array('bundle' => $bundle_new)) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('bundle', $bundle_old) ->execute(); db_update($revision_name) ->fields(array('bundle' => $bundle_new)) - ->condition('etid', $etid) + ->condition('entity_type', $entity_type) ->condition('bundle', $bundle_old) ->execute(); } diff --git a/modules/field/modules/field_sql_storage/field_sql_storage.test b/modules/field/modules/field_sql_storage/field_sql_storage.test index b0ceeaa95..4a6f96a52 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.test +++ b/modules/field/modules/field_sql_storage/field_sql_storage.test @@ -37,19 +37,6 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { } - function testEntityTypeId() { - $t1 = _field_sql_storage_etid('t1'); - $t2 = _field_sql_storage_etid('t2'); - - $this->assertEqual($t1+1, $t2, 'Entity type ids are sequential'); - $this->assertIdentical(variable_get('field_sql_storage_t1_etid', NULL), $t1, 'First entity type variable is correct'); - $this->assertIdentical(variable_get('field_sql_storage_t2_etid', NULL), $t2, 'Second entity type variable is correct'); - $this->assertEqual(db_query("SELECT etid FROM {field_config_entity_type} WHERE type='t1'")->fetchField(), $t1, 'First entity type in database is correct'); - $this->assertEqual(db_query("SELECT etid FROM {field_config_entity_type} WHERE type='t2'")->fetchField(), $t2, 'Second entity type in database is correct'); - $this->assertEqual($t1, _field_sql_storage_etid('t1'), '_field_sql_storage_etid returns the same value for the first entity type'); - $this->assertEqual($t2, _field_sql_storage_etid('t2'), '_field_sql_storage_etid returns the same value for the second entity type'); - } - /** * Uses the mysql tables and records to verify * field_load_revision works correctly. @@ -59,8 +46,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { $eid = 0; $langcode = LANGUAGE_NONE; - $etid = _field_sql_storage_etid($entity_type); - $columns = array('etid', 'entity_id', 'revision_id', 'delta', 'language', $this->field_name . '_value'); + $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $this->field_name . '_value'); // Insert data for four revisions to the field revisions table $query = db_insert($this->revision_table)->fields($columns); @@ -70,7 +56,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { for ($delta = 0; $delta <= $this->field['cardinality']; $delta++) { $value = mt_rand(1, 127); $values[$evid][] = $value; - $query->values(array($etid, $eid, $evid, $delta, $langcode, $value)); + $query->values(array($entity_type, $eid, $evid, $delta, $langcode, $value)); } } $query->execute(); @@ -78,7 +64,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { // Insert data for the "most current revision" into the field table $query = db_insert($this->table)->fields($columns); foreach ($values[0] as $delta => $value) { - $query->values(array($etid, $eid, 0, $delta, $langcode, $value)); + $query->values(array($entity_type, $eid, 0, $delta, $langcode, $value)); } $query->execute(); @@ -112,7 +98,7 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { $eid = $evid = 1; $unavailable_language = 'xx'; $entity = field_test_create_stub_entity($eid, $evid, $this->instance['bundle']); - $values = array($etid, $eid, $evid, 0, $unavailable_language, mt_rand(1, 127)); + $values = array($entity_type, $eid, $evid, 0, $unavailable_language, mt_rand(1, 127)); db_insert($this->table)->fields($columns)->values($values)->execute(); db_insert($this->revision_table)->fields($columns)->values($values)->execute(); field_attach_load($entity_type, array($eid => $entity)); @@ -256,8 +242,8 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { // Add a translation in an unavailable language. $unavailable_language = 'xx'; db_insert($this->table) - ->fields(array('etid', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'language')) - ->values(array(_field_sql_storage_etid($entity_type), $this->instance['bundle'], 0, 0, 0, 0, $unavailable_language)) + ->fields(array('entity_type', 'bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'language')) + ->values(array($entity_type, $this->instance['bundle'], 0, 0, 0, 0, $unavailable_language)) ->execute(); $count = db_select($this->table) ->countQuery() diff --git a/modules/node/node.module b/modules/node/node.module index 8535a3d3f..68b33781d 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -3236,13 +3236,12 @@ function _node_query_node_access_alter($query, $base_table, $type) { if ($type == 'entity' && count($entity_conditions->conditions())) { // All the node access conditions are only for field values belonging to // nodes. - $etid = variable_get('field_sql_storage_node_etid'); - $entity_conditions->condition("$base_alias.etid", $etid); + $entity_conditions->condition("$base_alias.entity_type", 'node'); $or = db_or(); $or->condition($entity_conditions); // If the field value belongs to a non-node entity type then this function // does not do anything with it. - $or->condition("$base_alias.etid", $etid, '<>'); + $or->condition("$base_alias.entity_type", 'node', '<>'); // Add the compiled set of rules to the query. $query->condition($or); } diff --git a/modules/taxonomy/taxonomy.install b/modules/taxonomy/taxonomy.install index 15103ae48..73c115aa2 100644 --- a/modules/taxonomy/taxonomy.install +++ b/modules/taxonomy/taxonomy.install @@ -600,8 +600,6 @@ function taxonomy_update_7005(&$sandbox) { } } else { - $etid = _field_sql_storage_etid('node'); - // We do each pass in batches of 1000. $batch = 1000; @@ -687,8 +685,8 @@ function taxonomy_update_7005(&$sandbox) { // Column names and values in field storage are the same for current and // revision. - $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'language', 'delta', $value_column); - $values = array($etid, $record->nid, $record->vid, $record->type, LANGUAGE_NONE, $deltas[$field_name]++, $record->tid); + $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'language', 'delta', $value_column); + $values = array('node', $record->nid, $record->vid, $record->type, LANGUAGE_NONE, $deltas[$field_name]++, $record->tid); // Insert rows into the revision table. db_insert($revision_name)->fields($columns)->values($values)->execute(); |