Index: content_admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cck/content_admin.inc,v retrieving revision 1.28.2.42 diff -u -p -r1.28.2.42 content_admin.inc --- content_admin.inc 24 Mar 2008 01:05:32 -0000 1.28.2.42 +++ content_admin.inc 11 Apr 2008 21:01:13 -0000 @@ -1297,6 +1297,7 @@ function content_alter_db_field($previou * Additional optional attributes. Recognized attributes: * not null => TRUE|FALSE * default => NULL|FALSE|value (with or without '', it won't be added) + * index => TRUE|FALSE */ function content_db_add_column($table, $column, $type, $attributes = array()) { switch ($GLOBALS['db_type']) { @@ -1372,6 +1373,15 @@ function content_db_add_column($table, $ db_query('ALTER TABLE {'. $table .'} ADD COLUMN '. $column .' '. $type .' '. $not_null .' '. $default); break; } + + // We only support the simplest possible index right now + // TODO: add support for multi-column index, index lengths and index types + if ($attributes['index'] === TRUE) { + db_query('CREATE INDEX '. $column .' ON {'. $table .'} ('. $column .')'); + // For now, we also index the nid column automatically when any other + // column is indexed, because it is now likely to be used in views queries + db_query('CREATE INDEX nid ON {'. $table .'} (nid)'); + } } /** @@ -1394,6 +1404,7 @@ function content_db_add_column($table, $ * Additional optional attributes. Recognized attributes: * not null => TRUE|FALSE * default => NULL|FALSE|value (with or without '', it won't be added) + * index => TRUE|FALSE */ function content_db_change_column($table, $column, $column_new, $type, $attributes = array()) { switch ($GLOBALS['db_type']) { @@ -1474,4 +1485,10 @@ function content_db_change_column($table db_query('ALTER TABLE {'. $table .'} CHANGE '. $column .' '. $column_new .' '. $type .' '. $not_null .' '. $default); break; } + + // Recreate the index, if necessary + if ($attributes['index'] === TRUE) { + db_query('CREATE INDEX '. $column_new .' ON {'. $table .'} ('. $column_new .')'); + db_query('CREATE INDEX nid ON {'. $table .'} (nid)'); + } } \ No newline at end of file Index: nodereference.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cck/nodereference.module,v retrieving revision 1.39.2.35 diff -u -p -r1.39.2.35 nodereference.module --- nodereference.module 24 Mar 2008 01:58:34 -0000 1.39.2.35 +++ nodereference.module 11 Apr 2008 21:01:13 -0000 @@ -90,7 +90,7 @@ function nodereference_field_settings($o case 'database columns': $columns = array( - 'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), + 'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0', 'index' => TRUE), ); return $columns; @@ -104,6 +104,80 @@ function nodereference_field_settings($o 'extra' => array('field' => $field), ), ); + + case 'tables': + $tables = content_views_field_tables($field); + // Add another copy of the field table, joining on the referenced + $tables['node_data_'. $field['field_name'].'_rev'] = array( + 'name' => $tables['node_data_'. $field['field_name']]['name'], + 'join' => array( + 'left' => array( + 'table' => 'node', + 'field' => 'nid', + ), + 'right' => array( + 'field' => $field['field_name'].'_nid', + ), + ), + ); + return $tables; + + case 'arguments': + $arguments = content_views_field_arguments($field); + $field_types = _content_field_types(); + $arguments['content: '. $field['field_name'] .'_rev'] = array( + 'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' rev ('. $field['field_name'] .')', + 'handler' => 'noderef_views_argument_handler', + ); + return $arguments; + } +} + +function noderef_views_argument_handler($op, &$query, $argtype, $arg = '') { + if ($op == 'filter') { + $field_name = substr($argtype['type'], 9, -4); + } + else { + $field_name = substr($argtype, 9, -4); + } + $field = content_fields($field_name); + + // The table name used here is the Views alias for the table, not the actual + // table name. + $table = 'node_data_'. $field['field_name'].'_rev'; + + switch ($op) { + case 'summary': + // To produce summaries we need to join to the referrer nid, rather than the referenced nid + $joininfo = array( + 'left' => array( + 'table' => 'node', + 'field' => 'nid', + ), + 'right' => array( + 'field' => 'nid', + ), + ); + $query->add_table($table, FALSE, 1, $joininfo); + $query->add_field("title"); + $query->add_field('nid', $table); + $fieldinfo['field'] = "$table.nid"; + return $fieldinfo; + + case 'sort': + break; + + case 'filter': + $nid = intval($arg); + $query->ensure_table($table); + $query->add_where("$table.nid = %d", $nid); + break; + + case 'link': + return l($query->title, "$arg/$query->nid"); + + case 'title': + return content_format($field, $item, 'plain'); } } Index: userreference.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cck/userreference.module,v retrieving revision 1.30.2.22 diff -u -p -r1.30.2.22 userreference.module --- userreference.module 2 Mar 2008 23:49:03 -0000 1.30.2.22 +++ userreference.module 11 Apr 2008 21:01:13 -0000 @@ -55,7 +55,7 @@ function userreference_field_settings($o case 'database columns': $columns = array( - 'uid' => array('type' => 'int', 'not null' => FALSE, 'default' => NULL), + 'uid' => array('type' => 'int', 'not null' => FALSE, 'default' => NULL, 'index' => TRUE), ); return $columns;