Extract some duplicated query building logic and add support for multi-level link queries

Eliminates the duplicated handling for IN clauses as well as a few other bits, and in the process got multi-level links close enough to working that I just went ahead and finished it.

@alazier @jpsim 
rather than doing this we could change the line a few lines down:
```
 else if (exp1Type == NSKeyPathExpressionType && exp2Type == NSConstantValueExpressionType) {
```
to
```
 else if (exp1Type == NSKeyPathExpressionType && (exp2Type == NSConstantValueExpressionType || exp2Type == NSAggregateExpressionType) {
```
It's a few more characters but not hacky. If we leave it as is we should at least comment that we are collapsing the two types and split them out later.