Making fields, condition and join dynamic in db_select query
Don't chain your query, just put in some ifs and add to the query.  Like so

$query = db_select('node', 'n');
if(isset($field)) {
  // Add each field separate
  $query-&gt;addField('n', 'title', 'title');
}

if(isset($article)) {
  $query-&gt;condition('n.node-type', 'article', '=');
}

if(isset($join)) {
  $query-&gt;join()....
}

$query-&gt;execute();


I think this will get you to where you want.  Please also see this thread: http://drupal.org/node/310075