Ejemplo n.º 1
0
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    final SQLiteDatabase db = dbHelper.getWritableDatabase();
    final int match = uriMatcher.match(uri);
    int rowsUpdated;
    switch (match) {
      case BOOK:
        rowsUpdated =
            db.update(AlexandriaContract.BookEntry.TABLE_NAME, values, selection, selectionArgs);
        break;
      case AUTHOR:
        rowsUpdated =
            db.update(AlexandriaContract.AuthorEntry.TABLE_NAME, values, selection, selectionArgs);
        break;
      case CATEGORY:
        rowsUpdated =
            db.update(
                AlexandriaContract.CategoryEntry.TABLE_NAME, values, selection, selectionArgs);
        break;

      default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }
    if (rowsUpdated != 0) {
      getContext().getContentResolver().notifyChange(uri, null);
    }
    return rowsUpdated;
  }
Ejemplo n.º 2
0
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
   final SQLiteDatabase db = dbHelper.getWritableDatabase();
   final int match = uriMatcher.match(uri);
   int rowsDeleted;
   switch (match) {
     case BOOK:
       rowsDeleted = db.delete(AlexandriaContract.BookEntry.TABLE_NAME, selection, selectionArgs);
       break;
     case AUTHOR:
       rowsDeleted =
           db.delete(AlexandriaContract.AuthorEntry.TABLE_NAME, selection, selectionArgs);
       break;
     case CATEGORY:
       rowsDeleted =
           db.delete(AlexandriaContract.CategoryEntry.TABLE_NAME, selection, selectionArgs);
       break;
     case BOOK_ID:
       rowsDeleted =
           db.delete(
               AlexandriaContract.BookEntry.TABLE_NAME,
               AlexandriaContract.BookEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
               selectionArgs);
       break;
     default:
       throw new UnsupportedOperationException("Unknown uri: " + uri);
   }
   // Because a null deletes all rows
   if (selection == null || rowsDeleted != 0) {
     getContext().getContentResolver().notifyChange(uri, null);
   }
   return rowsDeleted;
 }
Ejemplo n.º 3
0
 @Override
 public Uri insert(Uri uri, ContentValues values) {
   final SQLiteDatabase db = dbHelper.getWritableDatabase();
   final int match = uriMatcher.match(uri);
   Uri returnUri;
   switch (match) {
     case QUEUE:
       {
         long _id = db.replace(AlexandriaContract.QueueEntry.TABLE_NAME, null, values);
         if (_id > 0) {
           returnUri = AlexandriaContract.QueueEntry.CONTENT_URI;
         } else {
           throw new android.database.SQLException("Failed to insert row into " + uri);
         }
         break;
       }
     case BOOK:
       {
         long _id = db.insert(AlexandriaContract.BookEntry.TABLE_NAME, null, values);
         if (_id > 0) {
           db.delete(
               AlexandriaContract.QueueEntry.TABLE_NAME,
               AlexandriaContract.QueueEntry._ID + " = '" + Long.toString(_id) + "'",
               null);
           returnUri = AlexandriaContract.BookEntry.buildBookUri(_id);
         } else {
           throw new android.database.SQLException("Failed to insert row into " + uri);
         }
         getContext()
             .getContentResolver()
             .notifyChange(AlexandriaContract.BookEntry.buildFullBookUri(_id), null);
         getContext()
             .getContentResolver()
             .notifyChange(AlexandriaContract.BookEntry.CONTENT_URI, null);
         break;
       }
     case AUTHOR:
       {
         long _id = db.insert(AlexandriaContract.AuthorEntry.TABLE_NAME, null, values);
         if (_id > 0)
           returnUri = AlexandriaContract.AuthorEntry.buildAuthorUri(values.getAsLong("_id"));
         else throw new android.database.SQLException("Failed to insert row into " + uri);
         break;
       }
     case CATEGORY:
       {
         long _id = db.insert(AlexandriaContract.CategoryEntry.TABLE_NAME, null, values);
         if (_id > 0)
           returnUri = AlexandriaContract.CategoryEntry.buildCategoryUri(values.getAsLong("_id"));
         else throw new android.database.SQLException("Failed to insert row into " + uri);
         break;
       }
     default:
       throw new UnsupportedOperationException("Unknown uri: " + uri);
   }
   return returnUri;
 }
