use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testLimitQuery.
public void testLimitQuery() {
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();
}
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);
ExpressionToken limit = new ExpressionToken("5");
assertNotNull(limit);
Cursor c = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, object.toSQLProjection(), null, null, SortOrderEncoder.encode(null, null, null, limit.toString()));
assertNotNull(c);
assertEquals(5, c.getCount());
assertEquals(true, c.moveToFirst());
int columnIndex = -1;
for (int i = 0; i < 5; 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);
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testDeleteDatabaseObjects.
public void testDeleteDatabaseObjects() {
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);
ExpressionToken selection = SampleObject2.COLUMN_LAT.gt(0.2).and(SampleObject2.COLUMN_LON.lt(0.8));
cr.delete(queryUri, selection.toString(), null);
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(9, c.getCount());
assertEquals(true, c.moveToFirst());
for (int i = 0; i < count; i++) {
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(DatabaseObject.classToTable(SampleObject2.class), null, null);
sqlDB.close();
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testUpdateDatabaseObjects.
public void testUpdateDatabaseObjects() {
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);
DatabaseObject updateObject = DatabaseObjectFactory.createDatabaseObject(SampleObject2.class);
updateObject.setValue(SampleObject2.COLUMN_ALT, 12.34);
ExpressionToken selection = SampleObject2.COLUMN_LAT.gt(0.2).and(SampleObject2.COLUMN_LON.lt(0.8));
cr.update(queryUri, updateObject.getValues(), selection.toString(), null);
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++) {
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")));
} 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(DatabaseObject.classToTable(SampleObject2.class), null, null);
sqlDB.close();
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class TimeColumnTest method testTimeInDay.
public void testTimeInDay() {
TimeColumn column = new TimeColumn("time");
assertNotNull(column);
ExpressionToken exp = column.timeInDay();
String expected = null;
;
expected = "( time % 86400000 )";
assertEquals(expected, exp.toString());
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class AbsTimeCapsuleModel method listObjects.
public List<T> listObjects(Context context, DatabaseObjectKeys keys, String listTokenType) {
if (context == null) {
return null;
}
TimeCapsuleDatabaseReader<T> reader = new TimeCapsuleDatabaseReader<>(context, mObjectClass);
Query query = new Query(mObjectClass);
if (keys != null && !TextUtils.isEmpty(listTokenType)) {
ExpressionToken token = objectsToken(keys, listTokenType);
if (token == null) {
return null;
}
query.setSelection(token);
}
OrderingToken orderByToken = orderByToken(listTokenType);
if (orderByToken != null) {
query.setOrderBy(orderByToken);
}
OrderingToken groupByToken = groupByToken(listTokenType);
if (groupByToken != null) {
query.setGroupBy(groupByToken);
}
return reader.query(query);
}
Aggregations