Two problems in api_db_rewrite_sql

Migrated issue

Reported by: jhodgdon

The function api_db_rewrite_sql() in api.module has two problems:

a) As it is written, it adds a where clause that says "type <> 'api'" with no table prefix on the type field. This is giving me an error when I try to do any search using the core Search module:

Column 'type' in where clause is ambiguous query: SELECT SUM(i.score * t.count) AS score FROM search_index i INNER JOIN search_total t ON i.word = t.word INNER JOIN node n ON n.nid = i.sid WHERE n.status = 1 AND (type <> 'api') AND (i.word = 'test') AND i.type = 'node' GROUP BY i.type, i.sid HAVING COUNT(*) >= 1 ORDER BY score DESC LIMIT 0, 1 in /opt/www/drupal-6.13/modules/search/search.module on line 946.

b) It is assuming that the primary table is called 'n', whereas someone could be calling db_rewrite_sql() with the node table aliased to something else. The right thing to do (I'm pretty sure) is ot check that the primary field is 'nid', and assume that means that whatever primary table is passed in, it's an alias for the node table.

So, I think the body of the function should be:

if( $primary_field == 'nid' ) {
    return array( 'where' => $primary_table . ".type <> 'api'" );
}

That change at least gets rid of the error message I'm seeing, and allows me to use the core Search module on my site (and it doesn't return any API nodes, though it sure would be nice if it wasn't trying to index all 1000+ of them that were generated for my small API site! The cron runs will never get through everything and index the rest of the site...).