Ejemplo n.º 4
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor retCursor;
    //        Log.v(LOG_TAG, "query, " + "uri = [" + uri + "], projection = [" + projection
    //                + "], selection = [" + selection + "], selectionArgs = [" + log(selectionArgs)
    //                + "], sortOrder = [" + sortOrder + "]");
    switch (uriMatcher.match(uri)) {
      case BOOK:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.BookEntry.TABLE_NAME,
                    projection,
                    selection,
                    //                        selection==null? null : selectionArgs,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case AUTHOR:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.AuthorEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case CATEGORY:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.CategoryEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case BOOK_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.BookEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.BookEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case AUTHOR_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.AuthorEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.AuthorEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case CATEGORY_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.CategoryEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.CategoryEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case BOOK_FULLDETAIL:
        String[] bfd_projection = {
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.TITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.SUBTITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.IMAGE_URL,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.DESC,
          "group_concat(DISTINCT "
              + AlexandriaContract.AuthorEntry.TABLE_NAME
              + "."
              + AlexandriaContract.AuthorEntry.AUTHOR
              + ") as "
              + AlexandriaContract.AuthorEntry.AUTHOR,
          "group_concat(DISTINCT "
              + AlexandriaContract.CategoryEntry.TABLE_NAME
              + "."
              + AlexandriaContract.CategoryEntry.CATEGORY
              + ") as "
              + AlexandriaContract.CategoryEntry.CATEGORY
        };
        retCursor =
            bookFull.query(
                dbHelper.getReadableDatabase(),
                bfd_projection,
                AlexandriaContract.BookEntry.TABLE_NAME
                    + "."
                    + AlexandriaContract.BookEntry._ID
                    + " = '"
                    + ContentUris.parseId(uri)
                    + "'",
                selectionArgs,
                AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry._ID,
                null,
                sortOrder);
        break;
      case BOOK_FULL:
        String[] bf_projection = {
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.TITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.IMAGE_URL,
          "group_concat(DISTINCT "
              + AlexandriaContract.AuthorEntry.TABLE_NAME
              + "."
              + AlexandriaContract.AuthorEntry.AUTHOR
              + ") as "
              + AlexandriaContract.AuthorEntry.AUTHOR,
          "group_concat(DISTINCT "
              + AlexandriaContract.CategoryEntry.TABLE_NAME
              + "."
              + AlexandriaContract.CategoryEntry.CATEGORY
              + ") as "
              + AlexandriaContract.CategoryEntry.CATEGORY
        };
        retCursor =
            bookFull.query(
                dbHelper.getReadableDatabase(),
                bf_projection,
                null,
                selectionArgs,
                AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry._ID,
                null,
                sortOrder);
        break;
      default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    retCursor.setNotificationUri(getContext().getContentResolver(), uri);

    return retCursor;
  }
