From ff3be47725919f5284a2a4e2348d1ab6fd561325 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 7 Mar 2019 06:57:53 +0200 Subject: [PATCH] Profile/storeAccount[Value],storeTransaction: get the db as an argument the calling code uses transactions, so it would be nice to use them --- .../async/RetrieveTransactionsTask.java | 14 +++++++------- .../mobileledger/model/MobileLedgerProfile.java | 10 +++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index dcc91b2f..acd1475d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -166,7 +166,7 @@ public class RetrieveTransactionsTask lastAccount = profile.loadAccount(acct_name); if (lastAccount == null) { lastAccount = new LedgerAccount(acct_name); - profile.storeAccount(lastAccount); + profile.storeAccount(db, lastAccount); } // make sure the parent account(s) are present, @@ -188,7 +188,7 @@ public class RetrieveTransactionsTask accountList.add(acc); L(String.format("gap-filling with %s", aName)); accountNames.put(aName, null); - profile.storeAccount(acc); + profile.storeAccount(db, acc); } } @@ -213,7 +213,7 @@ public class RetrieveTransactionsTask if (currency == null) currency = ""; value = value.replace(',', '.'); L("curr=" + currency + ", value=" + value); - profile.storeAccountValue(lastAccount.getName(), currency, + profile.storeAccountValue(db, lastAccount.getName(), currency, Float.valueOf(value)); lastAccount.addAmount(Float.parseFloat(value), currency); } @@ -291,7 +291,7 @@ public class RetrieveTransactionsTask } } else { - profile.storeTransaction(transaction); + profile.storeTransaction(db, transaction); matchedTransactionsCount = 0; progress.setTotal(maxTransactionId); } @@ -380,9 +380,9 @@ public class RetrieveTransactionsTask if (parsedAccount == null) break; LedgerAccount acc = new LedgerAccount(parsedAccount.getAname()); - profile.storeAccount(acc); + profile.storeAccount(db, acc); for (ParsedBalance b : parsedAccount.getAebalance()) { - profile.storeAccountValue(acc.getName(), b.getAcommodity(), + profile.storeAccountValue(db, acc.getName(), b.getAcommodity(), b.getAquantity().asFloat()); } @@ -445,7 +445,7 @@ public class RetrieveTransactionsTask } } else { - profile.storeTransaction(transaction); + profile.storeTransaction(db, transaction); matchedTransactionsCount = 0; progress.setTotal(maxTransactionId); } 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 ae877c33..168941f8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -182,9 +182,7 @@ public final class MobileLedgerProfile { db.endTransaction(); } } - public void storeAccount(LedgerAccount acc) { - SQLiteDatabase db = MLDB.getWritableDatabase(); - + public void storeAccount(SQLiteDatabase db, LedgerAccount acc) { // replace into is a bad idea because it would reset hidden to its default value // we like the default, but for new accounts only db.execSQL("update accounts set level = ?, keep = 1 where profile=? and name = ?", @@ -195,14 +193,12 @@ public final class MobileLedgerProfile { acc.getLevel() }); } - public void storeAccountValue(String name, String currency, Float amount) { - SQLiteDatabase db = MLDB.getWritableDatabase(); + public void storeAccountValue(SQLiteDatabase db, String name, String currency, Float amount) { db.execSQL("replace into account_values(profile, account, " + "currency, value, keep) values(?, ?, ?, ?, 1);", new Object[]{uuid, name, currency, amount}); } - public void storeTransaction(LedgerTransaction tr) { - SQLiteDatabase db = MLDB.getWritableDatabase(); + public void storeTransaction(SQLiteDatabase db, LedgerTransaction tr) { tr.fillDataHash(); db.execSQL("DELETE from transactions WHERE profile=? and id=?", new Object[]{uuid, tr.getId()}); -- 2.39.5