From 29abb03e7188ba41a9baf3707cc8a24c4aa5551f Mon Sep 17 00:00:00 2001 From: stepango Date: Fri, 23 Oct 2015 11:53:21 +0800 Subject: [PATCH 1/3] Auto migrations and updateMultiple bug fixes --- gradle/wrapper/gradle-wrapper.properties | 4 ++-- src/com/activeandroid/sebbia/Model.java | 18 +++++++++--------- .../sebbia/automigration/SQLTableInfo.java | 16 +++++++++++----- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 330a2c9f8..9770061d7 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Nov 29 09:06:20 EST 2013 +#Thu Feb 26 10:43:00 SGT 2015 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-bin.zip +distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-all.zip diff --git a/src/com/activeandroid/sebbia/Model.java b/src/com/activeandroid/sebbia/Model.java index 00ad57d11..670618639 100644 --- a/src/com/activeandroid/sebbia/Model.java +++ b/src/com/activeandroid/sebbia/Model.java @@ -16,11 +16,6 @@ * limitations under the License. */ -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -37,6 +32,11 @@ import com.activeandroid.sebbia.util.Log; import com.activeandroid.sebbia.util.ReflectionUtils; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + @SuppressWarnings("unchecked") public abstract class Model { @@ -103,7 +103,7 @@ public static void delete(Class type, long id) { public static T load(Class type, long id) { T model = (T) Cache.getEntity(type, id); - if (model == null) { + if (model == null) { TableInfo tableInfo = Cache.getTableInfo(type); model = new Select().from(type).where(tableInfo.getIdName() + "=?", id).executeSingle(); } @@ -128,8 +128,8 @@ public static void saveMultiple(List entities) { } } else { fillContentValues(entity, values); - db.update(entity.mTableInfo.getTableName(), values, "Id=" + entity.mId, null); - } + db.update(entity.mTableInfo.getTableName(), values, entity.mTableInfo.getIdName() + "=" + entity.mId, null); + } } } @@ -320,7 +320,7 @@ protected final List getMany(Class type, String foreignK protected String getIdName() { return idName; } - + protected void setModelId(long id) { mId = id; } diff --git a/src/com/activeandroid/sebbia/automigration/SQLTableInfo.java b/src/com/activeandroid/sebbia/automigration/SQLTableInfo.java index 63832b60a..4db3ab6fc 100644 --- a/src/com/activeandroid/sebbia/automigration/SQLTableInfo.java +++ b/src/com/activeandroid/sebbia/automigration/SQLTableInfo.java @@ -1,12 +1,17 @@ package com.activeandroid.sebbia.automigration; +import android.text.TextUtils; + import java.util.ArrayList; import java.util.List; import java.util.Locale; - -import android.text.TextUtils; +import java.util.regex.Pattern; public final class SQLTableInfo { + + private static final Pattern S_PLUS = Pattern.compile("\\s+"); + private static final Pattern CREATE_TABLE = Pattern.compile("(?i)CREATE TABLE "); + private static final Pattern UNIQUE = Pattern.compile(", UNIQUE \\(.*\\) ON CONFLICT (ROLLBACK|ABORT|FAIL|IGNORE|REPLACE)"); public static String constructSchema(String tableName, List columns) { String schema = "CREATE TABLE " + tableName + "(%s);"; @@ -29,14 +34,15 @@ public SQLTableInfo(String sqlSchema) { if (TextUtils.isEmpty(sqlSchema)) throw new IllegalArgumentException("Cannot construct SqlTableInfo from empty sqlSchema"); - sqlSchema = sqlSchema.replaceAll("\\s+", " "); - this.mSchema = new String(sqlSchema); + sqlSchema = S_PLUS.matcher(sqlSchema).replaceAll(" "); + this.mSchema = sqlSchema; if (!sqlSchema.toUpperCase(Locale.US).startsWith("CREATE TABLE") || !sqlSchema.contains("(") || !sqlSchema.contains(")")) throw new IllegalArgumentException("sqlSchema doesn't appears to be valid"); mColumns = new ArrayList(); - sqlSchema = sqlSchema.replaceAll("(?i)CREATE TABLE ", ""); + sqlSchema = CREATE_TABLE.matcher(sqlSchema).replaceAll(""); + sqlSchema = UNIQUE.matcher(sqlSchema).replaceAll(""); mTableName = sqlSchema.substring(0, sqlSchema.indexOf('(')).replace("\"", ""); String columnDefinitions = sqlSchema.substring(sqlSchema.indexOf('(') + 1, sqlSchema.lastIndexOf(')')); From b2b991f198d8b48a65c7604f74d09beb82a797ff Mon Sep 17 00:00:00 2001 From: stepango Date: Wed, 2 Dec 2015 16:25:10 +0800 Subject: [PATCH 2/3] preSave() method added --- src/com/activeandroid/sebbia/Model.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/com/activeandroid/sebbia/Model.java b/src/com/activeandroid/sebbia/Model.java index 670618639..e6abe0187 100644 --- a/src/com/activeandroid/sebbia/Model.java +++ b/src/com/activeandroid/sebbia/Model.java @@ -77,7 +77,12 @@ public final void delete() { .notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null); } + protected void preSave(){ + //Override it to execute logic before save() method would be called + } + public final Long save() { + preSave(); SQLiteDatabase db = Cache.openDatabase(); ContentValues values = new ContentValues(); fillContentValues(this, values); @@ -240,7 +245,7 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) { private void loadFromCursorWithFiller(Cursor cursor, ModelFiller filler) { int columnIndex = cursor.getColumnIndex(idName); - if (cursor.isNull(columnIndex) == false) + if (!cursor.isNull(columnIndex)) mId = cursor.getLong(columnIndex); else mId = null; @@ -296,7 +301,7 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) { } // Set the field value - if (value != null) { + if (value != null && field != null) { field.set(this, value); } } catch (IllegalArgumentException e) { From 740ba9d0d370f80920bf42bab6baba0ba895801b Mon Sep 17 00:00:00 2001 From: stepango Date: Wed, 2 Dec 2015 16:26:33 +0800 Subject: [PATCH 3/3] code cleaning --- src/com/activeandroid/sebbia/Model.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/activeandroid/sebbia/Model.java b/src/com/activeandroid/sebbia/Model.java index e6abe0187..b9dfba395 100644 --- a/src/com/activeandroid/sebbia/Model.java +++ b/src/com/activeandroid/sebbia/Model.java @@ -77,10 +77,6 @@ public final void delete() { .notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null); } - protected void preSave(){ - //Override it to execute logic before save() method would be called - } - public final Long save() { preSave(); SQLiteDatabase db = Cache.openDatabase(); @@ -330,6 +326,10 @@ protected void setModelId(long id) { mId = id; } + protected void preSave(){ + //Override it to execute logic before save() method would be called + } + // //////////////////////////////////////////////////////////////////////////////////// // OVERRIDEN METHODS // ////////////////////////////////////////////////////////////////////////////////////