]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java
table for storing account balance
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / MobileLedgerDB.java
index d8b84fe15372eb52f6b8c4bf378af8b093e3af93..7c2a8e2d83b6d726bcab4307a8dce3c22c9c5123 100644 (file)
@@ -1,7 +1,13 @@
 package net.ktnx.mobileledger;
 
+import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 
 class MobileLedgerDB {
     static final String DATABASE_NAME = "accounts";
@@ -23,6 +29,38 @@ class MobileLedgerDB {
         db.execSQL("create index if not exists idx_accounts_name on accounts(name);");
         db.execSQL("create table if not exists options(name varchar, value varchar);");
         db.execSQL("create unique index if not exists idx_options_name on options(name);");
+        db.execSQL("create table if not exists account_values(account varchar not null, currency varchar not null, value decimal(18,2) not null);");
+        db.execSQL("create index if not exists idx_account_values_account on account_values(account);");
+        db.execSQL("create unique index if not exists un_account_values on account_values(account,currency);");
+    }
+
+    static void applyRevisions(Resources rm) {
+        int next_ver = Integer.parseInt(get_option_value("db_version", "0")) + 1;
+
+        while (applyRevision(rm, next_ver)) {
+            next_ver++;
+        }
+    }
+    private static boolean applyRevision(Resources rm, int next_ver) {
+        try (InputStream res = rm.openRawResource(next_ver)) {
+            Log.d("db", "Applying revision " + String.valueOf(next_ver));
+            InputStreamReader isr = new InputStreamReader(res);
+            BufferedReader reader = new BufferedReader(isr);
+
+            String line;
+            while ((line = reader.readLine()) != null) {
+                db.execSQL(line);
+            }
+
+            db.setTransactionSuccessful();
+        } catch (Exception e) {
+            return false;
+        }
+        finally {
+            db.endTransaction();
+        }
+
+        return true;
     }
 
     static int get_option_value(String name, int default_value) {