Example usage for android.database.sqlite SQLiteQueryBuilder setProjectionMap

List of usage examples for android.database.sqlite SQLiteQueryBuilder setProjectionMap

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteQueryBuilder setProjectionMap.

Prototype

public void setProjectionMap(Map<String, String> columnMap) 

Source Link

Document

Sets the projection map for the query.

Usage

From source file:com.manning.androidhacks.hack043.provider.MySQLContentProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    switch (sUriMatcher.match(uri)) {
    case ITEM://from w w w. j a  va 2s .com
        qb.setTables(TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        break;
    case ITEM_ID:
        qb.setTables(TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        qb.appendWhere(COLUMN_ID + "=" + uri.getPathSegments().get(1));
        break;
    default:
        throw new RuntimeException("Unknown URI");
    }

    SQLiteDatabase db = getDatabaseHelper().getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

From source file:com.manning.androidhacks.hack043.provider.BatchNumbersContentProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    switch (sUriMatcher.match(uri)) {
    case ITEM:/*w ww  . j  ava  2  s.c  o  m*/
        qb.setTables(TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        break;
    case ITEM_ID:
        qb.setTables(TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        qb.appendWhere(COLUMN_ID + "=" + uri.getPathSegments().get(1));
        break;
    default:
        throw new RuntimeException("Unknown URI");
    }

    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

From source file:com.manning.androidhacks.hack023.provider.TodoContentProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    switch (sUriMatcher.match(uri)) {
    case TODO:/*ww w  .j av a2s . c  o  m*/
        qb.setTables(TODO_TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        break;
    case TODO_ID:
        qb.setTables(TODO_TABLE_NAME);
        qb.setProjectionMap(projectionMap);
        qb.appendWhere(COLUMN_ID + "=" + uri.getPathSegments().get(1));
        break;
    default:
        throw new RuntimeException("Unknown URI");
    }

    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

From source file:com.example.android.tvleanback2.data.VideoDatabase.java

/**
 * Performs a database query./*from ww  w . ja  va  2  s.c o m*/
 *
 * @param selection     The selection clause
 * @param selectionArgs Selection arguments for "?" components in the selection
 * @param columns       The columns to return
 * @return A Cursor over all rows matching the query
 */
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
    /* The SQLiteBuilder provides a map for all possible columns requested to
     * actual columns in the database, creating a simple column alias mechanism
     * by which the ContentProvider does not need to know the real column names
     */
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);
    builder.setProjectionMap(COLUMN_MAP);

    return builder.query(mDatabaseOpenHelper.getReadableDatabase(), columns, selection, selectionArgs, null,
            null, null);
}

From source file:com.example.android.dragonTV.data.VideoDatabase.java

/**
 * Performs a database query.//from w  ww .j  a va2s .  co  m
 *
 * @param selection     The selection clause
 * @param selectionArgs Selection arguments for "?" components in the selection
 * @param columns       The columns to return
 * @return A Cursor over all rows matching the query
 */
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
    /* The SQLiteBuilder provides a map for all possible columns requested to
     * actual columns in the database, creating a simple column alias mechanism
     * by which the ContentProvider does not need to know the real column names
     */
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);
    builder.setProjectionMap(COLUMN_MAP);

    Cursor cursor = new PaginatedCursor(builder.query(mDatabaseOpenHelper.getReadableDatabase(), columns,
            selection, selectionArgs, null, null, null));

    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

From source file:org.mozilla.labs.Soup.provider.AppsProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    qb.setTables(APPS_TABLE_NAME);//w  w  w.java  2  s  .  co  m

    switch (sUriMatcher.match(uri)) {
    case APPS:
        qb.setProjectionMap(sAppsProjectionMap);
        break;

    case APP_ID:
        qb.setProjectionMap(sAppsProjectionMap);
        qb.appendWhere(Apps._ID + "=" + uri.getPathSegments().get(1));
        break;

    case LIVE_FOLDER_APPS:
        qb.setProjectionMap(sLiveFolderProjectionMap);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // If no sort order is specified use the default
    String orderBy;
    if (TextUtils.isEmpty(sortOrder)) {
        orderBy = AppsContract.Apps.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sortOrder;
    }

    // Get the database and run the query
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

    // Tell the cursor what uri to watch, so it knows when its source data
    // changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

From source file:com.appsimobile.appsii.module.apps.AppsProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    // Generate the body of the query
    int match = sURLMatcher.match(uri);
    switch (match) {
    // loads all tagged-apps in a given tag.
    case TABLE_APPS_ITEM:
        long id = ContentUris.parseId(uri);
        qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME);
        qb.setProjectionMap(sAppsProjectionMap);
        qb.appendWhere(TaggedAppColumns.TAG_ID + "=" + id + " AND ");
        qb.appendWhere("taggedApps.tag_id=tags._id");
        break;/*w  ww  . j  a v  a2s . c o m*/
    // loads all tagged apps
    case TABLE_APPS:
        qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME);
        qb.setProjectionMap(sAppsProjectionMap);
        qb.appendWhere("taggedApps.tag_id=tags._id");
        break;
    // loads items in the launch history table
    case TABLE_HISTORY:
        qb.setTables(LaunchHistoryColumns.TABLE_NAME);
        qb.setProjectionMap(sHistoryProjectionMap);
        break;
    // loads all tags
    case TABLE_TAGS:
        qb.setTables(TagColumns.TABLE_NAME);
        qb.setProjectionMap(sTagsProjectionMap);
        break;
    default:
        throw new IllegalArgumentException("Invalid uri: " + uri);
    }
    return qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
}

