Skip to content

Commit

Permalink
Add byte array type of field mapping as blob in table.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinyu890807 committed Jan 20, 2016
1 parent a18aa74 commit 10f7d1f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
classpath 'com.android.tools.build:gradle:2.0.0-alpha5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
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 @@
#Wed Apr 10 15:27:10 PDT 2013
#Mon Jan 18 19:36:36 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
59 changes: 36 additions & 23 deletions litepal/src/main/java/org/litepal/LitePalBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.litepal.tablemanager.model.AssociationsModel;
import org.litepal.tablemanager.model.ColumnModel;
import org.litepal.tablemanager.model.TableModel;
import org.litepal.tablemanager.typechange.BlobOrm;
import org.litepal.tablemanager.typechange.BooleanOrm;
import org.litepal.tablemanager.typechange.DateOrm;
import org.litepal.tablemanager.typechange.DecimalOrm;
Expand All @@ -39,8 +40,10 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -69,7 +72,12 @@ public abstract class LitePalBase {
* All the supporting mapping types currently in the array.
*/
private OrmChange[] typeChangeRules = { new NumericOrm(), new TextOrm(), new BooleanOrm(),
new DecimalOrm(), new DateOrm() };
new DecimalOrm(), new DateOrm(), new BlobOrm()};

/**
* This is map of class name to fields list. Indicates that each class has which fields.
*/
private Map<String, List<Field>> classFieldsMap = new HashMap<String, List<Field>>();

/**
* The collection contains all association models.
Expand Down Expand Up @@ -154,29 +162,34 @@ protected Collection<AssociationsInfo> getAssociationInfo(String className) {
* @return A list of supported fields
*/
protected List<Field> getSupportedFields(String className) {
List<Field> supportedFields = new ArrayList<Field>();
Class<?> dynamicClass;
try {
dynamicClass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
}
Field[] fields = dynamicClass.getDeclaredFields();
for (Field field : fields) {
Column annotation = field.getAnnotation(Column.class);
if (annotation != null && annotation.ignore()) {
continue;
List<Field> fieldList = classFieldsMap.get(className);
if (fieldList == null) {
List<Field> supportedFields = new ArrayList<Field>();
Class<?> dynamicClass;
try {
dynamicClass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
}
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers)) {
Class<?> fieldTypeClass = field.getType();
String fieldType = fieldTypeClass.getName();
if (BaseUtility.isFieldTypeSupported(fieldType)) {
supportedFields.add(field);
}
}
}
return supportedFields;
Field[] fields = dynamicClass.getDeclaredFields();
for (Field field : fields) {
Column annotation = field.getAnnotation(Column.class);
if (annotation != null && annotation.ignore()) {
continue;
}
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers)) {
Class<?> fieldTypeClass = field.getType();
String fieldType = fieldTypeClass.getName();
if (BaseUtility.isFieldTypeSupported(fieldType)) {
supportedFields.add(field);
}
}
}
classFieldsMap.put(className, supportedFields);
return supportedFields;
}
return fieldList;
}

/**
Expand Down
39 changes: 22 additions & 17 deletions litepal/src/main/java/org/litepal/crud/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@

package org.litepal.crud;

import static org.litepal.util.BaseUtility.changeCase;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.SparseArray;

import org.litepal.LitePalBase;
import org.litepal.crud.model.AssociationsInfo;
import org.litepal.exceptions.DataSupportException;
import org.litepal.exceptions.DatabaseGenerateException;
import org.litepal.util.BaseUtility;
import org.litepal.util.Const;
import org.litepal.util.DBUtility;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand All @@ -27,18 +38,7 @@
import java.util.Date;
import java.util.List;

import org.litepal.LitePalBase;
import org.litepal.crud.model.AssociationsInfo;
import org.litepal.exceptions.DataSupportException;
import org.litepal.exceptions.DatabaseGenerateException;
import org.litepal.util.BaseUtility;
import org.litepal.util.Const;
import org.litepal.util.DBUtility;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.SparseArray;
import static org.litepal.util.BaseUtility.changeCase;

/**
* This is the base class for CRUD component. All the common actions which can
Expand Down Expand Up @@ -850,6 +850,9 @@ private Object getInitParamValue(Class<?> modelClass, Class<?> paramType) {
if ("char".equals(paramTypeName) || "java.lang.Character".equals(paramTypeName)) {
return ' ';
}
if ("[B".equals(paramTypeName) || "[Ljava.lang.Byte;".equals(paramTypeName)) {
return new byte[0];
}
if ("java.lang.String".equals(paramTypeName)) {
return "";
}
Expand Down Expand Up @@ -1048,9 +1051,9 @@ private String genGetColumnMethod(Class<?> fieldType) {
String typeName;
if (fieldType.isPrimitive()) {
typeName = BaseUtility.capitalize(fieldType.getName());
} else {
typeName = fieldType.getSimpleName();
}
} else {
typeName = fieldType.getSimpleName();
}
String methodName = "get" + typeName;
if ("getBoolean".equals(methodName)) {
methodName = "getInt";
Expand All @@ -1060,7 +1063,9 @@ private String genGetColumnMethod(Class<?> fieldType) {
methodName = "getLong";
} else if ("getInteger".equals(methodName)) {
methodName = "getInt";
}
} else if ("getbyte[]".equalsIgnoreCase(methodName)) {
methodName = "getBlob";
}
return methodName;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.litepal.tablemanager.typechange;

/**
* This class deals with byte type.
*
* @author Tony Green
* @since 1.3.1
*/
public class BlobOrm extends OrmChange{

/**
* If the field type passed in is byte, it will change it into blob as
* column type.
*/
@Override
public String object2Relation(String fieldType) {
if (fieldType != null) {
if (fieldType.equals("byte") || fieldType.equals("java.lang.Byte")) {
return "blob";
}
}
return null;
}

}
3 changes: 3 additions & 0 deletions litepal/src/main/java/org/litepal/util/BaseUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public static boolean isFieldTypeSupported(String fieldType) {
if ("char".equals(fieldType) || "java.lang.Character".equals(fieldType)) {
return true;
}
if ("[B".equals(fieldType) || "[Ljava.lang.Byte;".equals(fieldType)) {
return true;
}
if ("java.lang.String".equals(fieldType) || "java.util.Date".equals(fieldType)) {
return true;
}
Expand Down

0 comments on commit 10f7d1f

Please sign in to comment.