Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
27 changes: 16 additions & 11 deletions src/com/activeandroid/sebbia/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -78,6 +78,7 @@ public final void delete() {
}

public final Long save() {
preSave();
SQLiteDatabase db = Cache.openDatabase();
ContentValues values = new ContentValues();
fillContentValues(this, values);
Expand All @@ -103,7 +104,7 @@ public static void delete(Class<? extends Model> type, long id) {

public static <T extends Model> T load(Class<T> 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();
}
Expand All @@ -130,8 +131,8 @@ public static void saveMultiple(List<? extends Model> 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);
}
}
}

Expand Down Expand Up @@ -242,7 +243,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;
Expand Down Expand Up @@ -298,7 +299,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) {
Expand All @@ -322,11 +323,15 @@ protected final <T extends Model> List<T> getMany(Class<T> type, String foreignK
protected String getIdName() {
return idName;
}

protected void setModelId(long id) {
mId = id;
}

protected void preSave(){
//Override it to execute logic before save() method would be called
}

// ////////////////////////////////////////////////////////////////////////////////////
// OVERRIDEN METHODS
// ////////////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 11 additions & 5 deletions src/com/activeandroid/sebbia/automigration/SQLTableInfo.java
Original file line number Diff line number Diff line change
@@ -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<SQLColumnInfo> columns) {
String schema = "CREATE TABLE " + tableName + "(%s);";
Expand All @@ -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<SQLColumnInfo>();

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(')'));
Expand Down