@@ -23,6 +23,16 @@ public class DBHelper extends SQLiteOpenHelper {
23
23
public static final int ORIGINAL_DATABASE_VERSION = 1 ;
24
24
public static final int DATABASE_VERSION = 16 ;
25
25
26
+ public static class DBException extends Exception {
27
+ public DBException (String message ) {
28
+ super (message );
29
+ }
30
+
31
+ public DBException (String message , Exception rootCause ) {
32
+ super (message , rootCause );
33
+ }
34
+ }
35
+
26
36
public static class LoyaltyCardDbGroups {
27
37
public static final String TABLE = "groups" ;
28
38
public static final String ID = "_id" ;
@@ -323,6 +333,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
323
333
}
324
334
}
325
335
336
+ public static void clearDatabase (final SQLiteDatabase db ) {
337
+ db .execSQL ("DELETE FROM " + LoyaltyCardDbGroups .TABLE );
338
+ db .execSQL ("DELETE FROM " + LoyaltyCardDbIds .TABLE );
339
+ db .execSQL ("DELETE FROM " + LoyaltyCardDbIdsGroups .TABLE );
340
+ }
341
+
326
342
private static ContentValues generateFTSContentValues (final int id , final String store , final String note ) {
327
343
// FTS on Android is severely limited and can only search for word starting with a certain string
328
344
// So for each word, we grab every single substring
@@ -368,7 +384,7 @@ public static long insertLoyaltyCard(
368
384
final SQLiteDatabase database , final String store , final String note , final Date validFrom ,
369
385
final Date expiry , final BigDecimal balance , final Currency balanceType , final String cardId ,
370
386
final String barcodeId , final CatimaBarcode barcodeType , final Integer headerColor ,
371
- final int starStatus , final Long lastUsed , final int archiveStatus ) {
387
+ final int starStatus , final Long lastUsed , final int archiveStatus ) throws DBException {
372
388
database .beginTransaction ();
373
389
374
390
// Card
@@ -388,6 +404,8 @@ public static long insertLoyaltyCard(
388
404
contentValues .put (LoyaltyCardDbIds .ARCHIVE_STATUS , archiveStatus );
389
405
long id = database .insert (LoyaltyCardDbIds .TABLE , null , contentValues );
390
406
407
+ if (id == -1 ) throw new DBException ("Failed to insert card" );
408
+
391
409
// FTS
392
410
insertFTS (database , (int ) id , store , note );
393
411
@@ -402,7 +420,7 @@ public static long insertLoyaltyCard(
402
420
final Date validFrom , final Date expiry , final BigDecimal balance ,
403
421
final Currency balanceType , final String cardId , final String barcodeId ,
404
422
final CatimaBarcode barcodeType , final Integer headerColor , final int starStatus ,
405
- final Long lastUsed , final int archiveStatus ) {
423
+ final Long lastUsed , final int archiveStatus ) throws DBException {
406
424
database .beginTransaction ();
407
425
408
426
// Card
@@ -421,7 +439,9 @@ public static long insertLoyaltyCard(
421
439
contentValues .put (LoyaltyCardDbIds .STAR_STATUS , starStatus );
422
440
contentValues .put (LoyaltyCardDbIds .LAST_USED , lastUsed != null ? lastUsed : Utils .getUnixTime ());
423
441
contentValues .put (LoyaltyCardDbIds .ARCHIVE_STATUS , archiveStatus );
424
- database .insert (LoyaltyCardDbIds .TABLE , null , contentValues );
442
+ long rowid = database .insert (LoyaltyCardDbIds .TABLE , null , contentValues );
443
+
444
+ if (rowid == -1 ) throw new DBException ("Failed to insert card with ID " + id );
425
445
426
446
// FTS
427
447
insertFTS (database , id , store , note );
@@ -432,12 +452,12 @@ public static long insertLoyaltyCard(
432
452
return id ;
433
453
}
434
454
435
- public static boolean updateLoyaltyCard (
455
+ public static void updateLoyaltyCard (
436
456
SQLiteDatabase database , final int id , final String store , final String note ,
437
457
final Date validFrom , final Date expiry , final BigDecimal balance ,
438
458
final Currency balanceType , final String cardId , final String barcodeId ,
439
459
final CatimaBarcode barcodeType , final Integer headerColor , final int starStatus ,
440
- final Long lastUsed , final int archiveStatus ) {
460
+ final Long lastUsed , final int archiveStatus ) throws DBException {
441
461
database .beginTransaction ();
442
462
443
463
// Card
@@ -465,45 +485,45 @@ public static boolean updateLoyaltyCard(
465
485
database .setTransactionSuccessful ();
466
486
database .endTransaction ();
467
487
468
- return (rowsUpdated == 1 );
488
+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to update card with ID " + id + ": " + rowsUpdated + " rows updated" );
469
489
}
470
490
471
- public static boolean updateLoyaltyCardArchiveStatus (SQLiteDatabase database , final int id , final int archiveStatus ) {
491
+ public static void updateLoyaltyCardArchiveStatus (SQLiteDatabase database , final int id , final int archiveStatus ) throws DBException {
472
492
ContentValues contentValues = new ContentValues ();
473
493
contentValues .put (LoyaltyCardDbIds .ARCHIVE_STATUS , archiveStatus );
474
494
int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
475
495
whereAttrs (LoyaltyCardDbIds .ID ),
476
496
withArgs (id ));
477
- return (rowsUpdated == 1 );
497
+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to (un)archive card with ID " + id + ": " + rowsUpdated + " rows updated" );
478
498
}
479
499
480
- public static boolean updateLoyaltyCardStarStatus (SQLiteDatabase database , final int id , final int starStatus ) {
500
+ public static void updateLoyaltyCardStarStatus (SQLiteDatabase database , final int id , final int starStatus ) throws DBException {
481
501
ContentValues contentValues = new ContentValues ();
482
502
contentValues .put (LoyaltyCardDbIds .STAR_STATUS , starStatus );
483
503
int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
484
504
whereAttrs (LoyaltyCardDbIds .ID ),
485
505
withArgs (id ));
486
- return (rowsUpdated == 1 );
506
+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to (un)star card with ID " + id + ": " + rowsUpdated + " rows updated" );
487
507
}
488
508
489
- public static boolean updateLoyaltyCardLastUsed (SQLiteDatabase database , final int id ) {
509
+ public static void updateLoyaltyCardLastUsed (SQLiteDatabase database , final int id ) throws DBException {
490
510
ContentValues contentValues = new ContentValues ();
491
511
contentValues .put (LoyaltyCardDbIds .LAST_USED , System .currentTimeMillis () / 1000 );
492
512
int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
493
513
whereAttrs (LoyaltyCardDbIds .ID ),
494
514
withArgs (id ));
495
- return (rowsUpdated == 1 );
515
+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to update last used for card with ID " + id + ": " + rowsUpdated + " rows updated" );
496
516
}
497
517
498
- public static boolean updateLoyaltyCardZoomLevel (SQLiteDatabase database , int loyaltyCardId , int zoomLevel ) {
518
+ public static void updateLoyaltyCardZoomLevel (SQLiteDatabase database , int loyaltyCardId , int zoomLevel ) throws DBException {
499
519
ContentValues contentValues = new ContentValues ();
500
520
contentValues .put (LoyaltyCardDbIds .ZOOM_LEVEL , zoomLevel );
501
521
Log .d ("updateLoyaltyCardZLevel" , "Card Id = " + loyaltyCardId + " Zoom level= " + zoomLevel );
502
522
int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
503
523
whereAttrs (LoyaltyCardDbIds .ID ),
504
524
withArgs (loyaltyCardId ));
505
525
Log .d ("updateLoyaltyCardZLevel" , "Rows changed = " + rowsUpdated );
506
- return (rowsUpdated == 1 );
526
+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to update zoom level for card with ID " + loyaltyCardId + ": " + rowsUpdated + " rows updated" );
507
527
}
508
528
509
529
public static boolean updateLoyaltyCardBalance (SQLiteDatabase database , final int id , final BigDecimal newBalance ) {
@@ -569,7 +589,7 @@ public static void setLoyaltyCardGroups(SQLiteDatabase database, final int id, L
569
589
}
570
590
}
571
591
572
- public static boolean deleteLoyaltyCard (SQLiteDatabase database , Context context , final int id ) {
592
+ public static void deleteLoyaltyCard (SQLiteDatabase database , Context context , final int id ) throws DBException {
573
593
// Delete card
574
594
int rowsDeleted = database .delete (LoyaltyCardDbIds .TABLE ,
575
595
whereAttrs (LoyaltyCardDbIds .ID ),
@@ -594,7 +614,7 @@ public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context
594
614
}
595
615
}
596
616
597
- return (rowsDeleted == 1 );
617
+ if (rowsDeleted != 1 ) throw new DBException ( "Failed to delete card with ID " + id + ": " + rowsDeleted + " rows deleted" );
598
618
}
599
619
600
620
public static int getArchivedCardsCount (SQLiteDatabase database ) {
@@ -792,19 +812,20 @@ public static List<Integer> getGroupCardIds(SQLiteDatabase database, final Strin
792
812
return cardIds ;
793
813
}
794
814
795
- public static long insertGroup (SQLiteDatabase database , final String name ) {
796
- if (name .isEmpty ()) return - 1 ;
815
+ public static void insertGroup (SQLiteDatabase database , final String name ) throws DBException {
816
+ if (name .isEmpty ()) throw new DBException ( "Failed to insert group with empty name" ) ;
797
817
798
818
ContentValues contentValues = new ContentValues ();
799
819
contentValues .put (LoyaltyCardDbGroups .ID , name );
800
820
contentValues .put (LoyaltyCardDbGroups .ORDER , getGroupCount (database ));
801
- return database .insert (LoyaltyCardDbGroups .TABLE , null , contentValues );
802
- }
821
+ long rowid = database .insert (LoyaltyCardDbGroups .TABLE , null , contentValues );
803
822
804
- public static boolean updateGroup ( SQLiteDatabase database , final String groupName , final String newName ) {
805
- if ( newName . isEmpty ()) return false ;
823
+ if ( rowid == - 1 ) throw new DBException ( "Failed to insert group with name " + name );
824
+ }
806
825
807
- boolean success = false ;
826
+ public static void updateGroup (SQLiteDatabase database , final String groupName , final String newName ) throws DBException {
827
+ if (groupName .isEmpty ()) throw new DBException ("Failed to update group: empty old name" );
828
+ if (newName .isEmpty ()) throw new DBException ("Failed to update group: empty new name" );
808
829
809
830
ContentValues groupContentValues = new ContentValues ();
810
831
groupContentValues .put (LoyaltyCardDbGroups .ID , newName );
@@ -826,19 +847,17 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam
826
847
827
848
if (groupsChanged == 1 ) {
828
849
database .setTransactionSuccessful ();
829
- success = true ;
850
+ return ;
830
851
}
831
852
} catch (SQLiteException ignored ) {
832
853
} finally {
833
854
database .endTransaction ();
834
855
}
835
856
836
- return success ;
857
+ throw new DBException ( "Failed to update group" ) ;
837
858
}
838
859
839
- public static boolean deleteGroup (SQLiteDatabase database , final String groupName ) {
840
- boolean success = false ;
841
-
860
+ public static void deleteGroup (SQLiteDatabase database , final String groupName ) throws DBException {
842
861
database .beginTransaction ();
843
862
try {
844
863
// Delete group
@@ -853,16 +872,15 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam
853
872
854
873
if (groupsDeleted == 1 ) {
855
874
database .setTransactionSuccessful ();
856
- success = true ;
875
+ // Reorder after delete to ensure no bad order IDs
876
+ reorderGroups (database , getGroups (database ));
877
+ return ;
857
878
}
858
879
} finally {
859
880
database .endTransaction ();
860
881
}
861
882
862
- // Reorder after delete to ensure no bad order IDs
863
- reorderGroups (database , getGroups (database ));
864
-
865
- return success ;
883
+ throw new DBException ("Failed to delete group" );
866
884
}
867
885
868
886
public static int getGroupCardCount (SQLiteDatabase database , final String groupName ) {
0 commit comments