0

To query joins of table for cursorloader, i use ContentProvider at the bottom, however it is inside view pager and thus often recreates. This take considerable time to generate new table join.
Is it possible to create permanent join table, which will be automaticly updated its data, when source table get data update?

public class ContactsWithCategorisProvider extends ContentProvider {

  private MySQLiteHelper database;

  // used for the UriMacher
  private static String TABLE_NAME = buildTableName();
  private static String TABLE_ID = ContactsTable._ID;
  private static String AUTHORITY = "app.ContactsWithCategorisProvider";

  private static final int ITEMS = 10;
  private static final int ITEM_ID = 20;
  private static final String BASE_PATH = "app";
  private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

  private static String buildTableName() {
    StringBuilder sb = new StringBuilder();
    sb.append(ContactsTable.TABLE_NAME);
    sb.append(" LEFT OUTER JOIN ");
    sb.append(ContactsCategoriesTable.TABLE_NAME);
    sb.append(" ON(");
    sb.append(ContactsCategoriesTable.CONTACT_ID);
    sb.append(" = ");
    sb.append(ContactsTable.CONTACT_ID);
    sb.append(" AND ");
    sb.append(ContactsCategoriesTable.CURRENT_USER);
    sb.append(" = ");
    sb.append(ContactsTable.CURRENT_USER);
    sb.append(") ");

    return sb.toString();
  }

  static {
    sURIMatcher.addURI(AUTHORITY, BASE_PATH, ITEMS);
    sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", ITEM_ID);
  }

  @Override public boolean onCreate() {
    database = new MySQLiteHelper(getContext());
    return true;
  }

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

    // Uisng SQLiteQueryBuilder instead of query() method
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    // check if the caller has requested a column which does not exists
    //      checkColumns(projection);
    queryBuilder.setTables(TABLE_NAME);
    if (projection == null || projection.length == 0) {
      projection = new String[] {
          ContactsTable.TABLE_NAME + "." + ContactsTable._ID + " AS " + ContactsTable._ID,
          ContactsTable.NAME, ContactsTable.NAME_SHORT,ContactsTable.NUMBER, ContactsTable.NOTE,
          ContactsTable.CATEGORIES, ContactsCategoriesTable.NAME, ContactsTable.CONTACT_ID,ContactsCategoriesTable.CATALOG_ID
      };
    }
    if (TextUtils.isEmpty(sortOrder)) {
      sortOrder =
          ContactsCategoriesTable.NAME + " = 'UNSORTED' DESC, " + ContactsCategoriesTable.NAME
              + " ASC, " + ContactsTable.NAME + " ASC ";
    }
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
      case ITEMS:
        break;
      case ITEM_ID:
        // adding the _ID to the original query
        queryBuilder.appendWhere(TABLE_ID + "=" + uri.getLastPathSegment());
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor =
        queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    // make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
  }

  @Override public String getType(Uri uri) {
    return null;
  }

  @Override public Uri insert(Uri uri, ContentValues values) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    long id;
    switch (uriType) {
      case ITEMS:
        id = sqlDB.insert(TABLE_NAME, null, values);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(BASE_PATH + "/" + id);
  }

  public void insert(Uri uri, ArrayList<ContentValues> values) {

    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    switch (uriType) {
      case ITEMS:
        for (ContentValues c : values) {
          sqlDB.insert(TABLE_NAME, null, c);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
  }

  @Override public int delete(Uri uri, String selection, String[] selectionArgs) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsDeleted = 0;
    switch (uriType) {
      case ITEMS:
        rowsDeleted = sqlDB.delete(TABLE_NAME, selection, selectionArgs);
        break;
      case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
          rowsDeleted = sqlDB.delete(TABLE_NAME, TABLE_ID + "=" + id, null);
        } else {
          rowsDeleted =
              sqlDB.delete(TABLE_NAME, TABLE_ID + "=" + id + " and " + selection, selectionArgs);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsDeleted;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsUpdated = 0;
    switch (uriType) {
      case ITEMS:
        rowsUpdated = sqlDB.update(TABLE_NAME, values, selection, selectionArgs);
        break;
      case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
          rowsUpdated = sqlDB.update(TABLE_NAME, values, TABLE_ID + "=" + id, null);
        } else {
          rowsUpdated = sqlDB.update(TABLE_NAME, values, TABLE_ID + "=" + id + " and " + selection,
              selectionArgs);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsUpdated;
  }
}
3
  • 1
    What you are looking for is called a View. Search for SQLite CREATE VIEW Jan 18, 2016 at 10:48
  • 1. choose new content uri(for join tables query) ... 2. let query method return "join query" with this new content's uri 3. in inert/updat/delete method notify the new URI in opertation connected with tables in join ... edit: [email protected] comment ... it will not automatically notify the View content if you would not add point 3 from my comment
    – Selvin
    Jan 18, 2016 at 10:49
  • 1
    @HrundiV.Bakshi substituting a predefined VIEW instead of raw statement, worked well
    – Yarh
    Jan 18, 2016 at 12:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.