kerou / android-active-record

Automatically exported from code.google.com/p/android-active-record
0 stars 0 forks source link

Errors on Insert do not raise exception #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, just started to use your active record - very handy library.
It's early days for my app, and I'm changing the DB schema frequently. I forgot 
to update the database schema number, so insert failed. But it did it silently 
- no exception was thrown.

In ActiveRecordBase.insert() I'd recommend it throws an exception if the id 
returned in -1 at the end of the method. See below.

What do you think?

Cheers,
James

    public long insert() throws ActiveRecordException {
        List<Field> columns = _id > 0 ? getColumnFields()
                : getColumnFieldsWithoutID();
        ContentValues values = new ContentValues(columns.size());
        for (Field column : columns) {
            try {
                if (column.getType().getSuperclass() == ActiveRecordBase.class)
                    values.put(
                            CamelNotationHelper.toSQLName(column.getName()),
                            column.get(this) != null ? String
                                    .valueOf(((ActiveRecordBase) column
                                            .get(this))._id) : "0");
                else
                    values.put(CamelNotationHelper.toSQLName(column.getName()),
                            String.valueOf(column.get(this)));
            } catch (IllegalAccessException e) {
                throw new ActiveRecordException(e.getLocalizedMessage());
            }
        }
        _id = m_Database.insert(getTableName(), values);
        if (-1 != _id)
            m_NeedsInsert = false;

        return _id;
    }

Original issue reported on code.google.com by vod...@gmail.com on 3 Jan 2011 at 7:44

GoogleCodeExporter commented 9 years ago
Thanks for a feedback. 
Will definitely implement this

Original comment by Vladimir.Kroz on 29 Mar 2011 at 8:40