List of usage examples for android.database.sqlite SQLiteQueryBuilder setTables
public void setTables(String inTables)
From source file:com.android.talkback.labeling.LabelProvider.java
/** * Queries for a label or multiple labels in the labels database. * * @param uri The URI representing the type of query to perform: * {@code LABELS_CONTENT_URI} for a subset of all labels, * {@code LABELS_ID_CONTENT_URI} for a specific label, or * {@code PACKAGE_SUMMARY} for a label count per package. * @param projection The columns to return. * @param selection The WHERE clause for the query. * @param selectionArgs The arguments for the WHERE clause of the query. * @param sortOrder the ORDER BY clause for the query. * @return A cursor representing the data resulting from the query, or] * {@code null} if the query failed to execute. *///from ww w. ja v a 2s. c o m @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if (uri == null) { LogUtils.log(this, Log.WARN, NULL_URI_FORMAT_STRING); return null; } if (!UserManagerCompat.isUserUnlocked(getContext())) { return null; } final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables(LabelsTable.TABLE_NAME); String groupBy = null; switch (sUriMatcher.match(uri)) { case LABELS: if (TextUtils.isEmpty(sortOrder)) { sortOrder = LabelsTable.KEY_ID; } break; case LABELS_ID: final String labelIdString = uri.getLastPathSegment(); final int labelId; try { labelId = Integer.parseInt(labelIdString); } catch (NumberFormatException e) { LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri); return null; } final String where = String.format(Locale.ROOT, "%s = %d", LabelsTable.KEY_ID, labelId); queryBuilder.appendWhere(where); break; case PACKAGE_SUMMARY: projection = new String[] { LabelsTable.KEY_PACKAGE_NAME, "COUNT(*)" }; groupBy = LabelsTable.KEY_PACKAGE_NAME; sortOrder = LabelsTable.KEY_PACKAGE_NAME; break; default: LogUtils.log(this, Log.WARN, UNKNOWN_URI_FORMAT_STRING, uri); return null; } initializeDatabaseIfNull(); return queryBuilder.query(mDatabase, projection, selection, selectionArgs, groupBy, null /* having */, sortOrder); }
From source file:edu.mit.mobile.android.locast.data.interfaces.LocatableUtils.java
public Cursor queryByLocation(SQLiteQueryBuilder qb, SQLiteDatabase db, String locString, String locatableItemTable, String[] projection, String selection, String[] selectionArgs, String sortOrder) {// w ww .ja va 2 s . c om qb.setTables(locatableItemTable); final Matcher m = LOC_STRING_REGEX.matcher(locString); if (!m.matches()) { throw new IllegalArgumentException("bad location string '" + locString + "'"); } final String lon = m.group(1); final String lat = m.group(2); String dist = "1500"; if (m.groupCount() == 3) { dist = m.group(3); } final String cell = GeocellUtils.compute(new Point(Double.valueOf(lat), Double.valueOf(lon)), 8); final List<String> adjacent = GeocellUtils.allAdjacents(cell); adjacent.add(cell); final StringBuilder selSb = new StringBuilder(); boolean join = false; for (int i = 0; i < adjacent.size(); i++) { if (join) { selSb.append(" OR "); } else { join = true; } selSb.append(Locatable.COL_GEOCELL); selSb.append(" LIKE ? || '%'"); } final String selectionExtra = selSb.toString(); return qb.query(db, projection, ProviderUtils.addExtraWhere(selection, selectionExtra), ProviderUtils.addExtraWhereArgs(selectionArgs, adjacent.toArray(new String[] {})), null, null, sortOrder); // String extraWhere = // "(lat - 2) > ? AND (lon - 2) > ? AND (lat + 2) < ? AND (lat + 2) < ?"; // final String[] extraArgs = {lat, lon}; // return qb.query(db, projection, ProviderUtils.addExtraWhere(selection, // LocatableUtils.SELECTION_LAT_LON), ProviderUtils.addExtraWhereArgs(selectionArgs, // extraArgs), // null, null, sortOrder); }
From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferDBBase.java
/** * Query records from the database.// w w w. j a v a 2 s.c o m * * @param uri A Uri indicating which part of data to query. * @param projection The projection of columns. * @param selection The "where" clause of sql. * @param selectionArgs Strings in the "where" clause. * @param sortOrder Sorting order of the query. * @param type Type of transfers to query. * @return A Cursor pointing to records. */ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); // TODO: currently all methods calling this pass null to projection. // In the future we want to update projection to be more specific for // performance and must handle that here. queryBuilder.setTables(TransferTable.TABLE_TRANSFER); final int uriType = uriMatcher.match(uri); switch (uriType) { case TRANSFERS: queryBuilder.appendWhere(TransferTable.COLUMN_PART_NUM + "=" + 0); break; case TRANSFER_ID: queryBuilder.appendWhere(TransferTable.COLUMN_ID + "=" + uri.getLastPathSegment()); break; case TRANSFER_PART: queryBuilder.appendWhere(TransferTable.COLUMN_MAIN_UPLOAD_ID + "=" + uri.getLastPathSegment()); break; case TRANSFER_STATE: queryBuilder.appendWhere(TransferTable.COLUMN_STATE + "="); queryBuilder.appendWhereEscapeString(uri.getLastPathSegment()); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } ensureDatabaseOpen(); final Cursor cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder); return cursor; }
From source file:com.hivewallet.androidclient.wallet.AddressBookProvider.java
@Override public Cursor query(final Uri uri, final String[] projection, final String originalSelection, final String[] originalSelectionArgs, final String sortOrder) { final SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(DATABASE_TABLE); final List<String> pathSegments = uri.getPathSegments(); if (pathSegments.size() > 1) throw new IllegalArgumentException(uri.toString()); String selection = null;//from w ww .ja va 2 s .c o m String[] selectionArgs = null; if (pathSegments.size() == 1) { final String address = uri.getLastPathSegment(); qb.appendWhere(KEY_ADDRESS + "="); qb.appendWhereEscapeString(address); } else if (SELECTION_IN.equals(originalSelection)) { final String[] addresses = originalSelectionArgs[0].trim().split(","); qb.appendWhere(KEY_ADDRESS + " IN ("); appendAddresses(qb, addresses); qb.appendWhere(")"); } else if (SELECTION_NOTIN.equals(originalSelection)) { final String[] addresses = originalSelectionArgs[0].trim().split(","); qb.appendWhere(KEY_ADDRESS + " NOT IN ("); appendAddresses(qb, addresses); qb.appendWhere(")"); } else if (SELECTION_QUERY.equals(originalSelection)) { final String query = '%' + originalSelectionArgs[0].trim() + '%'; selection = KEY_ADDRESS + " LIKE ? OR " + KEY_LABEL + " LIKE ?"; selectionArgs = new String[] { query, query }; } final Cursor cursor = qb.query(helper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; }
From source file:com.money.manager.ex.MmxContentProvider.java
private Cursor query_internal(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {//from w w w . j a v a2s . c o m Timber.d("Querying URI: %s", uri); // find object from uri Object sourceObject = getObjectFromUri(uri); initializeDependencies(); SQLiteDatabase database = openHelper.get().getReadableDatabase(); if (database == null) { Timber.e("Database could not be opened"); return null; } Cursor cursor; // check type of instance data set if (Dataset.class.isInstance(sourceObject)) { Dataset dataset = ((Dataset) sourceObject); // logQuery(dataset, projection, selection, selectionArgs, sortOrder); switch (dataset.getType()) { case QUERY: String query = prepareQuery(dataset.getSource(), projection, selection, sortOrder); cursor = database.rawQuery(query, selectionArgs); break; case SQL: cursor = database.rawQuery(selection, selectionArgs); break; case TABLE: case VIEW: SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder.setTables(dataset.getSource()); cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder); break; default: throw new IllegalArgumentException("Type of dataset not defined"); } } else { throw new IllegalArgumentException("Object sourceObject of mapContent is not instance of dataset"); } // notify listeners waiting for the data is ready cursor.setNotificationUri(getContext().getContentResolver(), uri); if (!cursor.isClosed()) { Timber.d("Rows returned: %d", cursor.getCount()); } return cursor; }
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 av a 2 s . 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;// w w w. j a va2s .c o 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:org.mitre.svmp.common.DatabaseHandler.java
private Cursor _getConnectionInfoCursor(String selection, String... selectionArgs) { SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); queryBuilder .setTables(String.format("%s NATURAL LEFT JOIN %s", Tables[TABLE_CONNECTIONS], Tables[TABLE_APPS])); // prepared statement for speed and security return queryBuilder.query(getDb(), new String[] { "ConnectionID", "Description", "Username", "Host", "Port", "EncryptionType", "AuthType", "CertificateAlias", "COUNT(PackageName)", "Status" }, // columns (null == "*") selection, // selection ('where' clause) selectionArgs, // selection args "ConnectionID", // group by null, // having "Description" // order by );/* www . j a v a 2 s .co m*/ }
From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { final int code = contentHelper.matchUri(uri); if (code != UriMatcher.NO_MATCH) { if (code == ContentHelper.uriSyncCode) { return null; }/*from www . j a v a 2 s.co m*/ SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); final TableInfo tab = contentHelper.getTableFromCode(code & ContentHelper.uriCode); builder.setTables(tab.name); if (isItemCode(code)) { if (isItemRowIDCode(code)) { selection = "isDeleted=0 AND ROWID=" + uri.getPathSegments().get(2) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } else { selection = "isDeleted=0" + tab.getSelection() + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ")" : ""); int i = 0; final String[] old = selectionArgs; final int len = (old == null) ? 0 : old.length; selectionArgs = new String[len + tab.primaryKey.length]; for (; i < tab.primaryKey.length; i++) { selectionArgs[i] = uri.getPathSegments().get(i + 1); } if (len > 0) { for (; i < old.length; i++) { selectionArgs[i] = old[i - tab.primaryKey.length]; } } } } else { selection = "isDeleted=0" + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } builder.setProjectionMap(tab.map); Cursor cursor = builder.query(getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); if (DEBUG) { Log.d("Query", builder.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null)); } cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; } throw new IllegalArgumentException("Unknown Uri " + uri); }
From source file:com.nadmm.airports.ActivityBase.java
public Cursor getAirportDetails(String siteNumber) { SQLiteDatabase db = getDatabase(DatabaseManager.DB_FADDS); SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); builder.setTables(Airports.TABLE_NAME + " a LEFT OUTER JOIN " + States.TABLE_NAME + " s" + " ON a." + Airports.ASSOC_STATE + "=s." + States.STATE_CODE); Cursor c = builder.query(db, new String[] { "*" }, Airports.SITE_NUMBER + "=?", new String[] { siteNumber }, null, null, null, null);/*ww w . j ava 2s . c o m*/ if (!c.moveToFirst()) { return null; } return c; }