Search in sources :

Example 21 with ExpressionToken

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

the class DatabaseConnectivityDirectSQLImplTest 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 DatabaseConnectivityDirectSQLiteImpl(mTargetContext, 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 22 with ExpressionToken

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

the class DatabaseConnectivityDirectSQLImplTest 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 DatabaseConnectivityDirectSQLiteImpl(mTargetContext, 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 23 with ExpressionToken

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

the class DatabaseConnectivityDirectSQLImplTest 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 DatabaseConnectivityDirectSQLiteImpl(mTargetContext, 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)

Example 24 with ExpressionToken

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

the class DatabaseConnectivityDirectSQLImplTest method testUpdateDatabaseObjects.

public void testUpdateDatabaseObjects() {
    AbsDatabaseConnectivity connectivity1 = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, SampleObject1.class);
    assertNotNull(connectivity1);
    AbsDatabaseConnectivity connectivity2 = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, SampleObject2.class);
    assertNotNull(connectivity2);
    final int count = 10;
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            object = DatabaseObjectFactory.createDatabaseObject(SampleObject1.class);
            assertNotNull(object);
            object.setValue(SampleObject1.COLUMN_TIME, (1000l * i));
        } else {
            object = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
            assertNotNull(object);
            object.setValue(SampleObject2.COLUMN_LAT, 0.1 * i);
            object.setValue(SampleObject2.COLUMN_LON, 0.2 * i);
            object.setValue(SampleObject2.COLUMN_ALT, 0.3 * i);
            object.setValue(SampleObject2.COLUMN_BIN, fillBytesArray(i, (byte) i));
        }
        objects[i] = object;
    }
    connectivity1.insert(objects);
    connectivity2.insert(objects);
    DatabaseObject updateObject = null;
    Query query1 = new Query(SampleObject1.class);
    assertNotNull(query1);
    ExpressionToken selection1 = SampleObject1.COLUMN_TIME.gt(5000l);
    assertNotNull(selection1);
    query1.setSelection(selection1);
    updateObject = DatabaseObjectFactory.createDatabaseObject(SampleObject1.class);
    updateObject.setValue(SampleObject1.COLUMN_TIME, 0l);
    connectivity1.update(query1, updateObject);
    Query query2 = new Query(SampleObject2.class);
    assertNotNull(query2);
    ExpressionToken selection2 = SampleObject2.COLUMN_LAT.gt(0.2).and(SampleObject2.COLUMN_LON.lt(0.8));
    assertNotNull(selection2);
    query2.setSelection(selection2);
    updateObject = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
    updateObject.setValue(SampleObject2.COLUMN_ALT, 12.34);
    updateObject.setValue(SampleObject2.COLUMN_BIN, fillBytesArray(100, (byte) 0xFF));
    connectivity2.update(query2, updateObject);
    DatabaseOpenHandler handler1 = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject1.class), 0x1);
    assertNotNull(handler1);
    DatabaseOpenHandler handler2 = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
    assertNotNull(handler2);
    SQLiteDatabase sqlDB = null;
    Cursor c = null;
    sqlDB = handler1.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(SampleObject1.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(5, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            if (i * 1000l > 5000l) {
                assertEquals(0, c.getLong(c.getColumnIndex("time")));
            } else {
                assertEquals(i * 1000l, c.getLong(c.getColumnIndex("time")));
            }
            c.moveToNext();
        }
    }
    c.close();
    sqlDB.close();
    sqlDB = handler2.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(5, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 5; i++) {
        if (i % 2 != 0) {
            if ((0.1 * i > 0.2) && (0.2 * i < 0.8)) {
                assertEquals(0.1 * i, c.getDouble(c.getColumnIndex("latitude")));
                assertEquals(0.2 * i, c.getDouble(c.getColumnIndex("longitude")));
                assertEquals(12.34, c.getDouble(c.getColumnIndex("altitude")));
                object.setValue(SampleObject2.COLUMN_BIN, fillBytesArray(i, (byte) i));
            } else {
                assertEquals(0.1 * i, c.getDouble(c.getColumnIndex("latitude")));
                assertEquals(0.2 * i, c.getDouble(c.getColumnIndex("longitude")));
                assertEquals(0.3 * i, c.getDouble(c.getColumnIndex("altitude")));
                object.setValue(SampleObject2.COLUMN_BIN, fillBytesArray(100, (byte) 0xFF));
            }
            c.moveToNext();
        }
    }
    c.close();
    sqlDB.close();
    connectivity1.delete(new Query(SampleObject1.class));
    connectivity2.delete(new Query(SampleObject2.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SampleObject1(com.dailystudio.dataobject.samples.SampleObject1) SampleObject2(com.dailystudio.dataobject.samples.SampleObject2) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor)

Example 25 with ExpressionToken

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

the class DatabaseConnectivityProviderTest method testSimpleQuery.

public void testSimpleQuery() {
    ContentResolver cr = mTargetContext.getContentResolver();
    assertNotNull(cr);
    final int count = 10;
    ContentValues[] values = new ContentValues[count];
    assertNotNull(values);
    DatabaseObject object = null;
    for (int i = 0; i < count; 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));
        values[i] = object.getValues();
    }
    final Template tmpl = object.getTemplate();
    long serial = 0;
    Uri queryUri = null;
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
    assertNotNull(queryUri);
    cr.bulkInsert(queryUri, values);
    Column col = tmpl.getColumn("intValue");
    ExpressionToken selection = col.gt(5).and(col.lt(9));
    assertNotNull(selection);
    Cursor c = null;
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    c = cr.query(queryUri, object.toSQLProjection(), selection.toString(), null, null);
    assertNotNull(c);
    assertEquals(3, c.getCount());
    assertEquals(true, c.moveToFirst());
    int columnIndex = -1;
    for (int i = 6; i <= 8; i++) {
        columnIndex = c.getColumnIndex("intValue");
        assertEquals(i, c.getInt(columnIndex));
        columnIndex = c.getColumnIndex("doubleValue");
        assertEquals(((double) i * 2), c.getDouble(columnIndex));
        columnIndex = c.getColumnIndex("textValue");
        assertEquals(String.format("%04d", i * 3), c.getString(columnIndex));
        c.moveToNext();
    }
    c.close();
    cr.delete(queryUri, null, null);
}
Also used : ContentValues(android.content.ContentValues) Column(com.dailystudio.dataobject.Column) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Template(com.dailystudio.dataobject.Template)

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