Skip to content

Commit

Permalink
[models] make delete return boolean from ModelAdapter. Set + Insert n…
Browse files Browse the repository at this point in the history
…ow both support async wrapper method that simply returns builder handle to conveniently call it asnychronously.
  • Loading branch information
agrosner committed Jan 1, 2017
1 parent 1ff2e14 commit 1a5e08f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,19 @@ class OneToManyDeleteMethod(private val tableDefinition: TableDefinition,
builder.addStatement("getModelCache().removeModel(getCachingId(\$L))", ModelUtils.variable)
}

builder.addStatement("super.delete(\$L\$L)", ModelUtils.variable,
builder.addStatement("boolean successful = super.delete(\$L\$L)", ModelUtils.variable,
if (useWrapper) ", " + ModelUtils.wrapper else "")

tableDefinition.oneToManyDefinitions.forEach { it.writeDelete(builder, useWrapper) }

val delete = MethodSpec.methodBuilder("delete").addAnnotation(Override::class.java)
builder.addStatement("return successful")

val delete = MethodSpec.methodBuilder("delete")
.addAnnotation(Override::class.java)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addParameter(tableDefinition.elementClassName, ModelUtils.variable)
.addCode(builder.build()).returns(TypeName.VOID)
.addCode(builder.build())
.returns(TypeName.BOOLEAN)
if (useWrapper) {
delete.addParameter(ClassNames.DATABASE_WRAPPER, ModelUtils.wrapper)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.raizlabs.android.dbflow.sql.language.property.IProperty;
import com.raizlabs.android.dbflow.structure.ModelAdapter;
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper;
import com.raizlabs.android.dbflow.structure.database.transaction.ITransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -234,6 +236,20 @@ public long executeUpdateDelete() {
throw new IllegalStateException("Cannot call executeUpdateDelete() from an Insert");
}

/**
* @return A {@link Transaction.Builder} handle to begin an async transaction.
* A simple helper method.
*/
public Transaction.Builder async() {
return FlowManager.getDatabaseForTable(getTable())
.beginTransactionAsync(new ITransaction() {
@Override
public void execute(DatabaseWrapper databaseWrapper) {
Insert.this.execute(databaseWrapper);
}
});
}

@Override
public String getQuery() {
ValueQueryBuilder queryBuilder = new ValueQueryBuilder("INSERT ");
Expand All @@ -256,12 +272,12 @@ public String getQuery() {
} else {
if (valuesList == null || valuesList.size() < 1) {
throw new IllegalStateException("The insert of " + FlowManager.getTableName(getTable()) + " should have" +
"at least one value specified for the insert");
"at least one value specified for the insert");
} else if (columns != null) {
for (Object[] values : valuesList) {
if (values.length != columns.length) {
throw new IllegalStateException("The Insert of " + FlowManager.getTableName(getTable()) + " when specifying" +
"columns needs to have the same amount of values and columns");
"columns needs to have the same amount of values and columns");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import android.content.ContentValues;

import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.Query;
import com.raizlabs.android.dbflow.sql.QueryBuilder;
import com.raizlabs.android.dbflow.sql.SqlUtils;
import com.raizlabs.android.dbflow.sql.language.property.IProperty;
import com.raizlabs.android.dbflow.sql.queriable.Queriable;
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper;
import com.raizlabs.android.dbflow.structure.database.transaction.ITransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;

/**
* Description: Used to specify the SET part of an {@link com.raizlabs.android.dbflow.sql.language.Update} query.
Expand Down Expand Up @@ -132,6 +135,20 @@ public String getQuery() {
return queryBuilder.getQuery();
}

/**
* @return A {@link Transaction.Builder} handle to begin an async transaction.
* A simple helper method.
*/
public Transaction.Builder async() {
return FlowManager.getDatabaseForTable(getTable())
.beginTransactionAsync(new ITransaction() {
@Override
public void execute(DatabaseWrapper databaseWrapper) {
Set.this.executeUpdateDelete(databaseWrapper);
}
});
}

@Override
public Query getQueryBuilderBase() {
return update;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public interface InternalAdapter<TModel> {
*
* @param model The model to delete
*/
void delete(TModel model);
boolean delete(TModel model);

/**
* Deletes the model from the DB
*
* @param model The model to delete
* @param databaseWrapper The manually specified wrapper.
*/
void delete(TModel model, DatabaseWrapper databaseWrapper);
boolean delete(TModel model, DatabaseWrapper databaseWrapper);

/**
* Updates a {@link Collection} of models in the DB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ public void updateAll(Collection<TModel> models, DatabaseWrapper databaseWrapper
}

@Override
public void delete(TModel model) {
getModelSaver().delete(model);
public boolean delete(TModel model) {
return getModelSaver().delete(model);
}

@Override
public void delete(TModel model, DatabaseWrapper databaseWrapper) {
getModelSaver().delete(model, databaseWrapper);
public boolean delete(TModel model, DatabaseWrapper databaseWrapper) {
return getModelSaver().delete(model, databaseWrapper);
}

@Override
Expand Down

0 comments on commit 1a5e08f

Please sign in to comment.