Search in sources :

Example 11 with ExpressionToken

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

the class AbsTimeCapsuleModel method deleteObjects.

public int deleteObjects(Context context, DatabaseObjectKeys keys) {
    if (context == null) {
        return 0;
    }
    Query query = new Query(mObjectClass);
    if (keys != null) {
        ExpressionToken token = objectsToken(keys, TOKEN_TYPE_DELETION);
        if (token == null) {
            return 0;
        }
        query.setSelection(token);
    }
    TimeCapsuleDatabaseWriter<T> writer = new TimeCapsuleDatabaseWriter<>(context, mObjectClass);
    return writer.delete(query);
}
Also used : Query(com.dailystudio.dataobject.query.Query) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken)

Example 12 with ExpressionToken

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

the class TimeCapsuleDatabaseWriter method delete.

public int delete(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.delete(query);
}
Also used : Query(com.dailystudio.dataobject.query.Query) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseConnectivity(com.dailystudio.dataobject.database.DatabaseConnectivity)

Example 13 with ExpressionToken

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

the class TimeCapsuleQueryBuilder method getQueryForIntersect.

public Query getQueryForIntersect(ExpressionToken startExp, ExpressionToken durationExp, long start, long end, boolean orderByAscending) {
    if (startExp == null || durationExp == null) {
        return null;
    }
    String startExpStr = startExp.toString();
    Logger.debug("expression: startExp(%s), durationExp(%s)", startExp, durationExp, 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 sExp = new ExpressionToken(start);
            ExpressionToken eExp = new ExpressionToken(end);
            ExpressionToken sel1 = new ExpressionToken(startExpStr).lte(sExp).and(new ExpressionToken(startExpStr).plus(durationExp).gt(sExp));
            ExpressionToken sel2 = new ExpressionToken(startExpStr).gt(sExp).and(new ExpressionToken(startExpStr).plus(durationExp).lte(eExp));
            ExpressionToken selection = sel1.or(sel2);
            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)

Example 14 with ExpressionToken

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

the class DatabaseConnectivityTest method testQueryWithProjectionClass.

public void testQueryWithProjectionClass() {
    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, ProjectionObject.class);
    assertNotNull(results);
    assertEquals(1, results.size());
    DatabaseObject resultObject = results.get(0);
    Integer iCount = resultObject.getIntegerValue("count( _id )");
    assertNotNull(iCount);
    assertEquals(3, iCount.intValue());
    connectivity.delete(new Query(QueryObject.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) Column(com.dailystudio.dataobject.Column) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Template(com.dailystudio.dataobject.Template)

Example 15 with ExpressionToken

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

the class DatabaseConnectivityTest method testQueryCursor.

public void testQueryCursor() {
    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 = null;
    ExpressionToken selToken = null;
    DatabaseObject resultObject = null;
    Cursor c = null;
    query = new Query(QueryObject.class);
    assertNotNull(query);
    selToken = col.gt(5).and(col.lt(9));
    query.setSelection(selToken);
    c = connectivity.queryCursor(query);
    assertNotNull(c);
    assertEquals(3, c.getCount());
    assertTrue(c.moveToFirst());
    for (int i = 6; i <= 8; i++, c.moveToNext()) {
        resultObject = connectivity.fromCursor(c, QueryObject.class);
        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);
    }
    c.close();
    selToken = col.gt(5).and(col.lt(9));
    query.setSelection(selToken);
    c = connectivity.queryCursor(query, ProjectionObject.class);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertTrue(c.moveToFirst());
    resultObject = connectivity.fromCursor(c, ProjectionObject.class);
    Integer iCount = resultObject.getIntegerValue("count( _id )");
    assertNotNull(iCount);
    assertEquals(3, iCount.intValue());
    c.close();
    connectivity.delete(new Query(QueryObject.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) ProjectionObject(com.dailystudio.dataobject.samples.ProjectionObject) Cursor(android.database.Cursor) 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)

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