From 1a5e08f4b69ae620fc3a2d290f5fda4c541ac5b0 Mon Sep 17 00:00:00 2001 From: Andrew Grosner Date: Sun, 1 Jan 2017 09:13:53 -0500 Subject: [PATCH] [models] make delete return boolean from ModelAdapter. Set + Insert now both support async wrapper method that simply returns builder handle to conveniently call it asnychronously. --- .../dbflow/processor/definition/Methods.kt | 10 +++++++--- .../android/dbflow/sql/language/Insert.java | 20 +++++++++++++++++-- .../android/dbflow/sql/language/Set.java | 17 ++++++++++++++++ .../dbflow/structure/InternalAdapter.java | 4 ++-- .../dbflow/structure/ModelAdapter.java | 8 ++++---- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/Methods.kt b/dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/Methods.kt index 72166d571..8d5a8a364 100644 --- a/dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/Methods.kt +++ b/dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/Methods.kt @@ -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) } diff --git a/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Insert.java b/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Insert.java index 4dc1009c0..c351d3279 100644 --- a/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Insert.java +++ b/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Insert.java @@ -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; @@ -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 "); @@ -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"); } } } diff --git a/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Set.java b/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Set.java index 3a758d860..a964801bd 100644 --- a/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Set.java +++ b/dbflow/src/main/java/com/raizlabs/android/dbflow/sql/language/Set.java @@ -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. @@ -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; diff --git a/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/InternalAdapter.java b/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/InternalAdapter.java index 0cccaee59..89e08e4e8 100644 --- a/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/InternalAdapter.java +++ b/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/InternalAdapter.java @@ -114,7 +114,7 @@ public interface InternalAdapter { * * @param model The model to delete */ - void delete(TModel model); + boolean delete(TModel model); /** * Deletes the model from the DB @@ -122,7 +122,7 @@ public interface InternalAdapter { * @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. diff --git a/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/ModelAdapter.java b/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/ModelAdapter.java index 18a53140a..eb78dfa2d 100644 --- a/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/ModelAdapter.java +++ b/dbflow/src/main/java/com/raizlabs/android/dbflow/structure/ModelAdapter.java @@ -180,13 +180,13 @@ public void updateAll(Collection 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