use of com.activeandroid.query.Select in project ActiveAndroid by pardom.
the class Model method loadFromCursor.
// Model population
public final void loadFromCursor(Cursor cursor) {
/**
* Obtain the columns ordered to fix issue #106 (https://github.com/pardom/ActiveAndroid/issues/106)
* when the cursor have multiple columns with same name obtained from join tables.
*/
List<String> columnsOrdered = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
for (Field field : mTableInfo.getFields()) {
final String fieldName = mTableInfo.getColumnName(field);
Class<?> fieldType = field.getType();
final int columnIndex = columnsOrdered.indexOf(fieldName);
if (columnIndex < 0) {
continue;
}
field.setAccessible(true);
try {
boolean columnIsNull = cursor.isNull(columnIndex);
TypeSerializer typeSerializer = Cache.getParserForType(fieldType);
Object value = null;
if (typeSerializer != null) {
fieldType = typeSerializer.getSerializedType();
}
// can't know the type until runtime.
if (columnIsNull) {
field = null;
} else if (fieldType.equals(Byte.class) || fieldType.equals(byte.class)) {
value = cursor.getInt(columnIndex);
} else if (fieldType.equals(Short.class) || fieldType.equals(short.class)) {
value = cursor.getInt(columnIndex);
} else if (fieldType.equals(Integer.class) || fieldType.equals(int.class)) {
value = cursor.getInt(columnIndex);
} else if (fieldType.equals(Long.class) || fieldType.equals(long.class)) {
value = cursor.getLong(columnIndex);
} else if (fieldType.equals(Float.class) || fieldType.equals(float.class)) {
value = cursor.getFloat(columnIndex);
} else if (fieldType.equals(Double.class) || fieldType.equals(double.class)) {
value = cursor.getDouble(columnIndex);
} else if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
value = cursor.getInt(columnIndex) != 0;
} else if (fieldType.equals(Character.class) || fieldType.equals(char.class)) {
value = cursor.getString(columnIndex).charAt(0);
} else if (fieldType.equals(String.class)) {
value = cursor.getString(columnIndex);
} else if (fieldType.equals(Byte[].class) || fieldType.equals(byte[].class)) {
value = cursor.getBlob(columnIndex);
} else if (ReflectionUtils.isModel(fieldType)) {
final long entityId = cursor.getLong(columnIndex);
final Class<? extends Model> entityType = (Class<? extends Model>) fieldType;
Model entity = Cache.getEntity(entityType, entityId);
if (entity == null) {
entity = new Select().from(entityType).where(idName + "=?", entityId).executeSingle();
}
value = entity;
} else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
@SuppressWarnings("rawtypes") final Class<? extends Enum> enumType = (Class<? extends Enum>) fieldType;
value = Enum.valueOf(enumType, cursor.getString(columnIndex));
}
// Use a deserializer if one is available
if (typeSerializer != null && !columnIsNull) {
value = typeSerializer.deserialize(value);
}
// Set the field value
if (value != null) {
field.set(this, value);
}
} catch (IllegalArgumentException e) {
Log.e(e.getClass().getName(), e);
} catch (IllegalAccessException e) {
Log.e(e.getClass().getName(), e);
} catch (SecurityException e) {
Log.e(e.getClass().getName(), e);
}
}
if (mId != null) {
Cache.addEntity(this);
}
}
use of com.activeandroid.query.Select in project ActiveAndroid by pardom.
the class ExistsTest method testCountOrderBy.
/**
* Should not change the result if order by is used.
*/
public void testCountOrderBy() {
cleanTable();
populateTable();
From from = new Select().from(MockModel.class).where("intField = ?", 1).orderBy("intField ASC");
final List<MockModel> list = from.execute();
final boolean exists = from.exists();
assertTrue(exists);
assertTrue(list.size() > 0);
}
use of com.activeandroid.query.Select in project ActiveAndroid by pardom.
the class ExistsTest method testCountWhereClauseSql.
/**
* Should be an exists with the specified where-clause.
*/
public void testCountWhereClauseSql() {
final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField = ? )";
String actual = new Select().from(MockModel.class).where("intField = ?", 1).toExistsSql();
assertEquals(expected, actual);
}
use of com.activeandroid.query.Select in project ActiveAndroid by pardom.
the class SelectTest method testSelectAll.
public void testSelectAll() {
assertSqlEquals("SELECT ALL * ", new Select().all());
assertSqlEquals("SELECT ALL * ", new Select().distinct().all());
}
use of com.activeandroid.query.Select in project ActiveAndroid by pardom.
the class CountTest method testCountOrderBy.
/**
* Should not change the result if order by is used.
*/
public void testCountOrderBy() {
cleanTable();
populateTable();
From from = new Select().from(MockModel.class).where("intField = ?", 1).orderBy("intField ASC");
final List<MockModel> list = from.execute();
final int count = from.count();
assertEquals(2, count);
assertEquals(list.size(), count);
}
Aggregations