use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityTest 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 DatabaseConnectivity(mTargetContext, AUTHORITY, 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));
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityTest method testDeleteAllDatabaseObjects.
public void testDeleteAllDatabaseObjects() {
AbsDatabaseConnectivity connectivity = new DatabaseConnectivity(mTargetContext, AUTHORITY, 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));
}
use of com.dailystudio.dataobject.DatabaseObject 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));
}
use of com.dailystudio.dataobject.DatabaseObject 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.DatabaseObject in project devbricks by dailystudio.
the class DatabaseUpdaterDirectSQLImplTest method testDoUpdate.
public void testDoUpdate() {
final int count = 10;
DatabaseObject object = null;
DatabaseObject[] objects = new DatabaseObject[count];
assertNotNull(objects);
for (int i = 0; i < 10; i++) {
object = DatabaseObjectFactory.createDatabaseObject(DynamicColumnsObject.class, DatabaseObject.VERSION_START);
assertNotNull(object);
if (i % 2 != 0) {
object.setValue(DynamicColumnsObject.COLUMN_INT_VER1, i * 10);
}
object.setValue(DynamicColumnsObject.COLUMN_LONG_VER1, i * 10000000000l);
Logger.debug("object = %s", object);
objects[i] = object;
}
AbsDatabaseConnectivity connectivity = null;
connectivity = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, DynamicColumnsObject.class, DatabaseObject.VERSION_START);
assertNotNull(connectivity);
connectivity.insert(objects);
SQLiteDatabase sqlDB = null;
Cursor c = null;
DatabaseOpenHandler handler = null;
boolean catched = false;
handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START);
assertNotNull(handler);
sqlDB = handler.getReadableDatabase();
assertNotNull(sqlDB);
c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
assertNotNull(c);
assertEquals(10, c.getCount());
assertEquals(true, c.moveToFirst());
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) {
assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1.getName())));
}
assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
c.moveToNext();
}
c.close();
sqlDB.close();
DatabaseUpdateInfo info = null;
AbsDatabaseUpdater updater = null;
connectivity = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, DynamicColumnsObject.class, DatabaseObject.VERSION_START + 1);
info = connectivity.checkUpdates();
assertNotNull(info);
assertTrue(info.needUpdate());
assertEquals(DatabaseObject.VERSION_START + 1, info.getNewVersion());
assertEquals(DatabaseObject.VERSION_START, info.getOldVersion());
updater = new DynamicColumnsUpdater(mTargetContext, DynamicColumnsObject.class);
assertNotNull(updater);
updater.doUpdate(info.getNewVersion(), info.getOldVersion());
handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START + 1);
assertNotNull(handler);
sqlDB = handler.getReadableDatabase();
assertNotNull(sqlDB);
c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
assertNotNull(c);
assertEquals(10, c.getCount());
assertEquals(true, c.moveToFirst());
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) {
assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
} else {
assertEquals(999, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
}
catched = false;
try {
assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
catched = false;
} catch (IllegalStateException e) {
catched = true;
}
assertTrue(catched);
assertEquals(1234, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER2.getName())));
assertEquals(0.414, c.getDouble(c.getColumnIndex(DynamicColumnsObject.COLUMN_DOUBLE_VER2.getName())));
assertEquals("Text Version 2", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER2.getName())));
c.moveToNext();
}
c.close();
sqlDB.close();
connectivity = new DatabaseConnectivityDirectSQLiteImpl(mTargetContext, DynamicColumnsObject.class);
info = connectivity.checkUpdates();
assertNotNull(info);
assertTrue(info.needUpdate());
assertEquals(DatabaseObject.VERSION_START + 2, info.getNewVersion());
assertEquals(DatabaseObject.VERSION_START + 1, info.getOldVersion());
updater = new DynamicColumnsUpdater(mTargetContext, DynamicColumnsObject.class);
assertNotNull(updater);
updater.doUpdate(info.getNewVersion(), info.getOldVersion());
handler = DatabaseOpenHandler.getInstance(mTargetContext, DatabaseObject.classToDatabase(DynamicColumnsObject.class), DatabaseObject.VERSION_START + 2);
assertNotNull(handler);
sqlDB = handler.getReadableDatabase();
assertNotNull(sqlDB);
c = sqlDB.query(DatabaseObject.classToTable(DynamicColumnsObject.class), null, null, null, null, null, null);
assertNotNull(c);
assertEquals(10, c.getCount());
assertEquals(true, c.moveToFirst());
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) {
assertEquals(i * 10, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
} else {
assertEquals(999, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER1_UPDATED.getName())));
}
catched = false;
try {
assertEquals(i * 10000000000l, c.getLong(c.getColumnIndex(DynamicColumnsObject.COLUMN_LONG_VER1.getName())));
catched = false;
} catch (IllegalStateException e) {
catched = true;
}
assertTrue(catched);
assertEquals(1234, c.getInt(c.getColumnIndex(DynamicColumnsObject.COLUMN_INT_VER2.getName())));
assertEquals(0.414, c.getDouble(c.getColumnIndex(DynamicColumnsObject.COLUMN_DOUBLE_VER2.getName())));
assertEquals("Text Version 2", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER2.getName())));
assertEquals("Only In Version 3", c.getString(c.getColumnIndex(DynamicColumnsObject.COLUMN_TEXT_VER3.getName())));
c.moveToNext();
}
c.close();
sqlDB.close();
connectivity.delete(new Query(DynamicColumnsObject.class));
}
Aggregations