2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.Cursor;
23 import android.database.SQLException;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteOpenHelper;
26 import android.util.Log;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.Locale;
34 class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {
35 static final String DB_NAME = "mobile-ledger.db";
36 static final String ACCOUNTS_TABLE = "accounts";
37 static final String DESCRIPTION_HISTORY_TABLE = "description_history";
38 static final int LATEST_REVISION = 7;
40 final Context mContext;
43 MobileLedgerDatabase(Context context) {
44 super(context, DB_NAME, null, LATEST_REVISION);
45 Log.d("db", "creating helper instance");
51 void onCreate(SQLiteDatabase db) {
52 Log.d("db", "onCreate called");
53 onUpgrade(db, -1, LATEST_REVISION);
58 void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
59 Log.d("db", "onUpgrade called");
60 for(int i = oldVersion+1; i <= newVersion; i++) applyRevision(db, i);
63 private void applyRevision(SQLiteDatabase db, int
65 final Resources rm = mContext.getResources();
66 String rev_file = String.format(Locale.US, "sql_%d", rev_no);
68 int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
70 throw new SQLException(String.format(Locale.US, "No resource for revision %d", rev_no));
71 db.beginTransaction();
72 try (InputStream res = rm.openRawResource(res_id)) {
73 Log.d("db", "Applying revision " + String.valueOf(rev_no));
74 InputStreamReader isr = new InputStreamReader(res);
75 BufferedReader reader = new BufferedReader(isr);
78 while ((line = reader.readLine()) != null) {
82 db.setTransactionSuccessful();
84 catch (IOException e) {
85 Log.e("db", String.format("Error opening raw resource for revision %d", rev_no));
92 int get_option_value(String name, int default_value) {
93 String s = get_option_value(name, String.valueOf(default_value));
95 return Integer.parseInt(s);
102 long get_option_value(String name, long default_value) {
103 String s = get_option_value(name, String.valueOf(default_value));
105 return Long.parseLong(s);
107 catch (Exception e) {
108 Log.d("db", "returning default long value of "+name, e);
109 return default_value;
113 String get_option_value(String name, String default_value) {
114 Log.d("db", "about to fetch option "+name);
115 try(SQLiteDatabase db = getReadableDatabase()) {
116 try (Cursor cursor = db
117 .rawQuery("select value from options where name=?", new String[]{name}))
119 if (cursor.moveToFirst()) {
120 String result = cursor.getString(0);
122 if (result == null) result = default_value;
124 Log.d("db", "option " + name + "=" + result);
127 else return default_value;
129 catch (Exception e) {
130 Log.d("db", "returning default value for " + name, e);
131 return default_value;
136 void set_option_value(String name, String value) {
137 Log.d("db", "setting option "+name+"="+value);
138 try(SQLiteDatabase db = getWritableDatabase()) {
139 db.execSQL("insert or replace into options(name, value) values(?, ?);",
140 new String[]{name, value});
144 void set_option_value(String name, long value) {
145 set_option_value(name, String.valueOf(value));
147 static long get_option_value(Context context, String name, long default_value) {
148 try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
149 return db.get_option_value(name, default_value);
152 static void set_option_value(Context context, String name, String value) {
153 try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
154 db.set_option_value(name, value);