Search in sources :

Example 6 with TableDetails

use of me.himanshusoni.quantumflux.model.generate.TableDetails in project QuantumFlux by himanshu-soni.

the class ModelInflater method deflate.

public static ContentValues deflate(TableDetails tableDetails, Object dataModelObject) {
    List<TableDetails.ColumnDetails> columns = tableDetails.getColumns();
    ContentValues contentValues = new ContentValues(columns.size());
    for (int i = 0; i < columns.size(); i++) {
        TableDetails.ColumnDetails columnDetails = columns.get(i);
        if (columnDetails.isAutoIncrement())
            continue;
        try {
            columnDetails.setContentValue(contentValues, dataModelObject);
        } catch (IllegalAccessException e) {
            throw new QuantumFluxException("Unable to access protected field, change the access level: " + columnDetails.getColumnName());
        }
    }
    return contentValues;
}
Also used : ContentValues(android.content.ContentValues) TableDetails(me.himanshusoni.quantumflux.model.generate.TableDetails)

Example 7 with TableDetails

use of me.himanshusoni.quantumflux.model.generate.TableDetails in project QuantumFlux by himanshu-soni.

the class QuantumFluxContentProvider method delete.

@Override
public int delete(@NonNull Uri uri, String where, String[] args) {
    TableDetails tableDetails = mUriMatcherHelper.getTableDetails(uri);
    SQLiteDatabase db = mDatabase.getWritableDatabase();
    if (mDebugEnabled) {
        QuantumFluxLog.d("********* Delete **********");
        QuantumFluxLog.d("Uri: " + uri);
        QuantumFluxLog.d("Where: " + where);
        QuantumFluxLog.d("Args: " + Arrays.toString(args));
    }
    int deleteCount;
    if (mUriMatcherHelper.isSingleItemRequested(uri)) {
        String itemId = uri.getLastPathSegment();
        TableDetails.ColumnDetails primaryKeyColumn = tableDetails.findPrimaryKeyColumn();
        deleteCount = db.delete(tableDetails.getTableName(), primaryKeyColumn.getColumnName() + " = ?", new String[] { itemId });
    } else {
        deleteCount = db.delete(tableDetails.getTableName(), where, args);
    }
    if (deleteCount == 0) {
        return deleteCount;
    }
    notifyChanges(uri, tableDetails);
    return deleteCount;
}
Also used : TableDetails(me.himanshusoni.quantumflux.model.generate.TableDetails) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 8 with TableDetails

use of me.himanshusoni.quantumflux.model.generate.TableDetails in project QuantumFlux by himanshu-soni.

the class QuantumFluxContentProvider method query.

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    TableDetails tableDetails = mUriMatcherHelper.getTableDetails(uri);
    SQLiteDatabase db = mDatabase.getReadableDatabase();
    String limit = constructLimit(uri);
    if (mDebugEnabled) {
        QuantumFluxLog.d("********* Query **********");
        QuantumFluxLog.d("Uri: " + uri);
        QuantumFluxLog.d("Projection: " + Arrays.toString(projection));
        QuantumFluxLog.d("Selection: " + selection);
        QuantumFluxLog.d("Args: " + Arrays.toString(selectionArgs));
        QuantumFluxLog.d("Sort: " + sortOrder);
        QuantumFluxLog.d("Limit: " + limit);
    }
    Cursor cursor;
    if (mUriMatcherHelper.isSingleItemRequested(uri)) {
        String itemId = uri.getLastPathSegment();
        TableDetails.ColumnDetails primaryKeyColumn = tableDetails.findPrimaryKeyColumn();
        cursor = db.query(tableDetails.getTableName(), projection, primaryKeyColumn.getColumnName() + " = ?", new String[] { itemId }, null, sortOrder, limit);
    } else {
        cursor = db.query(tableDetails.getTableName(), projection, selection, selectionArgs, null, null, sortOrder, limit);
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
Also used : TableDetails(me.himanshusoni.quantumflux.model.generate.TableDetails) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Example 9 with TableDetails

use of me.himanshusoni.quantumflux.model.generate.TableDetails in project QuantumFlux by himanshu-soni.

the class QuantumFluxContentProvider method bulkInsert.

@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
    if (values.length == 0)
        return 0;
    TableDetails tableDetails = mUriMatcherHelper.getTableDetails(uri);
    SQLiteDatabase db = mDatabase.getWritableDatabase();
    if (mDebugEnabled) {
        QuantumFluxLog.d("********* Bulk Insert **********");
        QuantumFluxLog.d("Uri: " + uri);
    }
    int count = 0;
    try {
        db.beginTransactionNonExclusive();
        String tableName = tableDetails.getTableName();
        for (ContentValues value : values) {
            db.insertOrThrow(tableName, null, value);
            count++;
        }
        db.setTransactionSuccessful();
        notifyChanges(uri, tableDetails);
    } finally {
        db.endTransaction();
    }
    return count;
}
Also used : ContentValues(android.content.ContentValues) TableDetails(me.himanshusoni.quantumflux.model.generate.TableDetails) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 10 with TableDetails

use of me.himanshusoni.quantumflux.model.generate.TableDetails in project QuantumFlux by himanshu-soni.

the class QuantumFluxContentProvider method insert.

@Override
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
    TableDetails tableDetails = mUriMatcherHelper.getTableDetails(uri);
    SQLiteDatabase db = mDatabase.getWritableDatabase();
    if (mDebugEnabled) {
        QuantumFluxLog.d("********* Insert **********");
        QuantumFluxLog.d("Uri: " + uri);
        QuantumFluxLog.d("Content Values: " + contentValues);
    }
    long insertId = db.insertOrThrow(tableDetails.getTableName(), null, contentValues);
    if (insertId == -1) {
        throw new QuantumFluxException("Failed to insert row for into table " + tableDetails.getTableName() + " using values " + contentValues);
    }
    notifyChanges(uri, tableDetails);
    TableDetails.ColumnDetails primaryKeyColumn = tableDetails.findPrimaryKeyColumn();
    if (primaryKeyColumn.isAutoIncrement()) {
        return mUriMatcherHelper.generateSingleItemUri(tableDetails, insertId);
    } else {
        String primaryKeyValue = contentValues.getAsString(primaryKeyColumn.getColumnName());
        return mUriMatcherHelper.generateSingleItemUri(tableDetails, primaryKeyValue);
    }
}
Also used : TableDetails(me.himanshusoni.quantumflux.model.generate.TableDetails) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) QuantumFluxException(me.himanshusoni.quantumflux.model.util.QuantumFluxException)

Aggregations

TableDetails (me.himanshusoni.quantumflux.model.generate.TableDetails)32 Uri (android.net.Uri)22 ContentValues (android.content.ContentValues)17 ContentResolver (android.content.ContentResolver)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 Cursor (android.database.Cursor)2 UriMatcher (android.content.UriMatcher)1 ContentResolverValues (me.himanshusoni.quantumflux.model.util.ContentResolverValues)1 CursorIterator (me.himanshusoni.quantumflux.model.util.CursorIterator)1 QuantumFluxException (me.himanshusoni.quantumflux.model.util.QuantumFluxException)1