Created
May 30, 2015 13:00
-
-
Save anonymous/b290fbaa2947982fd28d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.bitfrom.chemtools.data; | |
import android.annotation.TargetApi; | |
import android.content.ContentProvider; | |
import android.content.ContentValues; | |
import android.content.UriMatcher; | |
import android.database.Cursor; | |
import android.database.SQLException; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteQueryBuilder; | |
import android.net.Uri; | |
import android.support.annotation.NonNull; | |
import android.text.TextUtils; | |
import static me.bitfrom.chemtools.data.ChemToolsContract.*; | |
/** | |
* Created by Constantine with love. | |
*/ | |
public class ChemToolsProvider extends ContentProvider { | |
private static final String LOG_TAG = ChemToolsProvider.class.getSimpleName(); | |
private ChemToolsDbHelper mOpenHelper; | |
// The URI Matcher used by this content provider. | |
private static final UriMatcher sUriMatcher = buildUriMatcher(); | |
static final int OBJECTS = 1000; | |
static final int OBJECT_ID = 1001; | |
static final int SECTORS = 2000; | |
static final int SECTOR_ID = 2001; | |
static final int WELLS = 3000; | |
static final int WELL_ID = 3001; | |
static final int WATER_LEVELS = 4000; | |
static final int WATER_LEVEL_ID = 4001; | |
static final int WATER_CHEMISTRY = 5000; | |
static final int WATER_CHEMISTRY_ID = 5001; | |
static final int INDICATORS = 6000; | |
static final int INDICATOR_ID = 6001; | |
static final int JOB_TITLES = 7000; | |
static final int JOB_TITLE_ID = 7001; | |
static final int PERFORMERS = 8000; | |
static final int PERFORMER_ID = 8001; | |
static UriMatcher buildUriMatcher() { | |
// 1) The code passed into the constructor represents the code to return for the root | |
// URI. It's common to use NO_MATCH as the code for this case. Add the constructor below. | |
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); | |
final String authority = CONTENT_AUTHORITY; | |
// 2) Use the addURI function to match each of the types. | |
matcher.addURI(authority, PATH_OBJECTS, OBJECTS); | |
matcher.addURI(authority, PATH_OBJECTS + "/#", OBJECT_ID); | |
matcher.addURI(authority, PATH_SECTORS, SECTORS); | |
matcher.addURI(authority, PATH_SECTORS + "/#", SECTOR_ID); | |
matcher.addURI(authority, PATH_WELLS, WELLS); | |
matcher.addURI(authority, PATH_WELLS + "/#", WELL_ID); | |
matcher.addURI(authority, PATH_WATER_LEVELS, WATER_LEVELS); | |
matcher.addURI(authority, PATH_WATER_LEVELS + "/#", WATER_LEVEL_ID); | |
matcher.addURI(authority, PATH_WATER_CHEMISTRY, WATER_CHEMISTRY); | |
matcher.addURI(authority, PATH_WATER_CHEMISTRY + "/#", WATER_CHEMISTRY_ID); | |
matcher.addURI(authority, PATH_INDICATORS, INDICATORS); | |
matcher.addURI(authority, PATH_INDICATORS + "/#", INDICATOR_ID); | |
matcher.addURI(authority, PATH_JOB_TITLES, JOB_TITLES); | |
matcher.addURI(authority, PATH_JOB_TITLES + "/#", JOB_TITLE_ID); | |
matcher.addURI(authority, PATH_PERFORMERS, PERFORMERS); | |
matcher.addURI(authority, PATH_PERFORMERS + "/#", PERFORMER_ID); | |
return matcher; | |
} | |
@Override | |
public String getType(Uri uri) { | |
// Use the Uri Matcher to determine what kind of URI this is. | |
final int match = sUriMatcher.match(uri); | |
switch (match) { | |
case OBJECTS: | |
return ObjectsEntry.CONTENT_TYPE; | |
case OBJECT_ID: | |
return ObjectsEntry.CONTENT_ITEM_TYPE; | |
case SECTORS: | |
return SectorsEntry.CONTENT_TYPE; | |
case SECTOR_ID: | |
return SectorsEntry.CONTENT_ITEM_TYPE; | |
case WELLS: | |
return WellsEntry.CONTENT_TYPE; | |
case WELL_ID: | |
return WellsEntry.CONTENT_ITEM_TYPE; | |
case WATER_LEVELS: | |
return WaterLevelsEntry.CONTENT_TYPE; | |
case WATER_LEVEL_ID: | |
return WaterLevelsEntry.CONTENT_ITEM_TYPE; | |
case WATER_CHEMISTRY: | |
return WaterChemistryEntry.CONTENT_TYPE; | |
case WATER_CHEMISTRY_ID: | |
return WaterChemistryEntry.CONTENT_ITEM_TYPE; | |
case INDICATORS: | |
return IndicatorsEntry.CONTENT_TYPE; | |
case INDICATOR_ID: | |
return IndicatorsEntry.CONTENT_ITEM_TYPE; | |
case JOB_TITLES: | |
return JobTitlesEntry.CONTENT_TYPE; | |
case JOB_TITLE_ID: | |
return JobTitlesEntry.CONTENT_ITEM_TYPE; | |
case PERFORMERS: | |
return PerformersEntry.CONTENT_TYPE; | |
case PERFORMER_ID: | |
return PerformersEntry.CONTENT_ITEM_TYPE; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
} | |
@Override | |
public boolean onCreate() { | |
mOpenHelper = new ChemToolsDbHelper(getContext()); | |
return true; | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
Cursor resultCursor; | |
final int match = sUriMatcher.match(uri); | |
switch (match) { | |
case OBJECTS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
ObjectsEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case SECTORS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
SectorsEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case WELLS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
WellsEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case WATER_LEVELS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
WaterLevelsEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case WATER_CHEMISTRY: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
WaterChemistryEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case INDICATORS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
IndicatorsEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case JOB_TITLES: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
JobTitlesEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
case PERFORMERS: { | |
resultCursor = mOpenHelper.getReadableDatabase().query( | |
PerformersEntry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri " + uri); | |
} | |
resultCursor.setNotificationUri(getContext().getContentResolver(), uri); | |
return resultCursor; | |
} | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
Uri returnUri; | |
switch (match) { | |
case OBJECTS: { | |
long _id = db.insert(ObjectsEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = ObjectsEntry.buildObjectUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case SECTORS: { | |
long _id = db.insert(SectorsEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = SectorsEntry.buildSectorUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case WELLS: { | |
long _id = db.insert(WellsEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = WellsEntry.buildWellUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case WATER_LEVELS: { | |
long _id = db.insert(WaterLevelsEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = WaterLevelsEntry.buildWaterLevelUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case WATER_CHEMISTRY: { | |
long _id = db.insert(WaterChemistryEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = WaterLevelsEntry.buildWaterLevelUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case INDICATORS: { | |
long _id = db.insert(IndicatorsEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = IndicatorsEntry.buildIndicatorUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case JOB_TITLES: { | |
long _id = db.insert(JobTitlesEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = JobTitlesEntry.buildJobTitleUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case PERFORMERS: { | |
long _id = db.insert(PerformersEntry.TABLE_NAME, null, values); | |
if (_id > 0) | |
returnUri = PerformersEntry.buildPerformerUri(_id); | |
else | |
throw new SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri " + uri); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return returnUri; | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
int rowsDeleted; | |
switch (match) { | |
case OBJECTS: { | |
rowsDeleted = db.delete(ObjectsEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, ObjectsEntry.TABLE_NAME); | |
break; | |
} | |
case OBJECT_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(ObjectsEntry.TABLE_NAME, ObjectsEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(ObjectsEntry.TABLE_NAME, ObjectsEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case SECTORS: { | |
rowsDeleted = db.delete(SectorsEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, SectorsEntry.TABLE_NAME); | |
break; | |
} | |
case SECTOR_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(SectorsEntry.TABLE_NAME, SectorsEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(SectorsEntry.TABLE_NAME, SectorsEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case WELLS: { | |
rowsDeleted = db.delete(WellsEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, WellsEntry.TABLE_NAME); | |
break; | |
} | |
case WELL_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(WellsEntry.TABLE_NAME, WellsEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(WellsEntry.TABLE_NAME, WellsEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case WATER_LEVELS: { | |
rowsDeleted = db.delete(WaterLevelsEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, WaterLevelsEntry.TABLE_NAME); | |
break; | |
} | |
case WATER_LEVEL_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(WaterLevelsEntry.TABLE_NAME, WaterLevelsEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(WaterLevelsEntry.TABLE_NAME, WaterLevelsEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case WATER_CHEMISTRY: { | |
rowsDeleted = db.delete(WaterChemistryEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, WaterChemistryEntry.TABLE_NAME); | |
break; | |
} | |
case WATER_CHEMISTRY_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(WaterChemistryEntry.TABLE_NAME, WaterChemistryEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(WaterChemistryEntry.TABLE_NAME, WaterChemistryEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case INDICATORS: { | |
rowsDeleted = db.delete(IndicatorsEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, IndicatorsEntry.TABLE_NAME); | |
break; | |
} | |
case INDICATOR_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(IndicatorsEntry.TABLE_NAME, IndicatorsEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(IndicatorsEntry.TABLE_NAME, IndicatorsEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case JOB_TITLES: { | |
rowsDeleted = db.delete(JobTitlesEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, JobTitlesEntry.TABLE_NAME); | |
break; | |
} | |
case JOB_TITLE_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(JobTitlesEntry.TABLE_NAME, JobTitlesEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(JobTitlesEntry.TABLE_NAME, JobTitlesEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
case PERFORMERS: { | |
rowsDeleted = db.delete(PerformersEntry.TABLE_NAME, selection, selectionArgs); | |
clearSQLiteSequence(db, PerformersEntry.TABLE_NAME); | |
break; | |
} | |
case PERFORMER_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsDeleted = db.delete(PerformersEntry.TABLE_NAME, PerformersEntry._ID + "=" + id, null); | |
} else { | |
rowsDeleted = db.delete(PerformersEntry.TABLE_NAME, PerformersEntry._ID + "=" + id + " and " + | |
selection, selectionArgs); | |
} | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri " + uri); | |
} | |
if (rowsDeleted != 0) getContext().getContentResolver().notifyChange(uri, null); | |
return rowsDeleted; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
int rowsUpdated; | |
switch (match) { | |
case OBJECTS: { | |
rowsUpdated = db.update(ObjectsEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case OBJECT_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(ObjectsEntry.TABLE_NAME, values, ObjectsEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(ObjectsEntry.TABLE_NAME, values, ObjectsEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case SECTORS: { | |
rowsUpdated = db.update(SectorsEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case SECTOR_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(SectorsEntry.TABLE_NAME, values, SectorsEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(SectorsEntry.TABLE_NAME, values, SectorsEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case WELLS: { | |
rowsUpdated = db.update(WellsEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case WELL_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(WellsEntry.TABLE_NAME, values, WellsEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(WellsEntry.TABLE_NAME, values, WellsEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case WATER_LEVELS: { | |
rowsUpdated = db.update(WaterLevelsEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case WATER_LEVEL_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(WaterLevelsEntry.TABLE_NAME, values, WaterLevelsEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(WaterLevelsEntry.TABLE_NAME, values, WaterLevelsEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case WATER_CHEMISTRY: { | |
rowsUpdated = db.update(WaterChemistryEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case WATER_CHEMISTRY_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(WaterChemistryEntry.TABLE_NAME, values, WaterChemistryEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(WaterChemistryEntry.TABLE_NAME, values, WaterChemistryEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case INDICATORS: { | |
rowsUpdated = db.update(IndicatorsEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case INDICATOR_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(IndicatorsEntry.TABLE_NAME, values, IndicatorsEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(IndicatorsEntry.TABLE_NAME, values, IndicatorsEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case JOB_TITLES: { | |
rowsUpdated = db.update(JobTitlesEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case JOB_TITLE_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(JobTitlesEntry.TABLE_NAME, values, JobTitlesEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(JobTitlesEntry.TABLE_NAME, values, JobTitlesEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
case PERFORMERS: { | |
rowsUpdated = db.update(PerformersEntry.TABLE_NAME, values, selection, selectionArgs); | |
break; | |
} | |
case PERFORMER_ID: { | |
String id = uri.getLastPathSegment(); | |
if (TextUtils.isEmpty(selection)) { | |
rowsUpdated = db.update(PerformersEntry.TABLE_NAME, values, PerformersEntry._ID + "=" + id, null); | |
} else { | |
rowsUpdated = db.update(PerformersEntry.TABLE_NAME, values, PerformersEntry._ID + | |
"=" + id + " and " + selection, selectionArgs); | |
} | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri " + uri); | |
} | |
if (rowsUpdated != 0) getContext().getContentResolver().notifyChange(uri, null); | |
return rowsUpdated; | |
} | |
@Override | |
public int bulkInsert(Uri uri, @NonNull ContentValues[] values) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
int rowsInserted = 0; | |
switch (match) { | |
case OBJECTS: { | |
clearSQLiteSequence(db, ObjectsEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(ObjectsEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case SECTORS: { | |
clearSQLiteSequence(db, SectorsEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(SectorsEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case WELLS: { | |
clearSQLiteSequence(db, WellsEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(WellsEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case WATER_CHEMISTRY: { | |
clearSQLiteSequence(db, WaterChemistryEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(WaterChemistryEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case WATER_LEVELS: { | |
clearSQLiteSequence(db, WaterLevelsEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(WaterLevelsEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case INDICATORS: { | |
clearSQLiteSequence(db, IndicatorsEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(IndicatorsEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case JOB_TITLES: { | |
clearSQLiteSequence(db, JobTitlesEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(JobTitlesEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
case PERFORMERS: { | |
clearSQLiteSequence(db, PerformersEntry.TABLE_NAME); | |
db.beginTransaction(); | |
try { | |
for (ContentValues value: values) { | |
long _id = db.insertWithOnConflict(PerformersEntry.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_IGNORE); | |
if (_id != -1) { | |
rowsInserted++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return rowsInserted; | |
} | |
default: | |
return super.bulkInsert(uri, values); | |
} | |
} | |
private void clearSQLiteSequence(SQLiteDatabase db, String tableName) { | |
db.execSQL("delete from sqlite_sequence where name= '" + tableName + "';"); | |
} | |
// You do not need to call this method. This is a method specifically to assist the testing | |
// framework in running smoothly. You can read more at: | |
// http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() | |
@Override | |
@TargetApi(11) | |
public void shutdown() { | |
mOpenHelper.close(); | |
super.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment