X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FMobileLedgerProfile.java;h=1971ca2661bb6d953e4eea8b9bc4e3b8cf0558d9;hp=17b6ac7ae9b8128007c95fefd93a17ab0f978a42;hb=83cac114e375728080194fb09758b49c50a8119b;hpb=c6fe580ac5a93cff593d95a0fe34bd6c2c7bddfc diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 17b6ac7a..1971ca26 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -78,7 +78,7 @@ public final class MobileLedgerProfile { public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) { MobileLedgerProfile result = null; List list = new ArrayList<>(); - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " + "auth_password, permit_posting, theme, order_no FROM " + "profiles order by order_no", null)) @@ -97,15 +97,16 @@ public final class MobileLedgerProfile { return result; } public static void storeProfilesOrder() { - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); db.beginTransaction(); try { int orderNo = 0; - for (MobileLedgerProfile p : Data.profiles.getList()) { - db.execSQL("update profiles set order_no=? where uuid=?", - new Object[]{orderNo, p.getUuid()}); - p.orderNo = orderNo; - orderNo++; + for (int i = 0; i < Data.profiles.size(); i++) { + MobileLedgerProfile p = Data.profiles.get(i); + db.execSQL("update profiles set order_no=? where uuid=?", + new Object[]{orderNo, p.getUuid()}); + p.orderNo = orderNo; + orderNo++; } db.setTransactionSuccessful(); } @@ -165,7 +166,7 @@ public final class MobileLedgerProfile { setAuthPassword(String.valueOf(text)); } public void storeInDB() { - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); db.beginTransaction(); try { // Log.d("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " + @@ -229,7 +230,7 @@ public final class MobileLedgerProfile { Log.d("profile", String.format("Transaction %d stored", tr.getId())); } public String getOption(String name, String default_value) { - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?", new String[]{uuid, name})) { @@ -273,7 +274,7 @@ public final class MobileLedgerProfile { } public void setOption(String name, String value) { Log.d("profile", String.format("setting option %s=%s", name, value)); - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);", new String[]{uuid, name, value}); } @@ -281,25 +282,36 @@ public final class MobileLedgerProfile { setOption(name, String.valueOf(value)); } public void removeFromDB() { - SQLiteDatabase db = MLDB.getWritableDatabase(); - Log.d("db", String.format("removing progile %s from DB", uuid)); - db.execSQL("delete from profiles where uuid=?", new Object[]{uuid}); + SQLiteDatabase db = MLDB.getDatabase(); + Log.d("db", String.format("removing profile %s from DB", uuid)); + db.beginTransaction(); + try { + db.execSQL("delete from profiles where uuid=?", new Object[]{uuid}); + db.execSQL("delete from accounts where profile=?", new Object[]{uuid}); + db.execSQL("delete from account_values where profile=?", new Object[]{uuid}); + db.execSQL("delete from transactions where profile=?", new Object[]{uuid}); + db.execSQL("delete from transaction_accounts where profile=?", new Object[]{uuid}); + db.setTransactionSuccessful(); + } + finally { + db.endTransaction(); + } } @NonNull public LedgerAccount loadAccount(String name) { - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); return loadAccount(db, name); } @Nullable public LedgerAccount tryLoadAccount(String acct_name) { - SQLiteDatabase db = MLDB.getReadableDatabase(); - return loadAccount(acct_name); + SQLiteDatabase db = MLDB.getDatabase(); + return tryLoadAccount(db, acct_name); } @NonNull public LedgerAccount loadAccount(SQLiteDatabase db, String accName) { LedgerAccount acc = tryLoadAccount(db, accName); - if (acc == null) throw new RuntimeException("Unable to load account with name "+accName); + if (acc == null) throw new RuntimeException("Unable to load account with name " + accName); return acc; } @@ -332,7 +344,7 @@ public final class MobileLedgerProfile { } public LedgerTransaction loadTransaction(int transactionId) { LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid); - tr.loadData(MLDB.getReadableDatabase()); + tr.loadData(MLDB.getDatabase()); return tr; } @@ -383,7 +395,7 @@ public final class MobileLedgerProfile { } public List loadChildAccountsOf(LedgerAccount acc) { List result = new ArrayList<>(); - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); try (Cursor c = db.rawQuery( "SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'", new String[]{uuid, acc.getName()})) @@ -401,7 +413,7 @@ public final class MobileLedgerProfile { ArrayList visibleList = new ArrayList<>(); visibleList.add(acc); - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); try (Cursor c = db.rawQuery( "SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'", new String[]{uuid, acc.getName()}))