]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/MobileLedgerDatabase.java
72a68eea2c08f9b6e83383afef4b83563a0274eb
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / MobileLedgerDatabase.java
1 package net.ktnx.mobileledger;
2
3 import android.content.Context;
4 import android.content.res.Resources;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.util.Log;
10
11 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.util.Locale;
16
17 class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {
18     static final String DB_NAME = "mobile-ledger.db";
19     static final String ACCOUNTS_TABLE = "accounts";
20     static final String DESCRIPTION_HISTORY_TABLE = "description_history";
21     static final int LATEST_REVISION = 6;
22
23     final Context mContext;
24
25     public
26     MobileLedgerDatabase(Context context) {
27         super(context, DB_NAME, null, LATEST_REVISION);
28         Log.d("db", "creating helper instance");
29         mContext = context;
30     }
31
32     @Override
33     public
34     void onCreate(SQLiteDatabase db) {
35         Log.d("db", "onCreate called");
36         onUpgrade(db, -1, LATEST_REVISION);
37     }
38
39     @Override
40     public
41     void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
42         Log.d("db", "onUpgrade called");
43         for(int i = oldVersion+1; i <= newVersion; i++) applyRevision(db, i);
44     }
45
46     private void applyRevision(SQLiteDatabase db, int
47             rev_no) {
48         final Resources rm = mContext.getResources();
49         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
50
51         int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
52         if (res_id == 0)
53             throw new SQLException(String.format(Locale.US, "No resource for revision %d", rev_no));
54         db.beginTransaction();
55         try (InputStream res = rm.openRawResource(res_id)) {
56             Log.d("db", "Applying revision " + String.valueOf(rev_no));
57             InputStreamReader isr = new InputStreamReader(res);
58             BufferedReader reader = new BufferedReader(isr);
59
60             String line;
61             while ((line = reader.readLine()) != null) {
62                 db.execSQL(line);
63             }
64
65             db.setTransactionSuccessful();
66         }
67         catch (IOException e) {
68             Log.e("db", String.format("Error opening raw resource for revision %d", rev_no));
69             e.printStackTrace();
70         }
71         finally {
72             db.endTransaction();
73         }
74     }
75     int get_option_value(String name, int default_value) {
76         String s = get_option_value(name, String.valueOf(default_value));
77         try {
78             return Integer.parseInt(s);
79         }
80         catch (Exception e) {
81             return default_value;
82         }
83     }
84
85     long get_option_value(String name, long default_value) {
86         String s = get_option_value(name, String.valueOf(default_value));
87         try {
88             return Long.parseLong(s);
89         }
90         catch (Exception e) {
91             Log.d("db", "returning default long value of "+name, e);
92             return default_value;
93         }
94     }
95
96     String get_option_value(String name, String default_value) {
97         Log.d("db", "about to fetch option "+name);
98         try(SQLiteDatabase db = getReadableDatabase()) {
99             try (Cursor cursor = db
100                     .rawQuery("select value from options where name=?", new String[]{name}))
101             {
102                 if (cursor.moveToFirst()) {
103                     String result = cursor.getString(0);
104
105                     if (result == null) result = default_value;
106
107                     Log.d("db", "option " + name + "=" + result);
108                     return result;
109                 }
110                 else return default_value;
111             }
112             catch (Exception e) {
113                 Log.d("db", "returning default value for " + name, e);
114                 return default_value;
115             }
116         }
117     }
118
119      void set_option_value(String name, String value) {
120         Log.d("db", "setting option "+name+"="+value);
121         try(SQLiteDatabase db = getWritableDatabase()) {
122             db.execSQL("insert or replace into options(name, value) values(?, ?);",
123                     new String[]{name, value});
124         }
125     }
126
127     void set_option_value(String name, long value) {
128         set_option_value(name, String.valueOf(value));
129     }
130     static long get_option_value(Context context, String name, long default_value) {
131         try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
132             return db.get_option_value(name, default_value);
133         }
134     }
135     static void set_option_value(Context context, String name, String value) {
136         try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
137             db.set_option_value(name, value);
138         }
139     }
140 }