Skip to content

Commit

Permalink
Fixing issue chennaione#243
Browse files Browse the repository at this point in the history
Conflicts:
	library/build.gradle
	sugartestapp/src/androidTest/java/me/jivimberg/android/sugartestapp/robolectric/SugarRecordTest.java
	sugartestapp/src/main/java/me/jivimberg/android/sugartestapp/model/annotated/Car.java
  • Loading branch information
jivimberg committed Feb 1, 2015
1 parent eadd351 commit 7e7df2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ buildscript {

dependencies {
compile 'com.android.support:support-v4:21.0.3'
compile group: 'com.google.guava', name: 'guava', version: '12.0'
}

android {
Expand Down
9 changes: 9 additions & 0 deletions library/src/com/orm/SugarContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import android.content.Context;

import com.google.common.collect.MapMaker;

import java.util.concurrent.ConcurrentMap;

public class SugarContext {

private static SugarContext instance = null;
private SugarDb sugarDb;
private Context context;
private ConcurrentMap<Object, Long> entitiesMap;

private SugarContext(Context context) {
this.context = context;
this.sugarDb = new SugarDb(context);
this.entitiesMap = new MapMaker().weakKeys().makeMap();
}

public static SugarContext getSugarContext() {
Expand Down Expand Up @@ -47,4 +53,7 @@ protected SugarDb getSugarDb() {
return sugarDb;
}

ConcurrentMap<Object, Long> getEntitiesMap() {
return entitiesMap;
}
}
16 changes: 16 additions & 0 deletions library/src/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,39 @@ public static long save(Object object) {
}

static long save(SQLiteDatabase db, Object object) {
Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
ContentValues values = new ContentValues(columns.size());
for (Field column : columns) {
ReflectionUtil.addFieldValueToColumn(values, column, object);
}

boolean isSugarEntity = isSugarEntity(object);
if (isSugarEntity && entitiesMap.containsKey(object)) {
values.put("id", entitiesMap.get(object));
}

long id = db.insertWithOnConflict(NamingHelper.toSQLName(object.getClass()), null, values,
SQLiteDatabase.CONFLICT_REPLACE);

//TODO should we remove the id field from SugarRecord?
if (SugarRecord.class.isAssignableFrom(object.getClass())) {
ReflectionUtil.setFieldValueForId(object, id);
}

if (isSugarEntity) {
entitiesMap.put(object, id);
}

Log.i("Sugar", object.getClass().getSimpleName() + " saved : " + id);

return id;
}

private static boolean isSugarEntity(Object object) {
return object.getClass().isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(object.getClass());
}

private static void inflate(Cursor cursor, Object object) {
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());

Expand Down

0 comments on commit 7e7df2f

Please sign in to comment.