Search in sources :

Example 11 with SampleObject1

use of com.dailystudio.dataobject.samples.SampleObject1 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 12 with SampleObject1

use of com.dailystudio.dataobject.samples.SampleObject1 in project devbricks by dailystudio.

the class DatabaseConnectivityTest method testInsertDatabaseObjects.

public void testInsertDatabaseObjects() {
    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);
    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) {
            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(5, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < 5; i++) {
        if (i % 2 != 0) {
            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) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor)

Example 13 with SampleObject1

use of com.dailystudio.dataobject.samples.SampleObject1 in project devbricks by dailystudio.

the class ProviderCommandUriParserTest method testGetVersion.

public void testGetVersion() {
    Uri queryUri = null;
    ProviderCommandUriParser parser = null;
    queryUri = ProviderUriBuilder.buildCommandUri(AUTHORITY, SampleObject1.class, GetUpdateInfoCmdCursor.COMMAND_NAME);
    assertNotNull(queryUri);
    parser = new ProviderCommandUriParser(queryUri);
    assertNotNull(parser);
    assertEquals(DatabaseObject.VERSION_START, parser.getVersion());
    queryUri = ProviderUriBuilder.buildCommandUri(AUTHORITY, SampleObject1.class, 10, GetUpdateInfoCmdCursor.COMMAND_NAME);
    assertNotNull(queryUri);
    parser = new ProviderCommandUriParser(queryUri);
    assertNotNull(parser);
    assertEquals(10, parser.getVersion());
}
Also used : SampleObject1(com.dailystudio.dataobject.samples.SampleObject1) Uri(android.net.Uri)

Example 14 with SampleObject1

use of com.dailystudio.dataobject.samples.SampleObject1 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 15 with SampleObject1

use of com.dailystudio.dataobject.samples.SampleObject1 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)

Aggregations

SampleObject1 (com.dailystudio.dataobject.samples.SampleObject1)24 DatabaseObject (com.dailystudio.dataobject.DatabaseObject)14 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)12 Uri (android.net.Uri)12 Query (com.dailystudio.dataobject.query.Query)12 Cursor (android.database.Cursor)9 SampleObject2 (com.dailystudio.dataobject.samples.SampleObject2)9 ExpressionToken (com.dailystudio.dataobject.query.ExpressionToken)6 SQLException (android.database.SQLException)3 Template (com.dailystudio.dataobject.Template)3