Skip to content
Merged
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
69 changes: 34 additions & 35 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,17 @@
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;
import android.util.Log;

import com.orm.annotation.Table;
import com.orm.annotation.Unique;
import com.orm.dsl.BuildConfig;
import com.orm.helper.ManifestHelper;
import com.orm.helper.NamingHelper;
import com.orm.inflater.EntityInflater;
import com.orm.util.QueryBuilder;
import com.orm.util.ReflectionUtil;
import com.orm.util.SugarCursor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.*;

import static com.orm.SugarContext.getSugarContext;

Expand Down Expand Up @@ -198,7 +191,7 @@ public static <T> List<T> find(Class<T> type, String whereClause, String... wher
public static <T> List<T> findWithQuery(Class<T> type, String query, String... arguments) {
Cursor cursor = getSugarDataBase().rawQuery(query, arguments);

return getEntitiesFromCursor(cursor, type);
return getEntitiesFromCursor(cursor, type);
}

public static void executeQuery(String query, String... arguments) {
Expand All @@ -216,13 +209,33 @@ public static <T> List<T> find(Class<T> type, String whereClause, String[] where
return getEntitiesFromCursor(cursor, type);
}

public static <T> List<T> findOneToMany(Class<T> type, String relationFieldName, Object relationObject, Long relationObjectId) {
String args[] = { String.valueOf(relationObjectId) };
String whereClause = NamingHelper.toSQLNameDefault(relationFieldName) + " = ?";

Cursor cursor = getSugarDataBase().query(NamingHelper.toTableName(type), null, whereClause, args,
null, null, null, null);

return getEntitiesFromCursor(cursor, type, relationFieldName, relationObject);
}

public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type){
return getEntitiesFromCursor(cursor, type, null, null);
}

public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type, String relationFieldName, Object relationObject){
T entity;
List<T> result = new ArrayList<>();
try {
while (cursor.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
new EntityInflater()
.withCursor(cursor)
.withObject(entity)
.withEntitiesMap(getSugarContext().getEntitiesMap())
.withRelationFieldName(relationFieldName)
.withRelationObject(relationObject)
.inflate();
result.add(entity);
}
} catch (Exception e) {
Expand Down Expand Up @@ -390,28 +403,6 @@ public static boolean isSugarEntity(Class<?> objectClass) {
return objectClass.isAnnotationPresent(Table.class) || SugarRecord.class.isAssignableFrom(objectClass);
}

private static void inflate(Cursor cursor, Object object, Map<Object, Long> entitiesMap) {
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
if (!entitiesMap.containsKey(object)) {
entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID"))));
}

for (Field field : columns) {
field.setAccessible(true);
Class<?> fieldType = field.getType();
if (isSugarEntity(fieldType)) {
try {
long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field)));
field.set(object, (id > 0) ? findById(fieldType, id) : null);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
ReflectionUtil.setFieldValueFromCursor(cursor, field, object);
}
}
}

