Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions ProviGen/src/com/tjeannin/provigen/ProviGenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
Expand Down Expand Up @@ -244,10 +245,10 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
case ITEM_ID:
String itemId = String.valueOf(ContentUris.parseId(uri));
if (TextUtils.isEmpty(selection)) {
cursor = database.query(contractHolder.getTable(), projection, contractHolder.getIdField() + " = ? ", new String[] { itemId }, "", "", sortOrder);
cursor = database.query(contractHolder.getTable(), projection, contractHolder.getIdField() + " = ? ", new String[]{itemId}, "", "", sortOrder);
} else {
cursor = database.query(contractHolder.getTable(), projection, selection + " AND " + contractHolder.getIdField() + " = ? ",
appendToStringArray(selectionArgs, itemId), "", "", sortOrder);
appendToStringArray(selectionArgs, itemId), "", "", sortOrder);
}
break;
default:
Expand Down Expand Up @@ -306,4 +307,34 @@ private static String[] appendToStringArray(String[] array, String element) {
}
}

/**
* Efficient bulk insert using sql transaction
* @param uri
* @param values
* @return the number of rows inserted
*/
public int bulkInsert(Uri uri, ContentValues[] values){
int numInserted = 0;

ContractHolder contractHolder = contracts.findMatching(uri);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Provigen 2.0.0.
This line and the next one can be replaced by
String table = uri.getPathSegments().get(0);

String table = contractHolder.getTable();

SQLiteDatabase sqlDB = openHelper.getWritableDatabase();
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);
numInserted = values.length;
} finally {
sqlDB.endTransaction();
}
return numInserted;
}

}