1 package net.ktnx.mobileledger;
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;
9 import java.io.BufferedReader;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.util.Locale;
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;
22 static String getDb_filename() {
26 static void setDb_filename(String db_filename) {
27 MobileLedgerDB.db_filename = db_filename;
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"));
34 Log.d("db", "Current DB revision is "+String.valueOf(cur_ver));
36 while (applyRevision(rm, pkg_name, cur_ver+1)) {
40 Log.d("db", "Database revision is "+String.valueOf(cur_ver)+" now");
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);
45 int res_id = rm.getIdentifier(rev_file, "raw", pkg_name);
47 Log.d("db", String.format(Locale.US, "No resource for revision %d", rev_no));
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);
57 while ((line = reader.readLine()) != null) {
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");
67 catch (SQLException e) {
68 Log.e("db", String.format(Locale.US, "Error applying revision %d: %s", rev_no, e.getMessage()));
72 Log.w("db", "Error reading revision" + String.valueOf(rev_no)+": "+e.getMessage());
82 static int get_option_value(String name, int default_value) {
83 String s = get_option_value(name, String.valueOf(default_value));
85 return Integer.parseInt(s);
92 static long get_option_value(String name, long default_value) {
93 String s = get_option_value(name, String.valueOf(default_value));
95 return Long.parseLong(s);
98 Log.d("db", "returning default long value of "+name, e);
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);
109 if (result == null ) result = default_value;
111 Log.d("db", "option "+name+"="+result);
114 else return default_value;
117 Log.d("db", "returning default value for "+name, e);
118 return default_value;
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});
127 static void set_option_value(String name, long value) {
128 set_option_value(name, String.valueOf(value));