]> git.ktnx.net Git - mobile-ledger.git/blob - 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
1 package net.ktnx.mobileledger;
2
3 import android.content.res.Resources;
4 import android.database.Cursor;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.util.Log;
7
8 import java.io.BufferedReader;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11
12 class MobileLedgerDB {
13     static final String DATABASE_NAME = "accounts";
14     private static String db_filename;
15     static SQLiteDatabase db;
16
17     static String getDb_filename() {
18         return db_filename;
19     }
20
21     static void setDb_filename(String db_filename) {
22         MobileLedgerDB.db_filename = db_filename;
23     }
24
25     static void initDB() {
26         db = SQLiteDatabase.openOrCreateDatabase(db_filename, null);
27
28         db.execSQL("create table if not exists accounts(name varchar);");
29         db.execSQL("create index if not exists idx_accounts_name on accounts(name);");
30         db.execSQL("create table if not exists options(name varchar, value varchar);");
31         db.execSQL("create unique index if not exists idx_options_name on options(name);");
32         db.execSQL("create table if not exists account_values(account varchar not null, currency varchar not null, value decimal(18,2) not null);");
33         db.execSQL("create index if not exists idx_account_values_account on account_values(account);");
34         db.execSQL("create unique index if not exists un_account_values on account_values(account,currency);");
35     }
36
37     static void applyRevisions(Resources rm) {
38         int next_ver = Integer.parseInt(get_option_value("db_version", "0")) + 1;
39
40         while (applyRevision(rm, next_ver)) {
41             next_ver++;
42         }
43     }
44     private static boolean applyRevision(Resources rm, int next_ver) {
45         try (InputStream res = rm.openRawResource(next_ver)) {
46             Log.d("db", "Applying revision " + String.valueOf(next_ver));
47             InputStreamReader isr = new InputStreamReader(res);
48             BufferedReader reader = new BufferedReader(isr);
49
50             String line;
51             while ((line = reader.readLine()) != null) {
52                 db.execSQL(line);
53             }
54
55             db.setTransactionSuccessful();
56         } catch (Exception e) {
57             return false;
58         }
59         finally {
60             db.endTransaction();
61         }
62
63         return true;
64     }
65
66     static int get_option_value(String name, int default_value) {
67         String s = get_option_value(name, String.valueOf(default_value));
68         try {
69             return Integer.parseInt(s);
70         }
71         catch (Exception e) {
72             return default_value;
73         }
74     }
75
76     static long get_option_value(String name, long default_value) {
77         String s = get_option_value(name, String.valueOf(default_value));
78         try {
79             return Long.parseLong(s);
80         }
81         catch (Exception e) {
82             Log.d("db", "returning default long value of "+name, e);
83             return default_value;
84         }
85     }
86
87     static String get_option_value(String name, String default_value) {
88         Log.d("db", "about fo fetch option "+name);
89         try (Cursor cursor = db.rawQuery("select value from options where name=?", new String[]{name})) {
90             if (cursor.moveToFirst()) {
91                 String result = cursor.getString(0);
92
93                 if (result == null ) result = default_value;
94
95                 Log.d("db", "option "+name+"="+result);
96                 return result;
97             }
98             else return default_value;
99         }
100         catch(Exception e) {
101             Log.d("db", "returning default value for "+name, e);
102             return default_value;
103         }
104     }
105
106     static void set_option_value(String name, String value) {
107         Log.d("db", "setting option "+name+"="+value);
108         db.execSQL("insert or replace into options(name, value) values(?, ?);", new String[]{name, value});
109     }
110
111     static void set_option_value(String name, long value) {
112         set_option_value(name, String.valueOf(value));
113     }
114 }