From source file:de.nware.app.hsDroid.provider.onlineService2Provider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor = null;/*from  w w  w. j  ava  2s . co m*/
    switch (mUriMatcher.match(uri)) {
    case EXAMS:
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(mOpenHelper.getTableName());
        qb.setProjectionMap(examsProjectionMap);
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        try {
            cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.d(TAG, "SqlError: " + e.getMessage());
        }
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        break;
    case EXAMS_UPDATE:
        MatrixCursor cur = new MatrixCursor(EXAMS_UPDATE_COLUMNS);
        Integer[] columnValues = updateGrades();
        cur.addRow(new Object[] { 0, columnValues[0], columnValues[1] });
        return cur;
    case EXAMINFOS:
        cursor = getExamInfos(selectionArgs[0], selectionArgs[1], false);
        break;
    case CERTIFICATIONS:
        cursor = getCertifications();
        break;
    default:
        throw new IllegalArgumentException("Unbekannte URI " + uri);
    }

    return cursor;
}

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

private Cursor querySource(@NonNull final Uri uri, final String[] projection, final String selection,
        final String[] selectionArgs, final String sortOrder) {
    ContentResolver contentResolver = getContext() != null ? getContext().getContentResolver() : null;
    if (contentResolver == null) {
        return null;
    }//from  w w w.j  a v a 2  s . co  m
    final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(MuzeiContract.Sources.TABLE_NAME);
    qb.setProjectionMap(allSourcesColumnProjectionMap);
    final SQLiteDatabase db = databaseHelper.getReadableDatabase();
    if (MuzeiProvider.uriMatcher.match(uri) == SOURCE_ID) {
        // If the incoming URI is for a single source identified by its ID, appends "_ID = <sourceId>"
        // to the where clause, so that it selects that single source
        qb.appendWhere(BaseColumns._ID + "=" + uri.getLastPathSegment());
    }
    String orderBy;
    if (TextUtils.isEmpty(sortOrder))
        orderBy = MuzeiContract.Sources.DEFAULT_SORT_ORDER;
    else
        orderBy = sortOrder;
    final Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy, null);
    c.setNotificationUri(contentResolver, uri);
    return c;
}

