Search in sources :

Example 46 with DatabaseObject

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

the class DatabaseConnectivityTest method testDeleteDatabaseObjects.

public void testDeleteDatabaseObjects() {
    AbsDatabaseConnectivity connectivity1 = new DatabaseConnectivity(mTargetContext, AUTHORITY, SampleObject1.class);
    assertNotNull(connectivity1);
    AbsDatabaseConnectivity connectivity2 = new DatabaseConnectivity(mTargetContext, AUTHORITY, 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);
        }
        objects[i] = object;
    }
    connectivity1.insert(objects);
    connectivity2.insert(objects);
    Query query1 = new Query(SampleObject1.class);
    assertNotNull(query1);
    ExpressionToken selection1 = SampleObject1.COLUMN_TIME.gt(5000l);
    assertNotNull(selection1);
    query1.setSelection(selection1);
    connectivity1.delete(query1);
    Query qParams2 = new Query(SampleObject2.class);
    assertNotNull(qParams2);
    ExpressionToken selection2 = SampleObject2.COLUMN_LAT.gt(0.2).and(SampleObject2.COLUMN_LON.lt(0.8));
    assertNotNull(selection2);
    qParams2.setSelection(selection2);
    connectivity2.delete(qParams2);
    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(3, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            if (i * 1000l > 5000l) {
                continue;
            } else {
                assertEquals(i * 1000l, c.getLong(c.getColumnIndex("time")));
            }
            c.moveToNext();
        }
    }
    c.close();
    // sqlDB.delete(AbsDatabaseConnectivity.classToTable(SampleObject1.class),
    // null, null);
    sqlDB.close();
    sqlDB = handler2.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(4, 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)) {
                continue;
            } 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")));
            }
            c.moveToNext();
        }
    }
    c.close();
    // sqlDB.delete(AbsDatabaseConnectivity.classToTable(SampleObject2.class),
    // null, null);
    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 47 with DatabaseObject

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

the class DatabaseConnectivityTest 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 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.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 48 with DatabaseObject

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

the class DatabaseUpdaterTest method testDoUpdate.