Ejemplo n.º 5
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor retCursor;
    switch (uriMatcher.match(uri)) {
      case BOOK:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.BookEntry.TABLE_NAME,
                    projection,
                    selection,
                    selection == null ? null : selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case AUTHOR:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.AuthorEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case CATEGORY:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.CategoryEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
        break;
      case BOOK_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.BookEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.BookEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    null /*selectionArgs*/, // AlexSt: mistake - selectionArgs will be ignored as
                                            // there is no '?' in selection clause
                    null,
                    null,
                    sortOrder);
        break;
      case AUTHOR_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.AuthorEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.AuthorEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    null /*selectionArgs*/, // AlexSt: mistake - selectionArgs will be ignored as
                                            // there is no '?' in selection clause
                    null,
                    null,
                    sortOrder);
        break;
      case CATEGORY_ID:
        retCursor =
            dbHelper
                .getReadableDatabase()
                .query(
                    AlexandriaContract.CategoryEntry.TABLE_NAME,
                    projection,
                    AlexandriaContract.CategoryEntry._ID + " = '" + ContentUris.parseId(uri) + "'",
                    null /*selectionArgs*/, // AlexSt: mistake - selectionArgs will be ignored as
                                            // there is no '?' in selection clause
                    null,
                    null,
                    sortOrder);
        break;
      case BOOK_FULLDETAIL:
        String[] bfd_projection = {
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.TITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.SUBTITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.IMAGE_URL,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.DESC,
          "group_concat(DISTINCT "
              + AlexandriaContract.AuthorEntry.TABLE_NAME
              + "."
              + AlexandriaContract.AuthorEntry.AUTHOR
              + ") as "
              + AlexandriaContract.AuthorEntry.AUTHOR,
          "group_concat(DISTINCT "
              + AlexandriaContract.CategoryEntry.TABLE_NAME
              + "."
              + AlexandriaContract.CategoryEntry.CATEGORY
              + ") as "
              + AlexandriaContract.CategoryEntry.CATEGORY
        };
        retCursor =
            bookFull.query(
                dbHelper.getReadableDatabase(),
                bfd_projection,
                AlexandriaContract.BookEntry.TABLE_NAME
                    + "."
                    + AlexandriaContract.BookEntry._ID
                    + " = '"
                    + ContentUris.parseId(uri)
                    + "'",
                null /*selectionArgs*/, // AlexSt: mistake - selectionArgs will be ignored as there
                                        // is no '?' in selection clause
                AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry._ID,
                null,
                sortOrder);
        break;
      case BOOK_FULL:
        String[] bf_projection = {
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.TITLE,
          AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry.IMAGE_URL,
          "group_concat(DISTINCT "
              + AlexandriaContract.AuthorEntry.TABLE_NAME
              + "."
              + AlexandriaContract.AuthorEntry.AUTHOR
              + ") as "
              + AlexandriaContract.AuthorEntry.AUTHOR,
          "group_concat(DISTINCT "
              + AlexandriaContract.CategoryEntry.TABLE_NAME
              + "."
              + AlexandriaContract.CategoryEntry.CATEGORY
              + ") as "
              + AlexandriaContract.CategoryEntry.CATEGORY
        };
        retCursor =
            bookFull.query(
                dbHelper.getReadableDatabase(),
                bf_projection,
                null,
                null /*selectionArgs*/, // AlexSt: mistake - selectionArgs will be ignored as there
                                        // is no '?' in selection clause
                AlexandriaContract.BookEntry.TABLE_NAME + "." + AlexandriaContract.BookEntry._ID,
                null,
                sortOrder);
        break;
      default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    retCursor.setNotificationUri(getContext().getContentResolver(), uri);

    return retCursor;
  }
Ejemplo n.º 6
0
  public void testInsertReadDb() {
    DbHelper dbHelper = new DbHelper(mContext);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues values = getBookValues();

    long retEan = db.insert(AlexandriaContract.BookEntry.TABLE_NAME, null, values);
    assertEquals(ean, retEan);

    String[] columns = {
      AlexandriaContract.BookEntry._ID,
      AlexandriaContract.BookEntry.TITLE,
      AlexandriaContract.BookEntry.IMAGE_URL,
      AlexandriaContract.BookEntry.SUBTITLE,
      AlexandriaContract.BookEntry.DESC
    };

    // A cursor is your primary interface to the query results.
    Cursor cursor =
        db.query(
            AlexandriaContract.BookEntry.TABLE_NAME, // Table to Query
            columns,
            null, // Columns for the "where" clause
            null, // Values for the "where" clause
            null, // columns to group by
            null, // columns to filter by row groups
            null // sort order
            );

    validateCursor(cursor, values);
    values = getAuthorValues();

    retEan = db.insert(AlexandriaContract.AuthorEntry.TABLE_NAME, null, values);

    columns =
        new String[] {AlexandriaContract.AuthorEntry._ID, AlexandriaContract.AuthorEntry.AUTHOR};

    cursor =
        db.query(
            AlexandriaContract.AuthorEntry.TABLE_NAME, // Table to Query
            columns,
            null, // Columns for the "where" clause
            null, // Values for the "where" clause
            null, // columns to group by
            null, // columns to filter by row groups
            null // sort order
            );

    validateCursor(cursor, values);
    // test category table
    values = getCategoryValues();
    retEan = db.insert(AlexandriaContract.CategoryEntry.TABLE_NAME, null, values);

    columns =
        new String[] {
          AlexandriaContract.CategoryEntry._ID, AlexandriaContract.CategoryEntry.CATEGORY
        };

    cursor =
        db.query(
            AlexandriaContract.CategoryEntry.TABLE_NAME, // Table to Query
            columns,
            null, // Columns for the "where" clause
            null, // Values for the "where" clause
            null, // columns to group by
            null, // columns to filter by row groups
            null // sort order
            );

    validateCursor(cursor, values);
    dbHelper.close();
  }