]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java
a constant for the transaction list stamp option
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MLDB.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 import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.READ;
35 import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.WRITE;
36
37 public final class MLDB {
38     public enum DatabaseMode {READ, WRITE}
39
40     public static final String ACCOUNTS_TABLE = "accounts";
41     public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
42     public static final String OPT_TRANSACTION_LIST_STAMP = "transaction_list_last_update";
43     private static MobileLedgerDatabase helperForReading, helperForWriting;
44
45     public static synchronized SQLiteDatabase getDatabase(Context context, DatabaseMode mode) {
46         if (mode == READ) {
47             if (helperForReading == null) helperForReading = new MobileLedgerDatabase(context);
48             return helperForReading.getReadableDatabase();
49         }
50         else {
51             if (helperForWriting == null) helperForWriting = new MobileLedgerDatabase(context);
52             return helperForWriting.getWritableDatabase();
53         }
54     }
55     public static SQLiteDatabase getReadableDatabase(Context context) {
56         return getDatabase(context, READ);
57     }
58     public static SQLiteDatabase getWritableDatabase(Context context) {
59         return getDatabase(context, WRITE);
60     }
61     static public int get_option_value(Context context, String name, int default_value) {
62         String s = get_option_value(context, name, String.valueOf(default_value));
63         try {
64             return Integer.parseInt(s);
65         }
66         catch (Exception e) {
67             Log.d("db", "returning default int value of " + name, e);
68             return default_value;
69         }
70     }
71
72     static public long get_option_value(Context context, String name, long default_value) {
73         String s = get_option_value(context, name, String.valueOf(default_value));
74         try {
75             return Long.parseLong(s);
76         }
77         catch (Exception e) {
78             Log.d("db", "returning default long value of " + name, e);
79             return default_value;
80         }
81     }
82
83     static public String get_option_value(Context context, String name, String default_value) {
84         Log.d("db", "about to fetch option " + name);
85         try (SQLiteDatabase db = getReadableDatabase(context)) {
86             try (Cursor cursor = db
87                     .rawQuery("select value from options where name=?", new String[]{name}))
88             {
89                 if (cursor.moveToFirst()) {
90                     String result = cursor.getString(0);
91
92                     if (result == null) result = default_value;
93
94                     Log.d("db", "option " + name + "=" + result);
95                     return result;
96                 }
97                 else return default_value;
98             }
99             catch (Exception e) {
100                 Log.d("db", "returning default value for " + name, e);
101                 return default_value;
102             }
103         }
104     }
105
106     static public void set_option_value(Context context, String name, String value) {
107         Log.d("db", "setting option " + name + "=" + value);
108         try (SQLiteDatabase db = getWritableDatabase(context)) {
109             db.execSQL("insert or replace into options(name, value) values(?, ?);",
110                     new String[]{name, value});
111         }
112     }
113
114     static public void set_option_value(Context context, String name, long value) {
115         set_option_value(context, name, String.valueOf(value));
116     }
117 }
118
119 class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {
120     public static final String DB_NAME = "mobile-ledger.db";
121     public static final int LATEST_REVISION = 9;
122
123     private final Context mContext;
124
125     public MobileLedgerDatabase(Context context) {
126         super(context, DB_NAME, null, LATEST_REVISION);
127         Log.d("db", "creating helper instance");
128         mContext = context;
129         super.setWriteAheadLoggingEnabled(true);
130     }
131
132     @Override
133     public void onCreate(SQLiteDatabase db) {
134         Log.d("db", "onCreate called");
135         onUpgrade(db, -1, LATEST_REVISION);
136     }
137
138     @Override
139     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
140         Log.d("db", "onUpgrade called");
141         for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
142     }
143
144     private void applyRevision(SQLiteDatabase db, int rev_no) {
145         final Resources rm = mContext.getResources();
146         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
147
148         int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
149         if (res_id == 0) throw new SQLException(
150                 String.format(Locale.US, "No resource for revision %d", rev_no));
151         db.beginTransaction();
152         try (InputStream res = rm.openRawResource(res_id)) {
153             Log.d("db", "Applying revision " + String.valueOf(rev_no));
154             InputStreamReader isr = new InputStreamReader(res);
155             BufferedReader reader = new BufferedReader(isr);
156
157             String line;
158             while ((line = reader.readLine()) != null) {
159                 db.execSQL(line);
160             }
161
162             db.setTransactionSuccessful();
163         }
164         catch (IOException e) {
165             Log.e("db", String.format("Error opening raw resource for revision %d", rev_no));
166             e.printStackTrace();
167         }
168         finally {
169             db.endTransaction();
170         }
171     }
172 }