16
16
* this class has method for executing db queries
17
17
* like: creating table, inserting into table, deleting table, dropping table
18
18
*/
19
-
19
+ @ SuppressWarnings ( "unused" )
20
20
public class DBHelper
21
21
{
22
22
private static final String TAG = DBHelper .class .getSimpleName ();
23
23
24
24
private Database db ;
25
- private Cursor cursor ;
26
25
27
26
/**
28
27
* Constructor of the class
29
28
* you have to set the db name first before using this class.
30
29
*
31
30
* @param context - context
32
31
**/
32
+ @ SuppressWarnings ("unused" )
33
33
public DBHelper (Context context )
34
34
{
35
35
SharedPreferenceData sharedPreferenceData = new SharedPreferenceData (context );
@@ -127,9 +127,10 @@ public DBHelper(Context context)
127
127
* then conditionalValues can be null
128
128
*
129
129
* @return true or false
130
- **/
130
+ **/
131
131
132
132
// endregion
133
+ @ SuppressWarnings ("unused" )
133
134
public boolean executeDatabaseOperations (String tableName ,
134
135
String operations ,
135
136
LinkedHashMap <String , String > values ,
@@ -328,7 +329,7 @@ public boolean executeDatabaseOperations(String tableName,
328
329
}
329
330
}
330
331
331
- // region COMMENTS FOR executeSelectQuery method
332
+ // region COMMENTS FOR executeQuery method
332
333
333
334
/**
334
335
* 2018 Feb 01 - Thursday - 03:52 PM
@@ -348,48 +349,45 @@ public boolean executeDatabaseOperations(String tableName,
348
349
* then the user has to pass conditionalValues
349
350
* else it can be null
350
351
*
351
- * the below lines are not in use to ignore it
352
+ * the below lines are not in use so ignore it
352
353
*** s - for selecting values from table
353
354
* - pass * in values parameter when doing select operations
354
355
* when you want to select every thing from the table
355
356
* no matter condition is there or not
356
357
* - pass values parameters with the name of the columns in the table
357
358
* when you want to select one or multiple columns from the table
358
359
* no matter condition is there or not
359
- **/
360
+ **/
360
361
361
- // endregion COMMENTS FOR executeSelectQuery method
362
+ // endregion COMMENTS FOR executeQuery method
363
+ @ SuppressWarnings ("unused" )
362
364
public Cursor executeSelectQuery (String tableName ,
363
365
String values ,
364
366
boolean hasConditions ,
365
- LinkedHashMap < String , String > conditionalValues )
367
+ StringBuilder conditionalValues )
366
368
{
367
369
try
368
370
{
369
- if (cursor != null )
370
- {
371
- cursor .close ();
372
- }
371
+ Cursor cursor ;
373
372
374
373
if (values != null )
375
374
{
376
375
String query ;
377
376
377
+ // check if has condition is tru
378
+ // if yes the conditional values should not be null
378
379
if (hasConditions )
379
380
{
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%
380
387
if (conditionalValues != null )
381
388
{
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 () + "" ;
393
391
Log .e (TAG , "executeSelectQuery: Select query with conditions is: " + query );
394
392
}
395
393
else
@@ -400,12 +398,16 @@ public Cursor executeSelectQuery(String tableName,
400
398
}
401
399
else
402
400
{
401
+ // building non conditional values
403
402
query = "SELECT " + values + " FROM " + tableName ;
404
403
Log .e (TAG , "executeSelectQuery: Select query without conditions is: " + query );
405
404
}
406
405
406
+ // executing query
407
407
cursor = db .getWritableDatabase ().rawQuery (query , null );
408
408
409
+ // if cursor is not null then moving the position to first
410
+ // and returning the cursor
409
411
if (cursor != null )
410
412
{
411
413
cursor .moveToFirst ();
@@ -461,38 +463,42 @@ public Cursor executeSelectQuery(String tableName,
461
463
* *********************************************************************************************
462
464
*
463
465
*** @return this method will return the count of the record in the table
464
- * */
466
+ * */
465
467
466
468
// endregion COMMENTS FOR getRecordCount method
469
+ @ SuppressWarnings ("unused" )
467
470
public int getRecordCount (String tableName ,
468
471
String values ,
469
472
boolean hasConditions ,
470
- LinkedHashMap < String , String > conditionalValues )
473
+ StringBuilder conditionalValues )
471
474
{
472
475
try
473
476
{
474
- values = values .replace ("[" , "" );
475
- values = values .replace ("]" , "" );
477
+ String query ;
476
478
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 )
484
482
{
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 )
491
486
{
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 );
493
490
}
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 );
496
502
}
497
503
498
504
if (!query .equalsIgnoreCase ("" ))
@@ -526,6 +532,7 @@ else if (conditionalValues != null)
526
532
* @return true - if table exists in database
527
533
* false - if table not exists in database
528
534
**/
535
+ @ SuppressWarnings ("unused" )
529
536
public boolean isTableExists (String tableName )
530
537
{
531
538
try
0 commit comments