@@ -23,6 +23,16 @@ public class DBHelper extends SQLiteOpenHelper {
2323 public static final int ORIGINAL_DATABASE_VERSION = 1 ;
2424 public static final int DATABASE_VERSION = 16 ;
2525
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+
2636 public static class LoyaltyCardDbGroups {
2737 public static final String TABLE = "groups" ;
2838 public static final String ID = "_id" ;
@@ -323,6 +333,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
323333 }
324334 }
325335
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+
326342 private static ContentValues generateFTSContentValues (final int id , final String store , final String note ) {
327343 // FTS on Android is severely limited and can only search for word starting with a certain string
328344 // So for each word, we grab every single substring
@@ -368,7 +384,7 @@ public static long insertLoyaltyCard(
368384 final SQLiteDatabase database , final String store , final String note , final Date validFrom ,
369385 final Date expiry , final BigDecimal balance , final Currency balanceType , final String cardId ,
370386 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 {
372388 database .beginTransaction ();
373389
374390 // Card
@@ -388,6 +404,8 @@ public static long insertLoyaltyCard(
388404 contentValues .put (LoyaltyCardDbIds .ARCHIVE_STATUS , archiveStatus );
389405 long id = database .insert (LoyaltyCardDbIds .TABLE , null , contentValues );
390406
407+ if (id == -1 ) throw new DBException ("Failed to insert card" );
408+
391409 // FTS
392410 insertFTS (database , (int ) id , store , note );
393411
@@ -402,7 +420,7 @@ public static long insertLoyaltyCard(
402420 final Date validFrom , final Date expiry , final BigDecimal balance ,
403421 final Currency balanceType , final String cardId , final String barcodeId ,
404422 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 {
406424 database .beginTransaction ();
407425
408426 // Card
@@ -421,7 +439,9 @@ public static long insertLoyaltyCard(
421439 contentValues .put (LoyaltyCardDbIds .STAR_STATUS , starStatus );
422440 contentValues .put (LoyaltyCardDbIds .LAST_USED , lastUsed != null ? lastUsed : Utils .getUnixTime ());
423441 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 );
425445
426446 // FTS
427447 insertFTS (database , id , store , note );
@@ -432,12 +452,12 @@ public static long insertLoyaltyCard(
432452 return id ;
433453 }
434454
435- public static boolean updateLoyaltyCard (
455+ public static void updateLoyaltyCard (
436456 SQLiteDatabase database , final int id , final String store , final String note ,
437457 final Date validFrom , final Date expiry , final BigDecimal balance ,
438458 final Currency balanceType , final String cardId , final String barcodeId ,
439459 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 {
441461 database .beginTransaction ();
442462
443463 // Card
@@ -465,45 +485,45 @@ public static boolean updateLoyaltyCard(
465485 database .setTransactionSuccessful ();
466486 database .endTransaction ();
467487
468- return (rowsUpdated == 1 );
488+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to update card with ID " + id + ": " + rowsUpdated + " rows updated" );
469489 }
470490
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 {
472492 ContentValues contentValues = new ContentValues ();
473493 contentValues .put (LoyaltyCardDbIds .ARCHIVE_STATUS , archiveStatus );
474494 int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
475495 whereAttrs (LoyaltyCardDbIds .ID ),
476496 withArgs (id ));
477- return (rowsUpdated == 1 );
497+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to (un)archive card with ID " + id + ": " + rowsUpdated + " rows updated" );
478498 }
479499
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 {
481501 ContentValues contentValues = new ContentValues ();
482502 contentValues .put (LoyaltyCardDbIds .STAR_STATUS , starStatus );
483503 int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
484504 whereAttrs (LoyaltyCardDbIds .ID ),
485505 withArgs (id ));
486- return (rowsUpdated == 1 );
506+ if (rowsUpdated != 1 ) throw new DBException ( "Failed to (un)star card with ID " + id + ": " + rowsUpdated + " rows updated" );
487507 }
488508
489- public static boolean updateLoyaltyCardLastUsed (SQLiteDatabase database , final int id ) {
509+ public static void updateLoyaltyCardLastUsed (SQLiteDatabase database , final int id ) throws DBException {
490510 ContentValues contentValues = new ContentValues ();
491511 contentValues .put (LoyaltyCardDbIds .LAST_USED , System .currentTimeMillis () / 1000 );
492512 int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
493513 whereAttrs (LoyaltyCardDbIds .ID ),
494514 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" );
496516 }
497517
498- public static boolean updateLoyaltyCardZoomLevel (SQLiteDatabase database , int loyaltyCardId , int zoomLevel ) {
518+ public static void updateLoyaltyCardZoomLevel (SQLiteDatabase database , int loyaltyCardId , int zoomLevel ) throws DBException {
499519 ContentValues contentValues = new ContentValues ();
500520 contentValues .put (LoyaltyCardDbIds .ZOOM_LEVEL , zoomLevel );
501521 Log .d ("updateLoyaltyCardZLevel" , "Card Id = " + loyaltyCardId + " Zoom level= " + zoomLevel );
502522 int rowsUpdated = database .update (LoyaltyCardDbIds .TABLE , contentValues ,
503523 whereAttrs (LoyaltyCardDbIds .ID ),
504524 withArgs (loyaltyCardId ));
505525 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" );
507527 }
508528
509529 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
569589 }
570590 }
571591
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 {
573593 // Delete card
574594 int rowsDeleted = database .delete (LoyaltyCardDbIds .TABLE ,
575595 whereAttrs (LoyaltyCardDbIds .ID ),
@@ -594,7 +614,7 @@ public static boolean deleteLoyaltyCard(SQLiteDatabase database, Context context
594614 }
595615 }
596616
597- return (rowsDeleted == 1 );
617+ if (rowsDeleted != 1 ) throw new DBException ( "Failed to delete card with ID " + id + ": " + rowsDeleted + " rows deleted" );
598618 }
599619
600620 public static int getArchivedCardsCount (SQLiteDatabase database ) {
@@ -792,19 +812,20 @@ public static List<Integer> getGroupCardIds(SQLiteDatabase database, final Strin
792812 return cardIds ;
793813 }
794814
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" ) ;
797817
798818 ContentValues contentValues = new ContentValues ();
799819 contentValues .put (LoyaltyCardDbGroups .ID , name );
800820 contentValues .put (LoyaltyCardDbGroups .ORDER , getGroupCount (database ));
801- return database .insert (LoyaltyCardDbGroups .TABLE , null , contentValues );
802- }
821+ long rowid = database .insert (LoyaltyCardDbGroups .TABLE , null , contentValues );
803822
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+ }
806825
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" );
808829
809830 ContentValues groupContentValues = new ContentValues ();
810831 groupContentValues .put (LoyaltyCardDbGroups .ID , newName );
@@ -826,19 +847,17 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam
826847
827848 if (groupsChanged == 1 ) {
828849 database .setTransactionSuccessful ();
829- success = true ;
850+ return ;
830851 }
831852 } catch (SQLiteException ignored ) {
832853 } finally {
833854 database .endTransaction ();
834855 }
835856
836- return success ;
857+ throw new DBException ( "Failed to update group" ) ;
837858 }
838859
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 {
842861 database .beginTransaction ();
843862 try {
844863 // Delete group
@@ -853,16 +872,15 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam
853872
854873 if (groupsDeleted == 1 ) {
855874 database .setTransactionSuccessful ();
856- success = true ;
875+ // Reorder after delete to ensure no bad order IDs
876+ reorderGroups (database , getGroups (database ));
877+ return ;
857878 }
858879 } finally {
859880 database .endTransaction ();
860881 }
861882
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" );
866884 }
867885
868886 public static int getGroupCardCount (SQLiteDatabase database , final String groupName ) {
0 commit comments