Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Relation#upsert() #379

Merged
merged 10 commits into from
Feb 7, 2017
Merged

implement Relation#upsert() #379

merged 10 commits into from
Feb 7, 2017

Conversation

gfx
Copy link
Member

@gfx gfx commented Jan 29, 2017

Resolve #294 by adding a new Relation#upsert() method.

Because this is a high-level method, it is defined in Relation, not in OrmaDatabase.

Also deprecates Relation#upserter() because it is very confusing. Use #inserter(OnConflict.REPLACE) if you know what to do.


the initial, generic version of #upsertWithoutTransaction():

@SuppressWarnings("unchecked")
@NonNull
public <PK> Model upsertWithoutTransaction(@NonNull final Model model) {
    Schema<Model> schema = getSchema();

    ContentValues contentValues = new ContentValues();

    for (ColumnDef<Model, ?> column : schema.getColumns()) {
        if (column.isPrimaryKey() && column.isAutoValue()) {
            // nothing to do
        } else if (column instanceof AssociationDef) {
            Object newAssociatedModel = updateOrInsertForAssociation(((AssociationDef) column).associationSchema,
                    column.get(model));
            schema.putToContentValues(contentValues, (ColumnDef) column, newAssociatedModel);
        } else if (column.type instanceof ParameterizedType
                && ((Class<?>) ((ParameterizedType) column.type).getRawType()).isAssignableFrom(SingleAssociation.class)) {
            SingleAssociation<?> assoc = (SingleAssociation<?>) column.get(model);
            Object newAssociatedModel = updateOrInsertForAssociation((Schema) Schemas.get(assoc.get().getClass()),
                    assoc.get());
            schema.putToContentValues(contentValues, (ColumnDef) column, SingleAssociation.just(newAssociatedModel));
        } else {
            schema.putToContentValues(contentValues, (ColumnDef) column, column.get(model));
        }
    }

    ColumnDef<Model, PK> primaryKey = (ColumnDef<Model, PK>) schema.getPrimaryKey();

    if (hasInitializedPrimaryKey(primaryKey, model)) {
        int updatedRows = updater()
                .where(primaryKey, "=", primaryKey.getSerialized(model))
                .putAll(contentValues)
                .execute();

        if (updatedRows != 0) {
            return model;
        }
    }

    long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
    return conn.findByRowId(schema, rowId);
}

private <M> boolean hasInitializedPrimaryKey(ColumnDef<M, ?> primaryKey, M model) {
    return primaryKey.get(model) != null
            && (!primaryKey.isAutoValue() || ((Number) primaryKey.get(model)).longValue() != 0);
}

private <T> Object updateOrInsertForAssociation(Schema<T> associatedSchema, T model) {
    return associatedSchema.createRelation(conn).upsertWithoutTransaction(model);
}

@gfx gfx merged commit b456f59 into master Feb 7, 2017
@gfx gfx deleted the upsert2 branch February 7, 2017 01:36
@gfx gfx mentioned this pull request Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant