]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
e0a0a421c58ca3e22495b17c6218e07bbe88c92e
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / MobileLedgerDatabase.java
1 /*
2  * Copyright © 2021 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 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 import static net.ktnx.mobileledger.utils.Logger.debug;
40
41 public class MobileLedgerDatabase extends SQLiteOpenHelper {
42     public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
43     public static final String DB_NAME = "MoLe.db";
44     private static final int LATEST_REVISION = 55;
45     private static final String CREATE_DB_SQL = "create_db";
46     private final Application mContext;
47
48     public MobileLedgerDatabase(Application context) {
49         super(context, DB_NAME, null, LATEST_REVISION);
50         debug("db", "creating helper instance");
51         mContext = context;
52         super.setWriteAheadLoggingEnabled(true);
53     }
54
55     @Override
56     public void onCreate(SQLiteDatabase db) {
57         debug("db", "onCreate called");
58         applyRevisionFile(db, CREATE_DB_SQL);
59     }
60
61     @Override
62     public void onConfigure(SQLiteDatabase db) {
63         super.onConfigure(db);
64         db.execSQL("pragma case_sensitive_like=ON;");
65         if (BuildConfig.DEBUG)
66             db.execSQL("PRAGMA foreign_keys=ON");
67     }
68     @Override
69     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
70         debug("db",
71                 String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion,
72                         newVersion));
73         for (int i = oldVersion + 1; i <= newVersion; i++)
74             applyRevision(db, i);
75     }
76     private void applyRevision(SQLiteDatabase db, int rev_no) {
77         String rev_file = String.format(Locale.US, "sql_%d", rev_no);
78
79         applyRevisionFile(db, rev_file);
80     }
81     private void applyRevisionFile(SQLiteDatabase db, String revFile) {
82         final Resources rm = mContext.getResources();
83         int res_id = rm.getIdentifier(revFile, "raw", mContext.getPackageName());
84         if (res_id == 0)
85             throw new SQLException(String.format(Locale.US, "No resource for %s", revFile));
86
87         try (InputStream res = rm.openRawResource(res_id)) {
88             debug("db", "Applying " + revFile);
89             InputStreamReader isr = new InputStreamReader(res);
90             BufferedReader reader = new BufferedReader(isr);
91
92             Pattern continuation = Pattern.compile("\\\\\\s*$");
93
94             String line;
95             String sqlStatement = null;
96             boolean eolPending;
97             int lineNo = 0;
98             while ((line = reader.readLine()) != null) {
99                 lineNo++;
100                 if (line.startsWith("--"))
101                     continue;
102                 if (line.isEmpty())
103                     continue;
104
105                 Matcher m = continuation.matcher(line);
106                 eolPending = false;
107                 if (m.find()) {
108                     line = m.replaceFirst("");
109                     eolPending = true;
110                 }
111
112                 if (sqlStatement == null)
113                     sqlStatement = line;
114                 else
115                     sqlStatement = sqlStatement.concat(line);
116
117                 if (eolPending)
118                     continue;
119
120                 try {
121                     db.execSQL(sqlStatement);
122                     sqlStatement = null;
123                 }
124                 catch (Exception e) {
125                     throw new RuntimeException(
126                             String.format("Error applying %s, line %d, statement: %s", revFile,
127                                     lineNo, sqlStatement), e);
128                 }
129             }
130
131             if (sqlStatement != null)
132                 throw new RuntimeException(
133                         String.format("Error applying %s: EOF after continuation", revFile));
134
135         }
136         catch (IOException e) {
137             throw new RuntimeException(String.format("Error opening raw resource for %s", revFile),
138                     e);
139         }
140     }
141 }