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