1 package net.ktnx.mobileledger;
3 import android.content.res.Resources;
4 import android.database.Cursor;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.util.Log;
8 import java.io.BufferedReader;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
12 class MobileLedgerDB {
13 static final String DATABASE_NAME = "accounts";
14 private static String db_filename;
15 static SQLiteDatabase db;
17 static String getDb_filename() {
21 static void setDb_filename(String db_filename) {
22 MobileLedgerDB.db_filename = db_filename;
25 static void initDB() {
26 db = SQLiteDatabase.openOrCreateDatabase(db_filename, null);
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);");
37 static void applyRevisions(Resources rm) {
38 int next_ver = Integer.parseInt(get_option_value("db_version", "0")) + 1;
40 while (applyRevision(rm, next_ver)) {
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);
51 while ((line = reader.readLine()) != null) {
55 db.setTransactionSuccessful();
56 } catch (Exception e) {
66 static int get_option_value(String name, int default_value) {
67 String s = get_option_value(name, String.valueOf(default_value));
69 return Integer.parseInt(s);
76 static long get_option_value(String name, long default_value) {
77 String s = get_option_value(name, String.valueOf(default_value));
79 return Long.parseLong(s);
82 Log.d("db", "returning default long value of "+name, e);
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);
93 if (result == null ) result = default_value;
95 Log.d("db", "option "+name+"="+result);
98 else return default_value;
101 Log.d("db", "returning default value for "+name, e);
102 return default_value;
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});
111 static void set_option_value(String name, long value) {
112 set_option_value(name, String.valueOf(value));