0

I'd like to pass a Collection of ids to get back a list of entities that match those ids. This doesn't seem to work:

public static List<Program> getByIds(Collection<Integer> ids) {
    return new Select().from(Program.class)
        .where("programId in (?)", ids)
        .execute();
}

nor does this when I know I had two ids in the Collection:

public static List<Program> getByIds(Collection<Integer> ids) {
    return new Select().from(Program.class)
        .where("programId in (?,?)", ids)
        .execute();
}

I just get back nothing. And no errors. The program entities do exist as I've done a Select with no where and printed the returned items' ids.

Having to convert the Integer ids into String's and then into a joined String for use in the where clause seems like re-inventing the wheel. Surely, there's a better way?

Royce C
  • 69
  • 1
  • 2

2 Answers2

1

Eh, this is what I have, but seems weird that everybody who wants to use ActiveAndroid would have to do this to use 'in':

public static List<Program> getByIds(Collection<Integer> ids) {
    return new Select().from(Program.class)
        .where("programId in " + DAOUtil.makePlaceholders(ids.size()),
            ids.toArray(new Integer[ids.size()]))
        .execute();
}

where makePlaceholders can be found:

IN clause and placeholders

Community
  • 1
  • 1
Royce C
  • 69
  • 1
  • 2
0

Try giving it an Array rather than a collection because in ActiveAndroid's source, it is expecting an Array of objects

in where()

public From where(String clause, Object... args) {
    where(clause).addArguments(args);
    return this;
}

Going furthur deep...

void addArguments(Object[] args) {
    for(Object arg : args) {
        if (arg.getClass() == boolean.class || arg.getClass() == Boolean.class) {
            arg = (arg.equals(true) ? 1 : 0);
        }
        mArguments.add(arg);
    }
}

This should hopefully solve your problem

Mayur Kaul
  • 703
  • 5
  • 12