use of com.amitshekhar.model.UpdateRowResponse in project Android-Debug-Database by amitshekhariitbhu.
the class RequestHandler method deleteTableDataAndGetResponse.
private String deleteTableDataAndGetResponse(String route) {
UpdateRowResponse response;
try {
Uri uri = Uri.parse(URLDecoder.decode(route, "UTF-8"));
String tableName = uri.getQueryParameter("tableName");
String updatedData = uri.getQueryParameter("deleteData");
List<RowDataRequest> rowDataRequests = mGson.fromJson(updatedData, new TypeToken<List<RowDataRequest>>() {
}.getType());
if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) {
response = PrefHelper.deleteRow(mContext, tableName, rowDataRequests);
} else {
response = DatabaseHelper.deleteRow(sqLiteDB, tableName, rowDataRequests);
}
return mGson.toJson(response);
} catch (Exception e) {
e.printStackTrace();
response = new UpdateRowResponse();
response.isSuccessful = false;
return mGson.toJson(response);
}
}
use of com.amitshekhar.model.UpdateRowResponse in project Android-Debug-Database by amitshekhariitbhu.
the class DatabaseHelper method updateRow.
public static UpdateRowResponse updateRow(SQLiteDatabase db, String tableName, List<RowDataRequest> rowDataRequests) {
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
if (rowDataRequests == null || tableName == null) {
updateRowResponse.isSuccessful = false;
return updateRowResponse;
}
ContentValues contentValues = new ContentValues();
String whereClause = null;
List<String> whereArgsList = new ArrayList<>();
for (RowDataRequest rowDataRequest : rowDataRequests) {
if (Constants.NULL.equals(rowDataRequest.value)) {
rowDataRequest.value = null;
}
if (rowDataRequest.isPrimary) {
if (whereClause == null) {
whereClause = rowDataRequest.title + "=? ";
} else {
whereClause = "and " + rowDataRequest.title + "=? ";
}
whereArgsList.add(rowDataRequest.value);
} else {
switch(rowDataRequest.dataType) {
case DataType.INTEGER:
contentValues.put(rowDataRequest.title, Long.valueOf(rowDataRequest.value));
break;
case DataType.REAL:
contentValues.put(rowDataRequest.title, Double.valueOf(rowDataRequest.value));
break;
case DataType.TEXT:
contentValues.put(rowDataRequest.title, rowDataRequest.value);
break;
default:
}
}
}
String[] whereArgs = new String[whereArgsList.size()];
for (int i = 0; i < whereArgsList.size(); i++) {
whereArgs[i] = whereArgsList.get(i);
}
db.update(tableName, contentValues, whereClause, whereArgs);
updateRowResponse.isSuccessful = true;
return updateRowResponse;
}
use of com.amitshekhar.model.UpdateRowResponse in project Android-Debug-Database by amitshekhariitbhu.
the class DatabaseHelper method addRow.
public static UpdateRowResponse addRow(SQLiteDB db, String tableName, List<RowDataRequest> rowDataRequests) {
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
if (rowDataRequests == null || tableName == null) {
updateRowResponse.isSuccessful = false;
return updateRowResponse;
}
tableName = getQuotedTableName(tableName);
ContentValues contentValues = new ContentValues();
for (RowDataRequest rowDataRequest : rowDataRequests) {
if (Constants.NULL.equals(rowDataRequest.value)) {
rowDataRequest.value = null;
}
switch(rowDataRequest.dataType) {
case DataType.INTEGER:
contentValues.put(rowDataRequest.title, Long.valueOf(rowDataRequest.value));
break;
case DataType.REAL:
contentValues.put(rowDataRequest.title, Double.valueOf(rowDataRequest.value));
break;
case DataType.TEXT:
contentValues.put(rowDataRequest.title, rowDataRequest.value);
break;
default:
contentValues.put(rowDataRequest.title, rowDataRequest.value);
break;
}
}
long result = db.insert(tableName, null, contentValues);
updateRowResponse.isSuccessful = result > 0;
return updateRowResponse;
}
use of com.amitshekhar.model.UpdateRowResponse in project Android-Debug-Database by amitshekhariitbhu.
the class PrefHelper method addOrUpdateRow.
public static UpdateRowResponse addOrUpdateRow(Context context, String tableName, List<RowDataRequest> rowDataRequests) {
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
if (tableName == null) {
return updateRowResponse;
}
RowDataRequest rowDataKey = rowDataRequests.get(0);
RowDataRequest rowDataValue = rowDataRequests.get(1);
String key = rowDataKey.value;
String value = rowDataValue.value;
String dataType = rowDataValue.dataType;
if (Constants.NULL.equals(value)) {
value = null;
}
SharedPreferences preferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
try {
switch(dataType) {
case DataType.TEXT:
preferences.edit().putString(key, value).apply();
updateRowResponse.isSuccessful = true;
break;
case DataType.INTEGER:
preferences.edit().putInt(key, Integer.valueOf(value)).apply();
updateRowResponse.isSuccessful = true;
break;
case DataType.LONG:
preferences.edit().putLong(key, Long.valueOf(value)).apply();
updateRowResponse.isSuccessful = true;
break;
case DataType.FLOAT:
preferences.edit().putFloat(key, Float.valueOf(value)).apply();
updateRowResponse.isSuccessful = true;
break;
case DataType.BOOLEAN:
preferences.edit().putBoolean(key, Boolean.valueOf(value)).apply();
updateRowResponse.isSuccessful = true;
break;
case DataType.STRING_SET:
JSONArray jsonArray = new JSONArray(value);
Set<String> stringSet = new HashSet<>();
for (int i = 0; i < jsonArray.length(); i++) {
stringSet.add(jsonArray.getString(i));
}
preferences.edit().putStringSet(key, stringSet).apply();
updateRowResponse.isSuccessful = true;
break;
default:
preferences.edit().putString(key, value).apply();
updateRowResponse.isSuccessful = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return updateRowResponse;
}
use of com.amitshekhar.model.UpdateRowResponse in project Android-Debug-Database by amitshekhariitbhu.
the class PrefHelper method deleteRow.
public static UpdateRowResponse deleteRow(Context context, String tableName, List<RowDataRequest> rowDataRequests) {
UpdateRowResponse updateRowResponse = new UpdateRowResponse();
if (tableName == null) {
return updateRowResponse;
}
RowDataRequest rowDataKey = rowDataRequests.get(0);
String key = rowDataKey.value;
SharedPreferences preferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
try {
preferences.edit().remove(key).apply();
updateRowResponse.isSuccessful = true;
} catch (Exception ex) {
updateRowResponse.isSuccessful = false;
}
return updateRowResponse;
}
Aggregations