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.utils;
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 public class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {
35 public static final String DB_NAME = "mobile-ledger.db";
36 public static final String ACCOUNTS_TABLE = "accounts";
37 public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
38 public static final int LATEST_REVISION = 8;
40 private 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 public 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);
98 Log.d("db", "returning default int value of "+name, e);
103 public long get_option_value(String name, long default_value) {
104 String s = get_option_value(name, String.valueOf(default_value));
106 return Long.parseLong(s);
108 catch (Exception e) {
109 Log.d("db", "returning default long value of "+name, e);
110 return default_value;
114 public String get_option_value(String name, String default_value) {
115 Log.d("db", "about to fetch option "+name);
116 try(SQLiteDatabase db = getReadableDatabase()) {
117 try (Cursor cursor = db
118 .rawQuery("select value from options where name=?", new String[]{name}))
120 if (cursor.moveToFirst()) {
121 String result = cursor.getString(0);
123 if (result == null) result = default_value;
125 Log.d("db", "option " + name + "=" + result);
128 else return default_value;
130 catch (Exception e) {
131 Log.d("db", "returning default value for " + name, e);
132 return default_value;
137 public void set_option_value(String name, String value) {
138 Log.d("db", "setting option "+name+"="+value);
139 try(SQLiteDatabase db = getWritableDatabase()) {
140 db.execSQL("insert or replace into options(name, value) values(?, ?);",
141 new String[]{name, value});
145 public void set_option_value(String name, long value) {
146 set_option_value(name, String.valueOf(value));
148 public static long get_option_value(Context context, String name, long default_value) {
149 try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
150 return db.get_option_value(name, default_value);
153 public static void set_option_value(Context context, String name, String value) {
154 try(MobileLedgerDatabase db = new MobileLedgerDatabase(context)) {
155 db.set_option_value(name, value);