jonatasdaniel / andorm

Persistence framework for Android
23 stars 4 forks source link

return primary key value to new entity #47

Open yukiofera opened 12 years ago

yukiofera commented 12 years ago

class AndroidPersistenceManager

public long save(Object o) {
    { ... }

    Long primaryKey = 0L;
    String tableName = cache.getTableName();
    try {
        primaryKey = database.insert(tableName, null, values);
    } catch(SQLException e) {
        throw new AndOrmPersistenceException(MessageFormat.format(bundle.getString("save_error"), o.getClass().getCanonicalName(), e.getMessage()));
    }

    PrimaryKeyProperty primaryKeyProperty = cache.getPk();
    if (primaryKeyProperty.isAutoInc()) {
        if (primaryKeyProperty.getDatabaseFieldType().equals(Integer.class)) {
            primaryKeyProperty.set(o, primaryKey.intValue());
        } else if (primaryKeyProperty.getDatabaseFieldType().equals(Long.class)) {
            primaryKeyProperty.set(o, primaryKey);
        }
    }

    if (cache.getAfterSaveMethod() != null) {
        invoke(o, cache.getAfterSaveMethod()).withoutParams();
    }

    return primaryKey;
}

Would be interesting to return the primary key value.

[]'s

jonatasdaniel commented 12 years ago

Can you fork the project and push this feature?

yukiofera commented 12 years ago

Yes I can. :)

jonatasdaniel commented 12 years ago

If possible, write the unit test ;)