Search in sources :

Example 6 with DatabaseObject

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

the class AsyncDatabaseHelperTest method testQueryWithOrderBy.

public void testQueryWithOrderBy() {
    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", j);
            object.setValue("doubleValue", ((double) j * 2));
            object.setValue("textValue", String.format("%04d", j * 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 = null;
    OrderingToken orderByToken = null;
    query = new Query(QueryObject.class);
    assertNotNull(query);
    orderByToken = col.orderByAscending();
    query.setOrderBy(orderByToken);
    helper.startQuery(mRandom.nextInt(), "testQueryWithOrderBy1", query);
    waitHelper(helper);
    query = new Query(QueryObject.class);
    assertNotNull(query);
    orderByToken = col.orderByDescending();
    query.setOrderBy(orderByToken);
    helper.startQuery(mRandom.nextInt(), "testQueryWithOrderBy2", 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) Column(com.dailystudio.dataobject.Column) OrderingToken(com.dailystudio.dataobject.query.OrderingToken) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) QueryObject(com.dailystudio.dataobject.samples.QueryObject) Template(com.dailystudio.dataobject.Template)

Example 7 with DatabaseObject

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

the class DatabaseConnectivityDirectSQLImplTest method testDeleteAllDatabaseObjects.

public void testDeleteAllDatabaseObjects() {
    AbsDatabaseConnectivity connectivity = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, SampleObject1.class);
    assertNotNull(connectivity);
    final int count = 10;
    DatabaseObject object = null;
    DatabaseObject[] objects = new DatabaseObject[count];
    assertNotNull(objects);
    for (int i = 0; i < 10; i++) {
        object = DatabaseObjectFactory.createDatabaseObject(SampleObject1.class);
        assertNotNull(object);
        object.setValue(SampleObject1.COLUMN_TIME, (1000l * i));
        objects[i] = object;
    }
    connectivity.insert(objects);
    Query query = new Query(SampleObject1.class);
    assertNotNull(query);
    connectivity.delete(query);
    DatabaseOpenHandler handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(SampleObject1.class), 0x1);
    assertNotNull(handler);
    SQLiteDatabase sqlDB = handler.getReadableDatabase();
    assertNotNull(sqlDB);
    boolean catched = false;
    try {
        sqlDB.query(DatabaseObject.classToTable(SampleObject1.class), null, null, null, null, null, null);
        catched = false;
    } catch (SQLException e) {
        e.printStackTrace();
        catched = true;
    }
    sqlDB.close();
    assertTrue(catched);
    connectivity.delete(new Query(SampleObject1.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) SampleObject1(com.dailystudio.dataobject.samples.SampleObject1) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 8 with DatabaseObject

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

the class DatabaseConnectivityDirectSQLImplTest method testQueryWithOrderBy.

public void testQueryWithOrderBy() {
    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", j);
            object.setValue("doubleValue", ((double) j * 2));
            object.setValue("textValue", String.format("%04d", j * 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 = null;
    List<DatabaseObject> results = null;
    OrderingToken orderByToken = null;
    DatabaseObject resultObject = null;
    query = new Query(QueryObject.class);
    assertNotNull(query);
    orderByToken = col.orderByAscending();
    query.setOrderBy(orderByToken);
    results = connectivity.query(query);
    assertNotNull(results);
    assertEquals(30, results.size());
    for (int j = 0; j < 10; j++) {
        for (int i = 0; i < 3; i++) {
            resultObject = results.get(j * 3 + i);
            assertNotNull(resultObject);
            Logger.debug("resultObject(%s)", resultObject.toSQLSelectionString());
            Integer iVal = resultObject.getIntegerValue("intValue");
            assertNotNull(iVal);
            assertEquals(j, iVal.intValue());
            Double dVal = resultObject.getDoubleValue("doubleValue");
            assertNotNull(dVal);
            assertEquals(((double) j * 2), dVal.doubleValue());
            String sVal = resultObject.getTextValue("textValue");
            assertNotNull(sVal);
            assertEquals(String.format("%04d", j * 3), sVal);
        }
    }
    query = new Query(QueryObject.class);
    assertNotNull(query);
    orderByToken = col.orderByDescending();
    query.setOrderBy(orderByToken);
    results = connectivity.query(query);
    assertNotNull(results);
    assertEquals(30, results.size());
    for (int j = 9; j >= 0; j--) {
        for (int i = 0; i < 3; i++) {
            resultObject = results.get((9 - j) * 3 + i);
            assertNotNull(resultObject);
            Logger.debug("resultObject(%s)", resultObject.toSQLSelectionString());
            Integer iVal = resultObject.getIntegerValue("intValue");
            assertNotNull(iVal);
            assertEquals(j, iVal.intValue());
            Double dVal = resultObject.getDoubleValue("doubleValue");
            assertNotNull(dVal);
            assertEquals(((double) j * 2), dVal.doubleValue());
            String sVal = resultObject.getTextValue("textValue");
            assertNotNull(sVal);
            assertEquals(String.format("%04d", j * 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) DatabaseObject(com.dailystudio.dataobject.DatabaseObject)

Example 9 with DatabaseObject

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

the class DatabaseConnectivityDirectSQLImplTest method testInsertDatabaseObject.

public void testInsertDatabaseObject() {
    AbsDatabaseConnectivity connectivity = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, SampleObject2.class);
    assertNotNull(connectivity);
    DatabaseObject object = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
    assertNotNull(object);
    object.setValue(SampleObject2.COLUMN_LAT, 0.1);
    object.setValue(SampleObject2.COLUMN_LON, 0.2);
    object.setValue(SampleObject2.COLUMN_ALT, 0.3);
    final byte[] rbtyes = randomBytesArray(1024);
    object.setValue(SampleObject2.COLUMN_BIN, rbtyes);
    long rowId = connectivity.insert(object);
    assertEquals(true, (rowId > 0));
    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.1, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LAT.getName())));
    assertEquals(0.2, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LON.getName())));
    assertEquals(0.3, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_ALT.getName())));
    Asserts.assertEquals(rbtyes, c.getBlob(c.getColumnIndex(SampleObject2.COLUMN_BIN.getName())));
    c.close();
    sqlDB.close();
    connectivity.delete(new Query(SampleObject2.class));
}
Also used : Query(com.dailystudio.dataobject.query.Query) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SampleObject2(com.dailystudio.dataobject.samples.SampleObject2) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor)

Example 10 with DatabaseObject

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

the class DatabaseConnectivityDirectSQLImplTest method testSimpleQuery.

public void testSimpleQuery() {
    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);
    assertNotNull(results);
    assertEquals(3, results.size());
    DatabaseObject resultObject = null;
    for (int i = 6; i <= 8; i++) {
        resultObject = results.get(i - 6);
        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) 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