Permalink
Browse files

Merge branch 'serialize'

Conflicts:
	src/com/michaldabski/msqlite/DataTypes.java
  • Loading branch information...
mick88 committed Oct 5, 2013
2 parents 74dd97b + 5d420e6 commit 05e68c510d70fc437c5bb84ccab904dc55e46f72
@@ -8,6 +8,7 @@
TYPE_OTHER = 100,
TYPE_STRING = 101,
TYPE_COLLECTION = 102,
+ TYPE_SERIALIZABLE = 103,
TYPE_INT = 201,
TYPE_LONG = 202,
TYPE_SHORT = 203,
@@ -27,7 +28,8 @@
DATA_TYPE_DOUBLE = DATA_TYPE_REAL,
DATA_TYPE_FLOAT = DATA_TYPE_REAL,
DATA_TYPE_BOOLEAN = DATA_TYPE_NUMERIC,
- DATA_TYPE_NONE = "NONE";
+ DATA_TYPE_NONE = "NONE",
+ DATA_TYPE_BLOB = DATA_TYPE_NONE;
/**
* Gets database data type
@@ -56,7 +58,7 @@ public static String getDataType(int fieldType)
return DataTypes.DATA_TYPE_FLOAT;
default:
- return DataTypes.DATA_TYPE_NONE;
+ return DataTypes.DATA_TYPE_BLOB;
}
}
@@ -82,6 +84,6 @@ else if (cls.isAssignableFrom(Boolean.class) || cls.isAssignableFrom(boolean.cla
return TYPE_BOOL;
// else if (cls.isAssignableFrom(Collection.class))
// return TYPE_COLLECTION;
- else return TYPE_OTHER;
+ else return TYPE_SERIALIZABLE;
}
}
@@ -0,0 +1,61 @@
+package com.michaldabski.msqlite;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.io.StreamCorruptedException;
+
+public class SerializationUtils
+{
+ private SerializationUtils()
+ {
+
+ }
+
+ public static byte[] serialize(Object object) throws IOException, NotSerializableException
+ {
+ if (object instanceof Serializable == false)
+ throw new NotSerializableException();
+
+ ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
+ ObjectOutput objectOut = null;
+
+ try
+ {
+ objectOut = new ObjectOutputStream(byteOutStream);
+ objectOut.writeObject(object);
+ return byteOutStream.toByteArray();
+ }
+ finally
+ {
+ byteOutStream.close();
+ if (objectOut != null)
+ objectOut.close();
+ }
+
+ }
+
+ public static Object deserialize(byte [] bytes) throws StreamCorruptedException, IOException, ClassNotFoundException
+ {
+ ByteArrayInputStream inStream = new ByteArrayInputStream(bytes);
+ ObjectInput objectIn = null;
+
+ try
+ {
+ objectIn = new ObjectInputStream(inStream);
+ return objectIn.readObject();
+ }
+ finally
+ {
+ inStream.close();
+ if (objectIn != null)
+ objectIn.close();
+ }
+ }
+}
@@ -1,6 +1,9 @@
package com.michaldabski.msqlite.models;
+import java.io.NotSerializableException;
+import java.io.Serializable;
import java.lang.reflect.Field;
+import java.util.Locale;
import android.util.Log;
@@ -121,6 +124,22 @@ public Object getValue(Object object) throws IllegalArgumentException, NoSuchFie
}
}
+ /**
+ * Sets raw value without interpreting or converting
+ */
+ public void setValue(Object object, Object value)
+ {
+ try
+ {
+ field.set(object, value);
+ } catch (IllegalAccessException e)
+ {
+ field.setAccessible(true);
+ setValue(object, value);
+ field.setAccessible(false);
+ }
+ }
+
public void setValue(Object object, Long value) throws IllegalArgumentException
{
try
@@ -145,6 +164,11 @@ public void setValue(Object object, Long value) throws IllegalArgumentException
}
}
+ public int getFieldType()
+ {
+ return fieldType;
+ }
+
public void setValueFromString(Object object, String value) throws IllegalArgumentException
{
try
@@ -1,5 +1,7 @@
package com.michaldabski.msqlite.models;
+import java.io.IOException;
+import java.io.NotSerializableException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -11,6 +13,7 @@
import com.michaldabski.msqlite.Annotations.TableName;
import com.michaldabski.msqlite.DataTypes;
+import com.michaldabski.msqlite.SerializationUtils;
/**
* Represents a table in SQLite database.
@@ -135,6 +138,8 @@ public ContentValues getContentValues(Object object)
* Get content values only for selected columns
* @param object Object from which values are used
* @param colNames Subset of object's field names to use
+ * @throws IOException
+ * @throws NotSerializableException
*/
public ContentValues getContentValues(Object object, Collection<String> colNames)
{
@@ -146,12 +151,19 @@ public ContentValues getContentValues(Object object, Collection<String> colNames
try
{
value = column.getValue(object);
- if (value == null) values.putNull(column.name);
- else values.put(column.name, value.toString());
- } catch (Exception e)
+ if (value == null)
+ values.putNull(column.name);
+ else if (column.getFieldType() == DataTypes.TYPE_SERIALIZABLE)
+ values.put(column.name, SerializationUtils.serialize(value));
+ else
+ values.put(column.name, value.toString());
+ } catch (NoSuchFieldException e)
{
e.printStackTrace();
values.putNull(column.name);
+ } catch (IOException e)
+ {
+ throw new RuntimeException(e);
}
}
return values;
@@ -248,7 +260,20 @@ public String getFullWhereClause()
for (Column column : columns)
{
if ((columnId = cursor.getColumnIndex(column.name)) == -1) continue;
- column.setValueFromString(result, cursor.getString(columnId));
+ if (cursor.isNull(columnId))
+ column.setValue(result, (Object)null);
+ else if (column.getFieldType() == DataTypes.TYPE_SERIALIZABLE)
+ {
+ try
+ {
+ column.setValue(result, SerializationUtils.deserialize(cursor.getBlob(columnId)));
+ } catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ else
+ column.setValueFromString(result, cursor.getString(columnId));
}
return result;

0 comments on commit 05e68c5

Please sign in to comment.