1 package net.ktnx.mobileledger;
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;
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;
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 = 4;
23 final Context mContext;
26 MobileLedgerDatabase(Context context) {
27 super(context, DB_NAME, null, LATEST_REVISION);
28 Log.d("db", "creating helper instance");
34 void onCreate(SQLiteDatabase db) {
35 Log.d("db", "onCreate called");
36 onUpgrade(db, -1, LATEST_REVISION);
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);
46 private void applyRevision(SQLiteDatabase db, int
48 final Resources rm = mContext.getResources();
49 String rev_file = String.format(Locale.US, "sql_%d", rev_no);
51 int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
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);
61 while ((line = reader.readLine()) != null) {
65 db.setTransactionSuccessful();
67 catch (IOException e) {
68 Log.e("db", String.format("Error opening raw resource for revision %d", rev_no));
75 int get_option_value(String name, int default_value) {
76 String s = get_option_value(name, String.valueOf(default_value));
78 return Integer.parseInt(s);
85 long get_option_value(String name, long default_value) {
86 String s = get_option_value(name, String.valueOf(default_value));
88 return Long.parseLong(s);
91 Log.d("db", "returning default long value of "+name, e);
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}))
102 if (cursor.moveToFirst()) {
103 String result = cursor.getString(0);
105 if (result == null) result = default_value;
107 Log.d("db", "option " + name + "=" + result);
110 else return default_value;
112 catch (Exception e) {
113 Log.d("db", "returning default value for " + name, e);
114 return default_value;
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});
127 void set_option_value(String name, long value) {
128 set_option_value(name, String.valueOf(value));
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);
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);