use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityDirectSQLImplTest 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 DatabaseConnectivityDirectSQLiteImpl(mTargetContext, 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));
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityDirectSQLImplTest method testQueryWithGroupBy.
public void testQueryWithGroupBy() {
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", i);
object.setValue("doubleValue", ((double) i * 2));
object.setValue("textValue", String.format("%04d", i * 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 = new Query(QueryObject.class);
assertNotNull(query);
ExpressionToken selToken = col.gte(0).and(col.lt(2));
query.setSelection(selToken);
OrderingToken groupByToken = col.groupBy();
query.setGroupBy(groupByToken);
List<DatabaseObject> results = connectivity.query(query);
assertNotNull(results);
assertEquals(2, results.size());
DatabaseObject resultObject = null;
for (int i = 0; i < 2; i++) {
resultObject = results.get(i);
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));
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testQueryWithProjectionClass.
public void testQueryWithProjectionClass() {
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);
DatabaseObject projectionObject = new ProjectionObject(mContext);
assertNotNull(projectionObject);
Cursor c = null;
serial = System.currentTimeMillis();
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, QueryObject.class, serial);
assertNotNull(queryUri);
c = cr.query(queryUri, projectionObject.toSQLProjection(), selection.toString(), null, null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertEquals(true, c.moveToFirst());
int columnIndex = -1;
columnIndex = c.getColumnIndex(ProjectionObject.COLUMN_ID_COUNT.getName());
assertEquals(3, c.getInt(columnIndex));
c.close();
cr.delete(queryUri, null, null);
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testInsertDatabaseObject.
public void testInsertDatabaseObject() {
ContentResolver cr = mTargetContext.getContentResolver();
assertNotNull(cr);
DatabaseObject object = new SampleObject2(mTargetContext);
object.setValue("latitude", 0.123);
object.setValue("longitude", 0.456);
object.setValue("altitude", 0.789);
final ContentValues values = object.getValues();
assertNotNull(values);
Uri queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject2.class);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.insert(queryUri, values);
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.123, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LAT.getName())));
assertEquals(0.456, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_LON.getName())));
assertEquals(0.789, c.getDouble(c.getColumnIndex(SampleObject2.COLUMN_ALT.getName())));
c.close();
sqlDB.delete(DatabaseObject.classToTable(SampleObject2.class), null, null);
sqlDB.close();
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityProviderTest method testQueryCommand.
public void testQueryCommand() {
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(VersionControlledObject.class, DatabaseObject.VERSION_START);
assertNotNull(object);
object.setValue(VersionControlledObject.COLUMN_INT_VER1, i);
object.setValue(VersionControlledObject.COLUMN_LONG_VER1, ((long) i * 10000000000l));
values[i] = object.getValues();
}
Uri queryUri = null;
queryUri = ProviderUriBuilder.buildQueryUri(AUTHORITY, VersionControlledObject.class);
assertNotNull(queryUri);
queryUri = ProviderUriBuilder.attachCreateTableParamter(queryUri, object.toSQLTableCreationString());
assertNotNull(queryUri);
cr.bulkInsert(queryUri, values);
Uri commandUri = ProviderUriBuilder.buildCommandUri(AUTHORITY, VersionControlledObject.class, DatabaseObject.VERSION_START + 1, GetUpdateInfoCmdCursor.COMMAND_NAME);
assertNotNull(commandUri);
Cursor c = null;
c = cr.query(commandUri, null, null, null, null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertEquals(true, c.moveToFirst());
final int newVer = c.getInt(c.getColumnIndex(GetUpdateInfoCmdCursor.COLUMN_NEW_VERSION));
final int oldVer = c.getInt(c.getColumnIndex(GetUpdateInfoCmdCursor.COLUMN_OLD_VERSION));
c.close();
assertEquals(2, newVer);
assertEquals(1, oldVer);
// cr.delete(queryUri, null, null);
}
Aggregations