]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java
auto-completion for the transaction description
[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.SQLException;
6 import android.database.sqlite.SQLiteDatabase;
7 import android.util.Log;
8
9 import java.io.BufferedReader;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.util.Locale;
13
14 class MobileLedgerDB {
15     static final String DATABASE_NAME = "accounts";
16     static final String OPT_DB_REVISION = "db_revision";
17     static final String ACCOUNTS_TABLE = "accounts";
18     static final String DESCRIPTION_HISTORY_TABLE = "description_history";
19     private static String db_filename;
20     static SQLiteDatabase db;
21
22     static String getDb_filename() {
23         return db_filename;
24     }
25
26     static void setDb_filename(String db_filename) {
27         MobileLedgerDB.db_filename = db_filename;
28     }
29
30     static void initDB(Resources rm, String pkg_name) {
31         db = SQLiteDatabase.openOrCreateDatabase(db_filename, null);
32         int cur_ver = Integer.parseInt(get_option_value(OPT_DB_REVISION, "-1"));
33
34         Log.d("db", "Current DB revision is "+String.valueOf(cur_ver));
35
36         while (applyRevision(rm, pkg_name, cur_ver+1)) {
37             cur_ver++;
38         }
39
40         Log.d("db", "Database revision is "+String.valueOf(cur_ver)+" now");
41     }
42     private static boolean applyRevision(Resources rm, String pkg_name, int rev_no) {
43         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
44
45         int res_id = rm.getIdentifier(rev_file, "raw", pkg_name);
46         if (res_id == 0) {
47             Log.d("db", String.format(Locale.US, "No resource for revision %d", rev_no));
48             return false;
49         }
50         db.beginTransaction();
51         try (InputStream res = rm.openRawResource(res_id)) {
52             Log.d("db", "Applying revision " + String.valueOf(rev_no));
53             InputStreamReader isr = new InputStreamReader(res);
54             BufferedReader reader = new BufferedReader(isr);
55
56             String line;
57             while ((line = reader.readLine()) != null) {
58                 db.execSQL(line);
59             }
60
61             set_option_value(OPT_DB_REVISION, rev_no);
62             db.setTransactionSuccessful();
63         } catch (Resources.NotFoundException e) {
64             Log.d("db", "SQL revision "+String.valueOf(rev_no)+" not found");
65             return false;
66         }
67         catch (SQLException e) {
68             Log.e("db", String.format(Locale.US, "Error applying revision %d: %s", rev_no, e.getMessage()));
69             return false;
70         }
71         catch (Exception e) {
72             Log.w("db", "Error reading revision" + String.valueOf(rev_no)+": "+e.getMessage());
73             return false;
74         }
75         finally {
76             db.endTransaction();
77         }
78
79         return true;
80     }
81
82     static int get_option_value(String name, int default_value) {
83         String s = get_option_value(name, String.valueOf(default_value));
84         try {
85             return Integer.parseInt(s);
86         }
87         catch (Exception e) {
88             return default_value;
89         }
90     }
91
92     static long get_option_value(String name, long default_value) {
93         String s = get_option_value(name, String.valueOf(default_value));
94         try {
95             return Long.parseLong(s);
96         }
97         catch (Exception e) {
98             Log.d("db", "returning default long value of "+name, e);
99             return default_value;
100         }
101     }
102
103     static String get_option_value(String name, String default_value) {
104         Log.d("db", "about fo fetch option "+name);
105         try (Cursor cursor = db.rawQuery("select value from options where name=?", new String[]{name})) {
106             if (cursor.moveToFirst()) {
107                 String result = cursor.getString(0);
108
109                 if (result == null ) result = default_value;
110
111                 Log.d("db", "option "+name+"="+result);
112                 return result;
113             }
114             else return default_value;
115         }
116         catch(Exception e) {
117             Log.d("db", "returning default value for "+name, e);
118             return default_value;
119         }
120     }
121
122     static void set_option_value(String name, String value) {
123         Log.d("db", "setting option "+name+"="+value);
124         db.execSQL("insert or replace into options(name, value) values(?, ?);", new String[]{name, value});
125     }
126
127     static void set_option_value(String name, long value) {
128         set_option_value(name, String.valueOf(value));
129     }
130 }