Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save electrolobzik/dc4fcb638beb6986952f to your computer and use it in GitHub Desktop.
Save electrolobzik/dc4fcb638beb6986952f to your computer and use it in GitHub Desktop.
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;
}
}
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);
}
public class TestJoinEntity {
public Long _id;
public String[] names;
}
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