diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-09-29 01:37:03 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-09-29 01:37:03 +0000 |
commit | f833423b504597d58657b731fc0eae327ed54db9 (patch) | |
tree | 3a71e23649d64713bb53a64e28da6e2104082941 | |
parent | c90c201e30084c272c73ed319f648a99edbbcb62 (diff) | |
download | brdo-f833423b504597d58657b731fc0eae327ed54db9.tar.gz brdo-f833423b504597d58657b731fc0eae327ed54db9.tar.bz2 |
- Patch #915168 by chx, sun: foreign key support is missing from text and file module.
-rw-r--r-- | modules/field/field.crud.inc | 9 | ||||
-rw-r--r-- | modules/field/modules/field_sql_storage/field_sql_storage.module | 15 | ||||
-rw-r--r-- | modules/field/modules/field_sql_storage/field_sql_storage.test | 22 | ||||
-rw-r--r-- | modules/field/modules/text/text.install | 8 | ||||
-rw-r--r-- | modules/file/file.install | 6 | ||||
-rw-r--r-- | modules/system/system.api.php | 8 |
6 files changed, 62 insertions, 6 deletions
diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc index dc5a08dfe..ed4452c46 100644 --- a/modules/field/field.crud.inc +++ b/modules/field/field.crud.inc @@ -79,6 +79,11 @@ * 'columns' setting are allowed. Note that field types can specify * default indexes, which can be modified or added to when * creating a field. + * - foreign keys: (optional) An associative array of relations, using the same + * structure as the 'foreign keys' definition of hook_schema(). Note, however, + * that the field data is not necessarily stored in SQL. Also, the possible + * usage is limited, as you cannot specify another field as related, only + * existing SQL tables, such as filter formats. * - settings (array) * A sub-array of key/value pairs of field-type-specific settings. Each * field type module defines and documents its own field settings. @@ -319,9 +324,11 @@ function field_create_field($field) { // Collect storage information. module_load_install($field['module']); $schema = (array) module_invoke($field['module'], 'field_schema', $field); - $schema += array('columns' => array(), 'indexes' => array()); + $schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); // 'columns' are hardcoded in the field type. $field['columns'] = $schema['columns']; + // 'foreign keys' are hardcoded in the field type. + $field['foreign keys'] = $schema['foreign keys']; // 'indexes' can be both hardcoded in the field type, and specified in the // incoming $field definition. $field += array( 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 bc1656bc4..ee9cc466e 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.module +++ b/modules/field/modules/field_sql_storage/field_sql_storage.module @@ -188,20 +188,31 @@ function _field_sql_storage_schema($field) { ), ); + $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); // Add field columns. - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ($field['columns'] as $column_name => $attributes) { $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); $current['fields'][$real_name] = $attributes; } // Add indexes. - foreach ((array) $field['indexes'] as $index_name => $columns) { + foreach ($field['indexes'] as $index_name => $columns) { $real_name = _field_sql_storage_indexname($field['field_name'], $index_name); foreach ($columns as $column_name) { $current['indexes'][$real_name][] = _field_sql_storage_columnname($field['field_name'], $column_name); } } + // Add foreign keys. + foreach ($field['foreign keys'] as $specifier => $specification) { + $real_name = _field_sql_storage_indexname($field['field_name'], $specifier); + $current['foreign keys'][$real_name]['table'] = $specification['table']; + foreach ($specification['columns'] as $column => $referenced) { + $sql_storage_column = _field_sql_storage_columnname($field['field_name'], $column_name); + $current['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced; + } + } + // Construct the revision table. The primary key includes // revision_id but not entity_id so that multiple revision loads can // use the IN operator. 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 6af10a0b7..b0ceeaa95 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.test +++ b/modules/field/modules/field_sql_storage/field_sql_storage.test @@ -392,4 +392,26 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase { $this->assertEqual($details[FIELD_LOAD_REVISION][$revision][$column_name], $storage_column_name, t('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => $revision))); } } + + /** + * Test foreign key support. + */ + function testFieldSqlStorageForeignKeys() { + // Create a decimal field. + $field_name = 'testfield'; + $field = array('field_name' => $field_name, 'type' => 'text'); + $field = field_create_field($field); + // Retrieve the field and instance with field_info and verify the foreign + // keys are in place. + $field = field_info_field($field_name); + $this->assertEqual($field['foreign keys']['format']['table'], 'filter_format', t('Foreign key table name preserved through CRUD')); + $this->assertEqual($field['foreign keys']['format']['columns']['format'], 'format', t('Foreign key column name preserved through CRUD')); + // Now grab the SQL schema and verify that too. + $schema = drupal_get_schema(_field_sql_storage_tablename($field)); + $this->assertEqual(count($schema['foreign keys']), 1, t("There is 1 foreign key in the schema")); + $foreign_key = reset($schema['foreign keys']); + $filter_column = _field_sql_storage_columnname($field['field_name'], 'format'); + $this->assertEqual($foreign_key['table'], 'filter_format', t('Foreign key table name preserved in the schema')); + $this->assertEqual($foreign_key['columns'][$filter_column], 'format', t('Foreign key column name preserved in the schema')); + } } diff --git a/modules/field/modules/text/text.install b/modules/field/modules/text/text.install index c2c5b2ec7..29cd07b87 100644 --- a/modules/field/modules/text/text.install +++ b/modules/field/modules/text/text.install @@ -20,6 +20,7 @@ function text_field_schema($field) { ), ); break; + case 'text_long': $columns = array( 'value' => array( @@ -29,6 +30,7 @@ function text_field_schema($field) { ), ); break; + case 'text_with_summary': $columns = array( 'value' => array( @@ -56,5 +58,11 @@ function text_field_schema($field) { 'indexes' => array( 'format' => array('format'), ), + 'foreign keys' => array( + 'format' => array( + 'table' => 'filter_format', + 'columns' => array('format' => 'format'), + ), + ), ); } diff --git a/modules/file/file.install b/modules/file/file.install index fdf908805..f313249e2 100644 --- a/modules/file/file.install +++ b/modules/file/file.install @@ -35,6 +35,12 @@ function file_field_schema($field) { 'indexes' => array( 'fid' => array('fid'), ), + 'foreign keys' => array( + 'fid' => array( + 'table' => 'file', + 'columns' => array('fid' => 'fid'), + ), + ), ); } diff --git a/modules/system/system.api.php b/modules/system/system.api.php index e349cb3ca..c497871e5 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -2633,9 +2633,11 @@ function hook_requirements($phase) { * details on schema definition structures. * * @return - * A schema definition structure array. For each element of the - * array, the key is a table name and the value is a table structure - * definition. + * A schema definition structure array. For each element of the + * array, the key is a table name and the value is a table structure + * definition. + * + * @ingroup schemaapi */ function hook_schema() { $schema['node'] = array( |