From source file:org.thomnichols.android.gmarks.GmarksProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    //        Log.d(TAG, "Managed query: " + uri);
    String groupBy = null;/*  ww  w.  j ava2 s.  co  m*/
    String orderBy = null;
    String limit = null;
    switch (sUriMatcher.match(uri)) {
    case BOOKMARKS_URI:
        qb.setTables(BOOKMARKS_TABLE_NAME);
        qb.setProjectionMap(bookmarksProjectionMap);

        String labelID = uri.getQueryParameter("label_id");
        if (labelID != null) {
            qb.setTables("bookmarks join bookmark_labels on bookmarks._id = bookmark_labels.bookmark_id");
            qb.appendWhere("bookmark_labels.label_id=?");
            selectionArgs = (String[]) ArrayUtils.addAll(selectionArgs, new String[] { labelID });
        }
        break;

    case BOOKMARK_SEARCH_URI:
    case BOOKMARK_SEARCH_SUGGEST_URI:
        String query = null;
        if (sUriMatcher.match(uri) == BOOKMARK_SEARCH_SUGGEST_URI) {
            qb.setProjectionMap(searchSuggestProjectionMap);
            // path looks like "search_suggest_query/[query]?limit=50
            query = uri.getLastPathSegment();
            limit = uri.getQueryParameter("limit");
            if (sortOrder == null)
                sortOrder = Bookmark.Columns.SORT_MODIFIED;
        } else
            query = uri.getQueryParameter("q");

        if (query != null) {
            qb.setTables("bookmarks join bookmarks_FTS on bookmarks._id = bookmarks_FTS.docid");
            qb.appendWhere("bookmarks_FTS MATCH ?");
            if (selectionArgs == null)
                selectionArgs = new String[] { query };
            else
                selectionArgs = (String[]) ArrayUtils.addAll(selectionArgs, new String[] { query });
        } else if (selectionArgs == null || selectionArgs.length < 1)
            throw new IllegalArgumentException("No search criteria given for query!");
        break;

    case BOOKMARK_ID_URI:
        qb.setTables(BOOKMARKS_TABLE_NAME);
        qb.setProjectionMap(bookmarksProjectionMap);
        qb.appendWhere(Bookmark.Columns._ID + "=" + uri.getPathSegments().get(1));
        break;

    case LABELS_URI:
        qb.setTables("labels join bookmark_labels on labels._id = bookmark_labels.label_id");
        groupBy = "label";
        if (sortOrder == null)
            sortOrder = Label.Columns.DEFAULT_SORT_ORDER;
        qb.setProjectionMap(labelsProjectionMap);
        break;

    case LIVE_FOLDER_BOOKMARKS_URI:
        qb.setTables(BOOKMARKS_TABLE_NAME);
        qb.setProjectionMap(sLiveFolderProjectionMap);
        String labelId = uri.getQueryParameter("label_id");
        if (labelId != null) {
            qb.setTables("bookmarks join bookmark_labels on bookmarks._id = bookmark_labels.bookmark_id");
            qb.appendWhere("bookmark_labels.label_id=?");
            selectionArgs = (String[]) ArrayUtils.addAll(selectionArgs, new String[] { labelId });
        }
        sortOrder = "modified DESC"; // for some reason this gets set to 'name ASC'
        break;

    case BOOKMARK_LISTS_URI:
        qb.setTables(BookmarkList.TABLE_NAME);
        qb.setProjectionMap(listsProjectionMap);
        if (sortOrder == null)
            sortOrder = BookmarkList.Columns.DEFAULT_SORT_ORDER;
        String type = uri.getQueryParameter(BookmarkList.PARAM_CATEGORY);
        if (BookmarkList.LISTS_PRIVATE.equals(type))
            qb.appendWhere("owned=1");
        else if (BookmarkList.LISTS_SHARED.equals(type))
            qb.appendWhere("shared=1");
        else if (BookmarkList.LISTS_PUBLIC.equals(type))
            qb.appendWhere("publshed=1");
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // If no sort order is specified use the default
    if (TextUtils.isEmpty(sortOrder)) {
        orderBy = Bookmark.Columns.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sortOrder;
    }

    // Get the database and run the query
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, null, orderBy, limit);

    // Tell the cursor what uri to watch, so it knows when its source data changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}