use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityDirectSQLiteImpl method onInsert.
@Override
protected int onInsert(DatabaseObject[] objects) {
if (objects == null) {
return 0;
}
final int count = objects.length;
if (count <= 0) {
return 0;
}
if (mOpenHandler == null) {
return 0;
}
final SQLiteDatabase db = mOpenHandler.getWritableDatabase();
if (db == null) {
return 0;
}
int successful = 0;
try {
db.beginTransaction();
ContentValues values = null;
DatabaseObject object = null;
Template template = null;
String table = null;
long rowId = -1;
for (int i = 0; i < count; i++) {
object = objects[i];
if (object == null || object.isEmpty()) {
continue;
}
values = object.getValues();
if (values == null) {
continue;
}
template = object.getTemplate();
if (template == null) {
continue;
}
table = DatabaseObject.classToTable(object.getClass());
if (table == null) {
continue;
}
if (checkOrCreateTable(db, table, object) == false) {
continue;
}
rowId = db.insert(table, object.handleNullProjection(), values);
if (rowId <= 0) {
continue;
}
successful++;
}
db.setTransactionSuccessful();
} catch (SQLException e) {
Logger.warn("database failure: %s", e.toString());
} catch (IllegalStateException e) {
Logger.warn("database failure: %s", e.toString());
} finally {
try {
db.endTransaction();
} catch (Exception e) {
Logger.warn("database failure: %s", e.toString());
}
}
db.close();
return successful;
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivityDirectSQLiteImpl method onQuery.
@Override
protected List<DatabaseObject> onQuery(Query query, Class<? extends DatabaseObject> projectionClass) {
if (mOpenHandler == null) {
return null;
}
final SQLiteDatabase db = mOpenHandler.getReadableDatabase();
if (db == null) {
return null;
}
Cursor c = doQuery(db, query, projectionClass);
if (c == null) {
return null;
}
final Class<? extends DatabaseObject> objectClass = query.getObjectClass();
if (objectClass == null) {
return null;
}
List<DatabaseObject> objects = new ArrayList<DatabaseObject>();
DatabaseObject object = null;
try {
if (c.moveToFirst()) {
do {
object = fromCursor(c, projectionClass);
if (object != null) {
objects.add(object);
}
} while (c.moveToNext());
}
} catch (SQLException e) {
Logger.warn("database failure: %s", e.toString());
} catch (IllegalStateException e) {
Logger.warn("database failure: %s", e.toString());
}
c.close();
db.close();
return objects;
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseConnectivity method onQuery.
@Override
protected List<DatabaseObject> onQuery(Query query, Class<? extends DatabaseObject> projectionClass) {
final long serial = System.currentTimeMillis();
// Logger.debug("OPEN DB: serial = %d", serial);
Cursor c = doQueryCursor(serial, query, projectionClass, false);
if (c == null) {
return null;
}
List<DatabaseObject> objects = new ArrayList<DatabaseObject>();
DatabaseObject object = null;
try {
if (c.moveToFirst()) {
do {
object = fromCursor(c, projectionClass);
if (object != null) {
object.fillValuesFromCursor(c);
objects.add(object);
}
} while (c.moveToNext());
}
} finally {
}
c.close();
return objects;
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class ProviderUriBuilderTest method testAttachCreateTableParamter.
public void testAttachCreateTableParamter() {
final long serial = System.currentTimeMillis();
Uri uri = ProviderUriBuilder.buildQueryUri(AUTHORITY, SampleObject1.class, serial);
assertNotNull(uri);
DatabaseObject sample = new SampleObject1(mContext);
assertNotNull(sample);
final Template template = sample.getTemplate();
assertNotNull(template);
final String table = DatabaseObject.classToTable(SampleObject1.class);
assertNotNull(table);
uri = ProviderUriBuilder.attachCreateTableParamter(uri, sample.toSQLTableCreationString());
String uristr = String.format("content://com.dailystudio/query/com.dailystudio.dataobject.samples.SampleObject1.db/1/SampleObject1?serial=%d&createTable=%s", serial, "CREATE%20TABLE%20IF%20NOT%20EXISTS%20SampleObject1%20(%20_id%20INTEGER%20NOT%20NULL%20PRIMARY%20KEY%2C%20time%20LONG%20NOT%20NULL%20)%3B");
Uri expectedUri = Uri.parse(uristr);
assertEquals(expectedUri, uri);
}
use of com.dailystudio.dataobject.DatabaseObject in project devbricks by dailystudio.
the class DatabaseReader method query.
@SuppressWarnings("unchecked")
public List<T> query(Query query) {
final DatabaseConnectivity connectivity = getConnectivity();
if (connectivity == null) {
return null;
}
List<DatabaseObject> objects = connectivity.query(query, null);
if (objects == null || objects.size() < 0) {
return null;
}
return (List<T>) objects;
}
Aggregations