public boolean delete() {
Long id = getId();
Class<?> type = getClass();
Expand Down Expand Up @@ -478,7 +469,11 @@ public long update() {

@SuppressWarnings("unchecked")
void inflate(Cursor cursor) {
inflate(cursor, this, getSugarContext().getEntitiesMap());
new EntityInflater()
.withCursor(cursor)
.withObject(this)
.withEntitiesMap(getSugarContext().getEntitiesMap())
.inflate();
}

public Long getId() {
Expand Down Expand Up @@ -516,7 +511,11 @@ public E next() {

try {
entity = type.getDeclaredConstructor().newInstance();
inflate(cursor, entity, getSugarContext().getEntitiesMap());
new EntityInflater()
.withCursor(cursor)
.withObject(entity)
.withEntitiesMap(getSugarContext().getEntitiesMap())
.inflate();
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/com/orm/annotation/OneToMany.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.orm.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created by Łukasz Wesołowski on 28.07.2016.
*/

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface OneToMany {
String targetField();
}
75 changes: 75 additions & 0 deletions library/src/main/java/com/orm/inflater/EntityInflater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.orm.inflater;

import android.database.Cursor;
import com.orm.SugarRecord;
import com.orm.inflater.field.*;
import com.orm.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public class EntityInflater {
private Cursor cursor;
private Object object;
private Object relationObject;
private String relationFieldName;
private Map<Object, Long> entitiesMap;

public EntityInflater withCursor(Cursor cursor) {
this.cursor = cursor;
return this;
}

public EntityInflater withObject(Object object) {
this.object = object;
return this;
}

public EntityInflater withRelationObject(Object relationObject) {
this.relationObject = relationObject;
return this;
}

public EntityInflater withRelationFieldName(String relationFieldName) {
this.relationFieldName = relationFieldName;
return this;
}

public EntityInflater withEntitiesMap(Map<Object, Long> entitiesMap) {
this.entitiesMap = entitiesMap;
return this;
}

public void inflate() {
List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
Long objectId = cursor.getLong(cursor.getColumnIndex(("ID")));
if (!entitiesMap.containsKey(object)) {
entitiesMap.put(object, objectId);
}

FieldInflater fieldInflater;

for (Field field : columns) {
field.setAccessible(true);
Class<?> fieldType = field.getType();

if (SugarRecord.isSugarEntity(fieldType)) {
if (field.getName().equals(relationFieldName)) {
fieldInflater = new RelationEntityFieldInflater(field, cursor, object, fieldType, relationObject);
} else {
fieldInflater = new EntityFieldInflater(field, cursor, object, fieldType);
}
} else if (fieldType.equals(List.class)) {
fieldInflater = new ListFieldInflater(field, cursor, object, fieldType);
} else {
fieldInflater = new DefaultFieldInflater(field, cursor, object, fieldType);
}

fieldInflater.inflate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.orm.inflater.field;

import android.database.Cursor;
import com.orm.util.ReflectionUtil;

import java.lang.reflect.Field;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public class DefaultFieldInflater extends FieldInflater {

public DefaultFieldInflater(Field field, Cursor cursor, Object object, Class<?> fieldType) {
super(field, cursor, object, fieldType);
}

@Override
public void inflate() {
ReflectionUtil.setFieldValueFromCursor(cursor, field, object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.orm.inflater.field;

import android.database.Cursor;
import android.util.Log;
import com.orm.SugarRecord;
import com.orm.helper.NamingHelper;

import java.lang.reflect.Field;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public class EntityFieldInflater extends FieldInflater {
private static final String LOG_TAG = "EntityFieldInflater";

public EntityFieldInflater(Field field, Cursor cursor, Object object, Class<?> fieldType) {
super(field, cursor, object, fieldType);
}

@Override
public void inflate() {
try {
long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toColumnName(field)));
field.set(object, (id > 0) ? SugarRecord.findById(fieldType, id) : null);
} catch (IllegalAccessException e) {
Log.e(LOG_TAG, String.format("Error while inflating entity field %s", field), e);
}
}
}
24 changes: 24 additions & 0 deletions library/src/main/java/com/orm/inflater/field/FieldInflater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.orm.inflater.field;

import android.database.Cursor;

import java.lang.reflect.Field;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public abstract class FieldInflater {
protected Field field;
protected Cursor cursor;
protected Object object;
protected Class<?> fieldType;

public FieldInflater(Field field, Cursor cursor, Object object, Class<?> fieldType) {
this.field = field;
this.cursor = cursor;
this.object = object;
this.fieldType = fieldType;
}

public abstract void inflate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.orm.inflater.field;

import android.database.Cursor;
import android.util.Log;
import com.orm.SugarRecord;
import com.orm.annotation.OneToMany;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public class ListFieldInflater extends FieldInflater {
private static final String LOG_TAG = "ListFieldInflater";

public ListFieldInflater(Field field, Cursor cursor, Object object, Class<?> fieldType) {
super(field, cursor, object, fieldType);
}

@Override
public void inflate() {
if (field.isAnnotationPresent(OneToMany.class)) {
try {
Long objectId = cursor.getLong(cursor.getColumnIndex(("ID")));

ParameterizedType genericListType = (ParameterizedType) field.getGenericType();
Class<?> genericListClass = (Class<?>) genericListType.getActualTypeArguments()[0];
String targetName = field.getAnnotation(OneToMany.class).targetField();
field.set(object, SugarRecord.findOneToMany(genericListClass, targetName, object, objectId));
} catch (IllegalAccessException e) {
Log.e(LOG_TAG, String.format("Error while inflating list field %s", field), e);
}
} else {
Log.w(LOG_TAG, String.format("List field %s has not OneToMany annotation", field));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.orm.inflater.field;

import android.database.Cursor;
import android.util.Log;

import java.lang.reflect.Field;

/**
* Created by Łukasz Wesołowski on 03.08.2016.
*/
public class RelationEntityFieldInflater extends EntityFieldInflater {
private static final String LOG_TAG = "RelEntityFieldInflater";

protected Object relationObject;

public RelationEntityFieldInflater(Field field, Cursor cursor, Object object, Class<?> fieldType, Object relationObject) {
super(field, cursor, object, fieldType);
this.relationObject = relationObject;
}

@Override
public void inflate() {
try {
field.set(object, relationObject);
} catch (IllegalAccessException e) {
Log.e(LOG_TAG, String.format("Error while inflating %s field", field), e);
}
}
}
2 changes: 2 additions & 0 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj
} else {
values.put(columnName, (byte[]) columnValue);
}
} else if (columnType.equals(List.class)) {
//ignore
} else {
if (columnValue == null) {
values.putNull(columnName);
Expand Down
2 changes: 1 addition & 1 deletion library/src/test/java/com/orm/SchemaGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void testAllTableCreation() {
Cursor c = sqLiteDatabase.rawQuery(sql, null);

if (c.moveToFirst()) {
Assert.assertEquals(43, c.getInt(0));
Assert.assertEquals(47, c.getInt(0));
}

if (!c.isClosed()) {
Expand Down
Loading