Search in sources :

Example 6 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest 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);
    }
}
Also used : UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) RowDataRequest(com.amitshekhar.model.RowDataRequest) TypeToken(com.google.gson.reflect.TypeToken) Uri(android.net.Uri) IOException(java.io.IOException)

Example 7 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest 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;
}
Also used : ContentValues(android.content.ContentValues) UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) RowDataRequest(com.amitshekhar.model.RowDataRequest) ArrayList(java.util.ArrayList)

Example 8 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest 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;
}
Also used : ContentValues(android.content.ContentValues) UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) RowDataRequest(com.amitshekhar.model.RowDataRequest)

Example 9 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest 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;
}
Also used : UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) SharedPreferences(android.content.SharedPreferences) RowDataRequest(com.amitshekhar.model.RowDataRequest) JSONArray(org.json.JSONArray) HashSet(java.util.HashSet)

Example 10 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest 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;
}
Also used : UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) SharedPreferences(android.content.SharedPreferences) RowDataRequest(com.amitshekhar.model.RowDataRequest)

Aggregations

RowDataRequest (com.amitshekhar.model.RowDataRequest)11 UpdateRowResponse (com.amitshekhar.model.UpdateRowResponse)11 ArrayList (java.util.ArrayList)4 ContentValues (android.content.ContentValues)3 SharedPreferences (android.content.SharedPreferences)3 Uri (android.net.Uri)3 TypeToken (com.google.gson.reflect.TypeToken)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2