Skip to content

Commit dbed041

Browse files
committed
1. Modified executeSelectQuery and getRecordCount method.
1 parent fa63221 commit dbed041

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

app/src/main/java/com/amit/db/DBHelper.java

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
* this class has method for executing db queries
1717
* like: creating table, inserting into table, deleting table, dropping table
1818
*/
19-
19+
@SuppressWarnings("unused")
2020
public class DBHelper
2121
{
2222
private static final String TAG = DBHelper.class.getSimpleName();
2323

2424
private Database db;
25-
private Cursor cursor;
2625

2726
/**
2827
* Constructor of the class
2928
* you have to set the db name first before using this class.
3029
*
3130
* @param context - context
3231
**/
32+
@SuppressWarnings("unused")
3333
public DBHelper(Context context)
3434
{
3535
SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(context);
@@ -127,9 +127,10 @@ public DBHelper(Context context)
127127
* then conditionalValues can be null
128128
*
129129
* @return true or false
130-
**/
130+
**/
131131

132132
// endregion
133+
@SuppressWarnings("unused")
133134
public boolean executeDatabaseOperations(String tableName,
134135
String operations,
135136
LinkedHashMap<String, String> values,
@@ -328,7 +329,7 @@ public boolean executeDatabaseOperations(String tableName,
328329
}
329330
}
330331

331-
// region COMMENTS FOR executeSelectQuery method
332+
// region COMMENTS FOR executeQuery method
332333

333334
/**
334335
* 2018 Feb 01 - Thursday - 03:52 PM
@@ -348,48 +349,45 @@ public boolean executeDatabaseOperations(String tableName,
348349
* then the user has to pass conditionalValues
349350
* else it can be null
350351
*
351-
* the below lines are not in use to ignore it
352+
* the below lines are not in use so ignore it
352353
*** s - for selecting values from table
353354
* - pass * in values parameter when doing select operations
354355
* when you want to select every thing from the table
355356
* no matter condition is there or not
356357
* - pass values parameters with the name of the columns in the table
357358
* when you want to select one or multiple columns from the table
358359
* no matter condition is there or not
359-
**/
360+
**/
360361

361-
// endregion COMMENTS FOR executeSelectQuery method
362+
// endregion COMMENTS FOR executeQuery method
363+
@SuppressWarnings("unused")
362364
public Cursor executeSelectQuery(String tableName,
363365
String values,
364366
boolean hasConditions,
365-
LinkedHashMap<String, String> conditionalValues)
367+
StringBuilder conditionalValues)
366368
{
367369
try
368370
{
369-
if (cursor != null)
370-
{
371-
cursor.close();
372-
}
371+
Cursor cursor;
373372

374373
if (values != null)
375374
{
376375
String query;
377376

377+
// check if has condition is tru
378+
// if yes the conditional values should not be null
378379
if (hasConditions)
379380
{
381+
// check ig conditional values is passed
382+
// it should be of string builder type
383+
// where user has to pass values to be passed in where clause
384+
//
385+
// FOR EX: firstName = 'FirstNameValue' OR
386+
// firstName LIKE %Term to be searched%
380387
if (conditionalValues != null)
381388
{
382-
String strConditionalValues = conditionalValues.toString();
383-
strConditionalValues = strConditionalValues.replace("{", "");
384-
strConditionalValues = strConditionalValues.replace("}", "");
385-
strConditionalValues = strConditionalValues.replace(",", " AND");
386-
387-
if (strConditionalValues.contains("LIKE ="))
388-
{
389-
strConditionalValues = strConditionalValues.replace("=", "");
390-
}
391-
392-
query = "SELECT " + values + " FROM " + tableName + " WHERE " + strConditionalValues;
389+
// building conditional query
390+
query = "SELECT " + values + " FROM " + tableName + " WHERE " + conditionalValues.toString() + "";
393391
Log.e(TAG, "executeSelectQuery: Select query with conditions is: " + query);
394392
}
395393
else
@@ -400,12 +398,16 @@ public Cursor executeSelectQuery(String tableName,
400398
}
401399
else
402400
{
401+
// building non conditional values
403402
query = "SELECT " + values + " FROM " + tableName;
404403
Log.e(TAG, "executeSelectQuery: Select query without conditions is: " + query);
405404
}
406405

406+
// executing query
407407
cursor = db.getWritableDatabase().rawQuery(query, null);
408408

409+
// if cursor is not null then moving the position to first
410+
// and returning the cursor
409411
if (cursor != null)
410412
{
411413
cursor.moveToFirst();
@@ -461,38 +463,42 @@ public Cursor executeSelectQuery(String tableName,
461463
* *********************************************************************************************
462464
*
463465
*** @return this method will return the count of the record in the table
464-
* */
466+
**/
465467

466468
// endregion COMMENTS FOR getRecordCount method
469+
@SuppressWarnings("unused")
467470
public int getRecordCount(String tableName,
468471
String values,
469472
boolean hasConditions,
470-
LinkedHashMap<String, String> conditionalValues)
473+
StringBuilder conditionalValues)
471474
{
472475
try
473476
{
474-
values = values.replace("[", "");
475-
values = values.replace("]", "");
477+
String query;
476478

477-
String query = "";
478-
479-
if (!hasConditions)
480-
{
481-
query = "SELECT " + values + " FROM " + tableName;
482-
}
483-
else if (conditionalValues != null)
479+
// check if has condition is true
480+
// if yes then conditional values should be passed
481+
if (hasConditions)
484482
{
485-
String strConditionalValues = conditionalValues.toString();
486-
strConditionalValues = strConditionalValues.replace("[", "");
487-
strConditionalValues = strConditionalValues.replace("]", "");
488-
strConditionalValues = strConditionalValues.replace(",", " AND");
489-
490-
if (strConditionalValues.contains("LIKE ="))
483+
// checking if conditional values is not null
484+
// if yes then then building query with conditions
485+
if (conditionalValues != null)
491486
{
492-
strConditionalValues = strConditionalValues.replace("=", "");
487+
// building conditional query
488+
query = "SELECT " + values + " FROM " + tableName + " WHERE " + conditionalValues.toString() + "";
489+
Log.e(TAG, "getRecordCount: query with condition is: " + query);
493490
}
494-
495-
query = "SELECT " + values + " FROM " + tableName + " WHERE " + strConditionalValues + "";
491+
else
492+
{
493+
// building non conditional query
494+
Log.e(TAG, "getRecordCount: conditional value was null.");
495+
return 0;
496+
}
497+
}
498+
else
499+
{
500+
query = "SELECT " + values + " FROM " + tableName + "";
501+
Log.e(TAG, "getRecordCount: query without condition is: " + query);
496502
}
497503

498504
if (!query.equalsIgnoreCase(""))
@@ -526,6 +532,7 @@ else if (conditionalValues != null)
526532
* @return true - if table exists in database
527533
* false - if table not exists in database
528534
**/
535+
@SuppressWarnings("unused")
529536
public boolean isTableExists(String tableName)
530537
{
531538
try

0 commit comments

Comments
 (0)