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