ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

Bug in Update(Table.class).Set() causes boolean parameters to be stored as strings

Open vessnv opened this issue 9 years ago • 3 comments

I was running

new Update(Table.class).set("columnName = ?", false);
new Select().from(Table.class).where("columnName = ?", false);

The select returns 0 results. The issue is that Where actually looks at the parameter type and converts it correctly, whereas Set simply runs .toString() on all arguments, which results in booleans being stored in the db as true and false instead of 1 and 0.

This is from Set.java:

public String[] getArguments() {
    final int setSize = mSetArguments.size();
    final int whereSize = mWhereArguments.size();
    final String[] args = new String[setSize + whereSize];

    for (int i = 0; i < setSize; i++) {
        args[i] = mSetArguments.get(i).toString();
    }

    for (int i = 0; i < whereSize; i++) {
        args[i + setSize] = mWhereArguments.get(i).toString();
    }

    return args;
}

This is from From.java:

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);
    }
}

Set needs to handle booleans like Where, otherwise you end up with a table that looks like this: image

vessnv avatar Feb 08 '16 18:02 vessnv

I can confirm this, that's why I use set("SomeBoolean = ?", condition ? 1 : 0) for boolean columns

jlhonora avatar Feb 08 '16 20:02 jlhonora

@jlhonora : Your solution saved my efforts to migrate to any other ORM.

uniruddh avatar Apr 08 '16 08:04 uniruddh

bump

chandru9279 avatar Nov 22 '16 05:11 chandru9279