public void testDoUpdate() {
    final int count = 10;
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 10; i++) {
        object = DatabaseObjectFactory.createDatabaseObject(DynamicColumnsObject.class, DatabaseObject.VERSION_START);
        assertNotNull(object);
        if (i % 2 != 0) {
            object.setValue(DynamicColumnsObject.COLUMN_INT_VER1, i * 10);
        }
        object.setValue(DynamicColumnsObject.COLUMN_LONG_VER1, i * 10000000000l);
        Logger.debug("object = %s", object);
        objects[i] = object;
    }
    AbsDatabaseConnectivity connectivity = null;
    connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, DynamicColumnsObject.class, DatabaseObject.VERSION_START);
    assertNotNull(connectivity);
    connectivity.insert(objects);
    SQLiteDatabase sqlDB = null;
    Cursor c = null;
    DatabaseOpenHandler handler = null;
    boolean catched = false;
    handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START);
    assertNotNull(handler);
    sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(10, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 10; i++) {
        if (i % 2 != 0) {
            assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1.getName())));
        }
        assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
        c.moveToNext();
    }
    c.close();
    sqlDB.close();
    DatabaseUpdateInfo info = null;
    AbsDatabaseUpdater updater = null;
    connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, DynamicColumnsObject.class, DatabaseObject.VERSION_START + 1);
    info = connectivity.checkUpdates();
    assertNotNull(info);
    assertTrue(info.needUpdate());
    assertEquals(DatabaseObject.VERSION_START + 1, info.getNewVersion());
    assertEquals(DatabaseObject.VERSION_START, info.getOldVersion());
    updater = new DynamicColumnsUpdater(mTargetContext, DynamicColumnsObject.class);
    assertNotNull(updater);
    updater.doUpdate(info.getNewVersion(), info.getOldVersion());
    handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START + 1);
    assertNotNull(handler);
    sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(10, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 10; i++) {
        if (i % 2 != 0) {
            assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
        } else {
            assertEquals(999, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
        }
        catched = false;
        try {
            assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
            catched = false;
        } catch (IllegalStateException e) {
            catched = true;
        }
        assertTrue(catched);
        assertEquals(1234, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER2.getName())));
        assertEquals(0.414, c.getDouble(c.getColumnIndex(DynamicColumnsObject.COLUMN_DOUBLE_VER2.getName())));
        assertEquals("Text Version 2", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER2.getName())));
        c.moveToNext();
    }
    c.close();
    sqlDB.close();
    connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, DynamicColumnsObject.class);
    info = connectivity.checkUpdates();
    assertNotNull(info);
    assertTrue(info.needUpdate());
    assertEquals(DatabaseObject.VERSION_START + 2, info.getNewVersion());
    assertEquals(DatabaseObject.VERSION_START + 1, info.getOldVersion());
    updater = new DynamicColumnsUpdater(mTargetContext, DynamicColumnsObject.class);
    assertNotNull(updater);
    updater.doUpdate(info.getNewVersion(), info.getOldVersion());
    handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START + 2);
    assertNotNull(handler);
    sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(10, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 10; i++) {
        if (i % 2 != 0) {
            assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
        } else {
            assertEquals(999, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
        }
        catched = false;
        try {
            assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
            catched = false;
        } catch (IllegalStateException e) {
            catched = true;
        }
        assertTrue(catched);
        assertEquals(1234, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER2.getName())));
        assertEquals(0.414, c.getDouble(c.getColumnIndex(DynamicColumnsObject.COLUMN_DOUBLE_VER2.getName())));
        assertEquals("Text Version 2", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER2.getName())));
        assertEquals("Only In Version 3", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER3.getName())));
        c.moveToNext();
    }
    c.close();
    sqlDB.close();
    connectivity.delete(new Query(DynamicColumnsObject.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) Cursor(android.database.Cursor) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) DynamicColumnsObject(com.dailystudio.dataobject.samples.DynamicColumnsObject)

Example 49 with DatabaseObject

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

the class ProviderQueryUriParserTest method testGetCreateTabelSQL.

public void testGetCreateTabelSQL() {
    final long now = System.currentTimeMillis();
    Uri queryUri = null;
    ProviderQueryUriParser parser = null;
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject1.class, now);
    assertNotNull(queryUri);
    parser = new ProviderQueryUriParser(queryUri);
    assertNotNull(parser);
    assertNull(parser.getCreateTableSQL());
    DatabaseObject sample = new SampleObject1(mContext);
    assertNotNull(sample);
    final Template template = sample.getTemplate();
    assertNotNull(template);
    final String table = DatabaseObject.classToTable(SampleObject1.class);
    assertNotNull(table);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, sample.toSQLTableCreationString());
    parser = new ProviderQueryUriParser(queryUri);
    assertNotNull(parser);
    String expected = sample.toSQLTableCreationString();
    assertEquals(expected, parser.getCreateTableSQL());
}
Also used : SampleObject1(com.dailystudio.dataobject.samples.SampleObject1) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Uri(android.net.Uri) Template(com.dailystudio.dataobject.Template)

Example 50 with DatabaseObject

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

the class AsyncDatabaseHelperTest 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;
        }
    }
    AsyncDatabaseHelper helper = new TestAsyncDatabaseHelp(mTargetContext, AUTHORITY, QueryObject.class, true);
    assertNotNull(helper);
    helper.startBulkInsert(mRandom.nextInt(), "BulkInsert1", objects);
    waitHelper(helper);
    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);
    helper.startQuery(mRandom.nextInt(), "testQueryWithGroupBy", query);
    waitHelper(helper);
    AbsDatabaseConnectivity connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, QueryObject.class);
    assertNotNull(connectivity);
    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)

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