Skip to content

Commit

Permalink
1. Modified executeSelectQuery and getRecordCount method.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjangid80 committed Sep 10, 2018
1 parent fa63221 commit dbed041
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions app/src/main/java/com/amit/db/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
* this class has method for executing db queries
* like: creating table, inserting into table, deleting table, dropping table
*/

@SuppressWarnings("unused")
public class DBHelper
{
private static final String TAG = DBHelper.class.getSimpleName();

private Database db;
private Cursor cursor;

/**
* Constructor of the class
* you have to set the db name first before using this class.
*
* @param context - context
**/
@SuppressWarnings("unused")
public DBHelper(Context context)
{
SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(context);
Expand Down Expand Up @@ -127,9 +127,10 @@ public DBHelper(Context context)
* then conditionalValues can be null
*
* @return true or false
**/
**/

// endregion
@SuppressWarnings("unused")
public boolean executeDatabaseOperations(String tableName,
String operations,
LinkedHashMap<String, String> values,
Expand Down Expand Up @@ -328,7 +329,7 @@ public boolean executeDatabaseOperations(String tableName,
}
}

// region COMMENTS FOR executeSelectQuery method
// region COMMENTS FOR executeQuery method

/**
* 2018 Feb 01 - Thursday - 03:52 PM
Expand All @@ -348,48 +349,45 @@ public boolean executeDatabaseOperations(String tableName,
* then the user has to pass conditionalValues
* else it can be null
*
* the below lines are not in use to ignore it
* the below lines are not in use so ignore it
*** s - for selecting values from table
* - pass * in values parameter when doing select operations
* when you want to select every thing from the table
* no matter condition is there or not
* - pass values parameters with the name of the columns in the table
* when you want to select one or multiple columns from the table
* no matter condition is there or not
**/
**/

// endregion COMMENTS FOR executeSelectQuery method
// endregion COMMENTS FOR executeQuery method
@SuppressWarnings("unused")
public Cursor executeSelectQuery(String tableName,
String values,
boolean hasConditions,
LinkedHashMap<String, String> conditionalValues)
StringBuilder conditionalValues)
{
try
{
if (cursor != null)
{
cursor.close();
}
Cursor cursor;

if (values != null)
{
String query;

// check if has condition is tru
// if yes the conditional values should not be null
if (hasConditions)
{
// check ig conditional values is passed
// it should be of string builder type
// where user has to pass values to be passed in where clause
//
// FOR EX: firstName = 'FirstNameValue' OR
// firstName LIKE %Term to be searched%
if (conditionalValues != null)
{
String strConditionalValues = conditionalValues.toString();
strConditionalValues = strConditionalValues.replace("{", "");
strConditionalValues = strConditionalValues.replace("}", "");
strConditionalValues = strConditionalValues.replace(",", " AND");

if (strConditionalValues.contains("LIKE ="))
{
strConditionalValues = strConditionalValues.replace("=", "");
}

query = "SELECT " + values + " FROM " + tableName + " WHERE " + strConditionalValues;
// building conditional query
query = "SELECT " + values + " FROM " + tableName + " WHERE " + conditionalValues.toString() + "";
Log.e(TAG, "executeSelectQuery: Select query with conditions is: " + query);
}
else
Expand All @@ -400,12 +398,16 @@ public Cursor executeSelectQuery(String tableName,
}
else
{
// building non conditional values
query = "SELECT " + values + " FROM " + tableName;
Log.e(TAG, "executeSelectQuery: Select query without conditions is: " + query);
}

// executing query
cursor = db.getWritableDatabase().rawQuery(query, null);

// if cursor is not null then moving the position to first
// and returning the cursor
if (cursor != null)
{
cursor.moveToFirst();
Expand Down Expand Up @@ -461,38 +463,42 @@ public Cursor executeSelectQuery(String tableName,
* *********************************************************************************************
*
*** @return this method will return the count of the record in the table
* */
**/

// endregion COMMENTS FOR getRecordCount method
@SuppressWarnings("unused")
public int getRecordCount(String tableName,
String values,
boolean hasConditions,
LinkedHashMap<String, String> conditionalValues)
StringBuilder conditionalValues)
{
try
{
values = values.replace("[", "");
values = values.replace("]", "");
String query;

String query = "";

if (!hasConditions)
{
query = "SELECT " + values + " FROM " + tableName;
}
else if (conditionalValues != null)
// check if has condition is true
// if yes then conditional values should be passed
if (hasConditions)
{
String strConditionalValues = conditionalValues.toString();
strConditionalValues = strConditionalValues.replace("[", "");
strConditionalValues = strConditionalValues.replace("]", "");
strConditionalValues = strConditionalValues.replace(",", " AND");

if (strConditionalValues.contains("LIKE ="))
// checking if conditional values is not null
// if yes then then building query with conditions
if (conditionalValues != null)
{
strConditionalValues = strConditionalValues.replace("=", "");
// building conditional query
query = "SELECT " + values + " FROM " + tableName + " WHERE " + conditionalValues.toString() + "";
Log.e(TAG, "getRecordCount: query with condition is: " + query);
}

query = "SELECT " + values + " FROM " + tableName + " WHERE " + strConditionalValues + "";
else
{
// building non conditional query
Log.e(TAG, "getRecordCount: conditional value was null.");
return 0;
}
}
else
{
query = "SELECT " + values + " FROM " + tableName + "";
Log.e(TAG, "getRecordCount: query without condition is: " + query);
}

if (!query.equalsIgnoreCase(""))
Expand Down Expand Up @@ -526,6 +532,7 @@ else if (conditionalValues != null)
* @return true - if table exists in database
* false - if table not exists in database
**/
@SuppressWarnings("unused")
public boolean isTableExists(String tableName)
{
try
Expand Down

0 comments on commit dbed041

Please sign in to comment.