Search in sources :

Example 51 with Select

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) TypeSerializer(com.activeandroid.serializer.TypeSerializer) Select(com.activeandroid.query.Select)

Example 52 with Select

use of com.activeandroid.query.Select in project ActiveAndroid by pardom.

the class CountTest method testCountWhereClause.

/**
 * Should return the same count as there are entries in the result set if the where-clause
 * matches several entries.
 */
public void testCountWhereClause() {
    cleanTable();
    populateTable();
    From from = new Select().from(MockModel.class).where("intField = ?", 1);
    final List<MockModel> list = from.execute();
    final int count = from.count();
    assertEquals(2, count);
    assertEquals(list.size(), count);
}
Also used : MockModel(com.activeandroid.test.MockModel) Select(com.activeandroid.query.Select) From(com.activeandroid.query.From)

Example 53 with Select

use of com.activeandroid.query.Select in project ActiveAndroid by pardom.

the class CountTest method testCountOrderBySql.

/**
 * Shouldn't include <i>order by</i> as it has no influence on the result of <i>count</i> and
 * should improve performance.
 */
public void testCountOrderBySql() {
    final String expected = "SELECT COUNT(*) FROM MockModel WHERE intField <> ? GROUP BY intField";
    String actual = new Select().from(MockModel.class).where("intField <> ?", 0).orderBy("intField").groupBy("intField").toCountSql();
    assertEquals(expected, actual);
}
Also used : Select(com.activeandroid.query.Select)

Example 54 with Select

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);
}
Also used : MockModel(com.activeandroid.test.MockModel) Select(com.activeandroid.query.Select) From(com.activeandroid.query.From)

Example 55 with Select

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);
}
Also used : Select(com.activeandroid.query.Select)

Aggregations

Select (com.activeandroid.query.Select)80 SharedPreferences (android.content.SharedPreferences)14 From (com.activeandroid.query.From)11 MockModel (com.activeandroid.test.MockModel)11 DecimalFormat (java.text.DecimalFormat)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 Cursor (android.database.Cursor)6 ActiveBluetoothDevice (com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice)6 JSONException (org.json.JSONException)5 GsonBuilder (com.google.gson.GsonBuilder)4 PendingIntent (android.app.PendingIntent)2 BluetoothDevice (android.bluetooth.BluetoothDevice)2 Intent (android.content.Intent)2 Update (com.activeandroid.query.Update)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 MissedReadingService (com.eveningoutpost.dexdrip.Services.MissedReadingService)2 Gson (com.google.gson.Gson)2 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2