Search in sources :

Example 31 with ExpressionToken

use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.

the class DatabaseConnectivityTest method testLimitQuery.

public void testLimitQuery() {
    final int count = 10;
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 10; i++) {
        object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
        assertNotNull(object);
        object.setValue("intValue", i);
        object.setValue("doubleValue", ((double) i * 2));
        object.setValue("textValue", String.format("%04d", i * 3));
        objects[i] = object;
    }
    AbsDatabaseConnectivity connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, QueryObject.class);
    assertNotNull(connectivity);
    connectivity.insert(objects);
    Query query = new Query(QueryObject.class);
    assertNotNull(query);
    ExpressionToken limit = new ExpressionToken("5");
    query.setLimit(limit);
    List<DatabaseObject> results = connectivity.query(query);
    assertNotNull(results);
    assertEquals(5, results.size());
    DatabaseObject resultObject = null;
    for (int i = 0; i < 5; i++) {
        resultObject = results.get(i);
        assertNotNull(resultObject);
        Logger.debug("resultObject(%s)", resultObject.toSQLSelectionString());
        Integer iVal = resultObject.getIntegerValue("intValue");
        assertNotNull(iVal);
        assertEquals(i, iVal.intValue());
        Double dVal = resultObject.getDoubleValue("doubleValue");
        assertNotNull(dVal);
        assertEquals(((double) i * 2), dVal.doubleValue());
        String sVal = resultObject.getTextValue("textValue");
        assertNotNull(sVal);
        assertEquals(String.format("%04d", i * 3), sVal);
    }
    connectivity.delete(new Query(QueryObject.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) QueryObject(com.dailystudio.dataobject.samples.QueryObject)

Example 32 with ExpressionToken

use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.

the class DatabaseConnectivityTest method testSimpleQuery.

public void testSimpleQuery() {
    final int count = 10;
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 10; i++) {
        object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
        assertNotNull(object);
        object.setValue("intValue", i);
        object.setValue("doubleValue", ((double) i * 2));
        object.setValue("textValue", String.format("%04d", i * 3));
        objects[i] = object;
    }
    AbsDatabaseConnectivity connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, QueryObject.class);
    assertNotNull(connectivity);
    connectivity.insert(objects);
    final Template templ = object.getTemplate();
    Column col = templ.getColumn("intValue");
    Query query = new Query(QueryObject.class);
    assertNotNull(query);
    ExpressionToken selToken = col.gt(5).and(col.lt(9));
    query.setSelection(selToken);
    List<DatabaseObject> results = connectivity.query(query);
    assertNotNull(results);
    assertEquals(3, results.size());
    DatabaseObject resultObject = null;
    for (int i = 6; i <= 8; i++) {
        resultObject = results.get(i - 6);
        assertNotNull(resultObject);
        Logger.debug("resultObject(%s)", resultObject.toSQLSelectionString());
        Integer iVal = resultObject.getIntegerValue("intValue");
        assertNotNull(iVal);
        assertEquals(i, iVal.intValue());
        Double dVal = resultObject.getDoubleValue("doubleValue");
        assertNotNull(dVal);
        assertEquals(((double) i * 2), dVal.doubleValue());
        String sVal = resultObject.getTextValue("textValue");
        assertNotNull(sVal);
        assertEquals(String.format("%04d", i * 3), sVal);
    }
    connectivity.delete(new Query(QueryObject.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Template(com.dailystudio.dataobject.Template) Column(com.dailystudio.dataobject.Column) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 33 with ExpressionToken

use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.

the class AbsTimeCapsuleModel method getObject.

public T getObject(Context context, DatabaseObjectKeys keys) {
    if (context == null || keys == null) {
        return null;
    }
    TimeCapsuleDatabaseReader<T> reader = new TimeCapsuleDatabaseReader<>(context, mObjectClass);
    Query query = new Query(mObjectClass);
    ExpressionToken selToken = objectsToken(keys, TOKEN_TYPE_EXISTENCE);
    if (selToken == null) {
        return null;
    }
    query.setSelection(selToken);
    return (reader.queryLastOne(query));
}
Also used : Query(com.dailystudio.dataobject.query.Query) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken)

Example 34 with ExpressionToken

use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.

the class TimeCapsuleDatabaseWriter method update.

public int update(T object) {
    if (object == null) {
        return 0;
    }
    final DatabaseConnectivity connectivity = getConnectivity();
    if (connectivity == null) {
        return 0;
    }
    Query query = getQuery();
    ExpressionToken token = TimeCapsule.COLUMN_ID.eq(object.getId());
    query.setSelection(token);
    return connectivity.update(query, object);
}
Also used : Query(com.dailystudio.dataobject.query.Query) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseConnectivity(com.dailystudio.dataobject.database.DatabaseConnectivity)

Example 35 with ExpressionToken

use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.

the class TimeCapsuleQueryBuilder method getQuery.

public Query getQuery(long start, long end, boolean orderByAscending) {
    Logger.debug("peroid: start(%s), end(%s), orderByAscending(%s)", CalendarUtils.timeToReadableString(start), CalendarUtils.timeToReadableString(end), orderByAscending);
    Query query = getQuery();
    if (query != null) {
        if (start < end) {
            ExpressionToken selection = TimeCapsule.COLUMN_TIME.gte(start).and(TimeCapsule.COLUMN_TIME.lte(end));
            if (selection != null) {
                query.setSelection(selection);
            }
        }
        OrderingToken orderBy = (orderByAscending ? TimeCapsule.COLUMN_TIME.orderByAscending() : TimeCapsule.COLUMN_TIME.orderByDescending());
        if (orderBy != null) {
            query.setOrderBy(orderBy);
        }
    }
    return query;
}
Also used : Query(com.dailystudio.dataobject.query.Query) OrderingToken(com.dailystudio.dataobject.query.OrderingToken) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken)

Aggregations

ExpressionToken (com.dailystudio.dataobject.query.ExpressionToken)35 Query (com.dailystudio.dataobject.query.Query)28 DatabaseObject (com.dailystudio.dataobject.DatabaseObject)27 QueryObject (com.dailystudio.dataobject.samples.QueryObject)19 Column (com.dailystudio.dataobject.Column)15 Template (com.dailystudio.dataobject.Template)15 Cursor (android.database.Cursor)14 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 SampleObject2 (com.dailystudio.dataobject.samples.SampleObject2)8 OrderingToken (com.dailystudio.dataobject.query.OrderingToken)7 ContentResolver (android.content.ContentResolver)6 ContentValues (android.content.ContentValues)6 Uri (android.net.Uri)6 SampleObject1 (com.dailystudio.dataobject.samples.SampleObject1)6 ProjectionObject (com.dailystudio.dataobject.samples.ProjectionObject)3 DatabaseConnectivity (com.dailystudio.dataobject.database.DatabaseConnectivity)2