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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
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;
26 import androidx.lifecycle.MutableLiveData;
28 import net.ktnx.mobileledger.BuildConfig;
29 import net.ktnx.mobileledger.db.DB;
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;
39 import static net.ktnx.mobileledger.utils.Logger.debug;
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 = 58;
45 private static final String CREATE_DB_SQL = "create_db";
46 private final Application mContext;
48 public void onOpen(SQLiteDatabase db) {
50 // force a check by Room to ensure everything is OK
51 // TODO: remove when all DB structure manipulation is via Room
53 .compileStatement("SELECT COUNT(*) FROM profiles");
55 public MobileLedgerDatabase(Application context) {
56 super(context, DB_NAME, null, LATEST_REVISION);
57 debug("db", "creating helper instance");
59 super.setWriteAheadLoggingEnabled(true);
63 public void onCreate(SQLiteDatabase db) {
64 debug("db", "onCreate called");
65 applyRevisionFile(db, CREATE_DB_SQL);
69 public void onConfigure(SQLiteDatabase db) {
70 super.onConfigure(db);
71 db.execSQL("pragma case_sensitive_like=ON;");
72 if (BuildConfig.DEBUG)
73 db.execSQL("PRAGMA foreign_keys=ON");
76 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
78 String.format(Locale.US, "needs upgrade from version %d to version %d", oldVersion,
80 for (int i = oldVersion + 1; i <= newVersion; i++)
83 private void applyRevision(SQLiteDatabase db, int rev_no) {
84 String rev_file = String.format(Locale.US, "sql_%d", rev_no);
86 applyRevisionFile(db, rev_file);
88 private void applyRevisionFile(SQLiteDatabase db, String revFile) {
89 final Resources rm = mContext.getResources();
90 int res_id = rm.getIdentifier(revFile, "raw", mContext.getPackageName());
92 throw new SQLException(String.format(Locale.US, "No resource for %s", revFile));
94 try (InputStream res = rm.openRawResource(res_id)) {
95 debug("db", "Applying " + revFile);
96 InputStreamReader isr = new InputStreamReader(res);
97 BufferedReader reader = new BufferedReader(isr);
99 Pattern endOfStatement = Pattern.compile(";\\s*(?:--.*)?$");
102 String sqlStatement = null;
104 while ((line = reader.readLine()) != null) {
106 if (line.startsWith("--"))
111 if (sqlStatement == null)
114 sqlStatement = sqlStatement.concat(line);
116 Matcher m = endOfStatement.matcher(line);
121 db.execSQL(sqlStatement);
124 catch (Exception e) {
125 throw new RuntimeException(
126 String.format("Error applying %s, line %d, statement: %s", revFile,
127 lineNo, sqlStatement), e);
131 if (sqlStatement != null)
132 throw new RuntimeException(String.format(
133 "Error applying %s: EOF after continuation. Line %s, Incomplete " +
134 "statement: %s", revFile, lineNo, sqlStatement));
137 catch (IOException e) {
138 throw new RuntimeException(String.format("Error opening raw resource for %s", revFile),