Created
May 24, 2014 08:40
-
-
Save hvisser/5590ba6782b8ee0e9e84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringArrayFieldConverter implements FieldConverter<String[]> { | |
@Override | |
public String[] fromCursorValue(Cursor cursor, int columnIndex) { | |
return cursor.getString(columnIndex).split(","); | |
} | |
@Override | |
public void toContentValue(String[] value, String key, ContentValues values) { | |
} | |
@Override | |
public ColumnType getColumnType() { | |
return ColumnType.JOIN; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testJoinEntity() { | |
final Cupboard cupboard = new CupboardBuilder().registerEntityConverterFactory(new EntityConverterFactory() { | |
@Override | |
public <T> EntityConverter<T> create(Cupboard cupboard, Class<T> type) { | |
if (type == TestJoinEntity.class) { | |
return (EntityConverter<T>) new TestJoinEntityConverter(); | |
} | |
return null; | |
} | |
}).build(); | |
SQLiteOpenHelper helper = new SQLiteOpenHelper(getContext(), "test_ls.db", null, 1) { | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
cupboard.withDatabase(db).createTables(); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
} | |
}; | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
cupboard.register(TestJoinEntity.class); | |
cupboard.withDatabase(db).createTables(); | |
db.execSQL("create table ids (_id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)"); | |
ContentValues values = new ContentValues(); | |
values.put("value", "test1"); | |
db.insert("ids", null, values); | |
values.put("value", "test2"); | |
db.insert("ids", null, values); | |
TestJoinEntity entity = new TestJoinEntity(); | |
cupboard.withDatabase(db).put(entity); | |
Cursor cursor = db.rawQuery("select e._id as _id, group_concat(j.value) as names from testjoinentity e, ids j", null); | |
assertEquals(1, cursor.getCount()); | |
entity = cupboard.withCursor(cursor).get(TestJoinEntity.class); | |
assertNotNull(entity.names); | |
} | |
public void testJoinEntityWithFieldConverter() { | |
final Cupboard cupboard = new CupboardBuilder().registerFieldConverter(String[].class, new StringArrayFieldConverter()).build(); | |
SQLiteOpenHelper helper = new SQLiteOpenHelper(getContext(), "test_ls.db", null, 1) { | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
cupboard.withDatabase(db).createTables(); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
} | |
}; | |
SQLiteDatabase db = helper.getWritableDatabase(); | |
cupboard.register(TestJoinEntity.class); | |
cupboard.withDatabase(db).createTables(); | |
db.execSQL("create table ids (_id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)"); | |
ContentValues values = new ContentValues(); | |
values.put("value", "test1"); | |
db.insert("ids", null, values); | |
values.put("value", "test2"); | |
db.insert("ids", null, values); | |
TestJoinEntity entity = new TestJoinEntity(); | |
cupboard.withDatabase(db).put(entity); | |
Cursor cursor = db.rawQuery("select e._id as _id, group_concat(j.value) as names from testjoinentity e, ids j", null); | |
assertEquals(1, cursor.getCount()); | |
entity = cupboard.withCursor(cursor).get(TestJoinEntity.class); | |
assertNotNull(entity.names); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestJoinEntity { | |
public Long _id; | |
public String[] names; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestJoinEntityConverter implements EntityConverter<TestJoinEntity> { | |
@Override | |
public TestJoinEntity fromCursor(Cursor cursor) { | |
TestJoinEntity entity = new TestJoinEntity(); | |
entity._id = cursor.getLong(0); | |
String names = cursor.getString(1); | |
if (names != null) { | |
entity.names = names.split(","); | |
} | |
return entity; | |
} | |
@Override | |
public void toValues(TestJoinEntity object, ContentValues values) { | |
if (object._id != null) { | |
values.put(BaseColumns._ID, object._id); | |
} | |
} | |
@Override | |
public List<Column> getColumns() { | |
return Arrays.asList(new Column("_id", ColumnType.INTEGER), new Column("names", ColumnType.JOIN)); | |
} | |
@Override | |
public void setId(Long id, TestJoinEntity instance) { | |
instance._id = id; | |
} | |
@Override | |
public Long getId(TestJoinEntity instance) { | |
return instance._id; | |
} | |
@Override | |
public String getTable() { | |
return TestJoinEntity.class.getSimpleName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment