]> git.ktnx.net Git - mobile-ledger.git/commitdiff
remove the distinction between "readable" and "writable" database connections
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 24 Mar 2019 09:29:12 +0000 (11:29 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 25 Mar 2019 06:17:36 +0000 (06:17 +0000)
it is artificial and serves no useful purpose

app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java
app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java

index 7368d712d88ce19eabddc12353d50753b9133372..1213647844dd64e2da4f16657c4b0a75a0514288 100644 (file)
@@ -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) {
index 68f3ddc8ff7f40f5e8cec548cae7cfc4789f19f3..41a58e980a1b09d817e85b92dcf55d17a73d1665 100644 (file)
@@ -34,7 +34,7 @@ public class RefreshDescriptionsTask extends AsyncTask<Void, Void, Void> {
         Map<String, Boolean> unique = new HashMap<>();
 
         Log.d("descriptions", "Starting refresh");
-        SQLiteDatabase db = MLDB.getWritableDatabase();
+        SQLiteDatabase db = MLDB.getDatabase();
 
         Data.backgroundTaskCount.incrementAndGet();
         try {
index de459ece7a4e6ef9bf615ba064a4bfb4c22fba4d..dc73944332b728ccae78e798089fa71c11d9a35c 100644 (file)
@@ -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<LedgerAccount> accountList = new ArrayList<>();
+                AccountListParser parser = new AccountListParser(resp);
+                ArrayList<LedgerAccount> 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()));
index ad030ef6a1d44a26d529ad73befcba4443c1eace..65f272220557501c2a9dc37fd0b24f741e2e699a 100644 (file)
@@ -42,7 +42,7 @@ public class UpdateAccountsTask extends AsyncTask<Void, Void, ArrayList<LedgerAc
             if (onlyStarred) sql += " AND a.hidden = 0";
             sql += " ORDER BY a.name";
 
-            SQLiteDatabase db = MLDB.getReadableDatabase();
+            SQLiteDatabase db = MLDB.getDatabase();
             try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
                 while (cursor.moveToNext()) {
                     final String accName = cursor.getString(0);
index 4edf5c126fcd33bb5d5aaf97cbbec257e3c272c3..fb1bd72fd2a359c790e6611a83fe15ad827847a1 100644 (file)
@@ -62,7 +62,7 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
             }
 
             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;
index 4be38d4aed442de5d3939dc22803fbf995eefbbd..e466d3d906312d440808e68957e5b9bcfcbb0b47 100644 (file)
@@ -78,7 +78,7 @@ public final class MobileLedgerProfile {
     public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
         MobileLedgerProfile result = null;
         List<MobileLedgerProfile> 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<LedgerAccount> loadChildAccountsOf(LedgerAccount acc) {
         List<LedgerAccount> 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<LedgerAccount> 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()}))
index 300e742d425ea42fd4ceda30139815998046cab7..3e8e9c569747f42ae25df98af2bf546b3793882a 100644 (file)
@@ -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()));
index b69710b3598c8c0d15d619e035e2fa8a930a49ef..135969dc4801a8c676eebf70f6685dcf2a8237f1 100644 (file)
@@ -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}))
         {
index 9d27ecfa1de9b3fe365a4db1c20947d46d4158f7..1e9d2d43fd046178180a0f3534353a0b0bbbbf76 100644 (file)
@@ -144,7 +144,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
             LedgerTransaction tr = p[0].transaction;
             boolean odd = p[0].odd;
 
-            SQLiteDatabase db = MLDB.getReadableDatabase();
+            SQLiteDatabase db = MLDB.getDatabase();
             tr.loadData(db);
 
             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
index 42996c9f36bfe40fb09e8bbb46ec1f548b4267ac..a7e124d7735ce8d83049295eac380f172f7db391 100644 (file)
@@ -45,9 +45,6 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.Locale;
 
-import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.READ;
-import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.WRITE;
-
 public final class MLDB {
     public static final String ACCOUNTS_TABLE = "accounts";
     public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
@@ -55,35 +52,23 @@ public final class MLDB {
     @NonNls
     public static final String OPT_PROFILE_UUID = "profile_uuid";
     private static final String NO_PROFILE = "-";
-    private static MobileLedgerDatabase helperForReading, helperForWriting;
+    private static MobileLedgerDatabase dbHelper;
     private static Application context;
     private static void checkState() {
         if (context == null)
             throw new IllegalStateException("First call init with a valid context");
     }
-    public static synchronized SQLiteDatabase getDatabase(DatabaseMode mode) {
+    public static synchronized SQLiteDatabase getDatabase() {
         checkState();
 
         SQLiteDatabase db;
 
-        if (mode == READ) {
-            if (helperForReading == null) helperForReading = new MobileLedgerDatabase(context);
-            db = helperForReading.getReadableDatabase();
-        }
-        else {
-            if (helperForWriting == null) helperForWriting = new MobileLedgerDatabase(context);
-            db = helperForWriting.getWritableDatabase();
-        }
+        if (dbHelper == null) dbHelper = new MobileLedgerDatabase(context);
+        db = dbHelper.getWritableDatabase();
 
         db.execSQL("pragma case_sensitive_like=ON;");
         return db;
     }
-    public static SQLiteDatabase getReadableDatabase() {
-        return getDatabase(READ);
-    }
-    public static SQLiteDatabase getWritableDatabase() {
-        return getDatabase(WRITE);
-    }
     static public int getIntOption(String name, int default_value) {
         String s = getOption(name, String.valueOf(default_value));
         try {
@@ -106,7 +91,7 @@ public final class MLDB {
     }
     static public String getOption(String name, String default_value) {
         Log.d("db", "about to fetch option " + name);
-        SQLiteDatabase db = getReadableDatabase();
+        SQLiteDatabase db = getDatabase();
         try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
                 new String[]{NO_PROFILE, name}))
         {
@@ -127,7 +112,7 @@ public final class MLDB {
     }
     static public void setOption(String name, String value) {
         Log.d("option", String.format("%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[]{NO_PROFILE, name, value});
     }
@@ -182,7 +167,7 @@ public final class MLDB {
                 params = new String[]{str, str, str, str};
             }
             Log.d("autocompletion", sql);
-            SQLiteDatabase db = MLDB.getReadableDatabase();
+            SQLiteDatabase db = MLDB.getDatabase();
 
             try (Cursor matches = db.rawQuery(sql, params)) {
                 int i = 0;
@@ -215,13 +200,11 @@ public final class MLDB {
         MLDB.context = context;
     }
     public static void done() {
-        if (helperForReading != null) helperForReading.close();
-
-        if ((helperForWriting != helperForReading) && (helperForWriting != null))
-            helperForWriting.close();
+        if (dbHelper != null) {
+            Log.d("db", "Closing DB helper");
+            dbHelper.close();
+        }
     }
-
-    public enum DatabaseMode {READ, WRITE}
 }
 
 class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {