List of usage examples for android.database.sqlite SQLiteQueryBuilder setTables
public void setTables(String inTables)
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; }/* w w w . j a va2 s.c o 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:com.google.android.apps.muzei.provider.MuzeiProvider.java
private Cursor queryArtwork(@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 .ja v a 2 s . c o m*/ final SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(MuzeiContract.Artwork.TABLE_NAME + " INNER JOIN " + MuzeiContract.Sources.TABLE_NAME + " ON " + MuzeiContract.Artwork.TABLE_NAME + "." + MuzeiContract.Artwork.COLUMN_NAME_SOURCE_COMPONENT_NAME + "=" + MuzeiContract.Sources.TABLE_NAME + "." + MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME); qb.setProjectionMap(allArtworkColumnProjectionMap); final SQLiteDatabase db = databaseHelper.getReadableDatabase(); if (MuzeiProvider.uriMatcher.match(uri) == ARTWORK_ID) { // If the incoming URI is for a single source identified by its ID, appends "_ID = <artworkId>" // to the where clause, so that it selects that single piece of artwork qb.appendWhere( MuzeiContract.Artwork.TABLE_NAME + "." + BaseColumns._ID + "=" + uri.getLastPathSegment()); } String orderBy; if (TextUtils.isEmpty(sortOrder)) orderBy = MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED + " DESC, " + MuzeiContract.Artwork.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:com.gmail.emerssso.srbase.database.SRContentProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selArgs, String sortOrder) { // Using SQLiteQueryBuilder instead of query() method SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); int uriType = sURIMatcher.match(uri); String tableName = ""; String[] columns;//w w w .ja v a 2s. c o m switch (uriType) { case SRS: case SR_ID: tableName = SRTable.TABLE_NAME; columns = SRTable.COLUMNS; break; case DAILIES: case DAILY_ID: tableName = DailyTable.TABLE_NAME; columns = DailyTable.COLUMNS; break; case PARTS: case PART_ID: tableName = PartTable.TABLE_NAME; columns = PartTable.COLUMNS; break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } checkColumns(projection, columns); queryBuilder.setTables(tableName); if (isIdType(uriType)) { queryBuilder.appendWhere("_id =" + uri.getLastPathSegment()); } SQLiteDatabase db = database.getWritableDatabase(); Cursor cursor = queryBuilder.query(db, projection, selection, selArgs, null, null, sortOrder); // make sure that potential listeners are getting notified cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
From source file:com.money.manager.ex.reports.PayeeReportFragment.java
@Override protected String prepareQuery(String whereClause) { SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); ViewMobileData mobileData = new ViewMobileData(getContext()); //data to compose builder String[] projectionIn = new String[] { ViewMobileData.PAYEEID + " AS _id", ViewMobileData.PAYEEID, ViewMobileData.PAYEE, "SUM(" + ViewMobileData.AmountBaseConvRate + ") AS TOTAL" }; String selection = ViewMobileData.Status + "<>'V' AND " + ViewMobileData.TransactionType + " IN ('Withdrawal', 'Deposit')"; if (!TextUtils.isEmpty(whereClause)) { selection += " AND " + whereClause; }//from w w w . j a v a2 s . c om String groupBy = ViewMobileData.PAYEEID + ", " + ViewMobileData.PAYEE; String having = null; String sortOrder = ViewMobileData.PAYEE; String limit = null; //compose builder builder.setTables(mobileData.getSource()); //return query if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { return builder.buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit); } else { return builder.buildQuery(projectionIn, selection, null, groupBy, having, sortOrder, limit); } }
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;// w ww . j a va2s. c o 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; }
From source file:com.appsimobile.appsii.module.home.provider.HomeContentProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { selection = fixWhereProjection(uri, selection); SqlArguments args = new SqlArguments(uri, selection, selectionArgs); SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); String tables = createTablesFromTableQuery(args.table); qb.setTables(tables); Map<String, String> projectionMap = getProjectionMapForTable(args.table); if (projectionMap != null) { qb.setProjectionMap(projectionMap); qb.setStrict(true);/* w ww. ja va2s . co m*/ } SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if (HomeContract.CELLS_TABLE_NAME.equals(args.table)) { sortOrder = fixSortOrder(sortOrder); } String where = args.where; if (HomeContract.HOTSPOT_PAGES_DETAILS_TABLE_NAME.equals(args.table)) { where = "_ht." + BaseColumns._ID + "=" + ContentUris.parseId(uri); } Cursor result = qb.query(db, projection, where, args.args, null, null, sortOrder); result.setNotificationUri(getContext().getContentResolver(), uri); return result; }
From source file:com.money.manager.ex.home.DashboardFragment.java
@SuppressWarnings("deprecation") private String prepareQueryTopPayees() { SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); ViewMobileData mobileData = new ViewMobileData(getContext()); // data to compose builder String[] projectionIn = new String[] { "ROWID AS _id", ViewMobileData.PAYEEID, ViewMobileData.PAYEE, "ABS(SUM(" + ViewMobileData.AmountBaseConvRate + ")) AS TOTAL", "COUNT(*) AS NUM" }; String selection = ViewMobileData.Status + "<>'V' AND " + ViewMobileData.TransactionType + " IN ('Withdrawal', 'Deposit') AND (julianday(date('now')) - julianday(" + ViewMobileData.Date + ") <= 30)"; String groupBy = ViewMobileData.PAYEEID + ", " + ViewMobileData.PAYEE; String having = null;//from www.ja v a 2 s . c o m String sortOrder = "ABS(SUM(" + ViewMobileData.AmountBaseConvRate + ")) DESC"; String limit = "10"; // compose builder builder.setTables(mobileData.getSource()); // return query if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { return builder.buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit); } else { return builder.buildQuery(projectionIn, selection, null, groupBy, having, sortOrder, limit); } }
From source file:org.kontalk.provider.UsersProvider.java
@Override public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {/* www. ja va 2 s .c om*/ SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); boolean offline = Boolean.parseBoolean(uri.getQueryParameter(Users.OFFLINE)); int match = sUriMatcher.match(uri); if (match == USERS || match == USERS_JID) { // use the same table name as an alias String table = offline ? (TABLE_USERS_OFFLINE + " " + TABLE_USERS) : TABLE_USERS; qb.setTables(table); qb.setProjectionMap(usersProjectionMap); } else if (match == KEYS || match == KEYS_JID || match == KEYS_JID_FINGERPRINT) { qb.setTables(TABLE_KEYS); qb.setProjectionMap(keysProjectionMap); } switch (match) { case USERS: // nothing to do break; case USERS_JID: { // TODO append to selection String userId = uri.getPathSegments().get(1); selection = TABLE_USERS + "." + Users.JID + " = ?"; selectionArgs = new String[] { userId }; break; } case KEYS: // nothing to do break; case KEYS_JID: case KEYS_JID_FINGERPRINT: String userId = uri.getPathSegments().get(1); selection = DatabaseUtilsCompat.concatenateWhere(selection, Keys.JID + "=?"); selectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { userId }); // TODO support for fingerprint in Uri break; default: throw new IllegalArgumentException("Unknown URI " + uri); } SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); if ((match == USERS || match == USERS_JID) && c.getCount() == 0 && (match != USERS_JID || !XMPPUtils.isDomainJID(uri.getPathSegments().get(1)))) { // empty result set and sync requested SyncAdapter.requestSync(getContext(), false); } if (Boolean.parseBoolean(uri.getQueryParameter(Users.EXTRA_INDEX)) && c.getCount() > 0) { UsersCursor uc = new UsersCursor(c); bundleFastScrollingIndexExtras(uc, uri, db, qb, selection, selectionArgs, sortOrder, null); c = uc; } c.setNotificationUri(getContext().getContentResolver(), uri); return c; }
From source file:com.money.manager.ex.home.DashboardFragment.java
private String prepareQueryTopWithdrawals() { SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); ViewMobileData mobileData = new ViewMobileData(getContext()); // data to compose builder String[] projectionIn = new String[] { "ROWID AS _id", ViewMobileData.CATEGID, ViewMobileData.Category, ViewMobileData.SubcategID, ViewMobileData.Subcategory, "SUM(" + ViewMobileData.AmountBaseConvRate + ") AS TOTAL", "COUNT(*) AS NUM" }; String selection = ViewMobileData.Status + "<>'V' AND " + ViewMobileData.TransactionType + " IN ('Withdrawal')" + " AND (julianday(date('now')) - julianday(" + ViewMobileData.Date + ") <= 30)"; String groupBy = ViewMobileData.CATEGID + ", " + ViewMobileData.Category + ", " + ViewMobileData.SubcategID + ", " + ViewMobileData.Subcategory; String having = "SUM(" + ViewMobileData.AmountBaseConvRate + ") < 0"; String sortOrder = "ABS(SUM(" + ViewMobileData.AmountBaseConvRate + ")) DESC"; String limit = "10"; // compose builder builder.setTables(mobileData.getSource()); // return query if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { return builder.buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit); } else {//from w w w .j a v a 2 s. c o m return builder.buildQuery(projectionIn, selection, null, groupBy, having, sortOrder, limit); } }
From source file:com.fututel.db.DBProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Constructs a new query builder and sets its table name SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); String finalSortOrder = sortOrder; String[] finalSelectionArgs = selectionArgs; String finalGrouping = null;//from w w w .jav a 2 s . c o m String finalHaving = null; int type = URI_MATCHER.match(uri); Uri regUri = uri; int remoteUid = Binder.getCallingUid(); int selfUid = android.os.Process.myUid(); if (remoteUid != selfUid) { if (type == ACCOUNTS || type == ACCOUNTS_ID) { for (String proj : projection) { if (proj.toLowerCase().contains(SipProfile.FIELD_DATA) || proj.toLowerCase().contains("*")) { throw new SecurityException("Password not readable from external apps"); } } } } Cursor c; long id; switch (type) { case ACCOUNTS: qb.setTables(SipProfile.ACCOUNTS_TABLE_NAME); if (sortOrder == null) { finalSortOrder = SipProfile.FIELD_PRIORITY + " ASC"; } break; case ACCOUNTS_ID: qb.setTables(SipProfile.ACCOUNTS_TABLE_NAME); qb.appendWhere(SipProfile.FIELD_ID + "=?"); finalSelectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { uri.getLastPathSegment() }); break; case CALLLOGS: qb.setTables(SipManager.CALLLOGS_TABLE_NAME); if (sortOrder == null) { finalSortOrder = CallLog.Calls.DATE + " DESC"; } break; case CALLLOGS_ID: qb.setTables(SipManager.CALLLOGS_TABLE_NAME); qb.appendWhere(CallLog.Calls._ID + "=?"); finalSelectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { uri.getLastPathSegment() }); break; case FILTERS: qb.setTables(SipManager.FILTERS_TABLE_NAME); if (sortOrder == null) { finalSortOrder = Filter.DEFAULT_ORDER; } break; case FILTERS_ID: qb.setTables(SipManager.FILTERS_TABLE_NAME); qb.appendWhere(Filter._ID + "=?"); finalSelectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { uri.getLastPathSegment() }); break; case MESSAGES: qb.setTables(SipMessage.MESSAGES_TABLE_NAME); if (sortOrder == null) { finalSortOrder = SipMessage.FIELD_DATE + " DESC"; } break; case MESSAGES_ID: qb.setTables(SipMessage.MESSAGES_TABLE_NAME); qb.appendWhere(SipMessage.FIELD_ID + "=?"); finalSelectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { uri.getLastPathSegment() }); break; case THREADS: qb.setTables(SipMessage.MESSAGES_TABLE_NAME); if (sortOrder == null) { finalSortOrder = SipMessage.FIELD_DATE + " DESC"; } projection = new String[] { "ROWID AS _id", SipMessage.FIELD_FROM, SipMessage.FIELD_FROM_FULL, SipMessage.FIELD_TO, "CASE " + "WHEN " + SipMessage.FIELD_FROM + "='SELF' THEN " + SipMessage.FIELD_TO + " WHEN " + SipMessage.FIELD_FROM + "!='SELF' THEN " + SipMessage.FIELD_FROM + " END AS message_ordering", SipMessage.FIELD_BODY, "MAX(" + SipMessage.FIELD_DATE + ") AS " + SipMessage.FIELD_DATE, "MIN(" + SipMessage.FIELD_READ + ") AS " + SipMessage.FIELD_READ, //SipMessage.FIELD_READ, "COUNT(" + SipMessage.FIELD_DATE + ") AS counter" }; //qb.appendWhere(SipMessage.FIELD_TYPE + " in (" + SipMessage.MESSAGE_TYPE_INBOX // + "," + SipMessage.MESSAGE_TYPE_SENT + ")"); finalGrouping = "message_ordering"; regUri = SipMessage.MESSAGE_URI; break; case THREADS_ID: qb.setTables(SipMessage.MESSAGES_TABLE_NAME); if (sortOrder == null) { finalSortOrder = SipMessage.FIELD_DATE + " DESC"; } projection = new String[] { "ROWID AS _id", SipMessage.FIELD_FROM, SipMessage.FIELD_TO, SipMessage.FIELD_BODY, SipMessage.FIELD_DATE, SipMessage.FIELD_MIME_TYPE, SipMessage.FIELD_TYPE, SipMessage.FIELD_STATUS, SipMessage.FIELD_FROM_FULL }; qb.appendWhere(MESSAGES_THREAD_SELECTION); String from = uri.getLastPathSegment(); finalSelectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs, new String[] { from, from }); regUri = SipMessage.MESSAGE_URI; break; case ACCOUNTS_STATUS: synchronized (profilesStatus) { ContentValues[] cvs = new ContentValues[profilesStatus.size()]; int i = 0; for (ContentValues ps : profilesStatus.values()) { cvs[i] = ps; i++; } c = getCursor(cvs); } if (c != null) { c.setNotificationUri(getContext().getContentResolver(), uri); } return c; case ACCOUNTS_STATUS_ID: id = ContentUris.parseId(uri); synchronized (profilesStatus) { ContentValues cv = profilesStatus.get(id); if (cv == null) { return null; } c = getCursor(new ContentValues[] { cv }); } c.setNotificationUri(getContext().getContentResolver(), uri); return c; default: throw new IllegalArgumentException(UNKNOWN_URI_LOG + uri); } SQLiteDatabase db = mOpenHelper.getReadableDatabase(); c = qb.query(db, projection, selection, finalSelectionArgs, finalGrouping, finalHaving, finalSortOrder); c.setNotificationUri(getContext().getContentResolver(), regUri); return c; }