Search in sources :

Example 31 with DatabaseObject

use of com.dailystudio.dataobject.DatabaseObject 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 32 with DatabaseObject

use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.

the class DatabaseConnectivityDirectSQLImplTest method testQueryWithGroupBy.

public void testQueryWithGroupBy() {
    final int count = (3 * 10);
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 10; j++) {
            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 * 10 + j] = 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.gte(0).and(col.lt(2));
    query.setSelection(selToken);
    OrderingToken groupByToken = col.groupBy();
    query.setGroupBy(groupByToken);
    List<DatabaseObject> results = connectivity.query(query);
    assertNotNull(results);
    assertEquals(2, results.size());
    DatabaseObject resultObject = null;
    for (int i = 0; i < 2; 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) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Template(com.dailystudio.dataobject.Template) Column(com.dailystudio.dataobject.Column) OrderingToken(com.dailystudio.dataobject.query.OrderingToken) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 33 with DatabaseObject

use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.

the class DatabaseConnectivityProviderTest method testQueryWithProjectionClass.

public void testQueryWithProjectionClass() {
    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);
    DatabaseObject projectionObject = new ProjectionObject(mContext);
    assertNotNull(projectionObject);
    Cursor c = null;
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    c = cr.query(queryUri, projectionObject.toSQLProjection(), selection.toString(), null, null);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertEquals(true, c.moveToFirst());
    int columnIndex = -1;
    columnIndex = c.getColumnIndex(ProjectionObject.COLUMN_ID_COUNT.getName());
    assertEquals(3, c.getInt(columnIndex));
    c.close();
    cr.delete(queryUri, null, null);
}
Also used : ContentValues(android.content.ContentValues) ProjectionObject(com.dailystudio.dataobject.samples.ProjectionObject) Cursor(android.database.Cursor) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Template(com.dailystudio.dataobject.Template) Column(com.dailystudio.dataobject.Column) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 34 with DatabaseObject

use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.

the class DatabaseConnectivityProviderTest method testInsertDatabaseObject.

public void testInsertDatabaseObject() {
    ContentResolver cr = mTargetContext.getContentResolver();
    assertNotNull(cr);
    DatabaseObject object = new SampleObject2(mTargetContext);
    object.setValue("latitude", 0.123);
    object.setValue("longitude", 0.456);
    object.setValue("altitude", 0.789);
    final ContentValues values = object.getValues();
    assertNotNull(values);
    Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
    assertNotNull(queryUri);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
    assertNotNull(queryUri);
    cr.insert(queryUri, values);
    DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
    assertNotNull(handler);
    SQLiteDatabase sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    Cursor c = sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertEquals(true, c.moveToFirst());
    assertEquals(0.123, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LAT.getName())));
    assertEquals(0.456, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LON.getName())));
    assertEquals(0.789, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_ALT.getName())));
    c.close();
    sqlDB.delete(DatabaseObject.classToTable(SampleObject2.class), null, null);
    sqlDB.close();
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SampleObject2(com.dailystudio.dataobject.samples.SampleObject2) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 35 with DatabaseObject

use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.

the class DatabaseConnectivityProviderTest method testQueryCommand.

public void testQueryCommand() {
    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(VersionControlledObject.class, DatabaseObject.VERSION_START);
        assertNotNull(object);
        object.setValue(VersionControlledObject.COLUMN_INT_VER1, i);
        object.setValue(VersionControlledObject.COLUMN_LONG_VER1, ((long) i * 10000000000l));
        values[i] = object.getValues();
    }
    Uri queryUri = null;
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, VersionControlledObject.class);
    assertNotNull(queryUri);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
    assertNotNull(queryUri);
    cr.bulkInsert(queryUri, values);
    Uri commandUri = ProviderUriBuilder.buildCommandUri(AUTHORITY, VersionControlledObject.class, DatabaseObject.VERSION_START + 1, GetUpdateInfoCmdCursor.COMMAND_NAME);
    assertNotNull(commandUri);
    Cursor c = null;
    c = cr.query(commandUri, null, null, null, null);
    assertNotNull(c);
    assertEquals(1, c.getCount());
    assertEquals(true, c.moveToFirst());
    final int newVer = c.getInt(c.getColumnIndex(GetUpdateInfoCmdCursor.COLUMN_NEW_VERSION));
    final int oldVer = c.getInt(c.getColumnIndex(GetUpdateInfoCmdCursor.COLUMN_OLD_VERSION));
    c.close();
    assertEquals(2, newVer);
    assertEquals(1, oldVer);
// cr.delete(queryUri, null, null);
}
Also used : ContentValues(android.content.ContentValues) VersionControlledObject(com.dailystudio.dataobject.samples.VersionControlledObject) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Aggregations

DatabaseObject (com.dailystudio.dataobject.DatabaseObject)58 Query (com.dailystudio.dataobject.query.Query)38 Cursor (android.database.Cursor)28 ExpressionToken (com.dailystudio.dataobject.query.ExpressionToken)27 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)24 Template (com.dailystudio.dataobject.Template)23 QueryObject (com.dailystudio.dataobject.samples.QueryObject)23 Column (com.dailystudio.dataobject.Column)19 SampleObject2 (com.dailystudio.dataobject.samples.SampleObject2)17 Uri (android.net.Uri)15 SampleObject1 (com.dailystudio.dataobject.samples.SampleObject1)14 ContentValues (android.content.ContentValues)13 ContentResolver (android.content.ContentResolver)12 OrderingToken (com.dailystudio.dataobject.query.OrderingToken)8 SQLException (android.database.SQLException)6 DatabaseConnectivity (com.dailystudio.dataobject.database.DatabaseConnectivity)3 ProjectionObject (com.dailystudio.dataobject.samples.ProjectionObject)3 ArrayList (java.util.ArrayList)3 DynamicColumnsObject (com.dailystudio.dataobject.samples.DynamicColumnsObject)2 List (java.util.List)2