]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
separate packages for the different aspects of the application
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
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;
27
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;
33
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 = 7;
39
40     private final Context mContext;
41
42     public
43     MobileLedgerDatabase(Context context) {
44         super(context, DB_NAME, null, LATEST_REVISION);
45         Log.d("db", "creating helper instance");
46         mContext = context;
47     }
48
49     @Override
50     public
51     void onCreate(SQLiteDatabase db) {
52         Log.d("db", "onCreate called");
53         onUpgrade(db, -1, LATEST_REVISION);
54     }
55
56     @Override
57     public
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);
61     }
62
63     private void applyRevision(SQLiteDatabase db, int
64             rev_no) {
65         final Resources rm = mContext.getResources();
66         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
67
68         int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
69         if (res_id == 0)
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);
76
77             String line;
78             while ((line = reader.readLine()) != null) {
79                 db.execSQL(line);
80             }
81
82             db.setTransactionSuccessful();
83         }
84         catch (IOException e) {
85             Log.e("db", String.format("Error opening raw resource for revision %d", rev_no));
86             e.printStackTrace();
87         }
88         finally {
89             db.endTransaction();
90         }
91     }
92     public int get_option_value(String name, int default_value) {
93         String s = get_option_value(name, String.valueOf(default_value));
94         try {
95             return Integer.parseInt(s);
96         }
97         catch (Exception e) {
98             return default_value;
99         }
100     }
101
102     public long get_option_value(String name, long default_value) {
103         String s = get_option_value(name, String.valueOf(default_value));
104         try {
105             return Long.parseLong(s);
106         }
107         catch (Exception e) {
108             Log.d("db", "returning default long value of "+name, e);
109             return default_value;
110         }
111     }
112
113     public 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}))
118             {
119                 if (cursor.moveToFirst()) {
120                     String result = cursor.getString(0);
121
122                     if (result == null) result = default_value;
123
124                     Log.d("db", "option " + name + "=" + result);
125                     return result;
126                 }
127                 else return default_value;
128             }
129             catch (Exception e) {
130                 Log.d("db", "returning default value for " + name, e);
131                 return default_value;
132             }
133         }
134     }
135
136      public 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});
141         }
142     }
143
144     public void set_option_value(String name, long value) {
145         set_option_value(name, String.valueOf(value));
146     }
147     public 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);
150         }
151     }
152     public 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);
155         }
156     }
157 }