From: Damyan Ivanov Date: Sun, 24 Mar 2019 09:29:12 +0000 (+0200) Subject: remove the distinction between "readable" and "writable" database connections X-Git-Tag: v0.8.1~20 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=abd5a19252bf81af903c3406132030e3ad63704f;p=mobile-ledger.git remove the distinction between "readable" and "writable" database connections it is artificial and serves no useful purpose --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java index 7368d712..12136478 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java @@ -35,7 +35,7 @@ public class CommitAccountsTask String profile = Data.profile.get().getUuid(); try { - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); db.beginTransaction(); try { for (LedgerAccount acc : params[0].accountList) { diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java index 68f3ddc8..41a58e98 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java @@ -34,7 +34,7 @@ public class RefreshDescriptionsTask extends AsyncTask { Map unique = new HashMap<>(); Log.d("descriptions", "Starting refresh"); - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); Data.backgroundTaskCount.incrementAndGet(); try { 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 de459ece..dc739443 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -124,7 +124,7 @@ public class RetrieveTransactionsTask default: throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { + try (SQLiteDatabase db = MLDB.getDatabase()) { try (InputStream resp = http.getInputStream()) { if (http.getResponseCode() != 200) throw new IOException(String.format("HTTP error %d", http.getResponseCode())); @@ -383,54 +383,53 @@ public class RetrieveTransactionsTask throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } publishProgress(progress); - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { - try (InputStream resp = http.getInputStream()) { - if (http.getResponseCode() != 200) - throw new IOException(String.format("HTTP error %d", http.getResponseCode())); + SQLiteDatabase db = MLDB.getDatabase(); + try (InputStream resp = http.getInputStream()) { + if (http.getResponseCode() != 200) + throw new IOException(String.format("HTTP error %d", http.getResponseCode())); - db.beginTransaction(); - try { - profile.markAccountsAsNotPresent(db); + db.beginTransaction(); + try { + profile.markAccountsAsNotPresent(db); - AccountListParser parser = new AccountListParser(resp); - ArrayList accountList = new ArrayList<>(); + AccountListParser parser = new AccountListParser(resp); + ArrayList accountList = new ArrayList<>(); - LedgerAccount prevAccount = null; + LedgerAccount prevAccount = null; - while (true) { - throwIfCancelled(); - ParsedLedgerAccount parsedAccount = parser.nextAccount(); - if (parsedAccount == null) break; + while (true) { + throwIfCancelled(); + ParsedLedgerAccount parsedAccount = parser.nextAccount(); + if (parsedAccount == null) break; - LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); - if (acc == null) acc = new LedgerAccount(parsedAccount.getAname()); - else acc.removeAmounts(); + LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); + if (acc == null) acc = new LedgerAccount(parsedAccount.getAname()); + else acc.removeAmounts(); - profile.storeAccount(db, acc); - for (ParsedBalance b : parsedAccount.getAebalance()) { - profile.storeAccountValue(db, acc.getName(), b.getAcommodity(), - b.getAquantity().asFloat()); - } - - if (acc.isVisible(accountList)) accountList.add(acc); + profile.storeAccount(db, acc); + for (ParsedBalance b : parsedAccount.getAebalance()) { + profile.storeAccountValue(db, acc.getName(), b.getAcommodity(), + b.getAquantity().asFloat()); + } - if (prevAccount != null) { - prevAccount.setHasSubAccounts( - acc.getName().startsWith(prevAccount.getName() + ":")); - } + if (acc.isVisible(accountList)) accountList.add(acc); - prevAccount = acc; + if (prevAccount != null) { + prevAccount.setHasSubAccounts( + acc.getName().startsWith(prevAccount.getName() + ":")); } - throwIfCancelled(); - profile.deleteNotPresentAccounts(db); - throwIfCancelled(); - db.setTransactionSuccessful(); - Data.accounts.set(accountList); - } - finally { - db.endTransaction(); + prevAccount = acc; } + throwIfCancelled(); + + profile.deleteNotPresentAccounts(db); + throwIfCancelled(); + db.setTransactionSuccessful(); + Data.accounts.set(accountList); + } + finally { + db.endTransaction(); } } @@ -452,7 +451,7 @@ public class RetrieveTransactionsTask default: throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { + try (SQLiteDatabase db = MLDB.getDatabase()) { try (InputStream resp = http.getInputStream()) { if (http.getResponseCode() != 200) throw new IOException(String.format("HTTP error %d", http.getResponseCode())); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java index ad030ef6..65f27222 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java @@ -42,7 +42,7 @@ public class UpdateAccountsTask extends AsyncTask { } Log.d("UTT", sql); - SQLiteDatabase db = MLDB.getReadableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); String lastDateString = Globals.formatLedgerDate(new Date()); Date lastDate = Globals.parseLedgerDate(lastDateString); boolean odd = true; 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 4be38d4a..e466d3d9 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,7 +97,7 @@ public final class MobileLedgerProfile { return result; } public static void storeProfilesOrder() { - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); db.beginTransaction(); try { int orderNo = 0; @@ -165,7 +165,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 +229,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 +273,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,7 +281,7 @@ public final class MobileLedgerProfile { setOption(name, String.valueOf(value)); } public void removeFromDB() { - SQLiteDatabase db = MLDB.getWritableDatabase(); + SQLiteDatabase db = MLDB.getDatabase(); Log.d("db", String.format("removing profile %s from DB", uuid)); try { db.beginTransaction(); @@ -298,12 +298,12 @@ public final class MobileLedgerProfile { } @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(); + SQLiteDatabase db = MLDB.getDatabase(); return tryLoadAccount(db, acct_name); } @NonNull @@ -343,7 +343,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; } @@ -394,7 +394,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()})) @@ -412,7 +412,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()})) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 300e742d..3e8e9c56 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -577,7 +577,7 @@ public class MainActivity extends ProfileThemedActivity { ViewPropertyAnimator animator = arrow.animate(); acc.toggleExpanded(); - Data.profile.get().storeAccount(MLDB.getWritableDatabase(), acc); + Data.profile.get().storeAccount(MLDB.getDatabase(), acc); if (wasExpanded) { Log.d("accounts", String.format("Collapsing account '%s'", acc.getName())); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java index b69710b3..135969dc 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java @@ -483,7 +483,7 @@ public class NewTransactionActivity extends ProfileThemedActivity Log.d("descr selected", description); if (!inputStateIsInitial()) return; - try (Cursor c = MLDB.getReadableDatabase().rawQuery( + try (Cursor c = MLDB.getDatabase().rawQuery( "select profile, id from transactions where description=? order by date desc " + "limit 1", new String[]{description})) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java index 9d27ecfa..1e9d2d43 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java @@ -144,7 +144,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter