是否可以在Join上创建表?

时间:2016-01-18 10:45:54

标签: android android-sqlite android-contentprovider

要查询tableloader的表连接,我在底部使用ContentProvider,但它在视图寻呼机内,因此经常重新创建。这需要相当长的时间来生成新的表连接 是否可以创建永久连接表,当源表获取数据更新时,它将自动更新其数据?

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;
  }
}

0 个答案:

没有答案