Search in sources :

Example 36 with DatabaseObject

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

the class DatabaseConnectivityProviderTest method testQueryWithGroupBy.

public void testQueryWithGroupBy() {
    ContentResolver cr = mTargetContext.getContentResolver();
    assertNotNull(cr);
    final int count = 3 * 10;
    ContentValues[] values = new ContentValues[count];
    assertNotNull(values);
    DatabaseObject object = null;
    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));
            values[i * 10 + j] = 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.gte(0).and(col.lt(2));
    assertNotNull(selection);
    OrderingToken groupByToken = col.groupBy();
    assertNotNull(groupByToken);
    Cursor c = null;
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    c = cr.query(queryUri, object.toSQLProjection(), selection.toString(), null, SortOrderEncoder.encode(groupByToken.toString(), null, null, null));
    assertNotNull(c);
    assertEquals(2, c.getCount());
    assertEquals(true, c.moveToFirst());
    int columnIndex = -1;
    for (int i = 0; i < 2; 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) 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) OrderingToken(com.dailystudio.dataobject.query.OrderingToken) ExpressionToken(com.dailystudio.dataobject.query.ExpressionToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 37 with DatabaseObject

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

the class DatabaseConnectivityProviderTest method testInsertDatabaseObjects.

public void testInsertDatabaseObjects() {
    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 < 10; i++) {
        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);
        values[i] = object.getValues();
    }
    Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
    assertNotNull(queryUri);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
    assertNotNull(queryUri);
    cr.bulkInsert(queryUri, values);
    DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
    assertNotNull(handler);
    SQLiteDatabase sqlDB = null;
    Cursor c = null;
    sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    c = sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
    assertNotNull(c);
    assertEquals(10, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int i = 0; i < count; i++) {
        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(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 38 with DatabaseObject

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

Example 39 with DatabaseObject

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

the class DatabaseConnectivityProviderTest method testQueryWithOrderBy.

public void testQueryWithOrderBy() {
    ContentResolver cr = mTargetContext.getContentResolver();
    assertNotNull(cr);
    final int count = 3 * 10;
    ContentValues[] values = new ContentValues[count];
    assertNotNull(values);
    DatabaseObject object = null;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 10; j++) {
            object = DatabaseObjectFactory.createDatabaseObject(QueryObject.class);
            assertNotNull(object);
            object.setValue("intValue", j);
            object.setValue("doubleValue", ((double) j * 2));
            object.setValue("textValue", String.format("%04d", j * 3));
            values[i * 10 + j] = 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");
    OrderingToken orderByToken = null;
    Cursor c = null;
    int columnIndex = -1;
    orderByToken = col.orderByAscending();
    assertNotNull(orderByToken);
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    c = cr.query(queryUri, object.toSQLProjection(), null, null, SortOrderEncoder.encode(null, null, orderByToken.toString(), null));
    assertNotNull(c);
    assertEquals(30, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 3; i++) {
            columnIndex = c.getColumnIndex("intValue");
            assertEquals(j, c.getInt(columnIndex));
            columnIndex = c.getColumnIndex("doubleValue");
            assertEquals(((double) j * 2), c.getDouble(columnIndex));
            columnIndex = c.getColumnIndex("textValue");
            assertEquals(String.format("%04d", j * 3), c.getString(columnIndex));
            c.moveToNext();
        }
    }
    c.close();
    orderByToken = col.orderByDescending();
    assertNotNull(orderByToken);
    serial = System.currentTimeMillis();
    queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
    assertNotNull(queryUri);
    c = cr.query(queryUri, object.toSQLProjection(), null, null, SortOrderEncoder.encode(null, null, orderByToken.toString(), null));
    assertNotNull(c);
    assertEquals(30, c.getCount());
    assertEquals(true, c.moveToFirst());
    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 3; i++) {
            columnIndex = c.getColumnIndex("intValue");
            assertEquals((9 - j), c.getInt(columnIndex));
            columnIndex = c.getColumnIndex("doubleValue");
            assertEquals(((double) (9 - j) * 2), c.getDouble(columnIndex));
            columnIndex = c.getColumnIndex("textValue");
            assertEquals(String.format("%04d", (9 - j) * 3), c.getString(columnIndex));
            c.moveToNext();
        }
    }
    c.close();
    cr.delete(queryUri, null, null);
}
Also used : ContentValues(android.content.ContentValues) Column(com.dailystudio.dataobject.Column) OrderingToken(com.dailystudio.dataobject.query.OrderingToken) 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)

Example 40 with DatabaseObject

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

the class DatabaseConnectivityProviderTest method testDeleteAllDatabaseObjects.

public void testDeleteAllDatabaseObjects() {
    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(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);
        values[i] = object.getValues();
    }
    Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
    assertNotNull(queryUri);
    queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
    assertNotNull(queryUri);
    cr.bulkInsert(queryUri, values);
    cr.delete(queryUri, null, null);
    DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject2.class), 0x1);
    assertNotNull(handler);
    SQLiteDatabase sqlDB = null;
    sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    boolean catched = false;
    try {
        sqlDB.query(DatabaseObject.classToTable(SampleObject2.class), null, null, null, null, null, null);
        catched = false;
    } catch (SQLException e) {
        e.printStackTrace();
        catched = true;
    }
    sqlDB.close();
    assertTrue(catched);
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) SampleObject2(com.dailystudio.dataobject.samples.SampleObject2) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) 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