use of android.database.sqlite.SQLiteQueryBuilder in project apps-android-commons by commons-app.
the class ContributionsContentProvider method query.
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Contribution.Table.TABLE_NAME);
int uriType = uriMatcher.match(uri);
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor;
switch(uriType) {
case CONTRIBUTIONS:
cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
break;
case CONTRIBUTIONS_ID:
cursor = queryBuilder.query(db, Contribution.Table.ALL_FIELDS, "_id = ?", new String[] { uri.getLastPathSegment() }, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI" + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
use of android.database.sqlite.SQLiteQueryBuilder in project android_frameworks_base by crdroidandroid.
the class LocalProvider method query.
@Override
public Cursor query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// Generate the body of the query
int match = sURLMatcher.match(url);
switch(match) {
case DATA:
qb.setTables("data");
break;
case DATA_ID:
qb.setTables("data");
qb.appendWhere("_id=");
qb.appendWhere(url.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URL " + url);
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = qb.query(db, projectionIn, selection, selectionArgs, null, null, sort);
if (ret == null) {
if (false)
Log.d(TAG, "Alarms.query: failed");
} else {
ret.setNotificationUri(getContext().getContentResolver(), url);
}
return ret;
}
use of android.database.sqlite.SQLiteQueryBuilder in project android_frameworks_base by crdroidandroid.
the class MtpDatabase method queryRoots.
/**
* Queries roots information.
* @param columnNames Column names defined in {@link android.provider.DocumentsContract.Root}.
* @return Database cursor.
*/
Cursor queryRoots(Resources resources, String[] columnNames) {
final String selection = COLUMN_ROW_STATE + " IN (?, ?) AND " + COLUMN_DOCUMENT_TYPE + " = ?";
final Cursor deviceCursor = mDatabase.query(TABLE_DOCUMENTS, strings(COLUMN_DEVICE_ID), selection, strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE), COLUMN_DEVICE_ID, null, null, null);
try {
final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(JOIN_ROOTS);
builder.setProjectionMap(COLUMN_MAP_ROOTS);
final MatrixCursor result = new MatrixCursor(columnNames);
final ContentValues values = new ContentValues();
while (deviceCursor.moveToNext()) {
final int deviceId = deviceCursor.getInt(0);
final Cursor storageCursor = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_STORAGE, deviceId), null, null, null);
try {
values.clear();
try (final Cursor deviceRoot = builder.query(mDatabase, columnNames, selection + " AND " + COLUMN_DEVICE_ID + " = ?", strings(ROW_STATE_VALID, ROW_STATE_INVALIDATED, DOCUMENT_TYPE_DEVICE, deviceId), null, null, null)) {
deviceRoot.moveToNext();
DatabaseUtils.cursorRowToContentValues(deviceRoot, values);
}
if (storageCursor.getCount() != 0) {
long capacityBytes = 0;
long availableBytes = 0;
final int capacityIndex = storageCursor.getColumnIndex(Root.COLUMN_CAPACITY_BYTES);
final int availableIndex = storageCursor.getColumnIndex(Root.COLUMN_AVAILABLE_BYTES);
while (storageCursor.moveToNext()) {
// don't calculate corresponding values.
if (capacityIndex != -1) {
capacityBytes += storageCursor.getLong(capacityIndex);
}
if (availableIndex != -1) {
availableBytes += storageCursor.getLong(availableIndex);
}
}
values.put(Root.COLUMN_CAPACITY_BYTES, capacityBytes);
values.put(Root.COLUMN_AVAILABLE_BYTES, availableBytes);
} else {
values.putNull(Root.COLUMN_CAPACITY_BYTES);
values.putNull(Root.COLUMN_AVAILABLE_BYTES);
}
if (storageCursor.getCount() == 1 && values.containsKey(Root.COLUMN_TITLE)) {
storageCursor.moveToFirst();
// Add storage name to device name if we have only 1 storage.
values.put(Root.COLUMN_TITLE, resources.getString(R.string.root_name, values.getAsString(Root.COLUMN_TITLE), storageCursor.getString(storageCursor.getColumnIndex(Root.COLUMN_TITLE))));
}
} finally {
storageCursor.close();
}
final RowBuilder row = result.newRow();
for (final String key : values.keySet()) {
row.add(key, values.get(key));
}
}
return result;
} finally {
deviceCursor.close();
}
}
Aggregations