public String getCombinedTableName(Number subj, Number obj, Number[] ctxs) throws SQLException {
String union = " UNION ALL ";
StringBuilder sb = new StringBuilder(1024);
sb.append("(");
for (Number pred : triples.getPredicateIds()) {
TripleTable predicate;
try {
predicate = triples.getPredicateTable(pred);
}
catch (SQLException e) {
throw new AssertionError(e);
}
TransactionTable table = findTable(pred);
if ((table == null || table.isEmpty()) && predicate.isEmpty())
continue;
sb.append("SELECT ctx, subj, ");
if (predicate.isPredColumnPresent()) {
sb.append(" pred,");
}
else {
sb.append(pred).append(" AS pred,");
}
sb.append(" obj,");
sb.append(" expl");
sb.append("\nFROM ");
sb.append(predicate.getNameWhenReady());
sb.append("\nWHERE 1 ");
if (ctxs != null && ctxs.length > 0) {
sb.append(" AND ctx IN(");
for (Number ctx : ctxs) {
sb.append(ctx);
sb.append(",");
}
sb.delete(sb.length() - 1, sb.length());
sb.append(")");
}
if (subj != null) {
sb.append(" AND subj = " + subj);
}
if (obj != null) {
sb.append(" AND obj = " + obj);
}
sb.append(union);
predicate.blockUntilReady();
}
if (sb.length() < union.length())
return getEmptyTableName();
sb.delete(sb.length() - union.length(), sb.length());
sb.append(")");