Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replaced SQL table String literals with a public static constant for

better code reuse. Still not sure if I want to put the columns in there
too because that might bloat the code. NodeProvider is also good to go.
  • Loading branch information...
commit 7e62b498450111a061c920787622c9acf40e561a 1 parent 7e3528e
@SeanPONeil authored
View
64 src/com/atami/mgodroid/provider/NodeIndicesProvider.java
@@ -17,6 +17,8 @@
public static final Uri NODE_INDICES_URI = Uri
.parse("content://com.mgoblog.nodeprovider/node_indices");
+
+ public static final String TABLE = "node_indices";
private static final int ALLROWS = 1;
private static final int SINGLE_ROW = 2;
@@ -24,8 +26,8 @@
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- uriMatcher.addURI("com.mgoblog.nodeprovider", "node_indices", ALLROWS);
- uriMatcher.addURI("com.mgoblog.nodeprovider", "node_indices/#",
+ uriMatcher.addURI("com.mgoblog.nodeprovider", TABLE, ALLROWS);
+ uriMatcher.addURI("com.mgoblog.nodeprovider", TABLE + "/#",
SINGLE_ROW);
}
@@ -55,7 +57,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
}
// Execute the deletion
- int deleteCount = db.getWritableDatabase().delete("node_indices",
+ int deleteCount = db.getWritableDatabase().delete(TABLE,
selection, selectionArgs);
// Notify any observers of the change in the data set
@@ -66,9 +68,9 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
@Override
public Uri insert(Uri uri, ContentValues values) {
- long id = db.getWritableDatabase().insert("node_indices", null, values);
- if(id > -1){
- //Construct and return the URI of the newly inserted row.
+ long id = db.getWritableDatabase().insert(TABLE, null, values);
+ if (id > -1) {
+ // Construct and return the URI of the newly inserted row.
Uri insertedId = ContentUris.withAppendedId(NODE_INDICES_URI, id);
getContext().getContentResolver().notifyChange(insertedId, null);
return insertedId;
@@ -79,21 +81,21 @@ public Uri insert(Uri uri, ContentValues values) {
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
SQLiteDatabase sqlDB = db.getWritableDatabase();
- sqlDB.beginTransaction();
- try {
-
- for (ContentValues cv : values) {
- long newID = sqlDB.insertOrThrow("node_indices", null, cv);
- if (newID <= 0) {
- throw new SQLException("Failed to insert row into " + uri);
- }
- }
- sqlDB.setTransactionSuccessful();
- getContext().getContentResolver().notifyChange(uri, null);
- } finally {
- sqlDB.endTransaction();
- }
- return values.length;
+ sqlDB.beginTransaction();
+ try {
+
+ for (ContentValues cv : values) {
+ long newID = sqlDB.insertOrThrow(TABLE, null, cv);
+ if (newID <= 0) {
+ throw new SQLException("Failed to insert row into " + uri);
+ }
+ }
+ sqlDB.setTransactionSuccessful();
+ getContext().getContentResolver().notifyChange(uri, null);
+ } finally {
+ sqlDB.endTransaction();
+ }
+ return values.length;
}
@Override
@@ -101,7 +103,7 @@ public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
- queryBuilder.setTables("node_indices");
+ queryBuilder.setTables(TABLE);
int uriType = uriMatcher.match(uri);
switch (uriType) {
@@ -121,8 +123,22 @@ public Cursor query(Uri uri, String[] projection, String selection,
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
- // TODO Auto-generated method stub
- return 0;
+ switch (uriMatcher.match(uri)) {
+ case SINGLE_ROW:
+ String rowID = uri.getPathSegments().get(1);
+ selection = "_id = "
+ + rowID
+ + (!TextUtils.isEmpty(selection) ? " AND (" + selection
+ + ')' : "");
+ default:
+ break;
+ }
+
+ int updateCount = db.getWritableDatabase().update(TABLE, values,
+ selection, selectionArgs);
+
+ getContext().getContentResolver().notifyChange(uri, null);
+ return updateCount;
}
@Override
View
136 src/com/atami/mgodroid/provider/NodeProvider.java
@@ -0,0 +1,136 @@
+package com.atami.mgodroid.provider;
+
+import android.content.ContentProvider;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.UriMatcher;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteQueryBuilder;
+import android.net.Uri;
+import android.text.TextUtils;
+
+public class NodeProvider extends ContentProvider {
+
+ private NodeDatabase db;
+
+ public static final Uri NODES_URI = Uri
+ .parse("content://com.mgoblog.nodeprovider/nodes");
+
+ public static final String TABLE = "nodes";
+
+ private static final int ALLROWS = 1;
+ private static final int SINGLE_ROW = 2;
+
+ private static final UriMatcher uriMatcher;
+ static {
+ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+ uriMatcher.addURI("com.mgoblog.nodeprovider", "nodes", ALLROWS);
+ uriMatcher.addURI("com.mgoblog.nodeprovider", "nodes/#",
+ SINGLE_ROW);
+ }
+
+ @Override
+ public boolean onCreate() {
+ db = new NodeDatabase(getContext());
+ return true;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ switch (uriMatcher.match(uri)) {
+ case SINGLE_ROW:
+ String rowID = uri.getPathSegments().get(1);
+ selection = "_id = "
+ + rowID
+ + (!TextUtils.isEmpty(selection) ? " AND (" + selection
+ + ')' : "");
+ default:
+ break;
+ }
+
+ // To return the number of deleted items, you must specify a where
+ // clause. To delete all rows and return a value, pass in "1"
+ if (selection == null) {
+ selection = "1";
+ }
+
+ // Execute the deletion
+ int deleteCount = db.getWritableDatabase().delete(TABLE,
+ selection, selectionArgs);
+
+ // Notify any observers of the change in the data set
+ getContext().getContentResolver().notifyChange(uri, null);
+
+ return deleteCount;
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values) {
+ long id = db.getWritableDatabase().insert(TABLE, null, values);
+ if(id > -1){
+ //Construct and return the URI of the newly inserted row.
+ Uri insertedId = ContentUris.withAppendedId(NODES_URI, id);
+ getContext().getContentResolver().notifyChange(insertedId, null);
+ return insertedId;
+ }
+ return null;
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection,
+ String[] selectionArgs, String sortOrder) {
+
+ SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
+ queryBuilder.setTables(TABLE);
+
+ int uriType = uriMatcher.match(uri);
+ switch (uriType) {
+ case SINGLE_ROW:
+ String rowID = uri.getPathSegments().get(1);
+ queryBuilder.appendWhere("_id = " + rowID);
+ default:
+ break;
+ }
+
+ Cursor cursor = queryBuilder.query(db.getReadableDatabase(),
+ projection, selection, selectionArgs, null, null, sortOrder);
+ cursor.setNotificationUri(getContext().getContentResolver(), uri);
+ return cursor;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection,
+ String[] selectionArgs) {
+ switch (uriMatcher.match(uri)) {
+ case SINGLE_ROW:
+ String rowID = uri.getPathSegments().get(1);
+ selection = "_id = "
+ + rowID
+ + (!TextUtils.isEmpty(selection) ? " AND (" + selection
+ + ')' : "");
+ default:
+ break;
+ }
+
+ int updateCount = db.getWritableDatabase().update(TABLE, values,
+ selection, selectionArgs);
+
+ getContext().getContentResolver().notifyChange(uri, null);
+ return updateCount;
+ }
+
+ @Override
+ public String getType(Uri uri) {
+ // Return a string that identifies the MIME type
+ // for a Content Provider URI
+ switch (uriMatcher.match(uri)) {
+ case ALLROWS:
+ return "vnd.android.cursor.dir/vnd.mgoblog.node";
+ case SINGLE_ROW:
+ return "vnd.android.cursor.item/vnd.mgoblog.node";
+ default:
+ throw new IllegalArgumentException("Unsupported URI: " + uri);
+ }
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.