Search in sources :

Example 1 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method deleteRow.

public static UpdateRowResponse deleteRow(SQLiteDatabase db, String tableName, List<RowDataRequest> rowDataRequests) {
    UpdateRowResponse updateRowResponse = new UpdateRowResponse();
    if (rowDataRequests == null || tableName == null) {
        updateRowResponse.isSuccessful = false;
        return updateRowResponse;
    }
    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);
        }
    }
    if (whereArgsList.size() == 0) {
        updateRowResponse.isSuccessful = true;
        return updateRowResponse;
    }
    String[] whereArgs = new String[whereArgsList.size()];
    for (int i = 0; i < whereArgsList.size(); i++) {
        whereArgs[i] = whereArgsList.get(i);
    }
    db.delete(tableName, whereClause, whereArgs);
    updateRowResponse.isSuccessful = true;
    return updateRowResponse;
}
Also used : UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) RowDataRequest(com.amitshekhar.model.RowDataRequest) ArrayList(java.util.ArrayList)

Example 2 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest in project Android-Debug-Database by amitshekhariitbhu.

the class PrefHelper method updateRow.

public static UpdateRowResponse updateRow(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 3 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method updateRow.

public static UpdateRowResponse updateRow(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();
    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 = 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 4 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest in project Android-Debug-Database by amitshekhariitbhu.

the class DatabaseHelper method deleteRow.

public static UpdateRowResponse deleteRow(SQLiteDB db, String tableName, List<RowDataRequest> rowDataRequests) {
    UpdateRowResponse updateRowResponse = new UpdateRowResponse();
    if (rowDataRequests == null || tableName == null) {
        updateRowResponse.isSuccessful = false;
        return updateRowResponse;
    }
    tableName = getQuotedTableName(tableName);
    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 = whereClause + "and " + rowDataRequest.title + "=? ";
            }
            whereArgsList.add(rowDataRequest.value);
        }
    }
    if (whereArgsList.size() == 0) {
        updateRowResponse.isSuccessful = true;
        return updateRowResponse;
    }
    String[] whereArgs = new String[whereArgsList.size()];
    for (int i = 0; i < whereArgsList.size(); i++) {
        whereArgs[i] = whereArgsList.get(i);
    }
    db.delete(tableName, whereClause, whereArgs);
    updateRowResponse.isSuccessful = true;
    return updateRowResponse;
}
Also used : UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) RowDataRequest(com.amitshekhar.model.RowDataRequest) ArrayList(java.util.ArrayList)

Example 5 with RowDataRequest

use of com.amitshekhar.model.RowDataRequest in project Android-Debug-Database by amitshekhariitbhu.

the class RequestHandler method updateTableDataAndGetResponse.

private String updateTableDataAndGetResponse(String route) {
    UpdateRowResponse response;
    try {
        Uri uri = Uri.parse(URLDecoder.decode(route, "UTF-8"));
        String tableName = uri.getQueryParameter("tableName");
        String updatedData = uri.getQueryParameter("updatedData");
        List<RowDataRequest> rowDataRequests = mGson.fromJson(updatedData, new TypeToken<List<RowDataRequest>>() {
        }.getType());
        if (Constants.APP_SHARED_PREFERENCES.equals(mSelectedDatabase)) {
            response = PrefHelper.addOrUpdateRow(mContext, tableName, rowDataRequests);
        } else {
            response = DatabaseHelper.updateRow(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)

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