List of usage examples for android.database Cursor setNotificationUri
void setNotificationUri(ContentResolver cr, Uri uri);
From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java
public Cursor queryFeedList(String realAppId, String[] projection, String selection, String[] selectionArgs, String sortOrder) {//w w w . j av a 2 s . c o m /*return getReadableDatabase().rawQuery("SELECT * FROM Group.TABLE, DBObject.TABLE WHERE Group.IS_CHILD_FEED != 1 AND Group.TABLE + "." + Group.LAST_OBJECT_ID = DBObject.TABLE + "." DBObject._ID ORDER BY Group.LAST_UPDATED DESC");*/ ContentResolver resolver = mContext.getContentResolver(); final String OBJECTS = DbObject.TABLE; final String GROUPS = Group.TABLE; String tables = GROUPS + ", " + OBJECTS; String selection2 = GROUPS + "." + Group.PARENT_FEED_ID + " = -1 " + " AND " + GROUPS + "." + Group.LAST_OBJECT_ID + " = " + OBJECTS + "." + DbObject._ID; if (!DungBeetleContentProvider.SUPER_APP_ID.equals(realAppId)) { selection2 += " AND " + OBJECTS + "." + DbObject.APP_ID + " = ?"; selectionArgs = andArguments(selectionArgs, new String[] { realAppId }); } selection = andClauses(selection, selection2); if (sortOrder == null) { sortOrder = Group.LAST_UPDATED + " DESC"; } Cursor c = getReadableDatabase().query(tables, projection, selection, selectionArgs, null, null, sortOrder, null); c.setNotificationUri(resolver, Feed.feedListUri()); return c; }
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;// w ww . j a v a 2s. 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; }
From source file:com.sonetel.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;/* w ww . ja va2 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; case ACCESSNO: qb.setTables(SipProfile.ACCESS_NUMS_TABLE_NAME); break; 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; }
From source file:com.csipsimple.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. j a v a 2s . c o m*/ String finalHaving = null; int type = URI_MATCHER.match(uri); Uri regUri = uri; // Security check to avoid data retrieval from outside 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"); } } } } // Security check to avoid project of invalid fields or lazy projection List<String> possibles = getPossibleFieldsForType(type); if (possibles == null) { throw new SecurityException("You are asking wrong values " + type); } checkProjection(possibles, projection); checkSelection(possibles, selection); 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; }
From source file:org.opendatakit.common.android.provider.impl.InstanceProviderImpl.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { List<String> segments = uri.getPathSegments(); if (segments.size() < 2 || segments.size() > 3) { throw new SQLException("Unknown URI (too many segments!) " + uri); }//ww w .j a v a 2 s.co m String appName = segments.get(0); ODKFileUtils.verifyExternalStorageAvailability(); ODKFileUtils.assertDirectoryStructure(appName); String tableId = segments.get(1); String instanceName = null; // _ID in UPLOADS_TABLE_NAME String instanceId = (segments.size() == 3 ? segments.get(2) : null); SQLiteDatabase db = null; String fullQuery; String filterArgs[]; Cursor c = null; String dbTableName; List<ColumnDefinition> orderedDefns; StringBuilder b = new StringBuilder(); try { db = DatabaseFactory.get().getDatabase(getContext(), appName); db.beginTransaction(); boolean success = false; try { success = ODKDatabaseUtils.get().hasTableId(db, tableId); } catch (Exception e) { e.printStackTrace(); throw new SQLException("Unknown URI (exception testing for tableId) " + uri); } if (!success) { throw new SQLException("Unknown URI (missing data table for tableId) " + uri); } dbTableName = "\"" + tableId + "\""; try { c = db.query(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, new String[] { KeyValueStoreColumns.VALUE }, KeyValueStoreColumns.TABLE_ID + "=? AND " + KeyValueStoreColumns.PARTITION + "=? AND " + KeyValueStoreColumns.ASPECT + "=? AND " + KeyValueStoreColumns.KEY + "=?", new String[] { tableId, KeyValueStoreConstants.PARTITION_TABLE, KeyValueStoreConstants.ASPECT_DEFAULT, KeyValueStoreConstants.XML_INSTANCE_NAME }, null, null, null); if (c.getCount() == 1) { c.moveToFirst(); int idxInstanceName = c.getColumnIndex(KeyValueStoreColumns.VALUE); instanceName = c.getString(idxInstanceName); } } finally { c.close(); } // ARGH! we must ensure that we have records in our UPLOADS_TABLE_NAME // for every distinct instance in the data table. b.setLength(0); //@formatter:off b.append("INSERT INTO ").append(DatabaseConstants.UPLOADS_TABLE_NAME).append("(") .append(InstanceColumns.DATA_INSTANCE_ID).append(",") .append(InstanceColumns.DATA_TABLE_TABLE_ID).append(") ").append("SELECT ") .append(InstanceColumns.DATA_INSTANCE_ID).append(",") .append(InstanceColumns.DATA_TABLE_TABLE_ID).append(" FROM (").append("SELECT DISTINCT ") .append(DATA_TABLE_ID_COLUMN).append(" as ").append(InstanceColumns.DATA_INSTANCE_ID) .append(",").append("? as ").append(InstanceColumns.DATA_TABLE_TABLE_ID).append(" FROM ") .append(dbTableName).append(" EXCEPT SELECT DISTINCT ").append(InstanceColumns.DATA_INSTANCE_ID) .append(",").append(InstanceColumns.DATA_TABLE_TABLE_ID).append(" FROM ") .append(DatabaseConstants.UPLOADS_TABLE_NAME).append(")"); //@formatter:on // TODO: should we collapse across FORM_ID or leave it this way? String[] args = { tableId }; db.execSQL(b.toString(), args); // Can't get away with dataTable.* because of collision with _ID column // get map of (elementKey -> ColumnDefinition) try { orderedDefns = TableUtil.get().getColumnDefinitions(db, appName, tableId); } catch (IllegalArgumentException e) { e.printStackTrace(); throw new SQLException("Unable to retrieve column definitions for tableId " + tableId); } db.setTransactionSuccessful(); } finally { if (db != null) { db.endTransaction(); db.close(); } } //////////////////////////////////////////////////////////////// // OK we have the info we need -- now build the query we want... // We can now join through and access the data table rows b.setLength(0); // @formatter:off b.append("SELECT "); b.append(DatabaseConstants.UPLOADS_TABLE_NAME).append(".").append(InstanceColumns._ID).append(" as ") .append(InstanceColumns._ID).append(",").append(DatabaseConstants.UPLOADS_TABLE_NAME).append(".") .append(InstanceColumns.DATA_INSTANCE_ID).append(" as ").append(InstanceColumns.DATA_INSTANCE_ID) .append(",").append(DatabaseConstants.UPLOADS_TABLE_NAME).append(".") .append(InstanceColumns.SUBMISSION_INSTANCE_ID).append(" as ") .append(InstanceColumns.SUBMISSION_INSTANCE_ID).append(","); // add the dataTable metadata except for _ID (which conflicts with InstanceColumns._ID) b.append(dbTableName).append(".").append(DataTableColumns.ROW_ETAG).append(" as ") .append(DataTableColumns.ROW_ETAG).append(",").append(dbTableName).append(".") .append(DataTableColumns.SYNC_STATE).append(" as ").append(DataTableColumns.SYNC_STATE).append(",") .append(dbTableName).append(".").append(DataTableColumns.CONFLICT_TYPE).append(" as ") .append(DataTableColumns.CONFLICT_TYPE).append(",").append(dbTableName).append(".") .append(DataTableColumns.FILTER_TYPE).append(" as ").append(DataTableColumns.FILTER_TYPE) .append(",").append(dbTableName).append(".").append(DataTableColumns.FILTER_VALUE).append(" as ") .append(DataTableColumns.FILTER_VALUE).append(",").append(dbTableName).append(".") .append(DataTableColumns.FORM_ID).append(" as ").append(DataTableColumns.FORM_ID).append(",") .append(dbTableName).append(".").append(DataTableColumns.LOCALE).append(" as ") .append(DataTableColumns.LOCALE).append(",").append(dbTableName).append(".") .append(DataTableColumns.SAVEPOINT_TYPE).append(" as ").append(DataTableColumns.SAVEPOINT_TYPE) .append(",").append(dbTableName).append(".").append(DataTableColumns.SAVEPOINT_TIMESTAMP) .append(" as ").append(DataTableColumns.SAVEPOINT_TIMESTAMP).append(",").append(dbTableName) .append(".").append(DataTableColumns.SAVEPOINT_CREATOR).append(" as ") .append(DataTableColumns.SAVEPOINT_CREATOR).append(","); // add the user-specified data fields in this dataTable for (ColumnDefinition cd : orderedDefns) { if (cd.isUnitOfRetention()) { b.append(dbTableName).append(".").append(cd.getElementKey()).append(" as ") .append(cd.getElementKey()).append(","); } } b.append("CASE WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" IS NULL THEN null") .append(" WHEN ").append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" IS NULL THEN null") .append(" WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" > ") .append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" THEN null").append(" ELSE ") .append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" END as ") .append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(","); b.append("CASE WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" IS NULL THEN null") .append(" WHEN ").append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" IS NULL THEN null") .append(" WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" > ") .append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" THEN null").append(" ELSE ") .append(InstanceColumns.XML_PUBLISH_STATUS).append(" END as ") .append(InstanceColumns.XML_PUBLISH_STATUS).append(","); b.append("CASE WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" IS NULL THEN null") .append(" WHEN ").append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" IS NULL THEN null") .append(" WHEN ").append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(" > ") .append(InstanceColumns.XML_PUBLISH_TIMESTAMP).append(" THEN null").append(" ELSE ") .append(InstanceColumns.DISPLAY_SUBTEXT).append(" END as ").append(InstanceColumns.DISPLAY_SUBTEXT) .append(","); if (instanceName == null) { b.append(DataTableColumns.SAVEPOINT_TIMESTAMP); } else { b.append(instanceName); } b.append(" as ").append(InstanceColumns.DISPLAY_NAME); b.append(" FROM "); b.append("( SELECT * FROM ").append(dbTableName).append(" AS T WHERE T.") .append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append("=(SELECT MAX(V.") .append(DATA_TABLE_SAVEPOINT_TIMESTAMP_COLUMN).append(") FROM ").append(dbTableName) .append(" AS V WHERE V.").append(DATA_TABLE_ID_COLUMN).append("=T.").append(DATA_TABLE_ID_COLUMN) .append(" AND V.").append(DATA_TABLE_SAVEPOINT_TYPE_COLUMN).append(" IS NOT NULL").append(")") .append(") as ").append(dbTableName); b.append(" JOIN ").append(DatabaseConstants.UPLOADS_TABLE_NAME).append(" ON ").append(dbTableName) .append(".").append(DATA_TABLE_ID_COLUMN).append("=").append(DatabaseConstants.UPLOADS_TABLE_NAME) .append(".").append(InstanceColumns.DATA_INSTANCE_ID).append(" AND ").append("? =") .append(DatabaseConstants.UPLOADS_TABLE_NAME).append(".") .append(InstanceColumns.DATA_TABLE_TABLE_ID); b.append(" WHERE ").append(DATA_TABLE_SAVEPOINT_TYPE_COLUMN).append("=?"); // @formatter:on if (instanceId != null) { b.append(" AND ").append(DatabaseConstants.UPLOADS_TABLE_NAME).append(".").append(InstanceColumns._ID) .append("=?"); String tempArgs[] = { tableId, InstanceColumns.STATUS_COMPLETE, instanceId }; filterArgs = tempArgs; } else { String tempArgs[] = { tableId, InstanceColumns.STATUS_COMPLETE }; filterArgs = tempArgs; } if (selection != null) { b.append(" AND (").append(selection).append(")"); } if (selectionArgs != null) { String[] tempArgs = new String[filterArgs.length + selectionArgs.length]; for (int i = 0; i < filterArgs.length; ++i) { tempArgs[i] = filterArgs[i]; } for (int i = 0; i < selectionArgs.length; ++i) { tempArgs[filterArgs.length + i] = selectionArgs[i]; } filterArgs = tempArgs; } if (sortOrder != null) { b.append(" ORDER BY ").append(sortOrder); } fullQuery = b.toString(); db = null; boolean success = false; try { db = DatabaseFactory.get().getDatabase(getContext(), appName); c = db.rawQuery(fullQuery, filterArgs); // Tell the cursor what uri to watch, so it knows when its source data // changes c.setNotificationUri(getContext().getContentResolver(), uri); success = true; return c; } finally { if (db != null && !success) { // leave database open for cursor... db.close(); } } }