use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class AbsTimeCapsuleModel method deleteObjects.
public int deleteObjects(Context context, DatabaseObjectKeys keys) {
if (context == null) {
return 0;
}
Query query = new Query(mObjectClass);
if (keys != null) {
ExpressionToken token = objectsToken(keys, TOKEN_TYPE_DELETION);
if (token == null) {
return 0;
}
query.setSelection(token);
}
TimeCapsuleDatabaseWriter<T> writer = new TimeCapsuleDatabaseWriter<>(context, mObjectClass);
return writer.delete(query);
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class TimeCapsuleDatabaseWriter method delete.
public int delete(T object) {
if (object == null) {
return 0;
}
final DatabaseConnectivity connectivity = getConnectivity();
if (connectivity == null) {
return 0;
}
Query query = getQuery();
ExpressionToken token = TimeCapsule.COLUMN_ID.eq(object.getId());
query.setSelection(token);
return connectivity.delete(query);
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class TimeCapsuleQueryBuilder method getQueryForIntersect.
public Query getQueryForIntersect(ExpressionToken startExp, ExpressionToken durationExp, long start, long end, boolean orderByAscending) {
if (startExp == null || durationExp == null) {
return null;
}
String startExpStr = startExp.toString();
Logger.debug("expression: startExp(%s), durationExp(%s)", startExp, durationExp, orderByAscending);
Logger.debug("peroid: start(%s), end(%s), orderByAscending(%s)", CalendarUtils.timeToReadableString(start), CalendarUtils.timeToReadableString(end), orderByAscending);
Query query = getQuery();
if (query != null) {
if (start < end) {
ExpressionToken sExp = new ExpressionToken(start);
ExpressionToken eExp = new ExpressionToken(end);
ExpressionToken sel1 = new ExpressionToken(startExpStr).lte(sExp).and(new ExpressionToken(startExpStr).plus(durationExp).gt(sExp));
ExpressionToken sel2 = new ExpressionToken(startExpStr).gt(sExp).and(new ExpressionToken(startExpStr).plus(durationExp).lte(eExp));
ExpressionToken selection = sel1.or(sel2);
if (selection != null) {
query.setSelection(selection);
}
}
OrderingToken orderBy = (orderByAscending ? TimeCapsule.COLUMN_TIME.orderByAscending() : TimeCapsule.COLUMN_TIME.orderByDescending());
if (orderBy != null) {
query.setOrderBy(orderBy);
}
}
return query;
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class DatabaseConnectivityTest method testQueryWithProjectionClass.
public void testQueryWithProjectionClass() {
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 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.gt(5).and(col.lt(9));
query.setSelection(selToken);
List<DatabaseObject> results = connectivity.query(query, ProjectionObject.class);
assertNotNull(results);
assertEquals(1, results.size());
DatabaseObject resultObject = results.get(0);
Integer iCount = resultObject.getIntegerValue("count( _id )");
assertNotNull(iCount);
assertEquals(3, iCount.intValue());
connectivity.delete(new Query(QueryObject.class));
}
use of com.dailystudio.dataobject.query.ExpressionToken in project devbricks by dailystudio.
the class DatabaseConnectivityTest method testQueryCursor.
public void testQueryCursor() {
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 DatabaseConnectivity(mTargetContext, AUTHORITY, QueryObject.class);
assertNotNull(connectivity);
connectivity.insert(objects);
final Template templ = object.getTemplate();
Column col = templ.getColumn("intValue");
Query query = null;
ExpressionToken selToken = null;
DatabaseObject resultObject = null;
Cursor c = null;
query = new Query(QueryObject.class);
assertNotNull(query);
selToken = col.gt(5).and(col.lt(9));
query.setSelection(selToken);
c = connectivity.queryCursor(query);
assertNotNull(c);
assertEquals(3, c.getCount());
assertTrue(c.moveToFirst());
for (int i = 6; i <= 8; i++, c.moveToNext()) {
resultObject = connectivity.fromCursor(c, QueryObject.class);
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);
}
c.close();
selToken = col.gt(5).and(col.lt(9));
query.setSelection(selToken);
c = connectivity.queryCursor(query, ProjectionObject.class);
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
resultObject = connectivity.fromCursor(c, ProjectionObject.class);
Integer iCount = resultObject.getIntegerValue("count( _id )");
assertNotNull(iCount);
assertEquals(3, iCount.intValue());
c.close();
connectivity.delete(new Query(QueryObject.class));
}
Aggregations