Search in sources :

Example 21 with DatabaseObject

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;
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) SQLException(android.database.SQLException) Template(com.dailystudio.dataobject.Template)

Example 22 with DatabaseObject

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;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) ArrayList(java.util.ArrayList) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Cursor(android.database.Cursor)

Example 23 with DatabaseObject

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;
}
Also used : ArrayList(java.util.ArrayList) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) AbstractWindowedCursor(android.database.AbstractWindowedCursor) Cursor(android.database.Cursor)

Example 24 with DatabaseObject

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);
}
Also used : SampleObject1(com.dailystudio.dataobject.samples.SampleObject1) DatabaseObject(com.dailystudio.dataobject.DatabaseObject) Uri(android.net.Uri) Template(com.dailystudio.dataobject.Template)

Example 25 with DatabaseObject

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;
}
Also used : DatabaseObject(com.dailystudio.dataobject.DatabaseObject) List(java.util.List) DatabaseConnectivity(com.dailystudio.dataobject.database.DatabaseConnectivity)

Aggregations

DatabaseObject (com.dailystudio.dataobject.DatabaseObject)58 Query (com.dailystudio.dataobject.query.Query)38 Cursor (android.database.Cursor)28 ExpressionToken (com.dailystudio.dataobject.query.ExpressionToken)27 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)24 Template (com.dailystudio.dataobject.Template)23 QueryObject (com.dailystudio.dataobject.samples.QueryObject)23 Column (com.dailystudio.dataobject.Column)19 SampleObject2 (com.dailystudio.dataobject.samples.SampleObject2)17 Uri (android.net.Uri)15 SampleObject1 (com.dailystudio.dataobject.samples.SampleObject1)14 ContentValues (android.content.ContentValues)13 ContentResolver (android.content.ContentResolver)12 OrderingToken (com.dailystudio.dataobject.query.OrderingToken)8 SQLException (android.database.SQLException)6 DatabaseConnectivity (com.dailystudio.dataobject.database.DatabaseConnectivity)3 ProjectionObject (com.dailystudio.dataobject.samples.ProjectionObject)3 ArrayList (java.util.ArrayList)3 DynamicColumnsObject (com.dailystudio.dataobject.samples.DynamicColumnsObject)2 List (java.util.List)2