]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
splash: setup DB in the background
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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  * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import android.app.Application;
21 import android.content.res.Resources;
22 import android.database.SQLException;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.database.sqlite.SQLiteOpenHelper;
25 import android.util.Log;
26
27 import androidx.lifecycle.MutableLiveData;
28
29 import net.ktnx.mobileledger.BuildConfig;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.Locale;
36
37 import static net.ktnx.mobileledger.utils.Logger.debug;
38
39 public class MobileLedgerDatabase extends SQLiteOpenHelper {
40     public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
41     private static final String DB_NAME = "MoLe.db";
42     private static final int LATEST_REVISION = 40;
43     private static final String CREATE_DB_SQL = "create_db";
44     private final Application mContext;
45
46     public MobileLedgerDatabase(Application context) {
47         super(context, DB_NAME, null, LATEST_REVISION);
48         debug("db", "creating helper instance");
49         mContext = context;
50         super.setWriteAheadLoggingEnabled(true);
51     }
52
53     @Override
54     public void onCreate(SQLiteDatabase db) {
55         debug("db", "onCreate called");
56         applyRevisionFile(db, CREATE_DB_SQL);
57     }
58
59     @Override
60     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
61         debug("db",
62                 String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion,
63                         newVersion));
64         for (int i = oldVersion + 1; i <= newVersion; i++)
65             applyRevision(db, i);
66     }
67     @Override
68     public void onOpen(SQLiteDatabase db) {
69         super.onOpen(db);
70         db.execSQL("pragma case_sensitive_like=ON;");
71         if (BuildConfig.DEBUG)
72             db.execSQL("PRAGMA foreign_keys=ON");
73     }
74
75     private void applyRevision(SQLiteDatabase db, int rev_no) {
76         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
77
78         applyRevisionFile(db, rev_file);
79     }
80     private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
81         final Resources rm = mContext.getResources();
82         int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
83         if (res_id == 0)
84             throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
85         db.beginTransaction();
86         try (InputStream res = rm.openRawResource(res_id)) {
87             debug("db", "Applying " + rev_file);
88             InputStreamReader isr = new InputStreamReader(res);
89             BufferedReader reader = new BufferedReader(isr);
90
91             String line;
92             int line_no = 1;
93             while ((line = reader.readLine()) != null) {
94                 if (line.startsWith("--")) {
95                     line_no++;
96                     continue;
97                 }
98                 if (line.isEmpty()) {
99                     line_no++;
100                     continue;
101                 }
102                 try {
103                     db.execSQL(line);
104                 }
105                 catch (Exception e) {
106                     throw new RuntimeException(
107                             String.format("Error applying %s, line %d", rev_file, line_no), e);
108                 }
109                 line_no++;
110             }
111
112             db.setTransactionSuccessful();
113         }
114         catch (IOException e) {
115             Log.e("db", String.format("Error opening raw resource for %s", rev_file));
116             e.printStackTrace();
117         }
118         finally {
119             db.endTransaction();
120         }
121     }
122 }